markdown-it-graph
Version:
A markdown-it plugin that parses custom graph syntax like ```graph bar and outputs structured graph data
3 lines (2 loc) • 1.45 kB
JavaScript
/*! markdown-it-graph 0.1.1 https://github.com/[object Object] @license MIT */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).markdownitGraph=t()}(this,(function(){"use strict";return function(e){const t="bar",r=e.renderer.rules.fence||function(e,t,r,n,i){return i.renderToken(e,t,r)};e.renderer.rules.fence=function(e,n,i,o,s){const c=e[n],f=c.info.trim(),[a,l=t]=f.split(/\s+/);if("graph"!==a)return r(e,n,i,o,s);const u={type:l,data:c.content.trim().split("\n").map((e=>{const[t,r]=e.split("|").map((e=>e.trim()));if(!t||!r)return null;const n=r.match(/^([\u2588\u2589\u258a\u258b\u258c\u258d\u258e\u258f ]*)([-+]?[\d.]+.*)$/);let i=r;n&&n.length>=3&&(i=n[2]?n[2].trim():r);return{label:t,value:parseFloat(i.replace(/[^0-9.-]/g,""))}})).filter(Boolean)};return`<graph-block type="${l}" data-graph='${JSON.stringify(u)}'></graph-block>`},e.block.ruler.before("fence","graph_block",(function(e,t,r,n){const i=e.bMarks[t]+e.tShift[t],o=e.eMarks[t],s=e.src.slice(i,o).trim();if(!s.startsWith("```graph"))return!1;if(n)return!0;let c=t;for(;++c<r;){const t=e.bMarks[c]+e.tShift[c],r=e.eMarks[c];if(e.src.slice(t,r).trim().startsWith("```"))break}const f=e.push("fence","code",0);return f.info=s.slice(3).trim(),f.content=e.getLines(t+1,c,e.tShift[t],!0),f.map=[t,c+1],f.markup="```",e.line=c+1,!0}))}}));