From f4c70084144c0635d887a01047b1bbad58f6dca6 Mon Sep 17 00:00:00 2001 From: ayilmaz <84680978+yilmaz08@users.noreply.github.com> Date: Fri, 14 Jun 2024 01:04:31 +0300 Subject: [PATCH] Create MarkdownToHTML.mjs --- src/core/operations/MarkdownToHTML.mjs | 69 ++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/core/operations/MarkdownToHTML.mjs diff --git a/src/core/operations/MarkdownToHTML.mjs b/src/core/operations/MarkdownToHTML.mjs new file mode 100644 index 00000000..c76cb07d --- /dev/null +++ b/src/core/operations/MarkdownToHTML.mjs @@ -0,0 +1,69 @@ +/** + * @author yilmaz08 + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import MarkdownIt from "markdown-it"; +import hljs from "highlight.js"; + +/** + * Render Markdown operation + */ +class MarkdownToHTML extends Operation { + + /** + * MarkdownToHTML constructor + */ + constructor() { + super(); + + this.name = "Convert Markdown to HTML"; + this.module = "Code"; + this.description = "Converts input Markdown as plain HTML."; + this.infoURL = "https://wikipedia.org/wiki/Markdown"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + name: "Autoconvert URLs to links", + type: "boolean", + value: false + }, + { + name: "Enable syntax highlighting", + type: "boolean", + value: true + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {html} + */ + run(input, args) { + const [convertLinks, enableHighlighting] = args, + md = new MarkdownIt({ + linkify: convertLinks, + html: false, + highlight: function(str, lang) { + if (lang && hljs.getLanguage(lang) && enableHighlighting) { + try { + return hljs.highlight(lang, str).value; + } catch (__) {} + } + + return ""; + } + }), + rendered = md.render(input); + + return rendered; + } + +} + +export default MarkdownToHTML;