Skip to content

Commit

Permalink
new extend api: highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Nov 5, 2022
1 parent 8ed98e8 commit 5e9a72a
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 79 deletions.
16 changes: 0 additions & 16 deletions lib/extend/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
29 changes: 29 additions & 0 deletions lib/extend/highlight.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions lib/extend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = {
post_asset_folder: false,
relative_link: false,
future: true,
highlighter: 'highlight.js',
highlight: {
enable: true,
auto_detect: false,
Expand Down
11 changes: 2 additions & 9 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -479,21 +480,13 @@ class Hexo extends EventEmitter {
});
}

queryFilterCount(type) {
return this.extend.filter.queryCount(type);
}

execFilter(type, data, options) {
return this.extend.filter.exec(type, data, options);
}

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;
Expand Down
2 changes: 1 addition & 1 deletion lib/hexo/load_highlight.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict';

module.exports = require('../plugins/filter/highlight');
module.exports = require('../plugins/highlight');
9 changes: 6 additions & 3 deletions lib/plugins/filter/before_post_render/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
+ '<hexoPostRenderCodeBlock>'
Expand Down
14 changes: 0 additions & 14 deletions lib/plugins/filter/highlight/index.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/plugins/filter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions lib/plugins/highlight/index.js
Original file line number Diff line number Diff line change
@@ -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));
};
File renamed without changes.
7 changes: 5 additions & 2 deletions lib/plugins/tag/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<pre><code>${escapeHTML(content)}</code></pre>`;
}

Expand All @@ -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, '&#123;').replace(/}/g, '&#125;');
};
7 changes: 5 additions & 2 deletions lib/plugins/tag/include_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<pre><code>${code}</code></pre>`;
Expand Down
9 changes: 4 additions & 5 deletions test/scripts/filters/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
19 changes: 7 additions & 12 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const escapeSwigTag = str => str.replace(/{/g, '&#123;').replace(/}/g, '&#125;')
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;
Expand Down Expand Up @@ -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}',
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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 `{{ }}` / `{% %}`';

Expand All @@ -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';
});
});
15 changes: 7 additions & 8 deletions test/scripts/tags/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -119,12 +119,12 @@ describe('code', () => {
});

it('disabled', () => {
hexo.extend.filter.store.highlight = [];
hexo.config.highlighter = '';

const result = code('', fixture);
result.should.eql('<pre><code>' + escapeHTML(fixture) + '</code></pre>');

hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo));
hexo.config.highlighter = 'highlight.js';
});

it('first_line', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -251,12 +250,12 @@ describe('code', () => {
});

it('disabled', () => {
hexo.extend.filter.store.highlight = [];
hexo.config.highlighter = '';

const result = code('', fixture);
result.should.eql('<pre><code>' + escapeHTML(fixture) + '</code></pre>');

hexo.extend.filter.register('highlight', require('../../../lib/plugins/filter/highlight/highlight')(hexo));
hexo.config.highlighter = 'highlight.js';
});

it('first_line', () => {
Expand Down

0 comments on commit 5e9a72a

Please sign in to comment.