Feature md link blanks (#660)
Co-authored-by: GCHQDeveloper581 <63102987+GCHQDeveloper581@users.noreply.github.com> (added tests)
This commit is contained in:
parent
5a1b121041
commit
97f35095ca
@ -35,6 +35,11 @@ class RenderMarkdown extends Operation {
|
||||
name: "Enable syntax highlighting",
|
||||
type: "boolean",
|
||||
value: true
|
||||
},
|
||||
{
|
||||
name: "Open links in new tab.",
|
||||
type: "boolean",
|
||||
value: false
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -45,7 +50,7 @@ class RenderMarkdown extends Operation {
|
||||
* @returns {html}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [convertLinks, enableHighlighting] = args,
|
||||
const [convertLinks, enableHighlighting, openLinksBlank] = args,
|
||||
md = new MarkdownIt({
|
||||
linkify: convertLinks,
|
||||
html: false, // Explicitly disable HTML rendering
|
||||
@ -58,12 +63,38 @@ class RenderMarkdown extends Operation {
|
||||
|
||||
return "";
|
||||
}
|
||||
}),
|
||||
rendered = md.render(input);
|
||||
|
||||
});
|
||||
if (openLinksBlank) {
|
||||
this.makeLinksOpenInNewTab(md);
|
||||
}
|
||||
const rendered = md.render(input);
|
||||
return `<div style="font-family: var(--primary-font-family)">${rendered}</div>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds target="_blank" to links.
|
||||
* @param {MarkdownIt} md
|
||||
*/
|
||||
makeLinksOpenInNewTab(md) {
|
||||
// Adapted from: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
|
||||
// Remember old renderer, if overridden, or proxy to default renderer
|
||||
const defaultRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) {
|
||||
return self.renderToken(tokens, idx, options);
|
||||
};
|
||||
|
||||
// eslint-disable-next-line camelcase
|
||||
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
|
||||
const token = tokens[idx];
|
||||
if (token.attrIndex("target") >= 0) {
|
||||
// Target attribute already set, do not replace.
|
||||
return;
|
||||
}
|
||||
token.attrPush(["target", "_blank"]); // add new attribute
|
||||
|
||||
// pass token to default renderer.
|
||||
return defaultRender(tokens, idx, options, env, self);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default RenderMarkdown;
|
||||
|
||||
@ -145,6 +145,7 @@ import "./tests/RAKE.mjs";
|
||||
import "./tests/Regex.mjs";
|
||||
import "./tests/Register.mjs";
|
||||
import "./tests/RegularExpression.mjs";
|
||||
import "./tests/RenderMarkdown.mjs";
|
||||
import "./tests/RisonEncodeDecode.mjs";
|
||||
import "./tests/Rotate.mjs";
|
||||
import "./tests/RSA.mjs";
|
||||
|
||||
110
tests/operations/tests/RenderMarkdown.mjs
Normal file
110
tests/operations/tests/RenderMarkdown.mjs
Normal file
@ -0,0 +1,110 @@
|
||||
/**
|
||||
* RenderMarkdown tests.
|
||||
*
|
||||
* @copyright Crown Copyright 2026
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import TestRegister from "../../lib/TestRegister.mjs";
|
||||
|
||||
TestRegister.addTests([
|
||||
{
|
||||
name: "Render Markdown: Nothing",
|
||||
input: "",
|
||||
expectedOutput: '<div style="font-family: var(--primary-font-family)"></div>',
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Render Markdown",
|
||||
"args": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Render Markdown: Basic Text",
|
||||
input: "Hello World!",
|
||||
expectedOutput: '<div style="font-family: var(--primary-font-family)"><p>Hello World!</p>\n</div>',
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Render Markdown",
|
||||
"args": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Render Markdown: Simple Markdown",
|
||||
input: "# Hello World!",
|
||||
expectedOutput: '<div style="font-family: var(--primary-font-family)"><h1>Hello World!</h1>\n</div>',
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Render Markdown",
|
||||
"args": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Render Markdown: URL (not expanded)",
|
||||
input: "https://gchq.github.io/CyberChef/",
|
||||
expectedOutput: '<div style="font-family: var(--primary-font-family)"><p>https://gchq.github.io/CyberChef/</p>\n</div>',
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Render Markdown",
|
||||
"args": [false, false, false]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Render Markdown: URL (expanded)",
|
||||
input: "https://gchq.github.io/CyberChef/",
|
||||
expectedOutput: '<div style="font-family: var(--primary-font-family)"><p><a href="https://gchq.github.io/CyberChef/">https://gchq.github.io/CyberChef/</a></p>\n</div>',
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Render Markdown",
|
||||
"args": [true, false, false]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Render Markdown: Link (not expanded)",
|
||||
input: "[CyberChef](https://gchq.github.io/CyberChef/)",
|
||||
expectedOutput: '<div style="font-family: var(--primary-font-family)"><p><a href="https://gchq.github.io/CyberChef/">CyberChef</a></p>\n</div>',
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Render Markdown",
|
||||
"args": [false, false, false]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Render Markdown: Link (expanded)",
|
||||
input: "[CyberChef](https://gchq.github.io/CyberChef/)",
|
||||
expectedOutput: '<div style="font-family: var(--primary-font-family)"><p><a href="https://gchq.github.io/CyberChef/">CyberChef</a></p>\n</div>',
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Render Markdown",
|
||||
"args": [true, false, false]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Render Markdown: Link (open in new window)",
|
||||
input: "[CyberChef](https://gchq.github.io/CyberChef/)",
|
||||
expectedOutput: '<div style="font-family: var(--primary-font-family)"><p><a href="https://gchq.github.io/CyberChef/" target="_blank">CyberChef</a></p>\n</div>',
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Render Markdown",
|
||||
"args": [true, false, true]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Render Markdown: URL (open in new window)",
|
||||
input: "https://gchq.github.io/CyberChef/",
|
||||
expectedOutput: '<div style="font-family: var(--primary-font-family)"><p><a href="https://gchq.github.io/CyberChef/" target="_blank">https://gchq.github.io/CyberChef/</a></p>\n</div>',
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Render Markdown",
|
||||
"args": [true, false, true]
|
||||
}
|
||||
]
|
||||
},
|
||||
]);
|
||||
Loading…
x
Reference in New Issue
Block a user