UNPKG

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.99 kB
/** * quikdown_ast - AST Markdown Parser * @version 1.2.22 * @license BSD-2-Clause * @copyright DeftIO 2025 */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).quikdown_ast=e()}(this,function(){"use strict";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 l=[],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(),i=[];for(r++;r<c.length;){if(c[r].match(/^(```|~~~)\s*$/)){r++;break}i.push(c[r]),r++}l.push({type:"code_block",lang:s||null,content:i.join("\n"),fence:t});continue}if(/^---+\s*$/.test(t)||/^\*\*\*+\s*$/.test(t)||/^___+\s*$/.test(t)){l.push({type:"hr"}),r++;continue}const h=t.match(/^(#{1,6})\s*(.+?)\s*#*$/);if(h){const[,t,e]=h;l.push({type:"heading",level:t.length,children:u(e)}),r++;continue}if(t.includes("|")){const t=i(c,r);if(t){l.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 i=t.length>0?t[0].trim().match(/^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*$/i):null;if(i){const e=i[1].toLowerCase();l.push({type:"alert",alertType:e,children:s(t.slice(1).join("\n"))})}else l.push({type:"blockquote",children:s(t.join("\n"))});continue}if(t.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/)){const t=o(c,r);l.push(t.node),r=t.nextIndex;continue}const f=[];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;f.push(t),r++}f.length>0&&l.push({type:"paragraph",children:u(f.join("\n"))})}return l}function i(t,e,n){if(e+1>=t.length)return null;const s=t[e],i=t[e+1];if(!/^\|?[\s\-:|]+\|?$/.test(i)||!i.includes("-"))return null;const c=l(s);if(0===c.length)return null;const r=l(i).map(t=>{const e=t.trim();return e.startsWith(":")&&e.endsWith(":")?"center":e.endsWith(":")?"right":"left"}),o=r.length,h=[];for(let t=0;t<o;t++){const e=t<c.length?c[t]:"";h.push(u(e.trim()))}const f=[];let a=e+2;for(;a<t.length;){const e=t[a];if(!e.includes("|")||""===e.trim())break;const n=l(e),s=[];for(let t=0;t<o;t++){const e=t<n.length?n[t]:"";s.push(u(e.trim()))}f.push(s),a++}return{node:{type:"table",headers:h,rows:f,alignments:r},nextIndex:a}}function l(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 o(t,e,n){const s=[];let i=e,l=0,h=null;const f=t[i].match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/),a=/^\d+\./.test(f[2]),p=f[1].length;for(;i<t.length&&l<1e3;){l++;const e=t[i],n=e.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/);if(!n){if(null!==h&&r(e)&&c(e)>=p){h+=(/( {2}|\\)$/.test(h)?"\n":" ")+e.trim(),s[s.length-1].children=u(h),i++;continue}break}const[,f,d,g]=n,m=f.length;if(m<p)break;const y=/^\d+\./.test(d);if(m===p&&y!==a)break;if(m>p){const e=[];let n=0;for(;i<t.length&&n<1e3;){n++;const s=t[i],l=s.match(/^(\s*)([*\-+]|\d+\.)\s+/);if(l){if(l[1].length<=p)break}else if(!(r(s)&&c(s)>p))break;e.push(s),i++}if(e.length>0&&s.length>0){const t=o(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)}h=null;continue}const $={type:"list_item",checked:null,children:null},k=g.match(/^\[([x ])\]\s*(.*)$/i);k&&!a?($.checked="x"===k[1].toLowerCase(),h=k[2]):h=g,$.children=u(h),s.push($),i++}return{node:{type:"list",ordered:a,items:s},nextIndex:i}}function h(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 i=t;for(;i.length>0;){if(i.match(/^(.+?)(?: {2}|\\\n|\n)/)&&i.includes("\n")){const t=i.indexOf("\n"),e=i.slice(0,t),n=i.slice(t+1);if(e.endsWith(" ")||e.endsWith("\\")){const t=e.replace(/\\$/,"").replace(/ +$/,"");t&&s.push(...f(t)),s.push({type:"br"}),i=n;continue}}const e=i.match(/^!\[([^\]]*)\]\(([^)]+)\)/);if(e){const{url:t,title:n}=h(e[2]),l={type:"image",alt:e[1],url:t.trim()};n&&(l.title=n),s.push(l),i=i.slice(e[0].length);continue}const l=i.match(/^\[([^\]]+)\]\(([^)]+)\)/);if(l){const{url:t,title:e}=h(l[2]),n={type:"link",url:t.trim(),children:f(l[1])};e&&(n.title=e),s.push(n),i=i.slice(l[0].length);continue}const c=i.match(/^`([^`\n]+)`/);if(c){s.push({type:"code",value:c[1]}),i=i.slice(c[0].length);continue}const r=i.match(/^(\*\*|__)(.+?)\1/);if(r){s.push({type:"strong",children:f(r[2])}),i=i.slice(r[0].length);continue}const o=i.match(/^~~(.+?)~~/);if(o){s.push({type:"del",children:f(o[1])}),i=i.slice(o[0].length);continue}const u=t[t.length-i.length-1]||"",a=!/[A-Za-z0-9_]/.test(u),p=i.match(/^\*(?!\*)(.+?)(?<!\*)\*(?!\*)/)||a&&i.match(/^_(?![_\s])(.+?)(?<![\s_])_(?![A-Za-z0-9_])/);if(p){s.push({type:"em",children:f(p[1])}),i=i.slice(p[0].length);continue}const d=i.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}),i=i.slice(d[0].length);continue}const g=i.search(/[`*_~![\\n]|https?:\/\//);if(-1===g){s.push({type:"text",value:i});break}0===g?(s.push({type:"text",value:i[0]}),i=i.slice(1)):(s.push({type:"text",value:i.slice(0,g)}),i=i.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 f(t,e){return u(t.replace(/\n/g," "))}return t.version="1.2.22","undefined"!=typeof module&&module.exports&&(module.exports=t),"undefined"!=typeof window&&(window.quikdown_ast=t),t}); //# sourceMappingURL=quikdown_ast.umd.min.js.map