remark-mdat 1.2.2 → 1.2.3

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
@@ -1,10 +1,248 @@
1
- export { mdat, type Options as MdatOptions } from './lib/mdast-utils/mdast-util-mdat';
2
- export { mdatCheck, type Options as MdatCheckOptions, } from './lib/mdast-utils/mdast-util-mdat-check';
3
- export { mdatClean, type Options as MdatCleanOptions, } from './lib/mdast-utils/mdast-util-mdat-clean';
4
- export { mdatExpand, type Options as MdatExpandOptions, } from './lib/mdast-utils/mdast-util-mdat-expand';
5
- export { mdatSplit } from './lib/mdast-utils/mdast-util-mdat-split';
6
- export { deepMergeDefined } from './lib/mdat/deep-merge-defined';
7
- export { default as log } from './lib/mdat/log';
8
- export { getMdatReports, type MdatFileReport, type MdatMessage, reporterMdat, } from './lib/mdat/mdat-log';
9
- export { getSoleRule, getSoleRuleKey, type NormalizedRule, type NormalizedRules, type Rule, type Rules, rulesSchema, type SimplifyDeep, } from './lib/mdat/rules';
10
- export { default, type Options, optionsSchema } from './lib/remark-mdat';
1
+ import { z } from "zod";
2
+ import { Root } from "mdast";
3
+ import { VFile } from "vfile";
4
+ import { JsonValue, MergeDeep, Simplify } from "type-fest";
5
+ import { Plugin } from "unified";
6
+
7
+ //#region src/lib/mdat/rules.d.ts
8
+ type SimplifyDeep<T> = Simplify<MergeDeep<T, T>>;
9
+ /**
10
+ * Strict normalized rules used internally.
11
+ * Rules normalized to a form with async content functions and other default metadata
12
+ * Simplifies processing elsewhere, while retaining flexibility for rule authors
13
+ */
14
+ type NormalizedRule = {
15
+ /**
16
+ * The order in which the rule should be applied during processing
17
+ * Helpful if a rule depends on the presence of content generated by another rule
18
+ * Defaults to 0.
19
+ */
20
+ applicationOrder: number;
21
+ /**
22
+ * The function that generates the expanded Markdown string.
23
+ * For 'compound' rules, this can be an array of rules (without keywords).
24
+ */
25
+ content: ((options: JsonValue, tree: Root) => Promise<string>) | NormalizedRule[];
26
+ /**
27
+ * The expected order of the keyword in the document relative to other expander comments.
28
+ * Used for validation purposes.
29
+ * Leave undefined to order skip validation.
30
+ * Defaults to undefined, which means order is not enforced.
31
+ */
32
+ order: number | undefined;
33
+ /**
34
+ * Whether the presence of the keyword comment in the document is required.
35
+ * Used for validation purposes.
36
+ * Defaults to false.
37
+ */
38
+ required: boolean;
39
+ };
40
+ type Rule =
41
+ /**
42
+ * Function that returns the Markdown string to expand at the comment site.
43
+ */
44
+ ((options: JsonValue, tree: Root) => Promise<string> | string)
45
+ /**
46
+ * Compound rules may be defined an array of rules, without keywords.
47
+ * Can be defined at the top level, if no validation metadata is required, or as the 'content' value
48
+ * of a rule object with validation metadata.
49
+ */
50
+ | Rule[]
51
+ /**
52
+ * The Markdown string to expand at the comment site.
53
+ */
54
+ | string
55
+ /**
56
+ * Rule object with optional validation metadata.
57
+ */
58
+ | {
59
+ /**
60
+ * The order in which the rule should be applied during processing.
61
+ * Defaults to 0.
62
+ */
63
+ applicationOrder?: number;
64
+ /**
65
+ * Gets content to expand into the comment.
66
+ * Can be a simple string for direct replacement, a function that returns a string, or an async function that returns a string.
67
+ *
68
+ * If a function is provided, it will be passed the following arguments:
69
+ * @param options
70
+ * JSON value of options parsed immediately after the comment keyword in the comment, e.g.:
71
+ * `<!-- keyword({something: true}) -->` or
72
+ * `<!-- keyword {something: true}-->`
73
+ * Sets options to {something: true}
74
+ * @param tree
75
+ * Markdown (mdast) abstract syntax tree containing the entire parsed document. Useful for expanders that need the entire document context, such as when generating a table of contents. Do not mutate the AST, instead return a new string.
76
+ * @returns A string with the generated content. The string will be parsed as Markdown and inserted into the document at the comment's location.
77
+ */
78
+ content: ((options: JsonValue, tree: Root) => Promise<string> | string) | Rule[] | string;
79
+ /**
80
+ * The expected order of the keyword in the document relative to other expander comments.
81
+ * Defaults to undefined, which means order is not enforced.
82
+ */
83
+ order?: number | undefined;
84
+ /**
85
+ * Whether the presence of the keyword comment in the document is required.
86
+ * Defaults to false.
87
+ */
88
+ required?: boolean;
89
+ };
90
+ /**
91
+ * Rules are record objects whose keys match strings inside a Markdown comment, and values explain what should be expanded at the comment site.
92
+ *
93
+ * The record value may be a string, or an object containing additional metadata, possibly with a function to invoke to generate content.
94
+ * @example
95
+ * Most basic rule:
96
+ * ```ts
97
+ * { basic: 'content' }
98
+ * ```
99
+ *
100
+ * Rule with dynamic content:
101
+ * ```ts
102
+ * { basic: () => `${new Date().toISOString()}` }
103
+ * ```
104
+ *
105
+ * Rule with metadata:
106
+ * ```ts
107
+ * { basic-meta: { required: true, content: 'content'} }
108
+ * ```
109
+ *
110
+ * Rule with dynamic content and metadata:
111
+ * { basic-date: { required: true, content: () => `${new Date().toISOString()}` } }
112
+ */
113
+ type Rules = SimplifyDeep<Record<string, Rule>>;
114
+ type NormalizedRules = SimplifyDeep<Record<string, NormalizedRule>>;
115
+ declare const rulesSchema: z.ZodRecord<z.ZodString, z.ZodType<any, z.ZodTypeDef, any>>;
116
+ /**
117
+ * Returns the rule value from a single-rule record.
118
+ * Useful when aliasing rules or invoking them programmatically.
119
+ *
120
+ * Throws if there are no entries or more than one entry.
121
+ */
122
+ declare function getSoleRule<T extends NormalizedRules | Rules>(rules: T): T[keyof T];
123
+ /**
124
+ * Returns the rule key from a single-rule record.
125
+ * Useful for comment placeholder validation.
126
+ *
127
+ * Throws if there are no entries or more than one entry.
128
+ */
129
+ declare function getSoleRuleKey<T extends NormalizedRules | Rules>(rules: T): keyof T;
130
+ //#endregion
131
+ //#region src/lib/mdast-utils/mdast-util-mdat.d.ts
132
+ type MdatOptions = {
133
+ addMetaComment: boolean | string;
134
+ closingPrefix: string;
135
+ keywordPrefix: string;
136
+ metaCommentIdentifier: string;
137
+ rules: Rules;
138
+ };
139
+ declare function mdat(tree: Root, file: VFile, options: MdatOptions): Promise<void>;
140
+ //#endregion
141
+ //#region src/lib/mdast-utils/mdast-util-mdat-check.d.ts
142
+ type MdatCheckOptions = {
143
+ addMetaComment: boolean | string;
144
+ closingPrefix: string;
145
+ keywordPrefix: string;
146
+ metaCommentIdentifier: string; /** Enable extra checks, too noisy for real life. */
147
+ paranoid: boolean;
148
+ rules: Rules;
149
+ };
150
+ /**
151
+ * Mdast utility function to check mdat source document, and output.
152
+ */
153
+ declare function mdatCheck(tree: Root, file: VFile, options: MdatCheckOptions): Promise<void>;
154
+ //#endregion
155
+ //#region src/lib/mdast-utils/mdast-util-mdat-clean.d.ts
156
+ type MdatCleanOptions = {
157
+ closingPrefix: string;
158
+ keywordPrefix: string;
159
+ metaCommentIdentifier: string;
160
+ };
161
+ /**
162
+ * Collapses any expanded mdat comments and removes meta comments,
163
+ * effectively resetting the document to its pre-expansion state. No-op if no
164
+ * mdat comments are found.
165
+ */
166
+ declare function mdatClean(tree: Root, file: VFile, options: MdatCleanOptions): void;
167
+ //#endregion
168
+ //#region src/lib/mdast-utils/mdast-util-mdat-expand.d.ts
169
+ type MdatExpandOptions = {
170
+ addMetaComment: boolean | string;
171
+ closingPrefix: string;
172
+ keywordPrefix: string;
173
+ metaCommentIdentifier: string;
174
+ rules: Rules;
175
+ };
176
+ declare function mdatExpand(tree: Root, file: VFile, options: MdatExpandOptions): Promise<void>;
177
+ //#endregion
178
+ //#region src/lib/mdast-utils/mdast-util-mdat-split.d.ts
179
+ /**
180
+ * Mdast utility plugin to split any multi-comment nodes and their content into individual MDAST HTML
181
+ * nodes. They're wrapped in a paragraph so as not to introduce new breaks.
182
+ */
183
+ declare function mdatSplit(tree: Root, file: VFile): void;
184
+ //#endregion
185
+ //#region src/lib/mdat/deep-merge-defined.d.ts
186
+ declare function deepMergeDefined<T extends Record<string, unknown>>(...objects: T[]): T;
187
+ //#endregion
188
+ //#region src/lib/mdat/log.d.ts
189
+ declare const log: {
190
+ verbose: boolean;
191
+ log(...data: unknown[]): void;
192
+ logPrefixed(prefix: string, ...data: unknown[]): void;
193
+ info(...data: unknown[]): void;
194
+ infoPrefixed(prefix: string, ...data: unknown[]): void;
195
+ warn(...data: unknown[]): void;
196
+ warnPrefixed(prefix: string, ...data: unknown[]): void;
197
+ error(...data: unknown[]): void;
198
+ errorPrefixed(prefix: string, ...data: unknown[]): void;
199
+ };
200
+ //#endregion
201
+ //#region src/lib/mdat/mdat-log.d.ts
202
+ /**
203
+ * Tries to provide a simpler wrapper to vfile.message
204
+ */
205
+ type MdatMessage = {
206
+ column?: number;
207
+ level: 'error' | 'info' | 'warn';
208
+ line?: number;
209
+ message: string;
210
+ source?: string;
211
+ };
212
+ type MdatFileReport = {
213
+ destinationPath?: string;
214
+ errors: MdatMessage[];
215
+ infos: MdatMessage[];
216
+ sourcePath: string;
217
+ warnings: MdatMessage[];
218
+ };
219
+ declare function getMdatReports(files: VFile[]): MdatFileReport[];
220
+ declare function reporterMdat(files: VFile[]): void;
221
+ //#endregion
222
+ //#region src/lib/remark-mdat.d.ts
223
+ type Options = Partial<MdatOptions>;
224
+ declare const optionsSchema: z.ZodObject<{
225
+ addMetaComment: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodString]>>;
226
+ closingPrefix: z.ZodOptional<z.ZodString>;
227
+ keywordPrefix: z.ZodOptional<z.ZodString>;
228
+ metaCommentIdentifier: z.ZodOptional<z.ZodString>;
229
+ rules: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodType<any, z.ZodTypeDef, any>>>;
230
+ }, "strip", z.ZodTypeAny, {
231
+ closingPrefix?: string | undefined;
232
+ keywordPrefix?: string | undefined;
233
+ addMetaComment?: string | boolean | undefined;
234
+ metaCommentIdentifier?: string | undefined;
235
+ rules?: Record<string, any> | undefined;
236
+ }, {
237
+ closingPrefix?: string | undefined;
238
+ keywordPrefix?: string | undefined;
239
+ addMetaComment?: string | boolean | undefined;
240
+ metaCommentIdentifier?: string | undefined;
241
+ rules?: Record<string, any> | undefined;
242
+ }>;
243
+ /**
244
+ * A remark plugin that expands HTML comments in Markdown files.
245
+ */
246
+ declare const remarkMdat: Plugin<[Options], Root>;
247
+ //#endregion
248
+ export { type MdatCheckOptions, type MdatCleanOptions, type MdatExpandOptions, type MdatFileReport, type MdatMessage, type MdatOptions, type NormalizedRule, type NormalizedRules, type Options, type Rule, type Rules, type SimplifyDeep, deepMergeDefined, remarkMdat as default, getMdatReports, getSoleRule, getSoleRuleKey, log, mdat, mdatCheck, mdatClean, mdatExpand, mdatSplit, optionsSchema, reporterMdat, rulesSchema };