remark-mdat 2.0.1 → 2.1.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/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ import { JsonValue, MergeDeep, Simplify } from "type-fest";
6
6
  import { Plugin } from "unified";
7
7
 
8
8
  //#region src/lib/mdat/rules.d.ts
9
+ /** Recursively simplifies a type for cleaner IDE hover display. Approximation of type-fest's internal `SimplifyDeep`. */
9
10
  type SimplifyDeep<T> = Simplify<MergeDeep<T, T>>;
10
11
  /**
11
12
  * Context passed to rule content functions during expansion.
@@ -97,7 +98,9 @@ type Rule =
97
98
  * { basic-date: { order: 1, content: () => `${new Date().toISOString()}` } }
98
99
  */
99
100
  type Rules = SimplifyDeep<Record<string, Rule>>;
101
+ /** A record mapping comment keywords to {@link NormalizedRule} objects. */
100
102
  type NormalizedRules = SimplifyDeep<Record<string, NormalizedRule>>;
103
+ /** Zod schema for validating {@link Rules} records. */
101
104
  declare const rulesSchema: z.ZodRecord<z.ZodString, z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
102
105
  /**
103
106
  * Returns the rule value from a single-rule record.
@@ -115,14 +118,26 @@ declare function getSoleRule<T extends NormalizedRules | Rules>(rules: T): T[key
115
118
  declare function getSoleRuleKey<T extends NormalizedRules | Rules>(rules: T): keyof T;
116
119
  //#endregion
117
120
  //#region src/lib/mdast-utils/mdast-util-mdat.d.ts
121
+ /**
122
+ * Mdast utility that splits, collapses, and then re-expands all mdat comments
123
+ * in the tree.
124
+ */
118
125
  declare function mdat(tree: Root, file: VFile, rules: NormalizedRules | Rules): Promise<void>;
119
126
  //#endregion
120
- //#region src/lib/mdast-utils/mdast-util-mdat-clean.d.ts
127
+ //#region src/lib/mdast-utils/mdast-util-mdat-collapse.d.ts
121
128
  /**
122
129
  * Collapses any expanded mdat comments, effectively resetting the document to
123
- * its pre-expansion state. No-op if no mdat comments are found.
130
+ * its pre-expansion state, preserving the original comments. No-op if no mdat
131
+ * comments are found.
132
+ */
133
+ declare function mdatCollapse(tree: Root, file: VFile): void;
134
+ //#endregion
135
+ //#region src/lib/mdast-utils/mdast-util-mdat-clean.d.ts
136
+ /**
137
+ * @deprecated Use {@link mdatCollapse} instead. This alias will be removed in a
138
+ * future major version.
124
139
  */
125
- declare function mdatClean(tree: Root, file: VFile): void;
140
+ declare const mdatClean: typeof mdatCollapse;
126
141
  //#endregion
127
142
  //#region src/lib/mdast-utils/mdast-util-mdat-expand.d.ts
128
143
  /**
@@ -137,6 +152,14 @@ declare function mdatExpand(tree: Root, file: VFile, rules: NormalizedRules | Ru
137
152
  */
138
153
  declare function mdatSplit(tree: Root, file: VFile): void;
139
154
  //#endregion
155
+ //#region src/lib/mdast-utils/mdast-util-mdat-strip.d.ts
156
+ /**
157
+ * Strips all mdat comment nodes (both opening and closing) from the tree,
158
+ * preserving any content between them. Code-style comments (`//`, `#`, `/*`)
159
+ * are left untouched.
160
+ */
161
+ declare function mdatStrip(tree: Root, _file: VFile): void;
162
+ //#endregion
140
163
  //#region src/lib/mdat/log.d.ts
141
164
  /**
142
165
  * Set the logger instance for the module.
@@ -146,30 +169,33 @@ declare function mdatSplit(tree: Root, file: VFile): void;
146
169
  declare function setLogger(logger?: ILogBasic | ILogLayer): void;
147
170
  //#endregion
148
171
  //#region src/lib/mdat/mdat-log.d.ts
149
- /**
150
- * Tries to provide a simpler wrapper to vfile.message
151
- */
172
+ /** A simplified representation of a {@link VFileMessage}. */
152
173
  type MdatMessage = {
153
- column?: number;
154
- level: 'error' | 'info' | 'warn';
155
- line?: number;
156
- message: string;
174
+ /** Starting column of the message origin. */column?: number; /** Severity level. */
175
+ level: 'error' | 'info' | 'warn'; /** Starting line of the message origin. */
176
+ line?: number; /** Human-readable description of the issue. */
177
+ message: string; /** Namespace that produced the message (e.g. the rule name). */
157
178
  source?: string;
158
179
  };
