diff --git a/scripts/syntaxHighlight.js b/scripts/syntaxHighlight.js index fb352d03..42494be0 100644 --- a/scripts/syntaxHighlight.js +++ b/scripts/syntaxHighlight.js @@ -1,12 +1,26 @@ -const fs = require('fs'); -const vsctm = require('vscode-textmate'); -const onig = require('onigasm'); -const onigWasm = require.resolve('onigasm/lib/onigasm.wasm'); +const fs = require("fs"); +const vsctm = require("vscode-textmate"); +const onig = require("onigasm"); +const onigWasm = require.resolve("onigasm/lib/onigasm.wasm"); const wasm = fs.readFileSync(onigWasm).buffer; -hexo.extend.filter.register('after_init', initialize); -hexo.extend.filter.register('marked:renderer', hookRenderer); +hexo.extend.filter.register("after_init", initialize); +hexo.extend.filter.register("marked:renderer", hookRenderer); + +function escapeHTML(str) { + return str.replace( + /[&<>'"]/g, + (tag) => + ({ + "&": "&", + "<": "<", + ">": ">", + "'": "'", + '"': """, + }[tag] || tag) + ); +} async function initialize() { await onig.loadWASM(wasm); @@ -14,59 +28,68 @@ async function initialize() { const registry = new vsctm.Registry({ onigLib: Promise.resolve({ createOnigScanner: (sources) => new onig.OnigScanner(sources), - createOnigString: (str) => new onig.OnigString(str) + createOnigString: (str) => new onig.OnigString(str), }), loadGrammar: async (scopeName) => { - if (scopeName === 'source.grain') { - let path = './grain-language-server/editor-extensions/vscode/syntaxes/grain.json' - let grammar = fs.readFileSync(path) - return vsctm.parseRawGrammar(grammar.toString(), path) + if (scopeName === "source.grain") { + let path = + "./grain-language-server/editor-extensions/vscode/syntaxes/grain.json"; + let grammar = fs.readFileSync(path); + return vsctm.parseRawGrammar(grammar.toString(), path); } console.log(`Unknown scope name: ${scopeName}`); return null; - } + }, }); - const grammar = await registry.loadGrammar('source.grain'); + const grammar = await registry.loadGrammar("source.grain"); this.grain = { registry, grammar }; } function makeGutter(numLines) { - let line = (num) => `${num}
` - let lines = [] + let line = (num) => `${num}
`; + let lines = []; for (let i = 1; i <= numLines; i++) { - lines.push(line(i)) + lines.push(line(i)); } - return `
${lines.join('')}
` + return `
${lines.join("")}
`; } function hookRenderer(renderer) { const { grammar } = this.grain; renderer.code = function (code, lang) { - let result = [] - let text = code.split('\n') + let result = []; + let text = code.split("\n"); - if (lang === 'grain' || lang === 'gr') { + if (lang === "grain" || lang === "gr") { let ruleStack = vsctm.INITIAL; for (let i = 0; i < text.length; i++) { const line = text[i]; const lineTokens = grammar.tokenizeLine(line, ruleStack); - const lineRes = [] + const lineRes = []; for (let j = 0; j < lineTokens.tokens.length; j++) { const token = lineTokens.tokens[j]; - const tokenString = line.substring(token.startIndex, token.endIndex) - const classString = token.scopes.map(scope => scope.replace(/\./g, ' ')).join(' ') - lineRes.push(`${tokenString}`) + const tokenString = line.substring(token.startIndex, token.endIndex); + const classString = token.scopes + .map((scope) => scope.replace(/\./g, " ")) + .join(" "); + lineRes.push(`${tokenString}`); } - result.push(`${lineRes.join('')}
`) + result.push(`${lineRes.join("")}
`); ruleStack = lineTokens.ruleStack; } } else { - result = text.map(line => `${line}
`) + result = text.map( + (line) => `${escapeHTML(line)}
` + ); } - return `
${makeGutter(text.length)}
${result.join('')}
`; - } + return `
${makeGutter( + text.length + )}
${result.join(
+      ""
+    )}
`; + }; }