mdat 0.1.0 → 0.2.0
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/bin/cli.js +3 -3
- package/dist/api.d.ts +16 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/package.json +3 -3
package/bin/cli.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{defaultLoaders as
|
|
3
|
-
${JSON.stringify(e,void 0,2)}`)}function k(e,
|
|
4
|
-
${JSON.stringify(e,void 0,2)}`)}import
|
|
2
|
+
import{defaultLoaders as W}from"cosmiconfig";function w(e,t){let n=W[".json"],a=n(e,t);return P(a)}function P(e,t="",n={}){for(let[a,i]of Object.entries(e)){let o=t?`${t}.${a}`:a;typeof i=="object"&&i!==null&&!Array.isArray(i)?P(i,o,n):i===null?n[o]="null":n[o]=i.toString()}return n}import M from"chalk";import{cosmiconfig as v}from"cosmiconfig";import _ from"node:fs/promises";import q from"node:path";import B from"plur";import{deepMergeDefined as b,log as h,optionsSchema as G,rulesSchema as Y}from"remark-mdat";async function C(e){let{additionalConfig:t,additionalRules:n,configExtensionSchema:a,searchFrom:i}=e??{},o={},d=G;a!==void 0&&(d=d.merge(a));let f=v("mdat"),u=await f.search(i);if(u){let{config:l,filepath:s}=u;h.info(`Using config from "${s}"`);let r=k(l,d);r&&(o=b(o,r))}if(t!==void 0){let l=Array.isArray(t)?t:[t];for(let s of l){let r;if(typeof s=="string"){let c=await f.load(s);if(c==null)continue;let{config:p,filepath:y}=c;h.info(`Loaded additional config from "${y}"`),r=p}else r=s;if(r===void 0)continue;h.info("Merging configuration object");let g=k(r,d);g!==void 0&&(o=b(o,g))}}if(n!==void 0){let l=Array.isArray(n)?n:[n],s=v("mdat",{loaders:{".json":w}});for(let r of l){let g;if(typeof r=="string"){let p;if(q.basename(r).endsWith("package.json")){let U=await _.readFile(r,"utf8");p={config:w(r,U),filepath:r}}else p=await s.load(r);if(p==null)continue;let{config:y,filepath:Z}=p;h.info(`Loaded additional config from "${Z}"`),g=y}else g=r;if(g===void 0)continue;h.info("Merging rules into configuration object");let c=H(g,Y);c!==void 0&&(o=b(o,c))}}if(o.rules){let l=Object.keys(o.rules).sort().map(s=>`"${M.bold.green(s)}"`);h.info(`Loaded ${M.bold(l.length)} mdat comment expansion ${B("rule",l.length)}:`);for(let s of l)h.info(` ${s}`)}else h.error("No rules loaded from additional configurations or rules, using default.");return o}function H(e,t){if(t.safeParse(e).success)return{rules:e};h.error(`Rules object has the wrong shape. Ignoring and using default configuration:
|
|
3
|
+
${JSON.stringify(e,void 0,2)}`)}function k(e,t){if(t.safeParse(e).success)return e;h.error(`Config object has the wrong shape. Ignoring and using default configuration:
|
|
4
|
+
${JSON.stringify(e,void 0,2)}`)}import Q from"node:fs";import x from"node:path";import{isFileSync as R}from"path-type";import X from"untildify";function K(e,t){let n=t===0?1:Math.floor(Math.log10(Math.abs(t))+1);return e.toString().padStart(n,"0")}function O(e,t,n,a){let i=[];for(let[o,d]of e.entries()){let f=n&&e.length>1?`-${K(o+1,e.length+1)}`:"",u=$(d,t,n,a,f);i.push(u)}return i}function $(e,t,n,a,i=""){let o=F(e),d=t?F(t):void 0;if(!R(o))throw new Error(`Input file not found: "${o}"`);if(d){if(R(d))throw new Error(`Output path must be a directory, received a file path: "${d}"`);Q.mkdirSync(d,{recursive:!0})}let f=n?x.basename(n,x.extname(n)):x.basename(o,x.extname(o)),u=`.${a??(n&&x.extname(n)!==""?x.extname(n):x.extname(e)===""?"":x.extname(e))}`,l=`${f}${i}${u}`,s=d??x.dirname(o);return{input:o,name:l,output:s}}function F(e){return X(e)}import{remark as S}from"remark";import A from"remark-gfm";import{mdatClean as ee,mdatSplit as ne,default as te}from"remark-mdat";import{read as E}from"to-vfile";import{VFile as Se}from"vfile";async function I(e,t,n,a,i){let o=await C({additionalConfig:a,additionalRules:i}),d=O(e,n,t,"md"),f=[],u=oe(o);for(let{input:l,name:s,output:r}of d){let g=await E(l),c=await u.process(g);c.dirname=r,c.basename=s,f.push(c)}return f}function oe(e){return S().use({settings:{bullet:"-",emphasis:"_"}}).use(A).use(te,e??{})}async function T(e,t,n,a){let i=await C({additionalConfig:a});console.log("----------------------------------"),console.log(`config: ${JSON.stringify(a,void 0,2)}`),console.log(`resolvedOptions: ${JSON.stringify(i,void 0,2)}`);let{closingPrefix:o,keywordPrefix:d,metaCommentIdentifier:f}=i,u=O(e,n,t,"md"),l=[],s=ie({closingPrefix:o,keywordPrefix:d,metaCommentIdentifier:f});for(let{input:r,name:g,output:c}of u){let p=await E(r),y=await s.process(p);y.dirname=c,y.basename=g,l.push(y)}return l}function ie(e){let{closingPrefix:t="/",keywordPrefix:n="",metaCommentIdentifier:a="+"}=e;return S().use({settings:{bullet:"-",emphasis:"_"}}).use(A).use(()=>function(o,d){ne(o,d),ee(o,d,{closingPrefix:t,keywordPrefix:n,metaCommentIdentifier:a})})}import j from"plur";import J from"pretty-ms";import{getMdatReports as V,log as m,reporterMdat as L}from"remark-mdat";import{write as N}from"to-vfile";import re from"yargs";import{hideBin as se}from"yargs/helpers";var D=performance.now(),z=re(se(process.argv));try{await z.scriptName("mdat").usage("$0 [command] [options]","Use the `mdat` comment expansion system with your Markdown files.").command(["$0 <files..> [options]","expand <files..> [options]"],"Expand comment placeholders in Markdown files.",e=>e.positional("files",{array:!0,demandOption:!0,describe:"Markdown file(s) with `mdat` placeholder comments to expand.",type:"string"}).option("config",{defaultDescription:"Configuration is loaded if found from the usual places, or defaults are used.",description:"Path(s) to files containing mdat configs.",string:!0,type:"array"}).option("rules",{alias:"r",description:"Path(s) to files containing `mdat` comment expansion rules.",string:!0,type:"array"}).option("output",{alias:"o",defaultDescription:"Same directory as input file.",description:"Output file directory.",type:"string"}).option("name",{alias:"n",defaultDescription:"Same name as input file. Overwrites the input file.",description:"Output file name.",type:"string"}).option("print",{default:!1,description:"Print the expanded Markdown to stdout instead of saving to a file. Ignores `--output` and `--name` options.",type:"boolean"}).option("prefix",{description:"Require a string prefix before all comments to be considered for expansion. Useful if you have a bunch of non-`mdat` comments in your Markdown file, or if you're willing to trade some verbosity for safety.",type:"string"}).option("meta",{alias:"m",default:!1,description:"Embed an extra comment at the top of the generated Markdown noting the date of generation and warning editors that certain sections of the document have been generated dynamically.",type:"boolean"}).option("check",{alias:"c",default:!1,describe:"Check the input files for rule violations without expanding them. Identifies things like missing comment placeholders and incorrect placeholder ordering.",type:"boolean"}).option("verbose",{default:!1,describe:"Enable verbose logging. All verbose logs and prefixed with their log level and are printed to stderr for ease of redirection.",type:"boolean"}),async({check:e,config:t,files:n,meta:a,name:i,output:o,prefix:d="",print:f,rules:u,verbose:l})=>{m.verbose=l,e&&(o&&(o=void 0,m.warn("Ignoring --output option because --check is set")),i&&(i=void 0,m.warn("Ignoring --name option because --check is set")),f&&(f=!1,m.warn("Ignoring --print option because --check is set"))),f&&(o&&(o=void 0,m.warn("Ignoring --output option because --print is set")),i&&(i=void 0,m.warn("Ignoring --name option because --print is set")));let s=[...t??[],{addMetaComment:a,keywordPrefix:d}];Array.isArray(n)||(n=[n]);let r=await I(n,i,o,s,u);if(f)for(let p of r)process.stdout.write(p.toString());if(L(r),!e&&!f)for(let p of r)await N(p);let c=V(r).reduce((p,y)=>p+y.errors.length,0);m.info(`Expanded comments in ${n.length} ${j("file",n.length)} in ${J(performance.now()-D)}.`),process.exitCode=c>0?1:0}).command(["clean <files..> [options]"],"Collapse `mdat` comment placeholders in Markdown files.",e=>e.positional("files",{array:!0,demandOption:!0,describe:"Markdown file(s) with `mdat` placeholder comments to collapse.",type:"string"}).option("config",{defaultDescription:"Configuration is loaded if found from the usual places, or defaults are used.",description:"Path(s) to files containing mdat configs.",string:!0,type:"array"}).option("output",{alias:"o",defaultDescription:"Same directory as input file.",description:"Output file directory.",type:"string"}).option("name",{alias:"n",defaultDescription:"Same name as input file. Overwrites the input file.",description:"Output file name.",type:"string"}).option("print",{default:!1,description:"Print the expanded Markdown to stdout instead of saving to a file. Ignores `--output` and `--name` options.",type:"boolean"}).option("verbose",{default:!1,describe:"Enable verbose logging. All verbose logs and prefixed with their log level and are printed to stderr for ease of redirection.",type:"boolean"}),async({config:e,files:t,name:n,output:a,print:i,verbose:o})=>{m.verbose=o,i&&(a&&(a=void 0,m.warn("Ignoring --output option because --print is set")),n&&(n=void 0,m.warn("Ignoring --name option because --print is set")));let d=e??[];Array.isArray(t)||(t=[t]);let f=await T(t,n,a,d);if(i)for(let s of f)process.stdout.write(s.toString());if(L(f),!i)for(let s of f)await N(s);let l=V(f).reduce((s,r)=>s+r.errors.length,0);m.info(`Cleaned comments in ${t.length} ${j("file",t.length)} in ${J(performance.now()-D)}.`),process.exitCode=l>0?1:0}).help().alias("h","help").version().alias("v","version").wrap(process.stdout.isTTY?Math.min(120,z.terminalWidth()):0).fail(!1).parse()}catch(e){e instanceof Error&&m.error(e.message),process.exitCode=1}
|
package/dist/api.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Options as MdatOptions, type Rules } from 'remark-mdat';
|
|
1
|
+
import { type MdatCleanOptions, type Options as MdatOptions, type Rules } from 'remark-mdat';
|
|
2
2
|
import { VFile } from 'vfile';
|
|
3
3
|
/**
|
|
4
4
|
* Generously accept either string paths to .ts, .js, or .json files with
|
|
@@ -6,6 +6,12 @@ import { VFile } from 'vfile';
|
|
|
6
6
|
* will be merged right to left.
|
|
7
7
|
*/
|
|
8
8
|
export type ExpandConfig<T extends MdatOptions = MdatOptions> = Array<T | string> | T | string;
|
|
9
|
+
/**
|
|
10
|
+
* Generously accept either string paths to .ts, .js, or .json files with
|
|
11
|
+
* `MdatOptions` type default exports. Takes a single value, or an array of values which
|
|
12
|
+
* will be merged right to left.
|
|
13
|
+
*/
|
|
14
|
+
export type CleanConfig<T extends Partial<MdatCleanOptions> = Partial<MdatCleanOptions>> = Array<T | string> | T | string;
|
|
9
15
|
/**
|
|
10
16
|
* Generously accept either string paths to .ts, .js, or .json files with
|
|
11
17
|
* `Rules` type default exports.
|
|
@@ -20,3 +26,12 @@ export declare function expandFiles(files: string[], name?: string, output?: str
|
|
|
20
26
|
*/
|
|
21
27
|
export declare function expandFile(file: string, name?: string, output?: string, config?: ExpandConfig, rules?: ExpandRules): Promise<VFile>;
|
|
22
28
|
export declare function expandString(markdown: string, config?: ExpandConfig, rules?: ExpandRules): Promise<VFile>;
|
|
29
|
+
/**
|
|
30
|
+
* Writing is the responsibility of the caller (e.g. via `await write(result)`)
|
|
31
|
+
*/
|
|
32
|
+
export declare function cleanFiles(files: string[], name?: string, output?: string, config?: CleanConfig): Promise<VFile[]>;
|
|
33
|
+
/**
|
|
34
|
+
* Writing is the responsibility of the caller (e.g. via `await write(result)`)
|
|
35
|
+
*/
|
|
36
|
+
export declare function cleanFile(file: string, name?: string, output?: string, config?: CleanConfig): Promise<VFile>;
|
|
37
|
+
export declare function cleanString(markdown: string, config: CleanConfig): Promise<VFile>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { type ExpandConfig, type ExpandRules, expandFile, expandFiles, expandString } from './api';
|
|
1
|
+
export { type CleanConfig, type ExpandConfig, type ExpandRules, cleanFile, cleanFiles, cleanString, expandFile, expandFiles, expandString, } from './api';
|
|
2
2
|
export { type MdatConfig, loadConfig } from './config';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{defaultLoaders as
|
|
2
|
-
${JSON.stringify(n,void 0,2)}`)}function
|
|
3
|
-
${JSON.stringify(n,void 0,2)}`)}import
|
|
1
|
+
import{defaultLoaders as v}from"cosmiconfig";function P(n,e){let t=v[".json"],i=t(n,e);return M(i)}function M(n,e="",t={}){for(let[i,s]of Object.entries(n)){let o=e?`${e}.${i}`:i;typeof s=="object"&&s!==null&&!Array.isArray(s)?M(s,o,t):s===null?t[o]="null":t[o]=s.toString()}return t}import S from"chalk";import{cosmiconfig as j}from"cosmiconfig";import L from"node:fs/promises";import N from"node:path";import z from"plur";import{deepMergeDefined as w,log as g,optionsSchema as Z,rulesSchema as _}from"remark-mdat";async function x(n){let{additionalConfig:e,additionalRules:t,configExtensionSchema:i,searchFrom:s}=n??{},o={},r=Z;i!==void 0&&(r=r.merge(i));let u=j("mdat"),l=await u.search(s);if(l){let{config:a,filepath:d}=l;g.info(`Using config from "${d}"`);let f=T(a,r);f&&(o=w(o,f))}if(e!==void 0){let a=Array.isArray(e)?e:[e];for(let d of a){let f;if(typeof d=="string"){let c=await u.load(d);if(c==null)continue;let{config:m,filepath:h}=c;g.info(`Loaded additional config from "${h}"`),f=m}else f=d;if(f===void 0)continue;g.info("Merging configuration object");let p=T(f,r);p!==void 0&&(o=w(o,p))}}if(t!==void 0){let a=Array.isArray(t)?t:[t],d=j("mdat",{loaders:{".json":P}});for(let f of a){let p;if(typeof f=="string"){let m;if(N.basename(f).endsWith("package.json")){let I=await L.readFile(f,"utf8");m={config:P(f,I),filepath:f}}else m=await d.load(f);if(m==null)continue;let{config:h,filepath:V}=m;g.info(`Loaded additional config from "${V}"`),p=h}else p=f;if(p===void 0)continue;g.info("Merging rules into configuration object");let c=D(p,_);c!==void 0&&(o=w(o,c))}}if(o.rules){let a=Object.keys(o.rules).sort().map(d=>`"${S.bold.green(d)}"`);g.info(`Loaded ${S.bold(a.length)} mdat comment expansion ${z("rule",a.length)}:`);for(let d of a)g.info(` ${d}`)}else g.error("No rules loaded from additional configurations or rules, using default.");return o}function D(n,e){if(e.safeParse(n).success)return{rules:n};g.error(`Rules object has the wrong shape. Ignoring and using default configuration:
|
|
2
|
+
${JSON.stringify(n,void 0,2)}`)}function T(n,e){if(e.safeParse(n).success)return n;g.error(`Config object has the wrong shape. Ignoring and using default configuration:
|
|
3
|
+
${JSON.stringify(n,void 0,2)}`)}import G from"node:fs";import y from"node:path";import{isFileSync as $}from"path-type";import U from"untildify";function W(n,e){let t=e===0?1:Math.floor(Math.log10(Math.abs(e))+1);return n.toString().padStart(t,"0")}function b(n,e,t,i){let s=[];for(let[o,r]of n.entries()){let u=t&&n.length>1?`-${W(o+1,n.length+1)}`:"",l=C(r,e,t,i,u);s.push(l)}return s}function C(n,e,t,i,s=""){let o=k(n),r=e?k(e):void 0;if(!$(o))throw new Error(`Input file not found: "${o}"`);if(r){if($(r))throw new Error(`Output path must be a directory, received a file path: "${r}"`);G.mkdirSync(r,{recursive:!0})}let u=t?y.basename(t,y.extname(t)):y.basename(o,y.extname(o)),l=`.${i??(t&&y.extname(t)!==""?y.extname(t):y.extname(n)===""?"":y.extname(n))}`,a=`${u}${s}${l}`,d=r??y.dirname(o);return{input:o,name:a,output:d}}function k(n){return U(n)}import{remark as J}from"remark";import A from"remark-gfm";import{mdatClean as q,mdatSplit as B,default as H}from"remark-mdat";import{read as O}from"to-vfile";import{VFile as E}from"vfile";async function Q(n,e,t,i,s){let o=await x({additionalConfig:i,additionalRules:s}),r=b(n,t,e,"md"),u=[],l=F(o);for(let{input:a,name:d,output:f}of r){let p=await O(a),c=await l.process(p);c.dirname=f,c.basename=d,u.push(c)}return u}async function X(n,e,t,i,s){let o=await x({additionalConfig:i,additionalRules:s}),r=C(n,t,e,"md"),u=await O(r.input),l=await F(o).process(u);return l.dirname=r.output,l.basename=r.name,l}async function Y(n,e,t){let i=await x({additionalConfig:e,additionalRules:t});return F(i).process(new E(n))}function F(n){return J().use({settings:{bullet:"-",emphasis:"_"}}).use(A).use(H,n??{})}async function K(n,e,t,i){let s=await x({additionalConfig:i});console.log("----------------------------------"),console.log(`config: ${JSON.stringify(i,void 0,2)}`),console.log(`resolvedOptions: ${JSON.stringify(s,void 0,2)}`);let{closingPrefix:o,keywordPrefix:r,metaCommentIdentifier:u}=s,l=b(n,t,e,"md"),a=[],d=R({closingPrefix:o,keywordPrefix:r,metaCommentIdentifier:u});for(let{input:f,name:p,output:c}of l){let m=await O(f),h=await d.process(m);h.dirname=c,h.basename=p,a.push(h)}return a}async function nn(n,e,t,i){let{closingPrefix:s,keywordPrefix:o,metaCommentIdentifier:r}=await x({additionalConfig:i}),u=C(n,t,e,"md"),l=await O(u.input),a=await R({closingPrefix:s,keywordPrefix:o,metaCommentIdentifier:r}).process(l);return a.dirname=u.output,a.basename=u.name,a}async function tn(n,e){let{closingPrefix:t,keywordPrefix:i,metaCommentIdentifier:s}=await x({additionalConfig:e});return R({closingPrefix:t,keywordPrefix:i,metaCommentIdentifier:s}).process(new E(n))}function R(n){let{closingPrefix:e="/",keywordPrefix:t="",metaCommentIdentifier:i="+"}=n;return J().use({settings:{bullet:"-",emphasis:"_"}}).use(A).use(()=>function(o,r){B(o,r),q(o,r,{closingPrefix:e,keywordPrefix:t,metaCommentIdentifier:i})})}export{nn as cleanFile,K as cleanFiles,tn as cleanString,X as expandFile,Q as expandFiles,Y as expandString,x as loadConfig};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mdat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "CLI tool and library for using comments as content templates in Markdown files.",
|
|
6
6
|
"repository": {
|
|
@@ -57,10 +57,10 @@
|
|
|
57
57
|
"vfile": "^6.0.1",
|
|
58
58
|
"yargs": "^17.7.2",
|
|
59
59
|
"zod": "^3.22.4",
|
|
60
|
-
"remark-mdat": "0.
|
|
60
|
+
"remark-mdat": "0.2.0"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@types/node": "^20.11.
|
|
63
|
+
"@types/node": "^20.11.16",
|
|
64
64
|
"@types/yargs": "^17.0.32",
|
|
65
65
|
"execa": "^8.0.1",
|
|
66
66
|
"nanoid": "^5.0.5",
|