Low-level highlighting w/ plugins #3172
-
I'm trying to use the import prism from "prismjs";
import "prismjs/plugins/line-numbers/prism-line-numbers";
export default function colorize(string) {
return prism.highlight(string, prism.languages.html, "html");
} //...
view(el, { html }, _children, _mChildren, { state }) {
return [
...this.renderChildren(),
state.rawRender ? el("pre.language-html.line-numbers",
el("code.mpageui-u-pad-relative-base.language-html", {
oncreate: (vnode) => {
vnode.dom.innerHTML = colorize(html);
}
})
) : el("div.ddnotecontent", {
/** Sets the innerHTML of the containing element to props.html */
oncreate: (vnode) => {
vnode.dom.innerHTML = html;
},
onclick: e => handleClick(this, e)
})
];
}
//... |
Beta Was this translation helpful? Give feedback.
Answered by
RunDevelopment
Oct 28, 2021
Replies: 1 comment 3 replies
-
The Line numbers plugin is implemented by reading and modifying the DOM nodes of code blocks ( It's really hacky but you could use the fact that DOM nodes Line numbers creates are rather static. You could do something like this: export default function colorize(string) {
const pre = document.createElement("pre");
pre.className = "language-html line-numbers";
const code = document.createElement("code");
code.textContent = string;
prism.highlightElement(code);
return pre.outerHTML;
} Really hacky but it works. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
kczx3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Line numbers plugin is implemented by reading and modifying the DOM nodes of code blocks (
<pre><code>
), so it requires Prism's higher-level APIhighlightElement
function.It's really hacky but you could use the fact that DOM nodes Line numbers creates are rather static. You could do something like this:
Really hacky but it works.