2019-07-12 10:33:13 +02:00
|
|
|
/**
|
|
|
|
* @author j433866 [j433866@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2019
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-08-29 15:08:07 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
2019-07-12 10:33:13 +02:00
|
|
|
import MarkdownIt from "markdown-it";
|
2019-08-29 15:08:07 +02:00
|
|
|
import hljs from "highlight.js";
|
2019-07-12 10:33:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render Markdown operation
|
|
|
|
*/
|
|
|
|
class RenderMarkdown extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RenderMarkdown constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Render Markdown";
|
2019-08-30 16:46:24 +02:00
|
|
|
this.module = "Code";
|
|
|
|
this.description = "Renders input Markdown as HTML. HTML rendering is disabled to avoid XSS.";
|
2019-07-12 10:33:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Markdown";
|
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "html";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
name: "Autoconvert URLs to links",
|
|
|
|
type: "boolean",
|
|
|
|
value: false
|
|
|
|
},
|
|
|
|
{
|
2019-08-29 15:08:07 +02:00
|
|
|
name: "Enable syntax highlighting",
|
2019-07-12 10:33:13 +02:00
|
|
|
type: "boolean",
|
2019-08-29 15:08:07 +02:00
|
|
|
value: true
|
2019-07-12 10:33:13 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {html}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
2019-08-29 15:08:07 +02:00
|
|
|
const [convertLinks, enableHighlighting] = args,
|
2019-07-12 10:33:13 +02:00
|
|
|
md = new MarkdownIt({
|
2019-08-29 15:08:07 +02:00
|
|
|
linkify: convertLinks,
|
|
|
|
html: false, // Explicitly disable HTML rendering
|
|
|
|
highlight: function(str, lang) {
|
|
|
|
if (lang && hljs.getLanguage(lang) && enableHighlighting) {
|
|
|
|
try {
|
|
|
|
return hljs.highlight(lang, str).value;
|
|
|
|
} catch (__) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
2019-07-12 10:33:13 +02:00
|
|
|
}),
|
|
|
|
rendered = md.render(input);
|
|
|
|
|
|
|
|
return `<div style="font-family: var(--primary-font-family)">${rendered}</div>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RenderMarkdown;
|