md.ts 1.0 KB

123456789101112131415161718192021222324252627282930
  1. // @ts-ignore
  2. import markdownit from 'markdown-it';
  3. import hljs from 'highlight.js'; // https://highlightjs.org
  4. import katexPlugin from '@iktakahiro/markdown-it-katex';
  5. const codeTool = (text: string) => `<svg id="copy" class="icon" aria-hidden="true"
  6. style="font-size:16px;display: inline-block;color:#fff;position:absolute;right:8px;top:6px;cursor:pointer;"
  7. data-copy="${text}">
  8. <use xlink:href="#gt-line-copy"></use>
  9. </svg>`;
  10. const md = markdownit({
  11. html: true,
  12. linkfy: true,
  13. highlight: function (str: string, lang: string) {
  14. const baseText = str
  15. if (lang && hljs.getLanguage(lang)) {
  16. try {
  17. return '<pre><code class="hljs">' +
  18. hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
  19. '</code>' + codeTool(baseText) + '</pre>';
  20. } catch (__) { }
  21. }
  22. return '<pre><code class="hljs">' + md.utils.escapeHtml(str) + '</code>' + codeTool(baseText) + '</pre>';
  23. }
  24. });
  25. md.use(katexPlugin);
  26. export default md;