Skip to content

Commit

Permalink
refactor/feat(tag/code): bring up prismjs again
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jun 5, 2020
1 parent 4124c2b commit c04b9b4
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions lib/plugins/tag/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Based on: https://raw.github.com/imathis/octopress/master/plugins/code_block.rb

const { escapeHTML, highlight } = require('hexo-util');
const { escapeHTML, highlight, prismHighlight } = require('hexo-util');

const rCaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/\S+)\s+(.+)/i;
const rCaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/\S+)/i;
Expand All @@ -25,13 +25,13 @@ const rCaption = /\S[\S\s]*/;
* Example: `mark:1,4-7,10` will mark line 1, 4 to 7 and 10.
* @param {Object} wrap Wrap the code block in <table>, value must be a boolean
* @returns {String} Code snippet with code highlighting
*/
*/

function getHighlightOptions(config, args) {
function parseArgs(args) {
const _else = [];
const len = args.length;
let lang = '';
let { line_number, wrap } = config;
let lang,
line_number, wrap;
let firstLine = 1;
const mark = [];
for (let i = 0; i < len; i++) {
Expand Down Expand Up @@ -86,8 +86,9 @@ function getHighlightOptions(config, args) {
}

const arg = _else.join(' ');
let caption = '';
let match;
// eslint-disable-next-line one-var
let match, caption = '';

if ((match = arg.match(rCaptionUrlTitle)) != null) {
caption = `<span>${match[1]}</span><a href="${match[2]}">${match[3]}</a>`;
} else if ((match = arg.match(rCaptionUrl)) != null) {
Expand All @@ -100,37 +101,60 @@ function getHighlightOptions(config, args) {
lang,
firstLine,
caption,
gutter: line_number,
hljs: config.hljs,
line_number,
mark,
tab: config.tab_replace,
autoDetect: config.auto_detect,
wrap
};
}

module.exports = ctx => function codeTag(args, content) {
const config = ctx.config.highlight || {};
let { enable } = config;
const hljsCfg = ctx.config.highlight || {};
const prismjsCfg = ctx.config.prismjs || {};

let index;
let enableHighlight = true;

if ((index = args.findIndex(item => item.startsWith('highlight:'))) !== -1) {
const arg = args[index];
const _enable = arg.slice(10);
enable = _enable === 'true';
const highlightStr = arg.slice(10);
enableHighlight = highlightStr === 'true';
args.splice(index, 1);
}

if (!enable) {
content = escapeHTML(content);
return `<pre><code>${content}</code></pre>`;
// If 'hilight: false' is given, return escaped code directly
// If neither highlight.js nor prism.js is enabled, return escaped code directly
if (!enableHighlight || (!hljsCfg.enable && !prismjsCfg.enable)) {
return `<pre><code>${escapeHTML(content)}</code></pre>`;
}

content = highlight(content, getHighlightOptions(config, args));

content = content.replace(/{/g, '&#123;')
.replace(/}/g, '&#125;');
const { lang, firstLine, caption, line_number, mark, wrap } = parseArgs(args);

if (prismjsCfg.enable) {
const prismjsOption = {
lang,
firstLine,
lineNumber: typeof line_number !== 'undefined' ? line_number : prismjsCfg.line_number,
mark,
tab: prismjsCfg.tab_replace,
isPreprocess: prismjsCfg.preprocess
};

content = prismHighlight(content, prismjsOption);
} else {
const hljsOption = {
lang: typeof lang !== 'undefined' ? lang : '',
firstLine,
caption,
gutter: typeof line_number !== 'undefined' ? line_number : hljsCfg.line_number,
hljs: hljsCfg.hljs,
mark,
tab: hljsCfg.tab_replace,
autoDetect: hljsCfg.auto_detect,
wrap: typeof wrap === 'undefined' ? wrap : hljsCfg.wrap
};

content = highlight(content, hljsOption);
}

return content;
return content.replace(/{/g, '&#123;').replace(/}/g, '&#125;');
};

0 comments on commit c04b9b4

Please sign in to comment.