Skip to content

Commit

Permalink
feat(backtick_code): remove strip-indent & add escape
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Mar 10, 2020
1 parent 78e187d commit c76c50a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/plugins/filter/before_post_render/backtick_code_block.js
Original file line number Diff line number Diff line change
@@ -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
+ '<escape>'
+ escapeSwigTag(content)
+ '</escape>'
+ end;
}

// Extrace langauge and caption of code blocks
const args = _args.split('=').shift();
let lang, caption;
Expand Down Expand Up @@ -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;

Expand All @@ -81,14 +88,14 @@ function backtickCodeBlock(data) {
}
}

content = highlight(stripIndent(content), options);
content = highlight(content, options);
}

return start
+ '<escape>'
+ content.replace(/{/g, '&#123;').replace(/}/g, '&#125;')
+ '</escape>'
+ end;
+ '<escape>'
+ escapeSwigTag(content)
+ '</escape>'
+ end;
});
}

Expand Down

0 comments on commit c76c50a

Please sign in to comment.