md2html.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {VDITOR_VERSION} from "../constants";
  2. import {addScript} from "../util/addScript";
  3. export const loadLuteJs = (vditor: IVditor | string) => {
  4. // let cdn = `https://cdn.jsdelivr.net/npm/vditor@${VDITOR_VERSION}`;
  5. // if (typeof vditor === "string" && vditor) {
  6. // cdn = vditor;
  7. // } else if (typeof vditor === "object" && vditor.options.cdn) {
  8. // cdn = vditor.options.cdn;
  9. // }
  10. // addScript(`${cdn}/dist/js/lute/lute.min.js`, "vditorLuteScript");
  11. addScript(`/src/js/lute/lute.min.js`, "vditorLuteScript");
  12. // addScript(`http://192.168.2.248:9090/lute.min.js?${new Date().getTime()}`, "vditorLuteScript");
  13. if (vditor && typeof vditor === "object" && !vditor.lute) {
  14. vditor.lute = Lute.New();
  15. vditor.lute.PutEmojis(vditor.options.hint.emoji);
  16. vditor.lute.SetEmojiSite(vditor.options.hint.emojiPath);
  17. vditor.lute.SetInlineMathAllowDigitAfterOpenMarker(vditor.options.preview.math.inlineDigit);
  18. vditor.lute.SetAutoSpace(vditor.options.preview.markdown.autoSpace);
  19. vditor.lute.SetToC(vditor.options.preview.markdown.toc);
  20. vditor.lute.SetFootnotes(vditor.options.preview.markdown.footnotes);
  21. vditor.lute.SetChinesePunct(vditor.options.preview.markdown.chinesePunct);
  22. vditor.lute.SetFixTermTypo(vditor.options.preview.markdown.fixTermTypo);
  23. }
  24. };
  25. export const md2htmlByPreview = (mdText: string, options?: IPreviewOptions) => {
  26. if (typeof Lute === "undefined") {
  27. loadLuteJs(options && options.cdn);
  28. }
  29. options = Object.assign({
  30. emojiSite: `${(options && options.cdn) ||
  31. `https://cdn.jsdelivr.net/npm/vditor@${VDITOR_VERSION}`}/dist/images/emoji`,
  32. emojis: {},
  33. }, options);
  34. options.math = Object.assign({}, {
  35. engine: "KaTeX",
  36. inlineDigit: false,
  37. macros: {},
  38. }, options.math);
  39. options.markdown = Object.assign({}, {
  40. autoSpace: false,
  41. chinesePunct: false,
  42. fixTermTypo: false,
  43. footnotes: true,
  44. toc: false,
  45. }, options.markdown);
  46. const lute: ILute = Lute.New();
  47. lute.PutEmojis(options.customEmoji);
  48. lute.SetEmojiSite(options.emojiPath);
  49. lute.SetHeadingAnchor(options.anchor);
  50. lute.SetInlineMathAllowDigitAfterOpenMarker(options.math.inlineDigit);
  51. lute.SetAutoSpace(options.markdown.autoSpace);
  52. lute.SetToC(options.markdown.toc);
  53. lute.SetFootnotes(options.markdown.footnotes);
  54. lute.SetChinesePunct(options.markdown.chinesePunct);
  55. lute.SetFixTermTypo(options.markdown.fixTermTypo);
  56. return lute.Md2HTML(mdText);
  57. };
  58. export const md2htmlByVditor = (mdText: string, vditor: IVditor) => {
  59. if (typeof vditor.lute === "undefined") {
  60. loadLuteJs(vditor.options.cdn);
  61. }
  62. return vditor.lute.Md2HTML(mdText);
  63. };