remark-highlight.js 824 B

1234567891011121314151617181920212223242526272829
  1. import { visit } from "unist-util-visit";
  2. import escape from "escape-html";
  3. export default function highlighter(options) {
  4. return function(ast) {
  5. visit(ast, "code", function(node) {
  6. if (!node.lang) {
  7. return;
  8. }
  9. const highlight = function(code) {
  10. const html = code == null ? escape(node.value) : code;
  11. node.type = "html";
  12. node.value = [
  13. "<pre>",
  14. "<code class=\"language-" + node.lang + "\">",
  15. html,
  16. "</code>",
  17. "</pre>",
  18. ].join("\n");
  19. };
  20. const result = options.highlight(node.value, node.lang);
  21. return highlight(result);
  22. });
  23. };
  24. };