remark-github-markdown-alerts
Version:
An unifiedjs (remark) plugin to convert GitHub Markdown alerts syntax into actual UI
3 lines (2 loc) • 4.41 kB
JavaScript
import{fromHtml as t}from"hast-util-from-html";import{toHtml as n}from"hast-util-to-html";import{h as r}from"hastscript";import{toString as e}from"mdast-util-to-string";import{u as o}from"unist-builder";import{visit as i}from"unist-util-visit";const a={iconElementHtml:"",tags:{container:"div",icon:"span",title:"div",content:"div"},classNames:{container:"markdown-alert",icon:"markdown-alert-icon",title:"markdown-alert-title",content:"markdown-alert-content"}},c={note:"",tip:"",important:"",warning:"",caution:""};function s(t){if(!t.children||0===t.children.length)return{isAlert:!1};const n=t.children[0];if(!n||"paragraph"!==n.type)return{isAlert:!1};const r=n.children[0];if(!r||"text"!==r.type)return{isAlert:!1};const e=(r.value.split("\n")[0]||"").match(/^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\](?:\s+(.*))?$/);if(!e)return{isAlert:!1};const o=e[1],i=e[2]?.trim();return{isAlert:!0,type:o,title:i||o.charAt(0)+o.slice(1).toLowerCase()}}function l(n,r,e,i){const a=n.toLowerCase(),c=[i.classNames.container,`${i.classNames.container}-${a}`].filter(Boolean).join(" "),s=function(n){if(!n||!n.trim())return[];try{const r=t(n,{fragment:!0});return"root"===r.type&&r.children&&r.children.length>0?r.children.filter(t=>"element"===t.type||"text"===t.type):[]}catch(t){return console.warn("Invalid HTML in icon configuration, skipping icon:",t),[]}}(i.iconElementHtml);return o(i.tags.container,{data:{hName:i.tags.container,hProperties:{className:c,"data-alert-type":a}}},[o(i.tags.title,{data:{hName:i.tags.title,hProperties:{className:i.classNames.title}}},[o(i.tags.icon,{data:{hName:i.tags.icon,hProperties:{className:i.classNames.icon},hChildren:s}}),o("text",r)]),o(i.tags.content,{data:{hName:i.tags.content,hProperties:{className:i.classNames.content}}},e)])}function u(t,n){return n?{iconElementHtml:n.iconElementHtml??t.iconElementHtml,tags:{container:n.tags?.container??t.tags.container,icon:n.tags?.icon??t.tags.icon,title:n.tags?.title??t.tags.title,content:n.tags?.content??t.tags.content},classNames:{container:n.classNames?.container??t.classNames.container,icon:n.classNames?.icon??t.classNames.icon,title:n.classNames?.title??t.classNames.title,content:n.classNames?.content??t.classNames.content}}:t}function m(t){const n=t.children[0],r=n.children[0];if(r.value.match(/^\[!(?:NOTE|TIP|IMPORTANT|WARNING|CAUTION)\](?:\s+.*)?$/)){const e=r.value.replace(/^\[!(?:NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*/,"").trim();e?r.value=e:(n.children.shift(),0===n.children.length&&t.children.shift())}return t.children}function f(t,n,r,e,o){if(!r||"number"!=typeof n)return!1;const i=s(t);if(!i.isAlert||!i.type||!i.title)return!1;const a=u(e,o[i.type.toLowerCase()]),c=m(t),f=l(i.type,i.title,c,a);return r&&"object"==typeof r&&"children"in r&&Array.isArray(r.children)&&(r.children[n]=f),!0}function p(i,a,l,f,p){if(!l||"number"!=typeof a)return!1;const h=s(i);if(!h.isAlert||!h.type||!h.title)return!1;const N=u(f,p[h.type.toLowerCase()]);m(i);const d=i.children.map(t=>{if("paragraph"===t.type){const o=e(t);return n(r("p",o))}return""}).join("\n "),T=function(e,o,i,a){const s=e.toLowerCase(),l=[a.classNames.container,`${a.classNames.container}-${s}`].filter(Boolean).join(" "),u=a.iconElementHtml||c[s];let m=[];if(u)try{const n=t(u,{fragment:!0});"root"===n.type&&n.children&&(m=n.children.filter(t=>"element"===t.type||"text"===t.type))}catch(t){console.warn("Invalid HTML in icon configuration, skipping icon:",t)}const f=r(a.tags.title,{class:a.classNames.title},[r(a.tags.icon,{class:a.classNames.icon},m),o]),p=r(a.tags.content,{class:a.classNames.content});p.children=[{type:"raw",value:i}];const h=r(a.tags.container,{class:l},[f,p]);return n(h,{allowDangerousHtml:!0})}(h.type,h.title,d,N),A=o("html",T);return l&&"object"==typeof l&&"children"in l&&Array.isArray(l.children)&&(l.children[a]=A),!0}function h(t,n,r,e,o,i="html"){return"component"===i?f(t,n,r,e,o):p(t,n,r,e,o)}const N=(t={})=>{const{alerts:n={},defaultConfig:r,mode:e="auto"}=t,o=u(a,r);return(t,r)=>{const a="auto"===e?function(t){if(!t)return"html";const n=t.data||{};return!0===n.reactMarkdown||!0===n.mdx||".mdx"===t.extname||!1===n.allowDangerousHtml?"component":"html"}(r):e;return i(t,"blockquote",(t,r,e)=>{h(t,r,e,o,n,a)}),t}};export{N as default,h as processBlockquote,f as processBlockquoteAsComponent,p as processBlockquoteAsHtml,N as remarkGitHubAlerts};
//# sourceMappingURL=index.esm.min.js.map