markdown.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.createMarkdownRenderer = void 0;
  7. const markdown_it_1 = __importDefault(require("markdown-it"));
  8. const parseHeader_1 = require("../utils/parseHeader");
  9. const highlight_1 = require("./plugins/highlight");
  10. const slugify_1 = require("./plugins/slugify");
  11. const highlightLines_1 = require("./plugins/highlightLines");
  12. const lineNumbers_1 = require("./plugins/lineNumbers");
  13. const component_1 = require("./plugins/component");
  14. const containers_1 = require("./plugins/containers");
  15. const snippet_1 = require("./plugins/snippet");
  16. const hoist_1 = require("./plugins/hoist");
  17. const preWrapper_1 = require("./plugins/preWrapper");
  18. const link_1 = require("./plugins/link");
  19. const header_1 = require("./plugins/header");
  20. const emoji = require('markdown-it-emoji');
  21. const anchor = require('markdown-it-anchor');
  22. const toc = require('markdown-it-table-of-contents');
  23. exports.createMarkdownRenderer = (options = {}) => {
  24. const md = markdown_it_1.default({
  25. html: true,
  26. linkify: true,
  27. highlight: highlight_1.highlight,
  28. ...options
  29. });
  30. // custom plugins
  31. md.use(component_1.componentPlugin)
  32. .use(highlightLines_1.highlightLinePlugin)
  33. .use(preWrapper_1.preWrapperPlugin)
  34. .use(snippet_1.snippetPlugin)
  35. .use(hoist_1.hoistPlugin)
  36. .use(containers_1.containerPlugin)
  37. .use(header_1.extractHeaderPlugin)
  38. .use(link_1.linkPlugin, {
  39. target: '_blank',
  40. rel: 'noopener noreferrer',
  41. ...options.externalLinks
  42. })
  43. // 3rd party plugins
  44. .use(emoji)
  45. .use(anchor, {
  46. slugify: slugify_1.slugify,
  47. permalink: true,
  48. permalinkBefore: true,
  49. permalinkSymbol: '#',
  50. permalinkAttrs: () => ({ 'aria-hidden': true }),
  51. ...options.anchor
  52. })
  53. .use(toc, {
  54. slugify: slugify_1.slugify,
  55. includeLevel: [2, 3],
  56. format: parseHeader_1.parseHeader,
  57. ...options.toc
  58. });
  59. // apply user config
  60. if (options.config) {
  61. options.config(md);
  62. }
  63. if (options.lineNumbers) {
  64. md.use(lineNumbers_1.lineNumberPlugin);
  65. }
  66. // wrap render so that we can return both the html and extracted data.
  67. const render = md.render;
  68. const wrappedRender = (src) => {
  69. ;
  70. md.__data = {};
  71. const html = render.call(md, src);
  72. return {
  73. html,
  74. data: md.__data
  75. };
  76. };
  77. md.render = wrappedRender;
  78. return md;
  79. };
  80. //# sourceMappingURL=markdown.js.map