180
+ /** Aggregated processing report for a single file. */
159
181
  type MdatFileReport = {
160
- destinationPath?: string;
161
- errors: MdatMessage[];
162
- infos: MdatMessage[];
163
- sourcePath: string;
182
+ /** Output path if the file was written to a different location. */destinationPath?: string; /** Fatal errors that prevented successful processing. */
183
+ errors: MdatMessage[]; /** Informational messages. */
184
+ infos: MdatMessage[]; /** Original input file path. */
185
+ sourcePath: string; /** Non-fatal warnings encountered during processing. */
164
186
  warnings: MdatMessage[];
165
187
  };
188
+ /** Converts an array of processed VFiles into {@link MdatFileReport} objects. */
166
189
  declare function getMdatReports(files: VFile[]): MdatFileReport[];
190
+ /** Logs a human-readable processing report for each VFile to the library logger. */
167
191
  declare function reporterMdat(files: VFile[]): void;
168
192
  //#endregion
169
193
  //#region src/lib/remark-mdat.d.ts
194
+ /** Configuration for the remarkMdat plugin. */
170
195
  type Options = {
171
- rules?: Rules;
196
+ /** Rules mapping comment keywords to expansion content. Merged with built-in defaults. */rules?: Rules;
172
197
  };
198
+ /** Zod schema for validating {@link Options}. */
173
199
  declare const optionsSchema: z.ZodObject<{
174
200
  rules: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>>;
175
201
  }, z.core.$strip>;
@@ -178,4 +204,4 @@ declare const optionsSchema: z.ZodObject<{
178
204
  */
179
205
  declare const remarkMdat: Plugin<[Options?], Root>;
180
206
  //#endregion
181
- export { type MdatFileReport, type MdatMessage, type NormalizedRule, type NormalizedRules, type Options, type Rule, type RuleContext, type Rules, type SimplifyDeep, remarkMdat as default, getMdatReports, getSoleRule, getSoleRuleKey, mdat, mdatClean, mdatExpand, mdatSplit, optionsSchema, reporterMdat, rulesSchema, setLogger };
207
+ export { type MdatFileReport, type MdatMessage, type NormalizedRule, type NormalizedRules, type Options, type Rule, type RuleContext, type Rules, type SimplifyDeep, remarkMdat as default, getMdatReports, getSoleRule, getSoleRuleKey, mdat, mdatClean, mdatCollapse, mdatExpand, mdatSplit, mdatStrip, optionsSchema, reporterMdat, rulesSchema, setLogger };
package/dist/index.js CHANGED
@@ -63,6 +63,7 @@ function vFileMessageToMdatMessage(vFileMessage) {
63
63
  source: vFileMessage.source
64
64
  };
65
65
  }
66
+ /** Converts an array of processed VFiles into {@link MdatFileReport} objects. */
66
67
  function getMdatReports(files) {
67
68
  return files.map((file) => getMdatReport(file));
68
69
  }
@@ -83,6 +84,7 @@ function getMdatReport(file) {
83
84
  }
84
85
  return mdatFileReport;
85
86
  }
