|
|
@@ -1,5 +1,4 @@
|
|
|
import MarkdownIt from 'markdown-it';
|
|
|
-import xss from 'xss';
|
|
|
|
|
|
import Linker from './PreProcessor/Linker';
|
|
|
import CsvToTable from './PreProcessor/CsvToTable';
|
|
|
@@ -19,6 +18,9 @@ import BlockdiagConfigurer from './markdown-it/blockdiag';
|
|
|
import TableWithHandsontableButtonConfigurer from './markdown-it/table-with-handsontable-button';
|
|
|
import HeaderWithEditLinkConfigurer from './markdown-it/header-with-edit-link';
|
|
|
|
|
|
+const logger = require('@alias/logger')('growi:util:GrowiRenderer');
|
|
|
+
|
|
|
+
|
|
|
export default class GrowiRenderer {
|
|
|
|
|
|
/**
|
|
|
@@ -34,8 +36,6 @@ export default class GrowiRenderer {
|
|
|
{ isAutoSetup: true }, // default options
|
|
|
options || {}); // specified options
|
|
|
|
|
|
- this.xssFilterForCode = new xss.FilterXSS();
|
|
|
-
|
|
|
// initialize processors
|
|
|
// that will be retrieved if originRenderer exists
|
|
|
this.preProcessors = this.originRenderer.preProcessors || [
|
|
|
@@ -166,33 +166,43 @@ export default class GrowiRenderer {
|
|
|
const config = this.crowi.getConfig();
|
|
|
const noborder = (!config.highlightJsStyleBorder) ? 'hljs-no-border' : '';
|
|
|
|
|
|
+ let citeTag = '';
|
|
|
+ let hljsLang = 'plaintext';
|
|
|
+ let showLinenumbers = false;
|
|
|
+
|
|
|
if (langExt) {
|
|
|
- // https://regex101.com/r/qGs7eZ/1
|
|
|
- const match = langExt.match(/^([^:=\n]+)(=([^:=\n]*))?(:([^:=\n]+))?(=([^:=\n]*))?$/);
|
|
|
+ // https://regex101.com/r/qGs7eZ/3
|
|
|
+ const match = langExt.match(/^([^:=\n]+)?(=([^:=\n]*))?(:([^:=\n]*))?(=([^:=\n]*))?$/);
|
|
|
|
|
|
const lang = match[1];
|
|
|
const fileName = match[5] || null;
|
|
|
- const showLinenumbers = (match[2] != null) || (match[6] != null);
|
|
|
-
|
|
|
- const citeTag = (fileName) ? `<cite>${fileName}</cite>` : '';
|
|
|
+ showLinenumbers = (match[2] != null) || (match[6] != null);
|
|
|
|
|
|
+ if (fileName != null) {
|
|
|
+ citeTag = `<cite>${fileName}</cite>`;
|
|
|
+ }
|
|
|
if (hljs.getLanguage(lang)) {
|
|
|
- try {
|
|
|
- const highlightCode = showLinenumbers ? hljs.lineNumbersValue(hljs.highlight(lang, code, true).value) : hljs.highlight(lang, code, true).value;
|
|
|
- return `<pre class="hljs ${noborder}">${citeTag}<code class="language-${lang}">${highlightCode}</code></pre>`;
|
|
|
- }
|
|
|
- catch (__) {
|
|
|
- return `<pre class="hljs ${noborder}">${citeTag}<code class="language-${lang}">${code}}</code></pre>`;
|
|
|
- }
|
|
|
+ hljsLang = lang;
|
|
|
}
|
|
|
- else {
|
|
|
- const escapedCode = this.xssFilterForCode.process(code);
|
|
|
- return `<pre class="hljs ${noborder}">${citeTag}<code>${escapedCode}</code></pre>`;
|
|
|
+ }
|
|
|
+
|
|
|
+ let highlightCode = code;
|
|
|
+ try {
|
|
|
+ highlightCode = hljs.highlight(hljsLang, code, true).value;
|
|
|
+
|
|
|
+ // add line numbers
|
|
|
+ if (showLinenumbers) {
|
|
|
+ highlightCode = hljs.lineNumbersValue((highlightCode));
|
|
|
}
|
|
|
}
|
|
|
+ catch (err) {
|
|
|
+ logger.error(err);
|
|
|
+ }
|
|
|
+
|
|
|
+ return `<pre class="hljs ${noborder}">${citeTag}<code>${highlightCode}</code></pre>`;
|
|
|
+ }
|
|
|
|
|
|
- const escapedCode = this.xssFilterForCode.process(code);
|
|
|
- return `<pre class="hljs ${noborder}"><code>${escapedCode}</code></pre>`;
|
|
|
+ highlightCode(code, lang) {
|
|
|
}
|
|
|
|
|
|
}
|