From c33a4cdbad7be954e438a4a962527b18f265660b Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 5 Nov 2022 10:12:09 +0800 Subject: [PATCH 01/12] refactor: highlight --- lib/extend/filter.js | 21 ++++++ lib/hexo/index.js | 8 +++ .../before_post_render/backtick_code_block.js | 63 ++---------------- lib/plugins/filter/highlight/highlight.js | 32 +++++++++ lib/plugins/filter/highlight/index.js | 14 ++++ lib/plugins/filter/highlight/prism.js | 24 +++++++ lib/plugins/filter/index.js | 1 + lib/plugins/tag/code.js | 66 +------------------ lib/plugins/tag/include_code.js | 38 ++--------- 9 files changed, 112 insertions(+), 155 deletions(-) create mode 100644 lib/plugins/filter/highlight/highlight.js create mode 100644 lib/plugins/filter/highlight/index.js create mode 100644 lib/plugins/filter/highlight/prism.js diff --git a/lib/extend/filter.js b/lib/extend/filter.js index 520b22668e..ab08288cf9 100644 --- a/lib/extend/filter.js +++ b/lib/extend/filter.js @@ -55,6 +55,11 @@ class Filter { if (index !== -1) list.splice(index, 1); } + queryCount(type) { + const filters = this.list(type); + return filters.length; + } + exec(type, data, options = {}) { const filters = this.list(type); if (filters.length === 0) return Promise.resolve(data); @@ -87,6 +92,22 @@ class Filter { return args[0]; } + + execFirstSync(type, data, options = {}) { + const filters = this.list(type); + const filtersLen = filters.length; + if (filtersLen === 0) return null; + + const ctx = options.context; + const args = options.args || []; + + args.unshift(data); + + const result = Reflect.apply(filters[0], ctx, args); + args[0] = result == null ? args[0] : result; + + return args[0]; + } } module.exports = Filter; diff --git a/lib/hexo/index.js b/lib/hexo/index.js index 30a1f78956..34e0365caf 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -478,6 +478,10 @@ class Hexo extends EventEmitter { }); } + queryFilterCount(type) { + return this.extend.filter.queryCount(type); + } + execFilter(type, data, options) { return this.extend.filter.exec(type, data, options); } @@ -485,6 +489,10 @@ class Hexo extends EventEmitter { execFilterSync(type, data, options) { return this.extend.filter.execSync(type, data, options); } + + execFirstFilterSync(type, data, options) { + return this.extend.filter.execFirstSync(type, data, options); + } } Hexo.lib_dir = libDir + sep; 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 871146c42e..d5569e8881 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -1,7 +1,5 @@ 'use strict'; -let highlight, prismHighlight; - const rBacktick = /^((?:[^\S\r\n]*>){0,3}[^\S\r\n]*)(`{3,}|~{3,})[^\S\r\n]*((?:.*?[^`\s])?)[^\S\r\n]*\n((?:[\s\S]*?\n)?)(?:(?:[^\S\r\n]*>){0,3}[^\S\r\n]*)\2[^\S\r\n]?(\n+|$)/gm; const rAllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/; const rLangCaption = /([^\s]+)\s*(.+)?/; @@ -11,16 +9,13 @@ const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}') function backtickCodeBlock(data) { const dataContent = data.content; - const hljsCfg = this.config.highlight || {}; - const prismCfg = this.config.prismjs || {}; - - if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || (!hljsCfg.enable && !prismCfg.enable)) return; + if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || !this.context.queryFilterCount('highlight')) return; data.content = dataContent.replace(rBacktick, ($0, start, $2, _args, _content, end) => { let content = _content.replace(/\n$/, ''); // neither highlight or prismjs is enabled, return escaped content directly. - if (!hljsCfg.enable && !prismCfg.enable) return escapeSwigTag($0); + if (!this.context.queryFilterCount('highlight')) return escapeSwigTag($0); // Extract language and caption of code blocks const args = _args.split('=').shift(); @@ -50,55 +45,11 @@ function backtickCodeBlock(data) { content = content.replace(regexp, ''); } - // Since prismjs have better performance, so prismjs should have higher priority. - if (prismCfg.enable) { - if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight; - - const options = { - lineNumber: prismCfg.line_number, - tab: prismCfg.tab_replace, - isPreprocess: prismCfg.preprocess, - lang, - caption - }; - - content = prismHighlight(content, options); - } else if (hljsCfg.enable) { - if (!highlight) highlight = require('hexo-util').highlight; - - const options = { - hljs: hljsCfg.hljs, - autoDetect: hljsCfg.auto_detect, - gutter: hljsCfg.line_number, - tab: hljsCfg.tab_replace, - wrap: hljsCfg.wrap, - lang, - languageAttr: hljsCfg.language_attr, - caption - }; - - if (options.gutter) { - hljsCfg.first_line_number = hljsCfg.first_line_number || 'always1'; - if (hljsCfg.first_line_number === 'inline') { - - // setup line number by inline - _args = _args.replace('=+', '='); - options.gutter = _args.includes('='); - - // setup firstLineNumber; - options.firstLine = options.gutter ? _args.split('=')[1] || 1 : 0; - } - } - - if (Array.isArray(hljsCfg.exclude_languages) && hljsCfg.exclude_languages.includes(options.lang)) { - // Only wrap with
- options.wrap = false; - options.gutter = false; - options.autoDetect = false; - } - - content = highlight(content, options); - } + const options = { + lang, + caption + }; + content = this.context.execFirstFilterSync('highlight', content, options); return start + '' diff --git a/lib/plugins/filter/highlight/highlight.js b/lib/plugins/filter/highlight/highlight.js new file mode 100644 index 0000000000..e392db6442 --- /dev/null +++ b/lib/plugins/filter/highlight/highlight.js @@ -0,0 +1,32 @@ +'use strict'; + +// Lazy require highlight.js +let highlight; + +function highlightFilter(code, options) { + const hljsCfg = this.config.highlight || {}; + const line_threshold = hljsCfg.line_threshold + ? hljsCfg.line_threshold : 0; + + const hljsOptions = { + caption: options.caption, + gutter: hljsCfg.line_number && options.lines_length > line_threshold, + hljs: hljsCfg.hljs, + lang: options.lang, + languageAttr: hljsCfg.language_attr, + tab: hljsCfg.tab_replace + }; + + if (Array.isArray(hljsCfg.exclude_languages) && hljsCfg.exclude_languages.includes(hljsOptions.lang)) { + // Only wrap with
+ hljsOptions.wrap = false; + hljsOptions.gutter = false; + hljsOptions.autoDetect = false; + } + + if (!highlight) highlight = require('hexo-util').highlight; + + return highlight(code, hljsOptions); +} + +module.exports = highlightFilter; diff --git a/lib/plugins/filter/highlight/index.js b/lib/plugins/filter/highlight/index.js new file mode 100644 index 0000000000..4a9cae6568 --- /dev/null +++ b/lib/plugins/filter/highlight/index.js @@ -0,0 +1,14 @@ +'use strict'; + +module.exports = ctx => { + const { filter } = ctx.extend; + const hljsCfg = this.config.highlight || {}; + const prismCfg = this.config.prismjs || {}; + + // Since prismjs have better performance, so prismjs should have higher priority. + if (prismCfg.enable) { + filter.register('highlight', require('./prism')); + } else if (hljsCfg.enable) { + filter.register('highlight', require('./highlight')); + } +}; diff --git a/lib/plugins/filter/highlight/prism.js b/lib/plugins/filter/highlight/prism.js new file mode 100644 index 0000000000..4b1a1e365e --- /dev/null +++ b/lib/plugins/filter/highlight/prism.js @@ -0,0 +1,24 @@ +'use strict'; + +// Lazy require prismjs +let prismHighlight; + +function prismFilter(code, options) { + const prismjsCfg = this.config.prismjs || {}; + const line_threshold = prismjsCfg.line_threshold + ? prismjsCfg.line_threshold : 0; + + const prismjsOptions = { + caption: options.caption, + isPreprocess: prismjsCfg.preprocess, + lang: options.lang, + lineNumber: prismjsCfg.line_number && options.lines_length > line_threshold, + tab: prismjsCfg.tab_replace + }; + + if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight; + + return prismHighlight(code, prismjsOptions); +} + +module.exports = prismFilter; diff --git a/lib/plugins/filter/index.js b/lib/plugins/filter/index.js index f349b99b29..f28ea91410 100644 --- a/lib/plugins/filter/index.js +++ b/lib/plugins/filter/index.js @@ -9,6 +9,7 @@ module.exports = ctx => { require('./before_exit')(ctx); require('./before_generate')(ctx); require('./template_locals')(ctx); + require('./highlight')(ctx); filter.register('new_post_path', require('./new_post_path')); filter.register('post_permalink', require('./post_permalink')); diff --git a/lib/plugins/tag/code.js b/lib/plugins/tag/code.js index ecb7ccce23..8029771517 100644 --- a/lib/plugins/tag/code.js +++ b/lib/plugins/tag/code.js @@ -4,9 +4,6 @@ const { escapeHTML } = require('hexo-util'); -// Lazy require highlight.js & prismjs -let highlight, prismHighlight; - const rCaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/\S+)\s+(.+)/i; const rCaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/\S+)/i; const rCaption = /\S[\S\s]*/; @@ -143,68 +140,7 @@ module.exports = ctx => function codeTag(args, content) { return `
${escapeHTML(content)}
`; } - const { lang, language_attr, firstLine, caption, line_number, line_threshold, mark, wrap } = parseArgs(args); - - if (prismjsCfg.enable) { - const shouldUseLineNumbers = typeof line_number !== 'undefined' ? line_number : prismjsCfg.line_number; - let surpassesLineThreshold; - - if (typeof line_threshold !== 'undefined') { - surpassesLineThreshold = content.split('\n').length > line_threshold; - } else { - surpassesLineThreshold = content.split('\n').length > (prismjsCfg.line_threshold || 0); - } - - const prismjsOption = { - lang, - firstLine, - caption, - lineNumber: shouldUseLineNumbers && surpassesLineThreshold, - mark, - tab: prismjsCfg.tab_replace, - isPreprocess: prismjsCfg.preprocess - }; - - if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight; - - content = prismHighlight(content, prismjsOption); - } else { - const shouldUseLineNumbers = typeof line_number !== 'undefined' - ? line_number : hljsCfg.line_number; - let surpassesLineThreshold; - - if (typeof line_threshold !== 'undefined') { - surpassesLineThreshold - = content.split('\n').length > line_threshold; - } else { - surpassesLineThreshold - = content.split('\n').length > (hljsCfg.line_threshold || 0); - } - - const hljsOption = { - lang: typeof lang !== 'undefined' ? lang : '', - firstLine, - caption, - gutter: shouldUseLineNumbers && surpassesLineThreshold, - hljs: hljsCfg.hljs, - mark, - tab: hljsCfg.tab_replace, - autoDetect: hljsCfg.auto_detect, - wrap: typeof wrap === 'boolean' ? wrap : hljsCfg.wrap, - languageAttr: typeof language_attr === 'boolean' ? language_attr : hljsCfg.language_attr - }; - - if (Array.isArray(hljsCfg.exclude_languages) && hljsCfg.exclude_languages.includes(hljsOption.lang)) { - // Only wrap with
- hljsOption.wrap = false; - hljsOption.gutter = false; - hljsOption.autoDetect = false; - } - - if (!highlight) highlight = require('hexo-util').highlight; - - content = highlight(content, hljsOption); - } + content = this.context.execFirstFilterSync('highlight', content, parseArgs(args)); return content.replace(/{/g, '{').replace(/}/g, '}'); }; diff --git a/lib/plugins/tag/include_code.js b/lib/plugins/tag/include_code.js index a024d81ac2..aba0bb5869 100644 --- a/lib/plugins/tag/include_code.js +++ b/lib/plugins/tag/include_code.js @@ -3,9 +3,6 @@ const { exists, readFile } = require('hexo-fs'); const { basename, extname, join, posix } = require('path'); -// Lazy require highlight.js & prismjs -let highlight, prismHighlight; - const rCaptionTitleFile = /(.*)?(?:\s+|^)(\/*\S+)/; const rLang = /\s*lang:(\w+)/i; const rFrom = /\s*from:(\d+)/i; @@ -57,9 +54,6 @@ module.exports = ctx => function includeCodeTag(args) { const title = match[1] || basename(path); const caption = `${title}view raw`; - const hljsCfg = ctx.config.highlight || {}; - const prismjsCfg = ctx.config.prismjs || {}; - return exists(src).then(exist => { if (exist) return readFile(src); }).then(code => { @@ -68,37 +62,13 @@ module.exports = ctx => function includeCodeTag(args) { const lines = code.split('\n'); code = lines.slice(from, to).join('\n').trim(); - if (prismjsCfg.enable) { - const line_threshold = prismjsCfg.line_threshold - ? prismjsCfg.line_threshold : 0; - - const prismjsOptions = { + if (this.context.queryFilterCount('highlight')) { + const options = { lang, caption, - lineNumber: prismjsCfg.line_number && lines.length > line_threshold, - tab: prismjsCfg.tab_replace, - isPreprocess: prismjsCfg.preprocess + lines_length: lines.length }; - - if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight; - - return prismHighlight(code, prismjsOptions); - } else if (hljsCfg.enable) { - const line_threshold = hljsCfg.line_threshold - ? hljsCfg.line_threshold : 0; - - const hljsOptions = { - lang, - languageAttr: hljsCfg.language_attr, - caption, - gutter: hljsCfg.line_number && lines.length > line_threshold, - hljs: hljsCfg.hljs, - tab: hljsCfg.tab_replace - }; - - if (!highlight) highlight = require('hexo-util').highlight; - - return highlight(code, hljsOptions); + return this.context.execFirstFilterSync('highlight', code, options); } return `
${code}
`; From 42cd604a55e1a86090c08f22c1f4ed9b874df3c7 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 5 Nov 2022 11:45:53 +0800 Subject: [PATCH 02/12] fix: undefined context --- lib/hexo/index.js | 3 +- lib/hexo/load_highlight.js | 3 + .../before_post_render/backtick_code_block.js | 86 +++++++++---------- .../filter/before_post_render/index.js | 2 +- lib/plugins/filter/highlight/highlight.js | 51 ++++++----- lib/plugins/filter/highlight/index.js | 8 +- lib/plugins/filter/highlight/prism.js | 35 ++++---- lib/plugins/tag/code.js | 14 ++- lib/plugins/tag/include_code.js | 4 +- test/scripts/filters/backtick_code_block.js | 7 +- test/scripts/hexo/post.js | 24 +++--- test/scripts/tags/code.js | 13 ++- test/scripts/tags/include_code.js | 13 ++- 13 files changed, 139 insertions(+), 124 deletions(-) create mode 100644 lib/hexo/load_highlight.js diff --git a/lib/hexo/index.js b/lib/hexo/index.js index 34e0365caf..61d637872c 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -240,7 +240,8 @@ class Hexo extends EventEmitter { 'update_package', // Update package.json 'load_config', // Load config 'load_theme_config', // Load alternate theme config - 'load_plugins' // Load external plugins & scripts + 'load_plugins', // Load external plugins & scripts + 'load_highlight' // Load highlight.js or prism.js ], name => require(`./${name}`)(this)).then(() => this.execFilter('after_init', null, { context: this })).then(() => { // Ready to go! this.emit('ready'); diff --git a/lib/hexo/load_highlight.js b/lib/hexo/load_highlight.js new file mode 100644 index 0000000000..c61572c3c8 --- /dev/null +++ b/lib/hexo/load_highlight.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('../plugins/filter/highlight'); 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 d5569e8881..012935f029 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -6,57 +6,57 @@ const rLangCaption = /([^\s]+)\s*(.+)?/; const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}'); -function backtickCodeBlock(data) { - const dataContent = data.content; +module.exports = ctx => { + return function backtickCodeBlock(data) { + const dataContent = data.content; - if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || !this.context.queryFilterCount('highlight')) return; + if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || !ctx.queryFilterCount('highlight')) return; - data.content = dataContent.replace(rBacktick, ($0, start, $2, _args, _content, end) => { - let content = _content.replace(/\n$/, ''); + data.content = dataContent.replace(rBacktick, ($0, start, $2, _args, _content, end) => { + let content = _content.replace(/\n$/, ''); - // neither highlight or prismjs is enabled, return escaped content directly. - if (!this.context.queryFilterCount('highlight')) return escapeSwigTag($0); + // neither highlight or prismjs is enabled, return escaped content directly. + if (!ctx.queryFilterCount('highlight')) return escapeSwigTag($0); - // Extract language and caption of code blocks - const args = _args.split('=').shift(); - let lang, caption; + // Extract language and caption of code blocks + const args = _args.split('=').shift(); + let lang, caption; - if (args) { - const match = rAllOptions.exec(args) || rLangCaption.exec(args); + if (args) { + const match = rAllOptions.exec(args) || rLangCaption.exec(args); - if (match) { - lang = match[1]; + if (match) { + lang = match[1]; - if (match[2]) { - caption = `${match[2]}`; + if (match[2]) { + caption = `${match[2]}`; - if (match[3]) { - caption += `${match[4] ? match[4] : 'link'}`; + if (match[3]) { + caption += `${match[4] ? match[4] : 'link'}`; + } } } } - } - - // PR #3765 - if (start.includes('>')) { - // heading of last line is already removed by the top RegExp "rBacktick" - const depth = start.split('>').length - 1; - const regexp = new RegExp(`^([^\\S\\r\\n]*>){0,${depth}}([^\\S\\r\\n]|$)`, 'mg'); - content = content.replace(regexp, ''); - } - - const options = { - lang, - caption - }; - content = this.context.execFirstFilterSync('highlight', content, options); - - return start - + '' - + escapeSwigTag(content) - + '' - + end; - }); -} - -module.exports = backtickCodeBlock; + + // PR #3765 + if (start.includes('>')) { + // heading of last line is already removed by the top RegExp "rBacktick" + const depth = start.split('>').length - 1; + const regexp = new RegExp(`^([^\\S\\r\\n]*>){0,${depth}}([^\\S\\r\\n]|$)`, 'mg'); + content = content.replace(regexp, ''); + } + + const options = { + lang, + caption + }; + content = ctx.execFirstFilterSync('highlight', content, { args: [options] }); + + return start + + '' + + escapeSwigTag(content) + + '' + + end; + }); + }; +}; diff --git a/lib/plugins/filter/before_post_render/index.js b/lib/plugins/filter/before_post_render/index.js index 934df0ad73..bcff7246b4 100644 --- a/lib/plugins/filter/before_post_render/index.js +++ b/lib/plugins/filter/before_post_render/index.js @@ -3,6 +3,6 @@ module.exports = ctx => { const { filter } = ctx.extend; - filter.register('before_post_render', require('./backtick_code_block')); + filter.register('before_post_render', require('./backtick_code_block')(ctx)); filter.register('before_post_render', require('./titlecase')); }; diff --git a/lib/plugins/filter/highlight/highlight.js b/lib/plugins/filter/highlight/highlight.js index e392db6442..0c73453cfa 100644 --- a/lib/plugins/filter/highlight/highlight.js +++ b/lib/plugins/filter/highlight/highlight.js @@ -3,30 +3,35 @@ // Lazy require highlight.js let highlight; -function highlightFilter(code, options) { - const hljsCfg = this.config.highlight || {}; - const line_threshold = hljsCfg.line_threshold - ? hljsCfg.line_threshold : 0; +module.exports = ctx => { + return function highlightFilter(code, options) { + const hljsCfg = ctx.config.highlight || {}; + const line_threshold = hljsCfg.line_threshold || 0; + const { shouldUseLineNumbers, surpassesLineThreshold } = options; + const gutter = (shouldUseLineNumbers || hljsCfg.line_number) && (surpassesLineThreshold || options.lines_length > line_threshold); - const hljsOptions = { - caption: options.caption, - gutter: hljsCfg.line_number && options.lines_length > line_threshold, - hljs: hljsCfg.hljs, - lang: options.lang, - languageAttr: hljsCfg.language_attr, - tab: hljsCfg.tab_replace - }; - - if (Array.isArray(hljsCfg.exclude_languages) && hljsCfg.exclude_languages.includes(hljsOptions.lang)) { - // Only wrap with
- hljsOptions.wrap = false; - hljsOptions.gutter = false; - hljsOptions.autoDetect = false; - } + const hljsOptions = { + autoDetect: hljsCfg.auto_detect, + caption: options.caption, + firstLine: options.firstLine, + gutter, + hljs: hljsCfg.hljs, + lang: options.lang, + languageAttr: hljsCfg.language_attr, + mark: options.mark, + tab: hljsCfg.tab_replace, + wrap: hljsCfg.wrap + }; - if (!highlight) highlight = require('hexo-util').highlight; + if (Array.isArray(hljsCfg.exclude_languages) && hljsCfg.exclude_languages.includes(hljsOptions.lang)) { + // Only wrap with
+ hljsOptions.wrap = false; + hljsOptions.gutter = false; + hljsOptions.autoDetect = false; + } - return highlight(code, hljsOptions); -} + if (!highlight) highlight = require('hexo-util').highlight; -module.exports = highlightFilter; + return highlight(code, hljsOptions); + }; +}; diff --git a/lib/plugins/filter/highlight/index.js b/lib/plugins/filter/highlight/index.js index 4a9cae6568..9242397990 100644 --- a/lib/plugins/filter/highlight/index.js +++ b/lib/plugins/filter/highlight/index.js @@ -2,13 +2,13 @@ module.exports = ctx => { const { filter } = ctx.extend; - const hljsCfg = this.config.highlight || {}; - const prismCfg = this.config.prismjs || {}; + const hljsCfg = ctx.config.highlight || {}; + const prismCfg = ctx.config.prismjs || {}; // Since prismjs have better performance, so prismjs should have higher priority. if (prismCfg.enable) { - filter.register('highlight', require('./prism')); + filter.register('highlight', require('./prism')(ctx)); } else if (hljsCfg.enable) { - filter.register('highlight', require('./highlight')); + filter.register('highlight', require('./highlight')(ctx)); } }; diff --git a/lib/plugins/filter/highlight/prism.js b/lib/plugins/filter/highlight/prism.js index 4b1a1e365e..a011f17806 100644 --- a/lib/plugins/filter/highlight/prism.js +++ b/lib/plugins/filter/highlight/prism.js @@ -3,22 +3,25 @@ // Lazy require prismjs let prismHighlight; -function prismFilter(code, options) { - const prismjsCfg = this.config.prismjs || {}; - const line_threshold = prismjsCfg.line_threshold - ? prismjsCfg.line_threshold : 0; +module.exports = ctx => { + return function(code, options) { + const prismjsCfg = ctx.config.prismjs || {}; + const line_threshold = prismjsCfg.line_threshold || 0; + const { shouldUseLineNumbers = false, surpassesLineThreshold = false } = options; + const lineNumber = (shouldUseLineNumbers || prismjsCfg.line_number) && (surpassesLineThreshold || options.lines_length > line_threshold); - const prismjsOptions = { - caption: options.caption, - isPreprocess: prismjsCfg.preprocess, - lang: options.lang, - lineNumber: prismjsCfg.line_number && options.lines_length > line_threshold, - tab: prismjsCfg.tab_replace - }; - - if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight; + const prismjsOptions = { + caption: options.caption, + firstLine: options.firstLine, + isPreprocess: prismjsCfg.preprocess, + lang: options.lang, + lineNumber, + mark: options.mark, + tab: prismjsCfg.tab_replace + }; - return prismHighlight(code, prismjsOptions); -} + if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight; -module.exports = prismFilter; + return prismHighlight(code, prismjsOptions); + }; +}; diff --git a/lib/plugins/tag/code.js b/lib/plugins/tag/code.js index 8029771517..bf83e720ff 100644 --- a/lib/plugins/tag/code.js +++ b/lib/plugins/tag/code.js @@ -117,11 +117,9 @@ function parseArgs(args) { } module.exports = ctx => function codeTag(args, content) { - const hljsCfg = ctx.config.highlight || {}; - const prismjsCfg = ctx.config.prismjs || {}; // If neither highlight.js nor prism.js is enabled, return escaped code directly - if (!hljsCfg.enable && !prismjsCfg.enable) { + if (!ctx.queryFilterCount('highlight')) { return `
${escapeHTML(content)}
`; } @@ -140,7 +138,15 @@ module.exports = ctx => function codeTag(args, content) { return `
${escapeHTML(content)}
`; } - content = this.context.execFirstFilterSync('highlight', content, parseArgs(args)); + const options = parseArgs(args); + const { line_number, line_threshold } = options; + if (typeof line_number !== 'undefined') { + options.shouldUseLineNumbers = line_number; + } + if (typeof line_threshold !== 'undefined') { + options.surpassesLineThreshold = content.split('\n').length > line_threshold; + } + content = ctx.execFirstFilterSync('highlight', content, { args: [options] }); return content.replace(/{/g, '{').replace(/}/g, '}'); }; diff --git a/lib/plugins/tag/include_code.js b/lib/plugins/tag/include_code.js index aba0bb5869..bf457e27bd 100644 --- a/lib/plugins/tag/include_code.js +++ b/lib/plugins/tag/include_code.js @@ -62,13 +62,13 @@ module.exports = ctx => function includeCodeTag(args) { const lines = code.split('\n'); code = lines.slice(from, to).join('\n').trim(); - if (this.context.queryFilterCount('highlight')) { + if (ctx.queryFilterCount('highlight')) { const options = { lang, caption, lines_length: lines.length }; - return this.context.execFirstFilterSync('highlight', code, options); + return ctx.execFirstFilterSync('highlight', code, { args: [options] }); } return `
${code}
`; diff --git a/test/scripts/filters/backtick_code_block.js b/test/scripts/filters/backtick_code_block.js index 990084b19c..be88d3a559 100644 --- a/test/scripts/filters/backtick_code_block.js +++ b/test/scripts/filters/backtick_code_block.js @@ -47,8 +47,7 @@ describe('Backtick code block', () => { const data = {content}; - hexo.config.highlight.enable = false; - hexo.config.prismjs.enable = false; + hexo.extend.filter.store.highlight = []; codeBlock(data); data.content.should.eql(content); }); @@ -503,8 +502,8 @@ describe('Backtick code block', () => { describe('prismjs', () => { beforeEach(() => { - hexo.config.highlight.enable = false; - hexo.config.prismjs.enable = true; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); }); it('default', () => { diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index 8927708bf4..e07ae4cfe9 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -903,8 +903,8 @@ describe('Post', () => { }); it('render() - shouln\'t break curly brackets', async () => { - hexo.config.prismjs.enable = true; - hexo.config.highlight.enable = false; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); const content = [ '\\begin{equation}', @@ -920,8 +920,8 @@ describe('Post', () => { data.content.should.include('\\begin{equation}'); data.content.should.include('\\end{equation}'); - hexo.config.prismjs.enable = false; - hexo.config.highlight.enable = true; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); }); // #2321 @@ -1320,8 +1320,8 @@ describe('Post', () => { }); it('render() - issue #4460', async () => { - hexo.config.prismjs.enable = true; - hexo.config.highlight.enable = false; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); const content = fixture.content_for_issue_4460; @@ -1332,13 +1332,13 @@ describe('Post', () => { data.content.should.not.include('hexoPostRenderEscape'); - hexo.config.prismjs.enable = false; - hexo.config.highlight.enable = true; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); }); it('render() - empty tag name', async () => { - hexo.config.prismjs.enable = true; - hexo.config.highlight.enable = false; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); const content = 'Disable rendering of Nunjucks tag `{{ }}` / `{% %}`'; @@ -1350,7 +1350,7 @@ describe('Post', () => { data.content.should.include(escapeSwigTag('{{ }}')); data.content.should.include(escapeSwigTag('{% %}')); - hexo.config.prismjs.enable = false; - hexo.config.highlight.enable = true; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); }); }); diff --git a/test/scripts/tags/code.js b/test/scripts/tags/code.js index 4a1220f9c1..a6a69a165f 100644 --- a/test/scripts/tags/code.js +++ b/test/scripts/tags/code.js @@ -114,12 +114,12 @@ describe('code', () => { }); it('disabled', () => { - hexo.config.highlight.enable = false; + hexo.extend.filter.store.highlight = []; const result = code('', fixture); result.should.eql('
' + escapeHTML(fixture) + '
'); - hexo.config.highlight.enable = true; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); }); it('first_line', () => { @@ -185,8 +185,8 @@ describe('code', () => { describe('prismjs', () => { beforeEach(() => { - hexo.config.highlight.enable = false; - hexo.config.prismjs.enable = true; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); }); it('default', () => { @@ -246,13 +246,12 @@ describe('code', () => { }); it('disabled', () => { - hexo.config.highlight.enable = false; - hexo.config.prismjs.enable = false; + hexo.extend.filter.store.highlight = []; const result = code('', fixture); result.should.eql('
' + escapeHTML(fixture) + '
'); - hexo.config.highlight.enable = true; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); }); it('first_line', () => { diff --git a/test/scripts/tags/include_code.js b/test/scripts/tags/include_code.js index 48af4a33c2..f39c228e38 100644 --- a/test/scripts/tags/include_code.js +++ b/test/scripts/tags/include_code.js @@ -30,8 +30,8 @@ describe('include_code', () => { describe('highlightjs', () => { it('default', async () => { - hexo.config.highlight.enable = true; - hexo.config.prismjs.enable = false; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); const expected = highlight(fixture, { lang: 'js', @@ -124,7 +124,7 @@ describe('include_code', () => { }); it('disabled', async () => { - hexo.config.highlight.enable = false; + hexo.extend.filter.store.highlight = []; const result = await code('test.js'); result.should.eql('
' + fixture + '
'); @@ -133,8 +133,8 @@ describe('include_code', () => { describe('prismjs', () => { beforeEach(() => { - hexo.config.highlight.enable = false; - hexo.config.prismjs.enable = true; + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); }); it('default', async () => { @@ -213,8 +213,7 @@ describe('include_code', () => { }); it('disabled', async () => { - hexo.config.highlight.enable = false; - hexo.config.prismjs.enable = false; + hexo.extend.filter.store.highlight = []; const result = await code('test.js'); result.should.eql('
' + fixture + '
'); From 7474bda2d039a558128a77940d84dca1b3ddd46d Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 5 Nov 2022 12:25:18 +0800 Subject: [PATCH 03/12] fix: incorrect line number --- .../filter/before_post_render/backtick_code_block.js | 3 ++- lib/plugins/filter/highlight/highlight.js | 7 ++++--- lib/plugins/filter/highlight/prism.js | 7 ++++--- lib/plugins/tag/code.js | 8 +------- test/scripts/filters/backtick_code_block.js | 7 ++++++- test/scripts/tags/code.js | 5 +++++ 6 files changed, 22 insertions(+), 15 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 012935f029..f86e75dd9a 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -48,7 +48,8 @@ module.exports = ctx => { const options = { lang, - caption + caption, + lines_length: content.split('\n').length }; content = ctx.execFirstFilterSync('highlight', content, { args: [options] }); diff --git a/lib/plugins/filter/highlight/highlight.js b/lib/plugins/filter/highlight/highlight.js index 0c73453cfa..6d3fbfb97b 100644 --- a/lib/plugins/filter/highlight/highlight.js +++ b/lib/plugins/filter/highlight/highlight.js @@ -6,9 +6,10 @@ let highlight; module.exports = ctx => { return function highlightFilter(code, options) { const hljsCfg = ctx.config.highlight || {}; - const line_threshold = hljsCfg.line_threshold || 0; - const { shouldUseLineNumbers, surpassesLineThreshold } = options; - const gutter = (shouldUseLineNumbers || hljsCfg.line_number) && (surpassesLineThreshold || options.lines_length > line_threshold); + const line_threshold = options.line_threshold || hljsCfg.line_threshold || 0; + const shouldUseLineNumbers = typeof options.line_number === 'undefined' ? hljsCfg.line_number : options.line_number; + const surpassesLineThreshold = options.lines_length > line_threshold; + const gutter = shouldUseLineNumbers && surpassesLineThreshold; const hljsOptions = { autoDetect: hljsCfg.auto_detect, diff --git a/lib/plugins/filter/highlight/prism.js b/lib/plugins/filter/highlight/prism.js index a011f17806..68b55531fe 100644 --- a/lib/plugins/filter/highlight/prism.js +++ b/lib/plugins/filter/highlight/prism.js @@ -6,9 +6,10 @@ let prismHighlight; module.exports = ctx => { return function(code, options) { const prismjsCfg = ctx.config.prismjs || {}; - const line_threshold = prismjsCfg.line_threshold || 0; - const { shouldUseLineNumbers = false, surpassesLineThreshold = false } = options; - const lineNumber = (shouldUseLineNumbers || prismjsCfg.line_number) && (surpassesLineThreshold || options.lines_length > line_threshold); + const line_threshold = options.line_threshold || prismjsCfg.line_threshold || 0; + const shouldUseLineNumbers = typeof options.line_number === 'undefined' ? prismjsCfg.line_number : options.line_number; + const surpassesLineThreshold = options.lines_length > line_threshold; + const lineNumber = shouldUseLineNumbers && surpassesLineThreshold; const prismjsOptions = { caption: options.caption, diff --git a/lib/plugins/tag/code.js b/lib/plugins/tag/code.js index bf83e720ff..6a9250375c 100644 --- a/lib/plugins/tag/code.js +++ b/lib/plugins/tag/code.js @@ -139,13 +139,7 @@ module.exports = ctx => function codeTag(args, content) { } const options = parseArgs(args); - const { line_number, line_threshold } = options; - if (typeof line_number !== 'undefined') { - options.shouldUseLineNumbers = line_number; - } - if (typeof line_threshold !== 'undefined') { - options.surpassesLineThreshold = content.split('\n').length > line_threshold; - } + options.lines_length = content.split('\n').length; content = ctx.execFirstFilterSync('highlight', content, { args: [options] }); return content.replace(/{/g, '{').replace(/}/g, '}'); diff --git a/test/scripts/filters/backtick_code_block.js b/test/scripts/filters/backtick_code_block.js index be88d3a559..24557dc6ff 100644 --- a/test/scripts/filters/backtick_code_block.js +++ b/test/scripts/filters/backtick_code_block.js @@ -6,7 +6,7 @@ const defaultConfig = require('../../../lib/hexo/default_config'); describe('Backtick code block', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(); - const codeBlock = require('../../../lib/plugins/filter/before_post_render/backtick_code_block').bind(hexo); + const codeBlock = require('../../../lib/plugins/filter/before_post_render/backtick_code_block')(hexo); const code = [ 'if (tired && night) {', @@ -74,6 +74,11 @@ describe('Backtick code block', () => { }); describe('highlightjs', () => { + beforeEach(() => { + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + }); + it('shorthand', () => { const data = { content: 'Hello, world!' diff --git a/test/scripts/tags/code.js b/test/scripts/tags/code.js index a6a69a165f..bb0d868c5c 100644 --- a/test/scripts/tags/code.js +++ b/test/scripts/tags/code.js @@ -32,6 +32,11 @@ describe('code', () => { } describe('highlightjs', () => { + beforeEach(() => { + hexo.extend.filter.store.highlight = []; + hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + }); + it('default', () => { const result = code('', fixture); result.should.eql(highlight(fixture)); From 8ed98e857d77d96d428364a7cd40b7d3a1b1c0f8 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 5 Nov 2022 14:36:46 +0800 Subject: [PATCH 04/12] fix: first_line_number --- .../filter/before_post_render/backtick_code_block.js | 7 +++++++ lib/plugins/filter/highlight/highlight.js | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) 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 f86e75dd9a..4f742c058c 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -51,6 +51,13 @@ module.exports = ctx => { caption, lines_length: content.split('\n').length }; + // setup line number by inline + _args = _args.replace('=+', '='); + + // setup firstLineNumber; + if (_args.includes('=')) { + options.firstLineNumber = _args.split('=')[1] || 1; + } content = ctx.execFirstFilterSync('highlight', content, { args: [options] }); return start diff --git a/lib/plugins/filter/highlight/highlight.js b/lib/plugins/filter/highlight/highlight.js index 6d3fbfb97b..9717a32aa4 100644 --- a/lib/plugins/filter/highlight/highlight.js +++ b/lib/plugins/filter/highlight/highlight.js @@ -10,6 +10,7 @@ module.exports = ctx => { const shouldUseLineNumbers = typeof options.line_number === 'undefined' ? hljsCfg.line_number : options.line_number; const surpassesLineThreshold = options.lines_length > line_threshold; const gutter = shouldUseLineNumbers && surpassesLineThreshold; + const languageAttr = typeof options.language_attr === 'undefined' ? hljsCfg.language_attr : options.language_attr; const hljsOptions = { autoDetect: hljsCfg.auto_detect, @@ -18,11 +19,18 @@ module.exports = ctx => { gutter, hljs: hljsCfg.hljs, lang: options.lang, - languageAttr: hljsCfg.language_attr, + languageAttr, mark: options.mark, tab: hljsCfg.tab_replace, wrap: hljsCfg.wrap }; + if (hljsCfg.first_line_number === 'inline') { + if (typeof options.firstLineNumber !== 'undefined') { + hljsOptions.firstLine = options.firstLineNumber; + } else { + hljsOptions.gutter = false; + } + } if (Array.isArray(hljsCfg.exclude_languages) && hljsCfg.exclude_languages.includes(hljsOptions.lang)) { // Only wrap with
From 5e9a72a96abb60a9d5788b3371bd16cdba869549 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 5 Nov 2022 16:58:44 +0800 Subject: [PATCH 05/12] new extend api: highlight --- lib/extend/filter.js | 16 ---------- lib/extend/highlight.js | 29 +++++++++++++++++++ lib/extend/index.js | 1 + lib/hexo/default_config.js | 1 + lib/hexo/index.js | 11 ++----- lib/hexo/load_highlight.js | 2 +- .../before_post_render/backtick_code_block.js | 9 ++++-- lib/plugins/filter/highlight/index.js | 14 --------- lib/plugins/filter/index.js | 1 - .../{filter => }/highlight/highlight.js | 0 lib/plugins/highlight/index.js | 8 +++++ lib/plugins/{filter => }/highlight/prism.js | 0 lib/plugins/tag/code.js | 7 +++-- lib/plugins/tag/include_code.js | 7 +++-- test/scripts/filters/backtick_code_block.js | 9 +++--- test/scripts/hexo/post.js | 19 +++++------- test/scripts/tags/code.js | 15 +++++----- test/scripts/tags/include_code.js | 11 ++++--- 18 files changed, 81 insertions(+), 79 deletions(-) create mode 100644 lib/extend/highlight.js delete mode 100644 lib/plugins/filter/highlight/index.js rename lib/plugins/{filter => }/highlight/highlight.js (100%) create mode 100644 lib/plugins/highlight/index.js rename lib/plugins/{filter => }/highlight/prism.js (100%) diff --git a/lib/extend/filter.js b/lib/extend/filter.js index ab08288cf9..966a971524 100644 --- a/lib/extend/filter.js +++ b/lib/extend/filter.js @@ -92,22 +92,6 @@ class Filter { return args[0]; } - - execFirstSync(type, data, options = {}) { - const filters = this.list(type); - const filtersLen = filters.length; - if (filtersLen === 0) return null; - - const ctx = options.context; - const args = options.args || []; - - args.unshift(data); - - const result = Reflect.apply(filters[0], ctx, args); - args[0] = result == null ? args[0] : result; - - return args[0]; - } } module.exports = Filter; diff --git a/lib/extend/highlight.js b/lib/extend/highlight.js new file mode 100644 index 0000000000..4419edb334 --- /dev/null +++ b/lib/extend/highlight.js @@ -0,0 +1,29 @@ +'use strict'; + +class Highlight { + constructor() { + this.store = {}; + } + + register(name, fn) { + if (typeof fn !== 'function') throw new TypeError('fn must be a function'); + + this.store[name] = fn; + } + + query(name) { + return name && this.store[name]; + } + + exec(name, options) { + const fn = this.store[name]; + + if (!fn) throw new TypeError(`highlighter ${name} is not registered`); + const ctx = options.context; + const args = options.args || []; + + return Reflect.apply(fn, ctx, args); + } +} + +module.exports = Highlight; diff --git a/lib/extend/index.js b/lib/extend/index.js index 5e4eb37b28..73ed1d602b 100644 --- a/lib/extend/index.js +++ b/lib/extend/index.js @@ -5,6 +5,7 @@ exports.Deployer = require('./deployer'); exports.Filter = require('./filter'); exports.Generator = require('./generator'); exports.Helper = require('./helper'); +exports.Highlight = require('./highlight'); exports.Injector = require('./injector'); exports.Migrator = require('./migrator'); exports.Processor = require('./processor'); diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index 07c0d5ed01..95ea520cc1 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -40,6 +40,7 @@ module.exports = { post_asset_folder: false, relative_link: false, future: true, + highlighter: 'highlight.js', highlight: { enable: true, auto_detect: false, diff --git a/lib/hexo/index.js b/lib/hexo/index.js index 61d637872c..7bbbdd2fb4 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -11,7 +11,7 @@ const Module = require('module'); const { runInThisContext } = require('vm'); const { version } = require('../../package.json'); const logger = require('hexo-log'); -const { Console, Deployer, Filter, Generator, Helper, Injector, Migrator, Processor, Renderer, Tag } = require('../extend'); +const { Console, Deployer, Filter, Generator, Helper, Highlight, Injector, Migrator, Processor, Renderer, Tag } = require('../extend'); const Render = require('./render'); const registerModels = require('./register_models'); const Post = require('./post'); @@ -123,6 +123,7 @@ class Hexo extends EventEmitter { filter: new Filter(), generator: new Generator(), helper: new Helper(), + highlight: new Highlight(), injector: new Injector(), migrator: new Migrator(), processor: new Processor(), @@ -479,10 +480,6 @@ class Hexo extends EventEmitter { }); } - queryFilterCount(type) { - return this.extend.filter.queryCount(type); - } - execFilter(type, data, options) { return this.extend.filter.exec(type, data, options); } @@ -490,10 +487,6 @@ class Hexo extends EventEmitter { execFilterSync(type, data, options) { return this.extend.filter.execSync(type, data, options); } - - execFirstFilterSync(type, data, options) { - return this.extend.filter.execFirstSync(type, data, options); - } } Hexo.lib_dir = libDir + sep; diff --git a/lib/hexo/load_highlight.js b/lib/hexo/load_highlight.js index c61572c3c8..7f352a5291 100644 --- a/lib/hexo/load_highlight.js +++ b/lib/hexo/load_highlight.js @@ -1,3 +1,3 @@ 'use strict'; -module.exports = require('../plugins/filter/highlight'); +module.exports = require('../plugins/highlight'); 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 4f742c058c..4732537618 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -10,13 +10,13 @@ module.exports = ctx => { return function backtickCodeBlock(data) { const dataContent = data.content; - if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || !ctx.queryFilterCount('highlight')) return; + if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || !ctx.extend.highlight.query(ctx.config.highlighter)) return; data.content = dataContent.replace(rBacktick, ($0, start, $2, _args, _content, end) => { let content = _content.replace(/\n$/, ''); // neither highlight or prismjs is enabled, return escaped content directly. - if (!ctx.queryFilterCount('highlight')) return escapeSwigTag($0); + if (!ctx.extend.highlight.query(ctx.config.highlighter)) return escapeSwigTag($0); // Extract language and caption of code blocks const args = _args.split('=').shift(); @@ -58,7 +58,10 @@ module.exports = ctx => { if (_args.includes('=')) { options.firstLineNumber = _args.split('=')[1] || 1; } - content = ctx.execFirstFilterSync('highlight', content, { args: [options] }); + content = ctx.extend.highlight.exec(ctx.config.highlighter, { + context: ctx, + args: [content, options] + }); return start + '' diff --git a/lib/plugins/filter/highlight/index.js b/lib/plugins/filter/highlight/index.js deleted file mode 100644 index 9242397990..0000000000 --- a/lib/plugins/filter/highlight/index.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -module.exports = ctx => { - const { filter } = ctx.extend; - const hljsCfg = ctx.config.highlight || {}; - const prismCfg = ctx.config.prismjs || {}; - - // Since prismjs have better performance, so prismjs should have higher priority. - if (prismCfg.enable) { - filter.register('highlight', require('./prism')(ctx)); - } else if (hljsCfg.enable) { - filter.register('highlight', require('./highlight')(ctx)); - } -}; diff --git a/lib/plugins/filter/index.js b/lib/plugins/filter/index.js index f28ea91410..f349b99b29 100644 --- a/lib/plugins/filter/index.js +++ b/lib/plugins/filter/index.js @@ -9,7 +9,6 @@ module.exports = ctx => { require('./before_exit')(ctx); require('./before_generate')(ctx); require('./template_locals')(ctx); - require('./highlight')(ctx); filter.register('new_post_path', require('./new_post_path')); filter.register('post_permalink', require('./post_permalink')); diff --git a/lib/plugins/filter/highlight/highlight.js b/lib/plugins/highlight/highlight.js similarity index 100% rename from lib/plugins/filter/highlight/highlight.js rename to lib/plugins/highlight/highlight.js diff --git a/lib/plugins/highlight/index.js b/lib/plugins/highlight/index.js new file mode 100644 index 0000000000..c5ed14442d --- /dev/null +++ b/lib/plugins/highlight/index.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = ctx => { + const { highlight } = ctx.extend; + + highlight.register('highlight.js', require('./highlight')(ctx)); + highlight.register('prismjs', require('./prism')(ctx)); +}; diff --git a/lib/plugins/filter/highlight/prism.js b/lib/plugins/highlight/prism.js similarity index 100% rename from lib/plugins/filter/highlight/prism.js rename to lib/plugins/highlight/prism.js diff --git a/lib/plugins/tag/code.js b/lib/plugins/tag/code.js index 6a9250375c..ca6ead4f70 100644 --- a/lib/plugins/tag/code.js +++ b/lib/plugins/tag/code.js @@ -119,7 +119,7 @@ function parseArgs(args) { module.exports = ctx => function codeTag(args, content) { // If neither highlight.js nor prism.js is enabled, return escaped code directly - if (!ctx.queryFilterCount('highlight')) { + if (!ctx.extend.highlight.query(ctx.config.highlighter)) { return `
${escapeHTML(content)}
`; } @@ -140,7 +140,10 @@ module.exports = ctx => function codeTag(args, content) { const options = parseArgs(args); options.lines_length = content.split('\n').length; - content = ctx.execFirstFilterSync('highlight', content, { args: [options] }); + content = ctx.extend.highlight.exec(ctx.config.highlighter, { + context: ctx, + args: [content, options] + }); return content.replace(/{/g, '{').replace(/}/g, '}'); }; diff --git a/lib/plugins/tag/include_code.js b/lib/plugins/tag/include_code.js index bf457e27bd..4676eb135b 100644 --- a/lib/plugins/tag/include_code.js +++ b/lib/plugins/tag/include_code.js @@ -62,13 +62,16 @@ module.exports = ctx => function includeCodeTag(args) { const lines = code.split('\n'); code = lines.slice(from, to).join('\n').trim(); - if (ctx.queryFilterCount('highlight')) { + if (ctx.extend.highlight.query(ctx.config.highlighter)) { const options = { lang, caption, lines_length: lines.length }; - return ctx.execFirstFilterSync('highlight', code, { args: [options] }); + return ctx.extend.highlight.exec(ctx.config.highlighter, { + context: ctx, + args: [code, options] + }); } return `
${code}
`; diff --git a/test/scripts/filters/backtick_code_block.js b/test/scripts/filters/backtick_code_block.js index 24557dc6ff..c2f77d1a5e 100644 --- a/test/scripts/filters/backtick_code_block.js +++ b/test/scripts/filters/backtick_code_block.js @@ -6,6 +6,7 @@ const defaultConfig = require('../../../lib/hexo/default_config'); describe('Backtick code block', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(); + require('../../../lib/plugins/highlight/')(hexo); const codeBlock = require('../../../lib/plugins/filter/before_post_render/backtick_code_block')(hexo); const code = [ @@ -47,7 +48,7 @@ describe('Backtick code block', () => { const data = {content}; - hexo.extend.filter.store.highlight = []; + hexo.config.highlighter = ''; codeBlock(data); data.content.should.eql(content); }); @@ -75,8 +76,7 @@ describe('Backtick code block', () => { describe('highlightjs', () => { beforeEach(() => { - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + hexo.config.highlighter = 'highlight.js'; }); it('shorthand', () => { @@ -507,8 +507,7 @@ describe('Backtick code block', () => { describe('prismjs', () => { beforeEach(() => { - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); + hexo.config.highlighter = 'prismjs'; }); it('default', () => { diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index e07ae4cfe9..3a0584eddf 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -12,6 +12,7 @@ const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}') describe('Post', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(join(__dirname, 'post_test')); + require('../../../lib/plugins/highlight/')(hexo); const { post } = hexo; const now = Date.now(); let clock; @@ -903,8 +904,7 @@ describe('Post', () => { }); it('render() - shouln\'t break curly brackets', async () => { - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); + hexo.config.highlighter = 'prismjs'; const content = [ '\\begin{equation}', @@ -920,8 +920,7 @@ describe('Post', () => { data.content.should.include('\\begin{equation}'); data.content.should.include('\\end{equation}'); - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + hexo.config.highlighter = 'highlight.js'; }); // #2321 @@ -1320,8 +1319,7 @@ describe('Post', () => { }); it('render() - issue #4460', async () => { - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); + hexo.config.highlighter = 'prismjs'; const content = fixture.content_for_issue_4460; @@ -1332,13 +1330,11 @@ describe('Post', () => { data.content.should.not.include('hexoPostRenderEscape'); - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + hexo.config.highlighter = 'highlight.js'; }); it('render() - empty tag name', async () => { - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); + hexo.config.highlighter = 'prismjs'; const content = 'Disable rendering of Nunjucks tag `{{ }}` / `{% %}`'; @@ -1350,7 +1346,6 @@ describe('Post', () => { data.content.should.include(escapeSwigTag('{{ }}')); data.content.should.include(escapeSwigTag('{% %}')); - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + hexo.config.highlighter = 'highlight.js'; }); }); diff --git a/test/scripts/tags/code.js b/test/scripts/tags/code.js index bb0d868c5c..4300dad6f9 100644 --- a/test/scripts/tags/code.js +++ b/test/scripts/tags/code.js @@ -6,6 +6,7 @@ const cheerio = require('cheerio'); describe('code', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(); + require('../../../lib/plugins/highlight/')(hexo); const codeTag = require('../../../lib/plugins/tag/code')(hexo); const { escapeHTML } = util; @@ -33,8 +34,7 @@ describe('code', () => { describe('highlightjs', () => { beforeEach(() => { - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + hexo.config.highlighter = 'highlight.js'; }); it('default', () => { @@ -119,12 +119,12 @@ describe('code', () => { }); it('disabled', () => { - hexo.extend.filter.store.highlight = []; + hexo.config.highlighter = ''; const result = code('', fixture); result.should.eql('
' + escapeHTML(fixture) + '
'); - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + hexo.config.highlighter = 'highlight.js'; }); it('first_line', () => { @@ -190,8 +190,7 @@ describe('code', () => { describe('prismjs', () => { beforeEach(() => { - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); + hexo.config.highlighter = 'prismjs'; }); it('default', () => { @@ -251,12 +250,12 @@ describe('code', () => { }); it('disabled', () => { - hexo.extend.filter.store.highlight = []; + hexo.config.highlighter = ''; const result = code('', fixture); result.should.eql('
' + escapeHTML(fixture) + '
'); - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + hexo.config.highlighter = 'highlight.js'; }); it('first_line', () => { diff --git a/test/scripts/tags/include_code.js b/test/scripts/tags/include_code.js index f39c228e38..170e63887c 100644 --- a/test/scripts/tags/include_code.js +++ b/test/scripts/tags/include_code.js @@ -8,6 +8,7 @@ const Promise = require('bluebird'); describe('include_code', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(join(__dirname, 'include_code_test')); + require('../../../lib/plugins/highlight/')(hexo); const includeCode = Promise.method(require('../../../lib/plugins/tag/include_code')(hexo)); const path = join(hexo.source_dir, hexo.config.code_dir, 'test.js'); const defaultCfg = JSON.parse(JSON.stringify(hexo.config)); @@ -30,8 +31,7 @@ describe('include_code', () => { describe('highlightjs', () => { it('default', async () => { - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo)); + hexo.config.highlighter = 'highlight.js'; const expected = highlight(fixture, { lang: 'js', @@ -124,7 +124,7 @@ describe('include_code', () => { }); it('disabled', async () => { - hexo.extend.filter.store.highlight = []; + hexo.config.highlighter = ''; const result = await code('test.js'); result.should.eql('
' + fixture + '
'); @@ -133,8 +133,7 @@ describe('include_code', () => { describe('prismjs', () => { beforeEach(() => { - hexo.extend.filter.store.highlight = []; - hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/prism')(hexo)); + hexo.config.highlighter = 'prismjs'; }); it('default', async () => { @@ -213,7 +212,7 @@ describe('include_code', () => { }); it('disabled', async () => { - hexo.extend.filter.store.highlight = []; + hexo.config.highlighter = ''; const result = await code('test.js'); result.should.eql('
' + fixture + '
'); From 3da4bdab6454d3c845e7f46ee00a79d59f42c462 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 5 Nov 2022 17:02:19 +0800 Subject: [PATCH 06/12] clean up --- lib/extend/filter.js | 5 ----- test/scripts/tags/code.js | 4 ---- 2 files changed, 9 deletions(-) diff --git a/lib/extend/filter.js b/lib/extend/filter.js index 966a971524..520b22668e 100644 --- a/lib/extend/filter.js +++ b/lib/extend/filter.js @@ -55,11 +55,6 @@ class Filter { if (index !== -1) list.splice(index, 1); } - queryCount(type) { - const filters = this.list(type); - return filters.length; - } - exec(type, data, options = {}) { const filters = this.list(type); if (filters.length === 0) return Promise.resolve(data); diff --git a/test/scripts/tags/code.js b/test/scripts/tags/code.js index 4300dad6f9..3b1690605c 100644 --- a/test/scripts/tags/code.js +++ b/test/scripts/tags/code.js @@ -33,10 +33,6 @@ describe('code', () => { } describe('highlightjs', () => { - beforeEach(() => { - hexo.config.highlighter = 'highlight.js'; - }); - it('default', () => { const result = code('', fixture); result.should.eql(highlight(fixture)); From 0088da5d2fd7464a576a2f8b378a55be75aa5410 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 5 Nov 2022 17:06:44 +0800 Subject: [PATCH 07/12] bind this --- lib/plugins/highlight/highlight.js | 68 +++++++++++++++--------------- lib/plugins/highlight/index.js | 4 +- lib/plugins/highlight/prism.js | 36 ++++++++-------- 3 files changed, 52 insertions(+), 56 deletions(-) diff --git a/lib/plugins/highlight/highlight.js b/lib/plugins/highlight/highlight.js index 9717a32aa4..c90ccd7994 100644 --- a/lib/plugins/highlight/highlight.js +++ b/lib/plugins/highlight/highlight.js @@ -3,44 +3,42 @@ // Lazy require highlight.js let highlight; -module.exports = ctx => { - return function highlightFilter(code, options) { - const hljsCfg = ctx.config.highlight || {}; - const line_threshold = options.line_threshold || hljsCfg.line_threshold || 0; - const shouldUseLineNumbers = typeof options.line_number === 'undefined' ? hljsCfg.line_number : options.line_number; - const surpassesLineThreshold = options.lines_length > line_threshold; - const gutter = shouldUseLineNumbers && surpassesLineThreshold; - const languageAttr = typeof options.language_attr === 'undefined' ? hljsCfg.language_attr : options.language_attr; +module.exports = function highlightFilter(code, options) { + const hljsCfg = this.config.highlight || {}; + const line_threshold = options.line_threshold || hljsCfg.line_threshold || 0; + const shouldUseLineNumbers = typeof options.line_number === 'undefined' ? hljsCfg.line_number : options.line_number; + const surpassesLineThreshold = options.lines_length > line_threshold; + const gutter = shouldUseLineNumbers && surpassesLineThreshold; + const languageAttr = typeof options.language_attr === 'undefined' ? hljsCfg.language_attr : options.language_attr; - const hljsOptions = { - autoDetect: hljsCfg.auto_detect, - caption: options.caption, - firstLine: options.firstLine, - gutter, - hljs: hljsCfg.hljs, - lang: options.lang, - languageAttr, - mark: options.mark, - tab: hljsCfg.tab_replace, - wrap: hljsCfg.wrap - }; - if (hljsCfg.first_line_number === 'inline') { - if (typeof options.firstLineNumber !== 'undefined') { - hljsOptions.firstLine = options.firstLineNumber; - } else { - hljsOptions.gutter = false; - } - } - - if (Array.isArray(hljsCfg.exclude_languages) && hljsCfg.exclude_languages.includes(hljsOptions.lang)) { - // Only wrap with
- hljsOptions.wrap = false; + const hljsOptions = { + autoDetect: hljsCfg.auto_detect, + caption: options.caption, + firstLine: options.firstLine, + gutter, + hljs: hljsCfg.hljs, + lang: options.lang, + languageAttr, + mark: options.mark, + tab: hljsCfg.tab_replace, + wrap: hljsCfg.wrap + }; + if (hljsCfg.first_line_number === 'inline') { + if (typeof options.firstLineNumber !== 'undefined') { + hljsOptions.firstLine = options.firstLineNumber; + } else { hljsOptions.gutter = false; - hljsOptions.autoDetect = false; } + } - if (!highlight) highlight = require('hexo-util').highlight; + if (Array.isArray(hljsCfg.exclude_languages) && hljsCfg.exclude_languages.includes(hljsOptions.lang)) { + // Only wrap with
+ hljsOptions.wrap = false; + hljsOptions.gutter = false; + hljsOptions.autoDetect = false; + } - return highlight(code, hljsOptions); - }; + if (!highlight) highlight = require('hexo-util').highlight; + + return highlight(code, hljsOptions); }; diff --git a/lib/plugins/highlight/index.js b/lib/plugins/highlight/index.js index c5ed14442d..9dc22fd7d4 100644 --- a/lib/plugins/highlight/index.js +++ b/lib/plugins/highlight/index.js @@ -3,6 +3,6 @@ module.exports = ctx => { const { highlight } = ctx.extend; - highlight.register('highlight.js', require('./highlight')(ctx)); - highlight.register('prismjs', require('./prism')(ctx)); + highlight.register('highlight.js', require('./highlight')); + highlight.register('prismjs', require('./prism')); }; diff --git a/lib/plugins/highlight/prism.js b/lib/plugins/highlight/prism.js index 68b55531fe..7d46461c96 100644 --- a/lib/plugins/highlight/prism.js +++ b/lib/plugins/highlight/prism.js @@ -3,26 +3,24 @@ // Lazy require prismjs let prismHighlight; -module.exports = ctx => { - return function(code, options) { - const prismjsCfg = ctx.config.prismjs || {}; - const line_threshold = options.line_threshold || prismjsCfg.line_threshold || 0; - const shouldUseLineNumbers = typeof options.line_number === 'undefined' ? prismjsCfg.line_number : options.line_number; - const surpassesLineThreshold = options.lines_length > line_threshold; - const lineNumber = shouldUseLineNumbers && surpassesLineThreshold; +module.exports = function(code, options) { + const prismjsCfg = this.config.prismjs || {}; + const line_threshold = options.line_threshold || prismjsCfg.line_threshold || 0; + const shouldUseLineNumbers = typeof options.line_number === 'undefined' ? prismjsCfg.line_number : options.line_number; + const surpassesLineThreshold = options.lines_length > line_threshold; + const lineNumber = shouldUseLineNumbers && surpassesLineThreshold; - const prismjsOptions = { - caption: options.caption, - firstLine: options.firstLine, - isPreprocess: prismjsCfg.preprocess, - lang: options.lang, - lineNumber, - mark: options.mark, - tab: prismjsCfg.tab_replace - }; + const prismjsOptions = { + caption: options.caption, + firstLine: options.firstLine, + isPreprocess: prismjsCfg.preprocess, + lang: options.lang, + lineNumber, + mark: options.mark, + tab: prismjsCfg.tab_replace + }; - if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight; + if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight; - return prismHighlight(code, prismjsOptions); - }; + return prismHighlight(code, prismjsOptions); }; From 05af9c306363c3710f92462b4b70dd6f763e04e6 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 5 Nov 2022 19:26:41 +0800 Subject: [PATCH 08/12] remove load_highlight --- lib/hexo/index.js | 4 ++-- lib/hexo/load_highlight.js | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) delete mode 100644 lib/hexo/load_highlight.js diff --git a/lib/hexo/index.js b/lib/hexo/index.js index 7bbbdd2fb4..58e63b49e2 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -231,6 +231,7 @@ class Hexo extends EventEmitter { require('../plugins/filter')(this); require('../plugins/generator')(this); require('../plugins/helper')(this); + require('../plugins/highlight')(this); require('../plugins/injector')(this); require('../plugins/processor')(this); require('../plugins/renderer')(this); @@ -241,8 +242,7 @@ class Hexo extends EventEmitter { 'update_package', // Update package.json 'load_config', // Load config 'load_theme_config', // Load alternate theme config - 'load_plugins', // Load external plugins & scripts - 'load_highlight' // Load highlight.js or prism.js + 'load_plugins' // Load external plugins & scripts ], name => require(`./${name}`)(this)).then(() => this.execFilter('after_init', null, { context: this })).then(() => { // Ready to go! this.emit('ready'); diff --git a/lib/hexo/load_highlight.js b/lib/hexo/load_highlight.js deleted file mode 100644 index 7f352a5291..0000000000 --- a/lib/hexo/load_highlight.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -module.exports = require('../plugins/highlight'); From 21149cc8eeb64c36111f2b7217690814c0184385 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 5 Nov 2022 20:35:19 +0800 Subject: [PATCH 09/12] update config --- lib/hexo/default_config.js | 2 -- lib/hexo/post.js | 2 +- test/scripts/hexo/load_config.js | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index 95ea520cc1..915b1dc03d 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -42,7 +42,6 @@ module.exports = { future: true, highlighter: 'highlight.js', highlight: { - enable: true, auto_detect: false, line_number: true, tab_replace: '', @@ -52,7 +51,6 @@ module.exports = { hljs: false }, prismjs: { - enable: false, preprocess: true, line_number: true, tab_replace: '' diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 7f0db61bbe..891a9f023d 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -406,7 +406,7 @@ class Post { } const options = data.markdown || {}; - if (!config.highlight.enable) options.highlight = null; + if (!config.highlighter) options.highlight = null; ctx.log.debug('Rendering post: %s', magenta(source)); // Render with markdown or other renderer diff --git a/test/scripts/hexo/load_config.js b/test/scripts/hexo/load_config.js index 1d6d20f6c0..ce9698d068 100644 --- a/test/scripts/hexo/load_config.js +++ b/test/scripts/hexo/load_config.js @@ -205,7 +205,7 @@ describe('Load config', () => { try { await writeFile(hexo.config_path, content); await loadConfig(hexo); - hexo.config.highlight.enable.should.be.true; + hexo.config.highlight.line_number.should.be.true; hexo.config.highlight.tab_replace.should.eql('yoooo'); } finally { await unlink(hexo.config_path); From 4e8acdd0a5fc73f53ae584dec49676d99ae5bc3d Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Tue, 13 Dec 2022 22:05:13 +0800 Subject: [PATCH 10/12] rename to syntax_highlighter --- lib/extend/highlight.js | 2 +- lib/hexo/default_config.js | 2 +- lib/hexo/post.js | 2 +- .../filter/before_post_render/backtick_code_block.js | 6 +++--- lib/plugins/tag/code.js | 4 ++-- lib/plugins/tag/include_code.js | 4 ++-- test/scripts/filters/backtick_code_block.js | 6 +++--- test/scripts/hexo/post.js | 12 ++++++------ test/scripts/tags/code.js | 10 +++++----- test/scripts/tags/include_code.js | 8 ++++---- 10 files changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/extend/highlight.js b/lib/extend/highlight.js index 4419edb334..d38a959cff 100644 --- a/lib/extend/highlight.js +++ b/lib/extend/highlight.js @@ -18,7 +18,7 @@ class Highlight { exec(name, options) { const fn = this.store[name]; - if (!fn) throw new TypeError(`highlighter ${name} is not registered`); + if (!fn) throw new TypeError(`syntax highlighter ${name} is not registered`); const ctx = options.context; const args = options.args || []; diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index 915b1dc03d..3c887a2400 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -40,7 +40,7 @@ module.exports = { post_asset_folder: false, relative_link: false, future: true, - highlighter: 'highlight.js', + syntax_highlighter: 'highlight.js', highlight: { auto_detect: false, line_number: true, diff --git a/lib/hexo/post.js b/lib/hexo/post.js index bfad097015..2b91d56187 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -417,7 +417,7 @@ class Post { } const options = data.markdown || {}; - if (!config.highlighter) options.highlight = null; + if (!config.syntax_highlighter) options.highlight = null; ctx.log.debug('Rendering post: %s', magenta(source)); // Render with markdown or other renderer 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 4732537618..32f3590e27 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -10,13 +10,13 @@ module.exports = ctx => { return function backtickCodeBlock(data) { const dataContent = data.content; - if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || !ctx.extend.highlight.query(ctx.config.highlighter)) return; + if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || !ctx.extend.highlight.query(ctx.config.syntax_highlighter)) return; data.content = dataContent.replace(rBacktick, ($0, start, $2, _args, _content, end) => { let content = _content.replace(/\n$/, ''); // neither highlight or prismjs is enabled, return escaped content directly. - if (!ctx.extend.highlight.query(ctx.config.highlighter)) return escapeSwigTag($0); + if (!ctx.extend.highlight.query(ctx.config.syntax_highlighter)) return escapeSwigTag($0); // Extract language and caption of code blocks const args = _args.split('=').shift(); @@ -58,7 +58,7 @@ module.exports = ctx => { if (_args.includes('=')) { options.firstLineNumber = _args.split('=')[1] || 1; } - content = ctx.extend.highlight.exec(ctx.config.highlighter, { + content = ctx.extend.highlight.exec(ctx.config.syntax_highlighter, { context: ctx, args: [content, options] }); diff --git a/lib/plugins/tag/code.js b/lib/plugins/tag/code.js index ca6ead4f70..d4620dcccc 100644 --- a/lib/plugins/tag/code.js +++ b/lib/plugins/tag/code.js @@ -119,7 +119,7 @@ function parseArgs(args) { module.exports = ctx => function codeTag(args, content) { // If neither highlight.js nor prism.js is enabled, return escaped code directly - if (!ctx.extend.highlight.query(ctx.config.highlighter)) { + if (!ctx.extend.highlight.query(ctx.config.syntax_highlighter)) { return `
${escapeHTML(content)}
`; } @@ -140,7 +140,7 @@ module.exports = ctx => function codeTag(args, content) { const options = parseArgs(args); options.lines_length = content.split('\n').length; - content = ctx.extend.highlight.exec(ctx.config.highlighter, { + content = ctx.extend.highlight.exec(ctx.config.syntax_highlighter, { context: ctx, args: [content, options] }); diff --git a/lib/plugins/tag/include_code.js b/lib/plugins/tag/include_code.js index 4676eb135b..8d9d1a9a95 100644 --- a/lib/plugins/tag/include_code.js +++ b/lib/plugins/tag/include_code.js @@ -62,13 +62,13 @@ module.exports = ctx => function includeCodeTag(args) { const lines = code.split('\n'); code = lines.slice(from, to).join('\n').trim(); - if (ctx.extend.highlight.query(ctx.config.highlighter)) { + if (ctx.extend.highlight.query(ctx.config.syntax_highlighter)) { const options = { lang, caption, lines_length: lines.length }; - return ctx.extend.highlight.exec(ctx.config.highlighter, { + return ctx.extend.highlight.exec(ctx.config.syntax_highlighter, { context: ctx, args: [code, options] }); diff --git a/test/scripts/filters/backtick_code_block.js b/test/scripts/filters/backtick_code_block.js index c2f77d1a5e..4b0d75e31b 100644 --- a/test/scripts/filters/backtick_code_block.js +++ b/test/scripts/filters/backtick_code_block.js @@ -48,7 +48,7 @@ describe('Backtick code block', () => { const data = {content}; - hexo.config.highlighter = ''; + hexo.config.syntax_highlighter = ''; codeBlock(data); data.content.should.eql(content); }); @@ -76,7 +76,7 @@ describe('Backtick code block', () => { describe('highlightjs', () => { beforeEach(() => { - hexo.config.highlighter = 'highlight.js'; + hexo.config.syntax_highlighter = 'highlight.js'; }); it('shorthand', () => { @@ -507,7 +507,7 @@ describe('Backtick code block', () => { describe('prismjs', () => { beforeEach(() => { - hexo.config.highlighter = 'prismjs'; + hexo.config.syntax_highlighter = 'prismjs'; }); it('default', () => { diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index 2bc207b3d5..23f39c9b1a 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -914,7 +914,7 @@ describe('Post', () => { }); it('render() - shouln\'t break curly brackets', async () => { - hexo.config.highlighter = 'prismjs'; + hexo.config.syntax_highlighter = 'prismjs'; const content = [ '\\begin{equation}', @@ -930,7 +930,7 @@ describe('Post', () => { data.content.should.include('\\begin{equation}'); data.content.should.include('\\end{equation}'); - hexo.config.highlighter = 'highlight.js'; + hexo.config.syntax_highlighter = 'highlight.js'; }); // #2321 @@ -1329,7 +1329,7 @@ describe('Post', () => { }); it('render() - issue #4460', async () => { - hexo.config.highlighter = 'prismjs'; + hexo.config.syntax_highlighter = 'prismjs'; const content = fixture.content_for_issue_4460; @@ -1340,11 +1340,11 @@ describe('Post', () => { data.content.should.not.include('hexoPostRenderEscape'); - hexo.config.highlighter = 'highlight.js'; + hexo.config.syntax_highlighter = 'highlight.js'; }); it('render() - empty tag name', async () => { - hexo.config.highlighter = 'prismjs'; + hexo.config.syntax_highlighter = 'prismjs'; const content = 'Disable rendering of Nunjucks tag `{{ }}` / `{% %}`'; @@ -1356,6 +1356,6 @@ describe('Post', () => { data.content.should.include(escapeSwigTag('{{ }}')); data.content.should.include(escapeSwigTag('{% %}')); - hexo.config.highlighter = 'highlight.js'; + hexo.config.syntax_highlighter = 'highlight.js'; }); }); diff --git a/test/scripts/tags/code.js b/test/scripts/tags/code.js index 3b1690605c..406fe69c24 100644 --- a/test/scripts/tags/code.js +++ b/test/scripts/tags/code.js @@ -115,12 +115,12 @@ describe('code', () => { }); it('disabled', () => { - hexo.config.highlighter = ''; + hexo.config.syntax_highlighter = ''; const result = code('', fixture); result.should.eql('
' + escapeHTML(fixture) + '
'); - hexo.config.highlighter = 'highlight.js'; + hexo.config.syntax_highlighter = 'highlight.js'; }); it('first_line', () => { @@ -186,7 +186,7 @@ describe('code', () => { describe('prismjs', () => { beforeEach(() => { - hexo.config.highlighter = 'prismjs'; + hexo.config.syntax_highlighter = 'prismjs'; }); it('default', () => { @@ -246,12 +246,12 @@ describe('code', () => { }); it('disabled', () => { - hexo.config.highlighter = ''; + hexo.config.syntax_highlighter = ''; const result = code('', fixture); result.should.eql('
' + escapeHTML(fixture) + '
'); - hexo.config.highlighter = 'highlight.js'; + hexo.config.syntax_highlighter = 'highlight.js'; }); it('first_line', () => { diff --git a/test/scripts/tags/include_code.js b/test/scripts/tags/include_code.js index 170e63887c..ca8b4c7884 100644 --- a/test/scripts/tags/include_code.js +++ b/test/scripts/tags/include_code.js @@ -31,7 +31,7 @@ describe('include_code', () => { describe('highlightjs', () => { it('default', async () => { - hexo.config.highlighter = 'highlight.js'; + hexo.config.syntax_highlighter = 'highlight.js'; const expected = highlight(fixture, { lang: 'js', @@ -124,7 +124,7 @@ describe('include_code', () => { }); it('disabled', async () => { - hexo.config.highlighter = ''; + hexo.config.syntax_highlighter = ''; const result = await code('test.js'); result.should.eql('
' + fixture + '
'); @@ -133,7 +133,7 @@ describe('include_code', () => { describe('prismjs', () => { beforeEach(() => { - hexo.config.highlighter = 'prismjs'; + hexo.config.syntax_highlighter = 'prismjs'; }); it('default', async () => { @@ -212,7 +212,7 @@ describe('include_code', () => { }); it('disabled', async () => { - hexo.config.highlighter = ''; + hexo.config.syntax_highlighter = ''; const result = await code('test.js'); result.should.eql('
' + fixture + '
'); From 30403c1175c595a9d42469df847c13ba32bde4c2 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Tue, 13 Dec 2022 22:42:56 +0800 Subject: [PATCH 11/12] rename to SyntaxHighlight --- lib/extend/highlight.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/extend/highlight.js b/lib/extend/highlight.js index d38a959cff..56d83ba2a8 100644 --- a/lib/extend/highlight.js +++ b/lib/extend/highlight.js @@ -1,6 +1,6 @@ 'use strict'; -class Highlight { +class SyntaxHighlight { constructor() { this.store = {}; } @@ -26,4 +26,4 @@ class Highlight { } } -module.exports = Highlight; +module.exports = SyntaxHighlight; From 46021e003dcf510886f09f5a543d6945d0e869ba Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Wed, 14 Dec 2022 15:04:22 +0800 Subject: [PATCH 12/12] rename to syntax_highlight.js --- lib/extend/index.js | 2 +- lib/extend/{highlight.js => syntax_highlight.js} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/extend/{highlight.js => syntax_highlight.js} (100%) diff --git a/lib/extend/index.js b/lib/extend/index.js index 98d36799ce..f4ddd1a2e5 100644 --- a/lib/extend/index.js +++ b/lib/extend/index.js @@ -5,7 +5,7 @@ const Deployer = require('./deployer'); const Filter = require('./filter'); const Generator = require('./generator'); const Helper = require('./helper'); -const Highlight = require('./highlight'); +const Highlight = require('./syntax_highlight'); const Injector = require('./injector'); const Migrator = require('./migrator'); const Processor = require('./processor'); diff --git a/lib/extend/highlight.js b/lib/extend/syntax_highlight.js similarity index 100% rename from lib/extend/highlight.js rename to lib/extend/syntax_highlight.js