Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependency highlight.js #246

Merged
merged 8 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 25 additions & 35 deletions lib/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const hljs = require('highlight.js');
const stripIndent = require('strip-indent');
const alias = require('../highlight_alias.json');
const escapeHTML = require('./escape_html');

function highlightUtil(str, options = {}) {
if (typeof str !== 'string') throw new TypeError('str must be a string!');
Expand Down Expand Up @@ -101,49 +100,40 @@ function highlight(str, options) {
let { lang } = options;
const { autoDetect = false } = options;

if (!lang && autoDetect) {
if (lang) {
lang = lang.toLowerCase();
} else if (autoDetect) {
const result = hljs.highlightAuto(str);
if (result.relevance > 0 && result.language) lang = result.language;
return closeTags(result);
}

if (!lang) {
lang = 'plain';
if (!lang || !alias.aliases[lang]) {
lang = 'plaintext';
}

const result = {
value: escapeHTML(str),
language: lang.toLowerCase()
};

if (result.language === 'plain') {
return result;
}

if (!alias.aliases[result.language]) {
result.language = 'plain';
return result;
}
const res = hljs.highlight(str, {
language: lang,
ignoreIllegals: true
});

return tryHighlight(str, result.language) || result;
return closeTags(res);
}

function tryHighlight(str, lang) {
try {
const matching = str.match(/(\r?\n)/);
const separator = matching ? matching[1] : '';
const lines = matching ? str.split(separator) : [str];
let result = hljs.highlight(lang, lines.shift());
let html = result.value;
while (lines.length > 0) {
result = hljs.highlight(lang, lines.shift(), false, result.top);
html += separator + result.value;
}

result.value = html;
return result;
} catch (err) {
// https://github.com/hexojs/hexo-util/issues/10
function closeTags(res) {
const tokenStack = [];

}
res.value = res.value.split('\n').map(line => {
const prepend = tokenStack.map(token => `<span class="${token}">`).join('');
const matches = line.matchAll(/(<span class="(.*?)">|<\/span>)/g);
for (const match of matches) {
if (match[0] === '</span>') tokenStack.shift();
else tokenStack.unshift(match[2]);
stevenjoezhang marked this conversation as resolved.
Show resolved Hide resolved
}
const append = '</span>'.repeat(tokenStack.length);
return prepend + line + append;
}).join('\n');
return res;
}

module.exports = highlightUtil;
2 changes: 1 addition & 1 deletion lib/prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function PrismUtil(str, options = {}) {
} = options;

// To be consistent with highlight.js
const language = lang === 'plain' || lang === 'none' ? 'none' : lang;
const language = lang === 'plaintext' || lang === 'none' ? 'none' : lang;

const preTagClassArr = [];
const preTagAttrArr = [];
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
"camel-case": "^4.0.0",
"cross-spawn": "^7.0.0",
"deepmerge": "^4.2.2",
"highlight.js": "^10.0.0",
"highlight.js": "^10.7.1",
"htmlparser2": "^6.0.0",
"prismjs": "^1.17.1",
"strip-indent": "^3.0.0"
},
"engines": {
"node": ">=10.13.0"
"node": ">=12.4.0"
}
}
34 changes: 23 additions & 11 deletions test/highlight.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const testJson = {

const testString = JSON.stringify(testJson, null, ' ');

const start = '<figure class="highlight plain"><table><tr>';
const start = '<figure class="highlight plaintext"><table><tr>';
const end = '</tr></table></figure>';

const gutterStart = '<td class="gutter"><pre>';
Expand All @@ -37,7 +37,9 @@ function code(str, lang) {
let data;

if (lang) {
data = hljs.highlight(lang.toLowerCase(), str);
data = hljs.highlight(str, {
language: lang.toLowerCase()
});
} else if (lang === null) {
data = {value: str};
} else {
Expand Down Expand Up @@ -93,7 +95,7 @@ describe('highlight', () => {
it('wrap: false (without hljs, without lang)', done => {
const result = highlight(testString, {gutter: false, wrap: false});
result.should.eql([
'<pre><code class="highlight plain">',
'<pre><code class="highlight plaintext">',
entities.encode(testString),
'</code></pre>'
].join(''));
Expand All @@ -103,7 +105,7 @@ describe('highlight', () => {
it('wrap: false (with hljs, without lang)', done => {
const result = highlight(testString, {gutter: false, wrap: false, hljs: true});
result.should.eql([
'<pre><code class="hljs plain">',
'<pre><code class="hljs plaintext">',
entities.encode(testString),
'</code></pre>'
].join(''));
Expand All @@ -115,7 +117,9 @@ describe('highlight', () => {
hljs.configure({classPrefix: ''});
result.should.eql([
'<pre><code class="highlight json">',
hljs.highlight('json', testString).value,
hljs.highlight(testString, {
language: 'json'
}).value,
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
Expand All @@ -126,7 +130,9 @@ describe('highlight', () => {
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'<pre><code class="hljs json">',
hljs.highlight('json', testString).value,
hljs.highlight(testString, {
language: 'json'
}).value,
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
Expand All @@ -137,7 +143,9 @@ describe('highlight', () => {
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'<pre><code class="hljs json">',
hljs.highlight('json', testString).value.replace('{', '<mark>{</mark>'),
hljs.highlight(testString, {
language: 'json'
}).value.replace('{', '<mark>{</mark>'),
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
Expand All @@ -148,7 +156,9 @@ describe('highlight', () => {
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'<pre><code class="hljs json">',
hljs.highlight('json', testString).value,
hljs.highlight(testString, {
language: 'json'
}).value,
'\n</code></pre>'
].join(''));
validateHtmlAsync(result, done);
Expand Down Expand Up @@ -199,7 +209,7 @@ describe('highlight', () => {
});

result.should.eql([
`<figure class="highlight plain"><figcaption>${caption}</figcaption><table><tr>`,
`<figure class="highlight plaintext"><figcaption>${caption}</figcaption><table><tr>`,
gutter(1, 4),
code(testString),
end
Expand All @@ -218,7 +228,7 @@ describe('highlight', () => {
result.should.eql([
'<pre>',
`<div class="caption">${caption}</div>`,
'<code class="highlight plain">',
'<code class="highlight plaintext">',
entities.encode(testString),
'</code></pre>'
].join(''));
Expand Down Expand Up @@ -252,7 +262,9 @@ describe('highlight', () => {
result.should.eql([
'<pre><code class="hljs json">',
spaces,
hljs.highlight('json', testString).value,
hljs.highlight(testString, {
language: 'json'
}).value,
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
Expand Down