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) • 8.75 kB
/** * quikdown_yaml - YAML 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_yaml=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 r=[],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(),i=[];for(c++;c<l.length;){if(l[c].match(/^(```|~~~)\s*$/)){c++;break}i.push(l[c]),c++}r.push({type:"code_block",lang:s||null,content:i.join("\n"),fence:t});continue}if(/^---+\s*$/.test(t)||/^\*\*\*+\s*$/.test(t)||/^___+\s*$/.test(t)){r.push({type:"hr"}),c++;continue}const u=t.match(/^(#{1,6})\s*(.+?)\s*#*$/);if(u){const[,t,e]=u;r.push({type:"heading",level:t.length,children:h(e)}),c++;continue}if(t.includes("|")){const t=i(l,c);if(t){r.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 i=t.length>0?t[0].trim().match(/^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*$/i):null;if(i){const e=i[1].toLowerCase();r.push({type:"alert",alertType:e,children:s(t.slice(1).join("\n"))})}else r.push({type:"blockquote",children:s(t.join("\n"))});continue}if(t.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/)){const t=o(l,c);r.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&&r.push({type:"paragraph",children:h(f.join("\n"))})}return r}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 l=r(s);if(0===l.length)return null;const c=r(i).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=r(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 r(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 i=e,r=0,u=null;const f=t[i].match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/),a=/^\d+\./.test(f[2]),p=f[1].length;for(;i<t.length&&r<1e3;){r++;const e=t[i],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),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],r=s.match(/^(\s*)([*\-+]|\d+\.)\s+/);if(r){if(r[1].length<=p)break}else if(!(c(s)&&l(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)}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($),i++}return{node:{type:"list",ordered:a,items:s},nextIndex:i}}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 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}=u(e[2]),r={type:"image",alt:e[1],url:t.trim()};n&&(r.title=n),s.push(r),i=i.slice(e[0].length);continue}const r=i.match(/^\[([^\]]+)\]\(([^)]+)\)/);if(r){const{url:t,title:e}=u(r[2]),n={type:"link",url:t.trim(),children:f(r[1])};e&&(n.title=e),s.push(n),i=i.slice(r[0].length);continue}const l=i.match(/^`([^`\n]+)`/);if(l){s.push({type:"code",value:l[1]}),i=i.slice(l[0].length);continue}const c=i.match(/^(\*\*|__)(.+?)\1/);if(c){s.push({type:"strong",children:f(c[2])}),i=i.slice(c[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 h=t[t.length-i.length-1]||"",a=!/[A-Za-z0-9_]/.test(h),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 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 i of t)if("object"!=typeof i||null===i||Array.isArray(i)){const t=p(i,e+1);s.push(`${n}- ${t}`)}else{const t=Object.entries(i);if(t.length>0){const[i,r]=t[0],l=d(r,e+1);s.push(`${n}- ${i}: ${l}`);for(let i=1;i<t.length;i++){const[r,l]=t[i],c=d(l,e+1);s.push(`${n} ${r}: ${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 i=[];for(const[t,r]of s){const s=d(r,e);i.push(`${n}${t}: ${s}`)}return i.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,'\\"')}"`}return 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),a}); //# sourceMappingURL=quikdown_yaml.umd.min.js.map