remark-mdat 0.6.10 → 0.6.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import h from"chalk";var
|
|
1
|
+
import h from"chalk";var D=process?.versions?.node!==void 0,se={verbose:!1,log(...e){if(!this.verbose)return;let r=h.gray("[Log]");D?console.warn(r,...e):console.log(r,...e)},logPrefixed(e,...r){this.info(h.blue(`[${e}]`),...r)},info(...e){if(!this.verbose)return;let r=h.green("[Info]");D?console.warn(r,...e):console.info(r,...e)},infoPrefixed(e,...r){this.info(h.blue(`[${e}]`),...r)},warn(...e){console.warn(h.yellow("[Warning]"),...e)},warnPrefixed(e,...r){this.warn(h.blue(`[${e}]`),...r)},error(...e){console.error(h.red("[Error]"),...e)},errorPrefixed(e,...r){this.error(h.blue(`[${e}]`),...r)}},y=se;import w from"chalk";import L from"node:path";function p(e,r,o,t,n,i){let a,s;n===void 0||typeof n=="number"?(a=n??0,s=i??0):(a=n?.position?.start.line??0,s=n?.position?.start.column??0);let l={place:{end:{column:s,line:a},start:{column:s,line:a}},source:o},d=e.message(t,l);d.fatal=r==="error"?!0:r==="warn"?!1:void 0}function ae(e){return{column:e.column,level:e.fatal?"error":e.fatal===!1?"warn":"info",line:e.line,message:e.reason,source:e.source}}function le(e){return e.map(r=>J(r))}function J(e){let r={destinationPath:e.history.length>0?e.history.at(-1):void 0,errors:[],infos:[],sourcePath:e.history.at(0)??e.path,warnings:[]};r.sourcePath?.startsWith(".")&&(r.sourcePath=L.relative(process.cwd(),L.join(process.cwd(),r.sourcePath)));for(let o of e.messages){let t=ae(o);t.level==="error"?r.errors.push(t):t.level==="warn"?r.warnings.push(t):r.infos.push(t)}return r}function me(e){for(let r of e){let o=J(r),{destinationPath:t,errors:n,infos:i,sourcePath:a,warnings:s}=o;y.info(`${w.bold("MDAT Report:")}`),y.info(` From: ${w.blue.bold(a)}`),y.info(` To: ${w.blue.bold(t)}`);for(let l of n)y.error(V(a,l));for(let l of s)y.warn(V(a,l));for(let l of i)y.info(V(a,l));n.length===0&&s.length===0?y.info(`No issues found in ${a}`):y.error(`${n.length} errors, ${s.length} warnings found in ${a}`)}}function V(e,r){let{column:o,level:t,line:n,message:i,source:a}=r,s=a?w.gray(`[${a}] `):"",l=n&&o?`:${n}:${o}`:"",d=de(i,t);return`${s}${d} ${w.whiteBright(e+l)}`}function de(e,r){return e.replaceAll(/<!--.+-->/g,o=>r==="info"?w.green(o):r==="warn"?w.yellow(o):w.red(o))}import pe from"json5";import{VFileMessage as b}from"vfile-message";function R(e,r,o){try{let t=ue(e.value,o);return t===void 0?void 0:{...t,node:e,parent:r}}catch(t){throw t instanceof b?(t.line=e.position?.start.line,t):t instanceof Error?new b(t.message,e):new b("Unknown error",e)}}function ue(e,r){if(!fe(e))return;let{closingPrefix:o,keywordPrefix:t,metaCommentIdentifier:n}=r,i=e.trim(),a=i.replace(/^\s*<!--+\s*/,"").replace(/\s*-*-->\s*$/,""),[s,...l]=a.split(/(\s+|\(|{)/),d=s.startsWith(n)?"meta":t!==""&&!s.startsWith(t)&&!s.startsWith(`${o}${t}`)?"native":s.startsWith(o)?"close":"open";if(d==="meta")return{content:ge(a,n),html:i,type:d};if(d==="native")return{content:a,html:i,type:d};let u=s.replace(new RegExp(`^${o}`),"").replace(new RegExp(`^${t}`),""),c=ce(l.join(""));if(d==="open"||d==="close"){let g={};try{g=pe.parse(c)}catch(f){if(f instanceof Error)throw new b(`Failed to parse comment options "${c}" for keyword "${u}": ${f.message}`)}return{closingPrefix:o,html:i,keyword:u,keywordPrefix:t,options:g,type:d}}}function fe(e){let r=e.trim();return r.startsWith("<!--")&&r.endsWith("-->")}function ce(e){return e=e.trim(),e=e.startsWith("(")?e.slice(1):e,e=e.endsWith(")")?e.slice(0,-1):e,e=e.trim(),!e.startsWith("{")&&!e.startsWith("[")&&(e="{"+e),!e.endsWith("}")&&!e.endsWith("]")&&(e+="}"),e}function ge(e,r){return e=e.trim(),e=e.startsWith(r)?e.slice(r.length):e,e=e.endsWith(r)?e.slice(0,-r.length):e,e}import{z as m}from"zod";function M(e){let r={};for(let[o,t]of Object.entries(e))if(typeof t=="string")r[o]={applicationOrder:0,content:async()=>t,order:void 0,required:!1};else if(typeof t=="function")r[o]={applicationOrder:0,content:async(n,i)=>t(n,i),order:void 0,required:!1};else if(Array.isArray(t))r[o]={applicationOrder:0,content:Object.values(M(Object.fromEntries(t.entries()))),order:void 0,required:!1};else if(typeof t.content=="string"){let n=t.content;r[o]={applicationOrder:t.applicationOrder??0,content:async()=>n,order:t.order??void 0,required:t.required??!1}}else if(Array.isArray(t.content))r[o]={applicationOrder:t.applicationOrder??0,content:Object.values(M(Object.fromEntries(t.content.entries()))),order:t.order??void 0,required:t.required??!1};else{let n=t.content;r[o]={applicationOrder:t.applicationOrder??0,content:async(i,a)=>n(i,a),order:t.order??void 0,required:t.required??!1}}return ye(r),r}function P(e){try{v.parse(e)}catch(r){if(r instanceof Error)throw new TypeError(`Error validating rules: ${r.message}`)}}function ye(e){try{he.parse(e)}catch(r){if(r instanceof Error)throw new TypeError(`Error validating rules: ${r.message}`)}}var K=m.any(),B=m.any(),G=m.lazy(()=>m.object({applicationOrder:m.number(),content:m.union([m.function().args(K.optional(),B.optional()).returns(m.promise(m.string())),m.array(G)]),order:m.number().optional(),required:m.boolean().default(!1)})),U=m.function().args(K.optional(),B.optional()).returns(m.union([m.string(),m.promise(m.string())])),S=m.lazy(()=>m.union([U,m.array(S),m.string(),m.object({applicationOrder:m.number().optional(),content:m.union([U,m.array(S),m.string()]),order:m.number().optional(),required:m.boolean().optional()})])),v=m.record(S).describe("MDAT Rules"),he=m.record(G).describe("MDAT Rules");async function C(e,r,o,t=!1){if(Array.isArray(e.content)){let n=[];for(let[i,a]of e.content.entries()){let s=Array.isArray(r)?r.at(i):void 0;try{n.push(await C(a,s??{},o))}catch(l){if(t)throw l}}return n.join(`
|
|
2
2
|
|
|
3
|
-
`)}try{return await e.content(r,
|
|
4
|
-
${a.toString()}`))}function
|
|
3
|
+
`)}try{return await e.content(r,o)}catch(n){if(t)throw n}throw new Error("Failed to expand content")}function we(e){return Re(e)}function ke(e){let r=Object.keys(e);if(r.length!==1)throw new Error(`Expected exactly one rule, found ${r.length}`);return r[0]}function Re(e){let r=Object.values(e);if(r.length===0)throw new Error('Found no entries in a "sole record" record. This should never happen');if(r.length>1)throw new Error('Found multiple entries in "sole record" record. This should never happen');return r[0]}import Z from"chalk";import Me from"cli-table3";import{CONTINUE as Ce,visit as xe}from"unist-util-visit";async function F(e,r,o){let{closingPrefix:t,keywordPrefix:n,metaCommentIdentifier:i,paranoid:a=!1,rules:s}=o;P(s);let l=M(s),d=[];xe(e,"html",(u,c,g)=>{if(g===void 0||c===void 0)return Ce;let f=R(u,g,{closingPrefix:t,keywordPrefix:n,metaCommentIdentifier:i});if(f!==void 0){let T=f.type==="open"||f.type==="close"?l[f.keyword]:void 0;d.push({...f,rule:T})}}),Te(r,d,l),$e(r,d),Ve(r,d,o),await Oe(r,d,e),a&&ve(r,d,l),Pe(r,d),be(r,d,l,o)}async function Oe(e,r,o){for(let t of r)if(t.type==="open"&&t.rule!==void 0)try{(await C(t.rule,t.options,o,!0)).trim()===""&&p(e,t.rule.required?"error":"warn","check",`${t.html} returned an empty string.`,t.node)}catch(n){n instanceof Error&&p(e,t.rule.required?"error":"warn","check",`Could not get content for ${t.html}. ${n.message}`,t.node)}}function be(e,r,o,t){if(t.keywordPrefix==="")return;let n=Object.keys(o);for(let i of r)i.type==="native"&&n.includes(i.content)&&p(e,"warn","check",`Missing prefix: ${i.html}`,i.node)}function Pe(e,r){for(let o of r)o.type==="open"&&o.rule===void 0&&p(e,"warn","check",`Missing rule for: ${o.html}`,o.node)}function ve(e,r,o){for(let[t,n]of Object.entries(o))!n.required&&!r.some(i=>i.type==="open"&&i.keyword===t)&&!X(n,r)&&p(e,"warn","check",`Missing optional: <!-- ${t} -->`)}function Te(e,r,o){for(let[t,n]of Object.entries(o))n.required&&!r.some(i=>i.type==="open"&&i.keyword===t)&&!X(n,r)&&p(e,"error","check",`Missing required: <!-- ${t} -->`)}function X(e,r){return r.reduce((o,t)=>{if(Array.isArray(t.rule?.content))for(let n of t.rule.content)n.content.toString()===e.content.toString()&&(o=!0);return o},!1)}function $e(e,r){let o=r.filter(s=>s.type==="open"&&s.rule?.order!==void 0),t=[...o].sort((s,l)=>{let d=s.rule?.order,u=l.rule?.order;if(d===void 0||u===void 0)throw new Error("Unexpected undefined rule order");return d-u}),n=Q(o),i=Q(t),a=new Me({head:[Z.bold.red("Current Order"),Z.bold.green("Required Order")],style:{compact:!0}});n.join(",")!==i.join(",")&&(a.push(...n.map((s,l)=>[s,i[l]])),p(e,"error","check",`Out of order:
|
|
4
|
+
${a.toString()}`))}function Ve(e,r,o){let{addMetaComment:t}=o,n=r.filter(i=>i.type==="meta").length;t&&n!==1&&p(e,"error","check","Missing meta comment"),!t&&n!==0&&p(e,"error","check","Unexpected meta comment"),n>1&&p(e,"error","check","Multiple meta comments")}function Q(e){return e.map((r,o)=>{if(r.type==="open"||r.type==="close")return`${o+1}. ${r.html}`;throw new Error("Unexpected comment type")})}import{CONTINUE as k,visit as Se}from"unist-util-visit";function N(e,r,o){let t;Se(e,"html",(n,i,a)=>{if(a===void 0||i===void 0)return k;let s=R(n,a,o);if(s===void 0||s.type==="native")return k;if(s.type==="meta")return a.children.splice(i,1),[k,i];if(s.type==="open")return t=s,k;if(s.type==="close"){if(t===void 0)return p(r,"error","clean","Found closing marker without opening marker",n),k;if(t.parent!==s.parent)return p(r,"error","clean","Opening marker doesn't share a parent",n),k;if(t.keyword!==s.keyword)return p(r,"error","clean","Opening marker doesn't share a keyword",n),k;let l=a.children.indexOf(t.node),u=a.children.indexOf(s.node)-l+1;return a.children.splice(l+1,u-1),t=void 0,[k,i-u+1]}})}import{remark as Fe}from"remark";import Ne from"remark-gfm";import{CONTINUE as Ee,visit as ze}from"unist-util-visit";async function E(e,r,o){let{addMetaComment:t,closingPrefix:n,keywordPrefix:i,metaCommentIdentifier:a,rules:s}=o;P(s);let l=M(s),d=[];ze(e,"html",(u,c,g)=>{if(g===void 0||c===void 0)return Ee;let f=R(u,g,{closingPrefix:n,keywordPrefix:i,metaCommentIdentifier:a});f!==void 0&&f.type==="open"&&l[f.keyword]!==void 0&&d.push(f)}),d.sort((u,c)=>l[u.keyword].applicationOrder-l[c.keyword].applicationOrder);for(let u of d){let{closingPrefix:c,html:g,keyword:f,keywordPrefix:T,node:O,options:re,parent:q}=u,te=l[f],$="";try{$=await C(te,re,e),$.trim()===""&&p(r,"error","expand",`Got empty content when expanding ${g}`,O)}catch(H){H instanceof Error&&p(r,"error","expand",`Caught error expanding ${g}, Error message: "${H.message}"`,O);continue}let oe=Fe().use(Ne).parse($).children,ne={type:"html",value:`<!-- ${c}${T}${f} -->`},ie=q.children.indexOf(O);q.children.splice(ie+1,0,...oe,ne),p(r,"info","expand",`Expanded: ${g}`,O)}if(t){let c={type:"html",value:`<!--${a} Warning: Content inside HTML comment blocks was generated by mdat and may be overwritten. ${a}-->`};e.children.unshift(c)}}import{fromHtml as We}from"hast-util-from-html";import{CONTINUE as z,SKIP as Ae,visit as ee}from"unist-util-visit";function W(e,r){ee(e,"html",(o,t,n)=>{if(n===void 0||t===void 0)return z;let i=Ie(o);i.length>1&&(p(r,"warn","split","Multiple comments in a single HTML node.",o),n.children.splice(t,1,{children:i,type:"paragraph"}))})}function Ie(e){let r=We(e.value,{fragment:!0}),o=[];return ee(r,t=>t.type==="root"?z:t.type==="text"?(o.push({position:Y(t.position,e.position?.start),type:"text",value:_(e,t)}),z):(o.push({position:Y(t.position,e.position?.start),type:"html",value:_(e,t)}),Ae)),o}function Y(e,r){if(!(e===void 0||r===void 0))return{end:{column:e.end.column-1+r.column,line:e.end.line-1+r.line,offset:e.end.offset!==void 0&&r.offset!==void 0?e.end.offset+r.offset:void 0},start:{column:e.start.column-1+r.column,line:e.start.line-1+r.line,offset:e.start.offset!==void 0&&r.offset!==void 0?e.start.offset+r.offset:void 0}}}function _(e,r){if(r.position===void 0)throw new Error("Hast ElementContent node has no position!");return e.value.slice(r.position.start.offset,r.position.end.offset)}async function A(e,r,o){let{addMetaComment:t,closingPrefix:n,keywordPrefix:i,metaCommentIdentifier:a,rules:s}=o;W(e,r),N(e,r,{closingPrefix:n,keywordPrefix:i,metaCommentIdentifier:a}),await E(e,r,{addMetaComment:t,closingPrefix:n,keywordPrefix:i,metaCommentIdentifier:a,rules:s}),await F(e,r,{addMetaComment:t,closingPrefix:n,keywordPrefix:i,metaCommentIdentifier:a,paranoid:!1,rules:s})}import{deepmerge as je}from"deepmerge-ts";function I(e){return Array.isArray(e)?e.map(r=>r&&typeof r=="object"?I(r):r).filter(r=>r!==void 0):Object.entries(e).map(([r,o])=>[r,o&&typeof o=="object"?I(o):o]).reduce((r,[o,t])=>t===void 0?r:{...r,[o]:t},{})}function j(...e){let r=e.map((o,t)=>t===0?o:I(o));return je(...r)}import{z as x}from"zod";var qe={addMetaComment:!1,closingPrefix:"/",keywordPrefix:"",metaCommentIdentifier:"+",rules:{mdat:"Powered by the Markdown Autophagic Template system: [mdat](https://github.com/kitschpatrol/mdat)."}},He=x.object({addMetaComment:x.boolean().optional(),closingPrefix:x.string().optional(),keywordPrefix:x.string().optional(),metaCommentIdentifier:x.string().optional(),rules:v.optional()}).describe("MDAT Options"),De=function(e){let r=j(qe,e);return async function(o,t){await A(o,t,r)}},Le=De;export{j as deepMergeDefined,Le as default,le as getMdatReports,we as getSoleRule,ke as getSoleRuleKey,y as log,A as mdat,F as mdatCheck,N as mdatClean,E as mdatExpand,W as mdatSplit,He as optionsSchema,me as reporterMdat,v as rulesSchema};
|