This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(script): code block escaping and hljs
- Loading branch information
ppoffice
committed
Jun 27, 2019
1 parent
469db43
commit 19f9772
Showing
2 changed files
with
41 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = /(?:<|<)\!--\uFFFD(\d+)--(?:>|>)/g; | ||
const rPlaceholder = /(?:<|<)epacse(?:[^>]*)(?:>|>)(\d+)(?:<|<)\/epacse(?:[^>]*)(?:>|>)/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; | ||
}); |
19f9772
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#101