quikdown
Version:
Small, safe, bidirectional Markdown parser and editor with streaming, undo/redo, rich fences, MCP tools, and a standalone airgapped build. Zero runtime deps. Browser and Node.js.
9 lines (8 loc) • 6.76 kB
JavaScript
/**
* quikdown_ast - AST Markdown Parser
* @version 1.2.22
* @license BSD-2-Clause
* @copyright DeftIO 2025
*/
function t(t,e={}){if(!t||"string"!=typeof t)return{type:"document",children:[]};return{type:"document",children:s(t.replace(/\r\n/g,"\n").replace(/\r/g,"\n"))}}function e(t){const e=t.trim();return""===e||(!!/^#{1,6}\s/.test(e)||(!!(/^---+\s*$/.test(e)||/^\*\*\*+\s*$/.test(e)||/^___+\s*$/.test(e))||(!!/^>\s*/.test(e)||(!!/^[-*+]\s/.test(e)||(!!/^\d+\.\s/.test(e)||(!!e.startsWith("|")||!!/^(```|~~~)/.test(e)))))))}function n(t){let e="";const n=/[.,;:!?)]/;for(;t.length>0&&n.test(t[t.length-1]);){const n=t[t.length-1];if(")"===n){if((t.match(/\(/g)||[]).length>=(t.match(/\)/g)||[]).length)break}e=n+e,t=t.slice(0,-1)}return{url:t,trailing:e}}function s(t,n){const i=[],c=t.split("\n");let r=0;for(;r<c.length;){const t=c[r];if(""===t.trim()){r++;continue}const n=t.match(/^(```|~~~)(.*)$/);if(n){const[,t,e]=n,s=e.trim(),l=[];for(r++;r<c.length;){if(c[r].match(/^(```|~~~)\s*$/)){r++;break}l.push(c[r]),r++}i.push({type:"code_block",lang:s||null,content:l.join("\n"),fence:t});continue}if(/^---+\s*$/.test(t)||/^\*\*\*+\s*$/.test(t)||/^___+\s*$/.test(t)){i.push({type:"hr"}),r++;continue}const o=t.match(/^(#{1,6})\s*(.+?)\s*#*$/);if(o){const[,t,e]=o;i.push({type:"heading",level:t.length,children:u(e)}),r++;continue}if(t.includes("|")){const t=l(c,r);if(t){i.push(t.node),r=t.nextIndex;continue}}if(t.match(/^>\s*/)){const t=[];let n=!0;for(;r<c.length;)if(c[r].match(/^>\s*/))t.push(c[r].replace(/^>\s*/,"")),n=!0,r++;else{if(!n||e(c[r]))break;t.push(c[r]),r++}const l=t.length>0?t[0].trim().match(/^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*$/i):null;if(l){const e=l[1].toLowerCase();i.push({type:"alert",alertType:e,children:s(t.slice(1).join("\n"))})}else i.push({type:"blockquote",children:s(t.join("\n"))});continue}if(t.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/)){const t=h(c,r);i.push(t.node),r=t.nextIndex;continue}const a=[];for(;r<c.length;){const t=c[r];if(""===t.trim())break;if(/^(```|~~~)/.test(t))break;if(/^#{1,6}\s/.test(t))break;if(/^---+\s*$/.test(t)||/^\*\*\*+\s*$/.test(t)||/^___+\s*$/.test(t))break;if(/^>\s*/.test(t))break;if(/^(\s*)([*\-+]|\d+\.)\s+/.test(t))break;if(t.includes("|")&&r+1<c.length&&/^\|?[\s\-:|]+\|?$/.test(c[r+1]))break;a.push(t),r++}a.length>0&&i.push({type:"paragraph",children:u(a.join("\n"))})}return i}function l(t,e,n){if(e+1>=t.length)return null;const s=t[e],l=t[e+1];if(!/^\|?[\s\-:|]+\|?$/.test(l)||!l.includes("-"))return null;const c=i(s);if(0===c.length)return null;const r=i(l).map(t=>{const e=t.trim();return e.startsWith(":")&&e.endsWith(":")?"center":e.endsWith(":")?"right":"left"}),h=r.length,o=[];for(let t=0;t<h;t++){const e=t<c.length?c[t]:"";o.push(u(e.trim()))}const a=[];let f=e+2;for(;f<t.length;){const e=t[f];if(!e.includes("|")||""===e.trim())break;const n=i(e),s=[];for(let t=0;t<h;t++){const e=t<n.length?n[t]:"";s.push(u(e.trim()))}a.push(s),f++}return{node:{type:"table",headers:o,rows:a,alignments:r},nextIndex:f}}function i(t){let e=t.trim();return e.startsWith("|")&&(e=e.slice(1)),e.endsWith("|")&&(e=e.slice(0,-1)),e.split("|")}function c(t){return t.match(/^[ \t]*/)[0].length}function r(t){const e=t.match(/^[ \t]+(\S.*)$/);if(!e)return!1;const n=e[1];return!/^(```|~~~|>|\||#{1,6}(\s|$))/.test(n)&&!/^([-*_])\1{2,}\s*$/.test(n)}function h(t,e,n){const s=[];let l=e,i=0,o=null;const a=t[l].match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/),f=/^\d+\./.test(a[2]),p=a[1].length;for(;l<t.length&&i<1e3;){i++;const e=t[l],n=e.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/);if(!n){if(null!==o&&r(e)&&c(e)>=p){o+=(/( {2}|\\)$/.test(o)?"\n":" ")+e.trim(),s[s.length-1].children=u(o),l++;continue}break}const[,a,d,g]=n,m=a.length;if(m<p)break;const y=/^\d+\./.test(d);if(m===p&&y!==f)break;if(m>p){const e=[];let n=0;for(;l<t.length&&n<1e3;){n++;const s=t[l],i=s.match(/^(\s*)([*\-+]|\d+\.)\s+/);if(i){if(i[1].length<=p)break}else if(!(r(s)&&c(s)>p))break;e.push(s),l++}if(e.length>0&&s.length>0){const t=h(e,0),n=s[s.length-1];n.children?Array.isArray(n.children)||(n.children=[{type:"paragraph",children:n.children}]):n.children=[],n.children.push(t.node)}o=null;continue}const $={type:"list_item",checked:null,children:null},k=g.match(/^\[([x ])\]\s*(.*)$/i);k&&!f?($.checked="x"===k[1].toLowerCase(),o=k[2]):o=g,$.children=u(o),s.push($),l++}return{node:{type:"list",ordered:f,items:s},nextIndex:l}}function o(t){if(null==t||""===t)return{url:"",title:null};const e=t.match(/^(.*)\s+"([^"]*)"\s*$/);if(e)return{url:e[1].replace(/\s+$/,""),title:e[2]};const n=t.match(/^(.*)\s+'([^']*)'\s*$/);return n?{url:n[1].replace(/\s+$/,""),title:n[2]}:t.startsWith("<")&&t.endsWith(">")?{url:t.slice(1,-1),title:null}:{url:t,title:null}}function u(t,e){if(!t)return[];const s=[];let l=t;for(;l.length>0;){if(l.match(/^(.+?)(?: {2}|\\\n|\n)/)&&l.includes("\n")){const t=l.indexOf("\n"),e=l.slice(0,t),n=l.slice(t+1);if(e.endsWith(" ")||e.endsWith("\\")){const t=e.replace(/\\$/,"").replace(/ +$/,"");t&&s.push(...a(t)),s.push({type:"br"}),l=n;continue}}const e=l.match(/^!\[([^\]]*)\]\(([^)]+)\)/);if(e){const{url:t,title:n}=o(e[2]),i={type:"image",alt:e[1],url:t.trim()};n&&(i.title=n),s.push(i),l=l.slice(e[0].length);continue}const i=l.match(/^\[([^\]]+)\]\(([^)]+)\)/);if(i){const{url:t,title:e}=o(i[2]),n={type:"link",url:t.trim(),children:a(i[1])};e&&(n.title=e),s.push(n),l=l.slice(i[0].length);continue}const c=l.match(/^`([^`\n]+)`/);if(c){s.push({type:"code",value:c[1]}),l=l.slice(c[0].length);continue}const r=l.match(/^(\*\*|__)(.+?)\1/);if(r){s.push({type:"strong",children:a(r[2])}),l=l.slice(r[0].length);continue}const h=l.match(/^~~(.+?)~~/);if(h){s.push({type:"del",children:a(h[1])}),l=l.slice(h[0].length);continue}const u=t[t.length-l.length-1]||"",f=!/[A-Za-z0-9_]/.test(u),p=l.match(/^\*(?!\*)(.+?)(?<!\*)\*(?!\*)/)||f&&l.match(/^_(?![_\s])(.+?)(?<![\s_])_(?![A-Za-z0-9_])/);if(p){s.push({type:"em",children:a(p[1])}),l=l.slice(p[0].length);continue}const d=l.match(/^(https?:\/\/[^\s<>[\]]+)/);if(d){const{url:t,trailing:e}=n(d[1]);s.push({type:"link",url:t,children:[{type:"text",value:t}]}),e&&s.push({type:"text",value:e}),l=l.slice(d[0].length);continue}const g=l.search(/[`*_~![\\n]|https?:\/\//);if(-1===g){s.push({type:"text",value:l});break}0===g?(s.push({type:"text",value:l[0]}),l=l.slice(1)):(s.push({type:"text",value:l.slice(0,g)}),l=l.slice(g))}return function(t){const e=[];for(const n of t)"text"===n.type&&e.length>0&&"text"===e[e.length-1].type?e[e.length-1].value+=n.value:e.push(n);return e}(s)}function a(t,e){return u(t.replace(/\n/g," "))}t.version="1.2.22","undefined"!=typeof module&&module.exports&&(module.exports=t),"undefined"!=typeof window&&(window.quikdown_ast=t);export{t as default};
//# sourceMappingURL=quikdown_ast.esm.min.js.map