markdown.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. export default {
  2. template: `<div></div>`,
  3. async mounted() {
  4. this.ensure_codehilite_css();
  5. if (this.use_mermaid) {
  6. this.mermaid = (await import("mermaid")).default;
  7. this.update(this.$el.innerHTML);
  8. }
  9. },
  10. data() {
  11. return {
  12. mermaid: null,
  13. };
  14. },
  15. methods: {
  16. update(content) {
  17. this.$el.innerHTML = content;
  18. this.$el.querySelectorAll(".mermaid-pre").forEach(async (pre, i) => {
  19. await this.mermaid.run({ nodes: [pre.children[0]] });
  20. });
  21. },
  22. ensure_codehilite_css() {
  23. if (!document.querySelector(`style[data-codehilite-css]`)) {
  24. const style = document.createElement("style");
  25. style.setAttribute("data-codehilite-css", "");
  26. style.innerHTML = this.codehilite_css;
  27. document.head.appendChild(style);
  28. }
  29. },
  30. },
  31. props: {
  32. codehilite_css: String,
  33. use_mermaid: {
  34. required: false,
  35. default: false,
  36. type: Boolean,
  37. },
  38. },
  39. };
  40. function decodeHtml(html) {
  41. const txt = document.createElement("textarea");
  42. txt.innerHTML = html;
  43. return txt.value;
  44. }