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

Markdown To HTML Converter Improvement #1837

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@
"BSON deserialise",
"To MessagePack",
"From MessagePack",
"Render Markdown"
"Render Markdown",
"Convert Markdown to HTML"
]
},
{
Expand Down
69 changes: 69 additions & 0 deletions src/core/operations/MarkdownToHTML.mjs
Original file line number Diff line number Diff line change
@@ -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";

/**
* Convert Markdown to HTML 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;
Loading