87
+ /** Logs a human-readable processing report for each VFile to the library logger. */
86
88
  function reporterMdat(files) {
87
89
  for (const file of files) {
88
90
  const { destinationPath, errors, infos, sourcePath, warnings } = getMdatReport(file);
@@ -171,12 +173,13 @@ function isComment(text) {
171
173
  return trimmed.startsWith("<!--") && trimmed.endsWith("-->");
172
174
  }
173
175
  //#endregion
174
- //#region src/lib/mdast-utils/mdast-util-mdat-clean.ts
176
+ //#region src/lib/mdast-utils/mdast-util-mdat-collapse.ts
175
177
  /**
176
178
  * Collapses any expanded mdat comments, effectively resetting the document to
177
- * its pre-expansion state. No-op if no mdat comments are found.
179
+ * its pre-expansion state, preserving the original comments. No-op if no mdat
180
+ * comments are found.
178
181
  */
179
- function mdatClean(tree, file) {
182
+ function mdatCollapse(tree, file) {
180
183
  let lastOpenMarker;
181
184
  visit(tree, "html", (node, index, parent) => {
182
185
  if (parent === void 0 || index === void 0) return CONTINUE;
@@ -188,15 +191,15 @@ function mdatClean(tree, file) {
188
191
  }
189
192
  if (marker.type === "close") {
190
193
  if (lastOpenMarker === void 0) {
191
- saveLog(file, "error", "clean", "Found closing marker without opening marker", node);
194
+ saveLog(file, "error", "collapse", "Found closing marker without opening marker", node);
192
195
  return CONTINUE;
193
196
  }
194
197
  if (lastOpenMarker.parent !== marker.parent) {
195
- saveLog(file, "error", "clean", "Opening marker doesn't share a parent", node);
198
+ saveLog(file, "error", "collapse", "Opening marker doesn't share a parent", node);
196
199
  return CONTINUE;
197
200
  }
198
201
  if (lastOpenMarker.keyword !== marker.keyword) {
199
- saveLog(file, "error", "clean", "Opening marker doesn't share a keyword", node);
202
+ saveLog(file, "error", "collapse", "Opening marker doesn't share a keyword", node);
200
203
  return CONTINUE;
201
204
  }
202
205
  const openMarkerIndex = parent.children.indexOf(lastOpenMarker.node);
@@ -215,6 +218,7 @@ const NORMALIZED = Symbol("normalized");
215
218
  function isNormalized(rules) {
216
219
  return NORMALIZED in rules;
217
220
  }
221
+ /** Converts flexible {@link Rules} into strict {@link NormalizedRules} for internal processing. */
218
222
  function normalizeRules(rules) {
219
223
  validateRules(rules);
220
224
  const normalizedRules = {};
@@ -250,6 +254,7 @@ function normalizeRules(rules) {
250
254
  Object.defineProperty(normalizedRules, NORMALIZED, { value: true });
251
255
  return normalizedRules;
252
256
  }
257
+ /** Validates rules against {@link rulesSchema}, throwing on invalid input. */
253
258
  function validateRules(rules) {
254
259
  try {
255
260
  rulesSchema.parse(rules);
@@ -273,6 +278,7 @@ const ruleSchema = z.lazy(() => z.union([
273
278
  ]));
274
279
  const COMMENT_PREFIX_REGEX = /^[/*#]/;
275
280
  const keywordSchema = z.string().check(z.refine((key) => !COMMENT_PREFIX_REGEX.test(key), { message: "Rule keywords must not start with \"/\", \"*\", or \"#\" — these prefixes are reserved for comment syntax" }));
281
+ /** Zod schema for validating {@link Rules} records. */
276
282
  const rulesSchema = z.record(keywordSchema, ruleSchema).describe("MDAT Rules");
277
283
  /**
278
284
  * Expand rule content. For compound rules (content arrays), individual
@@ -414,6 +420,7 @@ function mdatSplit(tree, file) {
414
420
  }
415
421
  });
416
422
  }
423
+ /** Splits a single mdast HTML node containing multiple comments into individual HTML and text nodes. Exported for testing. */
417
424
  function splitHtmlIntoMdastNodes(mdastNode) {
418
425
  const htmlTree = fromHtml(mdastNode.value, { fragment: true });
419
426
  const mdastNodes = [];
@@ -459,14 +466,41 @@ function getOriginalMarkup(mdastNode, hastNode) {
459
466
  }
460
467
  //#endregion
461
468
  //#region src/lib/mdast-utils/mdast-util-mdat.ts
469
+ /**
470
+ * Mdast utility that splits, collapses, and then re-expands all mdat comments
471
+ * in the tree.
472
+ */
462
473
  async function mdat(tree, file, rules) {
463
474
  mdatSplit(tree, file);
464
- mdatClean(tree, file);
475
+ mdatCollapse(tree, file);
465
476
  await mdatExpand(tree, file, rules);
466
477
  }
467
478
  //#endregion
479
+ //#region src/lib/mdast-utils/mdast-util-mdat-clean.ts
480
+ /**
481
+ * @deprecated Use {@link mdatCollapse} instead. This alias will be removed in a
482
+ * future major version.
483
+ */
484
+ const mdatClean = mdatCollapse;
485
+ //#endregion
486
+ //#region src/lib/mdast-utils/mdast-util-mdat-strip.ts
487
+ /**
488
+ * Strips all mdat comment nodes (both opening and closing) from the tree,
489
+ * preserving any content between them. Code-style comments (`//`, `#`, `/*`)
490
+ * are left untouched.
491
+ */
492
+ function mdatStrip(tree, _file) {
493
+ visit(tree, "html", (node, index, parent) => {
494
+ if (parent === void 0 || index === void 0) return CONTINUE;
495
+ if (parseCommentNode(node, parent) === void 0) return CONTINUE;
496
+ parent.children.splice(index, 1);
497
+ return [CONTINUE, index];
498
+ });
499
+ }
500
+ //#endregion
468
501
  //#region src/lib/remark-mdat.ts
469
502
  const defaultRules = { mdat: `Powered by the Markdown Autophagic Template system: [mdat](https://github.com/kitschpatrol/mdat).` };
503
+ /** Zod schema for validating {@link Options}. */
470
504
  const optionsSchema = z.object({ rules: rulesSchema.optional() }).describe("MDAT Plugin Options");
471
505
  /**
472
506
  * A remark plugin that expands HTML comments in Markdown files.
@@ -481,4 +515,4 @@ const remarkMdat = function(options) {
481
515
  };
482
516
  };
483
517
  //#endregion
484
- export { remarkMdat as default, getMdatReports, getSoleRule, getSoleRuleKey, mdat, mdatClean, mdatExpand, mdatSplit, optionsSchema, reporterMdat, rulesSchema, setLogger };
518
+ export { remarkMdat as default, getMdatReports, getSoleRule, getSoleRuleKey, mdat, mdatClean, mdatCollapse, mdatExpand, mdatSplit, mdatStrip, optionsSchema, reporterMdat, rulesSchema, setLogger };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remark-mdat",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "A remark plugin implementing the Markdown Autophagic Template (MDAT) system.",
5
5
  "keywords": [
6
6
  "mdat",
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "devDependencies": {
61
61
  "@arethetypeswrong/core": "^0.18.2",
62
- "@kitschpatrol/shared-config": "^7.0.0",
62
+ "@kitschpatrol/shared-config": "^7.0.1",
63
63
  "@types/node": "~20.19.37",
64
64
  "bumpp": "^11.0.1",
65
65
  "publint": "^0.3.18",
@@ -77,8 +77,8 @@
77
77
  }
78
78
  },
79
79
  "scripts": {
80
- "bench": "vitest bench --no-file-parallelism --compare test/benchmarks/baseline.json",
81
- "bench:baseline": "vitest bench --no-file-parallelism --outputJson test/benchmarks/baseline.json",
80
+ "bench": "vitest bench --run --no-file-parallelism --compare test/benchmarks/baseline.json",
81
+ "bench:baseline": "vitest bench --run --no-file-parallelism --outputJson test/benchmarks/baseline.json",
82
82
  "build": "tsdown",
83
83
  "clean": "git rm -f pnpm-lock.yaml ; git clean -fdX",
84
84
  "dev": "pnpm run test",
package/readme.md CHANGED
@@ -46,11 +46,13 @@
46
46
  - [Removed options](#removed-options)
47
47
  - [Removed rule properties](#removed-rule-properties)
48
48
  - [Changed rule properties](#changed-rule-properties)
49
+ - [Renamed `clean` to `collapse`](#renamed-clean-to-collapse)
49
50
  - [Removed validation utility](#removed-validation-utility)
50
51
  - [Rule function signature change](#rule-function-signature-change)
51
52
  - [Stricter argument syntax](#stricter-argument-syntax)
52
53
  - [Comment-style comments are ignored](#comment-style-comments-are-ignored)
53
54
  - [Compound rule error handling](#compound-rule-error-handling)
55
+ - [New utility: `mdatStrip`](#new-utility-mdatstrip)
54
56
  - [Removed export: `deepMergeDefined`](#removed-export-deepmergedefined)
55
57
  - [Implementation notes](#implementation-notes)
56
58
  - [Maintainers](#maintainers)
@@ -273,11 +275,11 @@ Errors and warnings are reported inline during expansion via [VFile messages](ht
273
275
 
274
276
  _Exported as `mdatSplit(tree: Root, file: VFile): void`_
275
277
 
276
- - [**`mdast-util-mdat-clean`**](./src/lib/mdast-utils/mdast-util-mdat-clean.ts)
278
+ - [**`mdast-util-mdat-collapse`**](./src/lib/mdast-utils/mdast-util-mdat-collapse.ts)
277
279
 
278
280
  Transformer function that resets all mdat comment expansions in a file, collapsing expanded comments back into single-line placeholders.
279
281
 
280
- _Exported as `mdatClean(tree: Root, file: VFile): void`_
282
+ _Exported as `mdatCollapse(tree: Root, file: VFile): void`_
281
283
 
282
284
  - [**`mdast-util-mdat-expand`**](./src/lib/mdast-utils/mdast-util-mdat-expand.ts)
283
285
 
@@ -285,6 +287,12 @@ Errors and warnings are reported inline during expansion via [VFile messages](ht
285
287
 
286
288
  _Exported as `mdatExpand(tree: Root, file: VFile, rules: Rules): Promise<void>`_
287
289
 
290
+ - [**`mdast-util-mdat-strip`**](./src/lib/mdast-utils/mdast-util-mdat-strip.ts)
291
+
292
+ Transformer function that strips all mdat comment nodes (both opening and closing) from the tree, preserving any content between them. Code-style comments (`<!-- // ... -->`, `<!-- # ... -->`, `<!-- /* ... */ -->`) are left untouched. Useful for producing a final Markdown document with all mdat scaffolding removed.
293
+
294
+ _Exported as `mdatStrip(tree: Root, file: VFile): void`_
295
+
288
296
  ## Migrating from 1.x to 2.x
289
297
 
290
298
  Version 2.0 simplifies and solidifies the API by removing several configuration options and validation features that added complexity without sufficient benefit. The core expansion behavior is unchanged — the plugin still matches HTML comments to rules and expands them — but the way you configure it has changed.
@@ -339,6 +347,10 @@ The following plugin options have been removed entirely:
339
347
  | ------------------ | ------------------ |
340
348
  | `applicationOrder` | Change to `order`. |
341
349
 
350
+ ### Renamed `clean` to `collapse`
351
+
352
+ The former `mdast-util-mdat-clean` / `mdatClean` is now `mdast-util-mdat-collapse` / `mdatCollapse` since this is more clearly the opposite of "expand", and aligns with language used in the Mdat CLI tool. (Note that `mdatClean` is still available as a deprecated alias, but it will be removed in 3.0.)
353
+
342
354
  ### Removed validation utility
343
355
 
344
356
  The `mdast-util-mdat-check` utility and its export `mdatCheck` have been removed. Validation logic (missing rules, empty content, rule errors) is now handled inline during expansion by `mdatExpand`, which reports issues as VFile messages. Use `reporterMdat` to format and display these messages.
@@ -391,6 +403,10 @@ HTML comments using code-style prefixes (`<!-- // ... -->`, `<!-- # ... -->`, `<
391
403
 
392
404
  In 1.x, a failing sub-rule in a compound rule (array of rules) caused the entire expansion to fail. In 2.x, individual sub-rule failures are reported as warnings and skipped — the expansion only fails if every sub-rule fails.
393
405
 
406
+ ### New utility: `mdatStrip`
407
+
408
+ A new `mdast-util-mdat-strip` utility is available for removing all mdat comment nodes from a document while preserving the content between them. Code-style comments (`//`, `#`, `/*`) are left untouched. This is useful for producing a final Markdown document with no mdat scaffolding. See the [Utilities](#utilities) section for details.
409
+
394
410
  ### Removed export: `deepMergeDefined`
395
411
 
396
412
  The `deepMergeDefined` utility has been moved to the [`mdat`](https://github.com/kitschpatrol/mdat) package. If you were importing it from `remark-mdat`, import it from `mdat` instead.