@mfyz/markdown-renderer-with-custom-directives
Version:
Zero-dependency markdown renderer with custom directives
5 lines (4 loc) • 3.34 kB
JavaScript
var MarkdownRenderer=(()=>{var T=Object.defineProperty;var _=(i,e,s)=>e in i?T(i,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[e]=s;var E=(i,e)=>()=>(e||i((e={exports:{}}).exports,e),e.exports);var n=(i,e,s)=>_(i,typeof e!="symbol"?e+"":e,s);var d=E((b,h)=>{var l=class{static parseButtonParams(e){let s={url:"",shape:"rounded",color:"blue"};return e.split(" ").forEach(t=>{let[r,o]=t.split("=");r&&o&&(s[r]=o)}),s}static processBasicMarkdown(e){return e.replace(this.PATTERNS.bold,"<strong>$1</strong>").replace(this.PATTERNS.italic,"<em>$1</em>").replace(this.PATTERNS.strike,"<del>$1</del>").replace(this.PATTERNS.link,'<a href="$2">$1</a>')}static processColorDirective(e){return e.replace(this.PATTERNS.color,(s,t,r)=>{let o=this.processBasicMarkdown(t);return`<span style="color:${r}">${o}</span>`})}static processButtonDirective(e){return e.replace(this.PATTERNS.button,(s,t,r)=>{let{url:o,shape:c,color:a}=this.parseButtonParams(r),p=[this.BUTTON_STYLES.base,this.BUTTON_STYLES.shapes[c]||this.BUTTON_STYLES.shapes.rounded,this.BUTTON_STYLES.colors[a]||this.BUTTON_STYLES.colors.blue].join("; "),u=this.processBasicMarkdown(t);return`<a href="${o}" style="${p}">${u}</a>`})}static processHeadlines(e){return e.replace(this.PATTERNS.h6,"<h6>$1</h6>__HEADLINE_MARKER__").replace(this.PATTERNS.h5,"<h5>$1</h5>__HEADLINE_MARKER__").replace(this.PATTERNS.h4,"<h4>$1</h4>__HEADLINE_MARKER__").replace(this.PATTERNS.h3,"<h3>$1</h3>__HEADLINE_MARKER__").replace(this.PATTERNS.h2,"<h2>$1</h2>__HEADLINE_MARKER__").replace(this.PATTERNS.h1,"<h1>$1</h1>__HEADLINE_MARKER__")}static processLists(e){e=e.replace(this.PATTERNS.ul,"<li>$1</li>");let s=e.split(`
`),t=!1,r=[];for(let o of s)o.startsWith("<li>")?(t||(r.push("<ul>"),t=!0),r.push(o)):(t&&(r.push("</ul>"),t=!1),r.push(o));return t&&r.push("</ul>"),r.join(`
`).replace(/\n<\/ul>/g,"</ul>").replace(/<ul>\n/g,"<ul>").replace(/(<li>.*?<\/li>)\n(?=<li>)/g,"$1")}static processLineBreaks(e){return e.replace(/(__HEADLINE_MARKER__)\n+/g,(s,t,r,o)=>{var a;let c=((a=s.match(/\n/g))==null?void 0:a.length)||0;return c>2?"<br>".repeat(c-2):""}).replace(/__HEADLINE_MARKER__/g,"").replace(/\n\n+/g,"<br><br>").replace(/\n/g,"<br>")}static render(e){if(!e)return"";let s=e;return s=this.processHeadlines(s),s=this.processLists(s),s=this.processBasicMarkdown(s),s=this.processColorDirective(s),s=this.processButtonDirective(s),s=this.processLineBreaks(s),s}};n(l,"PATTERNS",{bold:/\*\*(.*?)\*\*/g,italic:/\*(.*?)\*/g,strike:/~~(.*?)~~/g,link:/\[(.*?)\]\((.*?)\)/g,h6:/^#{6}\s+(.+)$/gm,h5:/^#{5}\s+(.+)$/gm,h4:/^#{4}\s+(.+)$/gm,h3:/^#{3}\s+(.+)$/gm,h2:/^#{2}\s+(.+)$/gm,h1:/^#\s+(.+)$/gm,ul:/^[\s]*[-*]\s+(.+)$/gm,lineBreak:/\n/g,color:/:color\[(.*?)\]{(.*?)}/g,button:/:button\[(.*?)\]{(.*?)}/g}),n(l,"BUTTON_STYLES",{shapes:{pill:"border-radius: 9999px",rect:"border-radius: 0",rounded:"border-radius: 5px"},colors:{purple:"background-color: #8a63d2",blue:"background-color: #3b82f6",green:"background-color: #22c55e",red:"background-color: #ef4444",yellow:"background-color: #eab308",gray:"background-color: #6b7280"},base:["display: inline-block","text-decoration: none","padding: 2px 10px","cursor: pointer","color: white"].join("; ")});function g(i){return l.render(i)}h.exports={render:g,MarkdownRenderer:l,default:l}});return d();})();
//# sourceMappingURL=index.browser.js.map