kroki-plugin.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const { deflateSync } = require('zlib')
  2. const krokiLangs = [
  3. 'actdiag',
  4. 'blockdiag',
  5. 'bpmn',
  6. 'bytefield',
  7. 'c4plantuml',
  8. 'ditaa',
  9. 'dot',
  10. 'erd',
  11. 'excalidraw',
  12. 'graphviz',
  13. 'mermaid',
  14. 'nomnoml',
  15. 'nwdiag',
  16. 'packetdiag',
  17. 'pikchr',
  18. 'plantuml',
  19. 'rackdiag',
  20. 'seqdiag',
  21. 'svgbob',
  22. 'umlet',
  23. 'vega',
  24. 'vegalite',
  25. 'wavedrom',
  26. ]
  27. const entrypoint = 'https://kroki.io/'
  28. const marpKrokiPlugin = (md) => {
  29. const { fence } = md.renderer.rules
  30. md.renderer.rules.fence = (tokens, idx, options, env, self) => {
  31. const info = md.utils.unescapeAll(tokens[idx].info).trim()
  32. if (info) {
  33. const [lang] = info.split(/(\s+)/g)
  34. if (krokiLangs.includes(lang)) {
  35. const data = deflateSync(tokens[idx].content).toString('base64url')
  36. // <marp-auto-scaling> is working only with Marp Core v3
  37. return `<p><marp-auto-scaling data-downscale-only><img src="${entrypoint}${lang}/svg/${data}"/></marp-auto-scaling></p>`
  38. }
  39. }
  40. return fence.call(self, tokens, idx, options, env, self)
  41. }
  42. }
  43. module.exports = marpKrokiPlugin