Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
fix(script): code block escaping and hljs
Browse files Browse the repository at this point in the history
  • Loading branch information
ppoffice committed Jun 27, 2019
1 parent 469db43 commit 19f9772
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
22 changes: 0 additions & 22 deletions scripts/99_content.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,3 @@ hexo.extend.helper.register('toc_list', (content) => {
});
return tocList;
});

function patchCodeHighlight(content) {
const $ = cheerio.load(content, { decodeEntities: false });
$('figure.highlight').addClass('hljs');
$('figure.highlight .code .line span').each(function () {
const classes = $(this).attr('class').split(' ');
if (classes.length === 1) {
$(this).addClass('hljs-' + classes[0]);
$(this).removeClass(classes[0]);
}
});
return $.html();
}

/**
* Add .hljs class name to the code blocks and code elements
*/
hexo.extend.filter.register('after_post_render', function (data) {
data.content = data.content ? patchCodeHighlight(data.content) : data.content;
data.excerpt = data.excerpt ? patchCodeHighlight(data.excerpt) : data.excerpt;
return data;
});
57 changes: 41 additions & 16 deletions scripts/99_tags.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
const cheerio = require('cheerio');

/**
* Colored quote block
*/
hexo.extend.tag.register('colorquote', function (args, content) {
var type = args[0];
return '<blockquote class="colorquote ' + type + '">' + hexo.render.renderSync({text: content, engine: 'markdown'}) + '</blockquote>';
}, {ends: true});
var type = args[0];
return '<blockquote class="colorquote ' + type + '">' + hexo.render.renderSync({ text: content, engine: 'markdown' }) + '</blockquote>';
}, { ends: true });

const rEscapeContent = /<escape(?:[^>]*)>([\s\S]*?)<\/escape>/g;
const placeholder = '\uFFFD';
const rPlaceholder = /(?:<|&lt;)\!--\uFFFD(\d+)--(?:>|&gt;)/g;
const rPlaceholder = /(?:<|&lt;)epacse(?:[^>]*)(?:>|&gt;)(\d+)(?:<|&lt;)\/epacse(?:[^>]*)(?:>|&gt;)/g;
const cache = [];
function escapeContent(str) {
return '<!--' + placeholder + (cache.push(str) - 1) + '-->';
return '<epacse hidden>' + (cache.push(str) - 1) + '</epacse>';
}
hexo.extend.filter.register('before_post_render', function(data) {
data.content = data.content.replace(rEscapeContent, function(match, content) {
return escapeContent(content);
});
return data;

hexo.extend.filter.register('before_post_render', function (data) {
data.content = data.content.replace(rEscapeContent, function (match, content) {
return escapeContent(content);
});
return data;
});

hexo.extend.filter.register('after_post_render', function (data) {
data.content = data.content.replace(rPlaceholder, function () {
return cache[arguments[1]];
});
return data;
});

hexo.extend.filter.register('after_post_render', function(data) {
data.content = data.content.replace(rPlaceholder, function() {
return cache[arguments[1]];
});
return data;
function patchCodeHighlight(content) {
const $ = cheerio.load(content, { decodeEntities: false });
$('figure.highlight').addClass('hljs');
$('figure.highlight .code .line span').each(function () {
const classes = $(this).attr('class').split(' ');
if (classes.length === 1) {
$(this).addClass('hljs-' + classes[0]);
$(this).removeClass(classes[0]);
}
});
return $.html();
}

/**
* Add .hljs class name to the code blocks and code elements.
* Note: must be put after the above escape patch (hexojs/hexo#2400)
*/
hexo.extend.filter.register('after_post_render', function (data) {
data.content = data.content ? patchCodeHighlight(data.content) : data.content;
data.excerpt = data.excerpt ? patchCodeHighlight(data.excerpt) : data.excerpt;
return data;
});

1 comment on commit 19f9772

@ppoffice
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please # to comment.