From c76c50ade70f624d2352afbf5d8e314589be150b Mon Sep 17 00:00:00 2001 From: SukkaW Date: Tue, 10 Mar 2020 16:42:51 +0800 Subject: [PATCH] feat(backtick_code): remove strip-indent & add escape --- .../before_post_render/backtick_code_block.js | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/plugins/filter/before_post_render/backtick_code_block.js b/lib/plugins/filter/before_post_render/backtick_code_block.js index dd8202a29c..3a0dea5a58 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -1,20 +1,27 @@ 'use strict'; -const stripIndent = require('strip-indent'); let highlight, prismHighlight; const rBacktick = /^((?:[^\S\r\n]*>){0,3}[^\S\r\n]*)(`{3,}|~{3,}) *(.*) *\n([\s\S]+?)\s*\2(\n+|$)/gm; const rAllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/; const rLangCaption = /([^\s]+)\s*(.+)?/; +const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}'); + function backtickCodeBlock(data) { const hljsCfg = this.config.highlight || {}; const prismCfg = this.config.prismjs || {}; - // neither highlight or prismjs is enabled, return directly. - if (!hljsCfg.enable && !prismCfg.enable) return; - data.content = data.content.replace(rBacktick, ($0, start, $2, _args, content, end) => { + // neither highlight or prismjs is enabled, return escaped content directly. + if (!hljsCfg.enable && !prismCfg.enable) { + return start + + '' + + escapeSwigTag(content) + + '' + + end; + } + // Extrace langauge and caption of code blocks const args = _args.split('=').shift(); let lang, caption; @@ -54,7 +61,7 @@ function backtickCodeBlock(data) { lang }; - content = prismHighlight(stripIndent(content), options); + content = prismHighlight(content, options); } else if (hljsCfg.enable) { if (!highlight) highlight = require('hexo-util').highlight; @@ -81,14 +88,14 @@ function backtickCodeBlock(data) { } } - content = highlight(stripIndent(content), options); + content = highlight(content, options); } return start - + '' - + content.replace(/{/g, '{').replace(/}/g, '}') - + '' - + end; + + '' + + escapeSwigTag(content) + + '' + + end; }); }