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) • 8.53 kB
JavaScript
/**
* quikdown_yaml - YAML 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=[],l=t.split("\n");let c=0;for(;c<l.length;){const t=l[c];if(""===t.trim()){c++;continue}const n=t.match(/^(```|~~~)(.*)$/);if(n){const[,t,e]=n,s=e.trim(),r=[];for(c++;c<l.length;){if(l[c].match(/^(```|~~~)\s*$/)){c++;break}r.push(l[c]),c++}i.push({type:"code_block",lang:s||null,content:r.join("\n"),fence:t});continue}if(/^---+\s*$/.test(t)||/^\*\*\*+\s*$/.test(t)||/^___+\s*$/.test(t)){i.push({type:"hr"}),c++;continue}const u=t.match(/^(#{1,6})\s*(.+?)\s*#*$/);if(u){const[,t,e]=u;i.push({type:"heading",level:t.length,children:h(e)}),c++;continue}if(t.includes("|")){const t=r(l,c);if(t){i.push(t.node),c=t.nextIndex;continue}}if(t.match(/^>\s*/)){const t=[];let n=!0;for(;c<l.length;)if(l[c].match(/^>\s*/))t.push(l[c].replace(/^>\s*/,"")),n=!0,c++;else{if(!n||e(l[c]))break;t.push(l[c]),c++}const r=t.length>0?t[0].trim().match(/^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*$/i):null;if(r){const e=r[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=o(l,c);i.push(t.node),c=t.nextIndex;continue}const f=[];for(;c<l.length;){const t=l[c];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("|")&&c+1<l.length&&/^\|?[\s\-:|]+\|?$/.test(l[c+1]))break;f.push(t),c++}f.length>0&&i.push({type:"paragraph",children:h(f.join("\n"))})}return i}function r(t,e,n){if(e+1>=t.length)return null;const s=t[e],r=t[e+1];if(!/^\|?[\s\-:|]+\|?$/.test(r)||!r.includes("-"))return null;const l=i(s);if(0===l.length)return null;const c=i(r).map(t=>{const e=t.trim();return e.startsWith(":")&&e.endsWith(":")?"center":e.endsWith(":")?"right":"left"}),o=c.length,u=[];for(let t=0;t<o;t++){const e=t<l.length?l[t]:"";u.push(h(e.trim()))}const f=[];let a=e+2;for(;a<t.length;){const e=t[a];if(!e.includes("|")||""===e.trim())break;const n=i(e),s=[];for(let t=0;t<o;t++){const e=t<n.length?n[t]:"";s.push(h(e.trim()))}f.push(s),a++}return{node:{type:"table",headers:u,rows:f,alignments:c},nextIndex:a}}function i(t){let e=t.trim();return e.startsWith("|")&&(e=e.slice(1)),e.endsWith("|")&&(e=e.slice(0,-1)),e.split("|")}function l(t){return t.match(/^[ \t]*/)[0].length}function c(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 r=e,i=0,u=null;const f=t[r].match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/),a=/^\d+\./.test(f[2]),p=f[1].length;for(;r<t.length&&i<1e3;){i++;const e=t[r],n=e.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/);if(!n){if(null!==u&&c(e)&&l(e)>=p){u+=(/( {2}|\\)$/.test(u)?"\n":" ")+e.trim(),s[s.length-1].children=h(u),r++;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(;r<t.length&&n<1e3;){n++;const s=t[r],i=s.match(/^(\s*)([*\-+]|\d+\.)\s+/);if(i){if(i[1].length<=p)break}else if(!(c(s)&&l(s)>p))break;e.push(s),r++}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)}u=null;continue}const $={type:"list_item",checked:null,children:null},b=g.match(/^\[([x ])\]\s*(.*)$/i);b&&!a?($.checked="x"===b[1].toLowerCase(),u=b[2]):u=g,$.children=h(u),s.push($),r++}return{node:{type:"list",ordered:a,items:s},nextIndex:r}}function u(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 h(t,e){if(!t)return[];const s=[];let r=t;for(;r.length>0;){if(r.match(/^(.+?)(?: {2}|\\\n|\n)/)&&r.includes("\n")){const t=r.indexOf("\n"),e=r.slice(0,t),n=r.slice(t+1);if(e.endsWith(" ")||e.endsWith("\\")){const t=e.replace(/\\$/,"").replace(/ +$/,"");t&&s.push(...f(t)),s.push({type:"br"}),r=n;continue}}const e=r.match(/^!\[([^\]]*)\]\(([^)]+)\)/);if(e){const{url:t,title:n}=u(e[2]),i={type:"image",alt:e[1],url:t.trim()};n&&(i.title=n),s.push(i),r=r.slice(e[0].length);continue}const i=r.match(/^\[([^\]]+)\]\(([^)]+)\)/);if(i){const{url:t,title:e}=u(i[2]),n={type:"link",url:t.trim(),children:f(i[1])};e&&(n.title=e),s.push(n),r=r.slice(i[0].length);continue}const l=r.match(/^`([^`\n]+)`/);if(l){s.push({type:"code",value:l[1]}),r=r.slice(l[0].length);continue}const c=r.match(/^(\*\*|__)(.+?)\1/);if(c){s.push({type:"strong",children:f(c[2])}),r=r.slice(c[0].length);continue}const o=r.match(/^~~(.+?)~~/);if(o){s.push({type:"del",children:f(o[1])}),r=r.slice(o[0].length);continue}const h=t[t.length-r.length-1]||"",a=!/[A-Za-z0-9_]/.test(h),p=r.match(/^\*(?!\*)(.+?)(?<!\*)\*(?!\*)/)||a&&r.match(/^_(?![_\s])(.+?)(?<![\s_])_(?![A-Za-z0-9_])/);if(p){s.push({type:"em",children:f(p[1])}),r=r.slice(p[0].length);continue}const d=r.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}),r=r.slice(d[0].length);continue}const g=r.search(/[`*_~![\\n]|https?:\/\//);if(-1===g){s.push({type:"text",value:r});break}0===g?(s.push({type:"text",value:r[0]}),r=r.slice(1)):(s.push({type:"text",value:r.slice(0,g)}),r=r.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 h(t.replace(/\n/g," "))}t.version="1.2.22","undefined"!=typeof module&&module.exports&&(module.exports=t),"undefined"!=typeof window&&(window.quikdown_ast=t);function a(e,n={}){return p(t(e,n),0)}function p(t,e){const n=" ".repeat(e);if(null==t)return"null";if("boolean"==typeof t)return t?"true":"false";if("number"==typeof t)return String(t);if("string"==typeof t)return g(t);if(Array.isArray(t)){if(0===t.length)return"[]";const s=[];for(const r of t)if("object"!=typeof r||null===r||Array.isArray(r)){const t=p(r,e+1);s.push(`${n}- ${t}`)}else{const t=Object.entries(r);if(t.length>0){const[r,i]=t[0],l=d(i,e+1);s.push(`${n}- ${r}: ${l}`);for(let r=1;r<t.length;r++){const[i,l]=t[r],c=d(l,e+1);s.push(`${n} ${i}: ${c}`)}}else s.push(`${n}- {}`)}return"\n"+s.join("\n")}if("object"==typeof t){const s=Object.entries(t);if(0===s.length)return"{}";const r=[];for(const[t,i]of s){const s=d(i,e);r.push(`${n}${t}: ${s}`)}return r.join("\n")}return String(t)}function d(t,e){if(null==t)return"null";if("boolean"==typeof t)return t?"true":"false";if("number"==typeof t)return String(t);if("string"==typeof t)return g(t);if(Array.isArray(t))return 0===t.length?"[]":p(t,e+1);if("object"==typeof t){return 0===Object.entries(t).length?"{}":"\n"+p(t,e+1)}return String(t)}function g(t){if(!(""===t||t.includes("\n")||t.includes(":")||t.includes("#")||t.includes("'")||t.includes('"')||t.startsWith(" ")||t.endsWith(" ")||t.startsWith("-")||t.startsWith("[")||t.startsWith("{")||"true"===t||"false"===t||"null"===t||"yes"===t||"no"===t||"on"===t||"off"===t||/^\d+$/.test(t)||/^\d+\.\d+$/.test(t)))return t;if(t.includes("\n")){return`"${t.replace(/\n/g,"\\n").replace(/"/g,'\\"')}"`}return`"${t.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`}a.parse=t,a.stringify=function(t){return p(t,0)},a.version="1.2.22","undefined"!=typeof module&&module.exports&&(module.exports=a),"undefined"!=typeof window&&(window.quikdown_yaml=a);export{a as default};
//# sourceMappingURL=quikdown_yaml.esm.min.js.map