Skip to content

Low-level highlighting w/ plugins #3172

Answered by RunDevelopment
kczx3 asked this question in Q&A
Discussion options

You must be logged in to vote

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 API highlightElement 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:

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.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@kczx3
Comment options

@RunDevelopment
Comment options

@kczx3
Comment options

Answer selected by kczx3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants