@markdoc/markdoc 0.5.13 → 0.6.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/README.md +29 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1228 -1143
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +1228 -1143
- package/dist/index.mjs.map +4 -4
- package/dist/react.js +1 -1
- package/dist/react.js.map +2 -2
- package/dist/src/ast/base.d.ts +1 -1
- package/dist/src/ast/base.d.ts.map +1 -1
- package/dist/src/ast/function.d.ts +1 -1
- package/dist/src/ast/function.d.ts.map +1 -1
- package/dist/src/ast/index.d.ts +2 -2
- package/dist/src/ast/index.d.ts.map +1 -1
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/ast/variable.d.ts +1 -1
- package/dist/src/ast/variable.d.ts.map +1 -1
- package/dist/src/formatter.d.ts.map +1 -1
- package/dist/src/functions/index.d.ts.map +1 -1
- package/dist/src/parser.d.ts.map +1 -1
- package/dist/src/renderers/html.d.ts.map +1 -1
- package/dist/src/renderers/react/react.d.ts +3 -3
- package/dist/src/renderers/react/react.d.ts.map +1 -1
- package/dist/src/renderers/react/static.d.ts +1 -1
- package/dist/src/renderers/react/static.d.ts.map +1 -1
- package/dist/src/schema-types/class.d.ts +1 -1
- package/dist/src/schema-types/class.d.ts.map +1 -1
- package/dist/src/schema-types/conditional.d.ts +1 -1
- package/dist/src/schema-types/conditional.d.ts.map +1 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/tags/conditional.d.ts +1 -1
- package/dist/src/tags/conditional.d.ts.map +1 -1
- package/dist/src/tokenizer/index.d.ts.map +1 -1
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
- package/dist/src/transformer.d.ts.map +1 -1
- package/dist/src/validator.d.ts +1 -1
- package/dist/src/validator.d.ts.map +1 -1
- package/index.ts +21 -22
- package/package.json +9 -14
- package/spec/support/matchers.d.ts +6 -4
- package/src/ast/base.test.ts +120 -0
- package/src/ast/base.ts +14 -9
- package/src/ast/function.ts +1 -1
- package/src/ast/index.ts +3 -4
- package/src/ast/node.test.ts +140 -45
- package/src/ast/node.ts +32 -13
- package/src/ast/variable.test.ts +5 -4
- package/src/ast/variable.ts +1 -1
- package/src/ast/walk.test.ts +283 -0
- package/src/formatter.test.ts +31 -3
- package/src/formatter.ts +12 -12
- package/src/functions/index.test.ts +8 -7
- package/src/functions/index.ts +1 -1
- package/src/grammar/tag.d.ts +1 -1
- package/src/grammar/tag.js +208 -285
- package/src/grammar/tag.pegjs +4 -4
- package/src/grammar/tag.test.ts +56 -52
- package/src/parser.test.ts +93 -70
- package/src/parser.ts +19 -6
- package/src/renderers/html.test.ts +56 -16
- package/src/renderers/html.ts +10 -2
- package/src/renderers/react/react.test.ts +49 -28
- package/src/renderers/react/react.ts +8 -9
- package/src/renderers/react/static.ts +7 -12
- package/src/schema-types/class.ts +1 -1
- package/src/schema-types/conditional.ts +1 -1
- package/src/schema.ts +13 -6
- package/src/tag.test.ts +2 -1
- package/src/tag.ts +1 -1
- package/src/tags/conditional.ts +4 -4
- package/src/tags/index.ts +2 -2
- package/src/tags/partial.ts +1 -1
- package/src/tokenizer/index.ts +6 -5
- package/src/tokenizer/plugins/annotations.test.ts +29 -28
- package/src/tokenizer/plugins/annotations.ts +12 -12
- package/src/tokenizer/plugins/comments.test.ts +11 -10
- package/src/tokenizer/plugins/comments.ts +2 -2
- package/src/tokenizer/plugins/frontmatter.test.ts +7 -6
- package/src/tokenizer/plugins/frontmatter.ts +2 -2
- package/src/tokenizer/plugins/link.test.ts +14 -13
- package/src/transformer.ts +7 -5
- package/src/transforms/table.test.ts +18 -17
- package/src/transforms/table.ts +1 -1
- package/src/utils.test.ts +23 -22
- package/src/utils.ts +3 -3
- package/src/validator.test.ts +60 -59
- package/src/validator.ts +43 -14
package/src/validator.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { globalAttributes } from './transformer';
|
|
2
1
|
import Ast from './ast/index';
|
|
3
|
-
import {
|
|
4
|
-
|
|
2
|
+
import { globalAttributes } from './transformer';
|
|
5
3
|
import type {
|
|
4
|
+
Config,
|
|
5
|
+
Function,
|
|
6
6
|
Location,
|
|
7
7
|
Node,
|
|
8
|
-
Function,
|
|
9
|
-
Config,
|
|
10
8
|
SchemaAttribute,
|
|
11
9
|
ValidateError,
|
|
12
10
|
ValidationError,
|
|
13
11
|
Value,
|
|
14
12
|
} from './types';
|
|
13
|
+
import { isPromise } from './utils';
|
|
15
14
|
|
|
16
15
|
const TypeMappings = {
|
|
17
16
|
String: String,
|
|
@@ -27,7 +26,7 @@ export function validateType(
|
|
|
27
26
|
type: TypeParam,
|
|
28
27
|
value: Value,
|
|
29
28
|
config: Config,
|
|
30
|
-
key: string
|
|
29
|
+
key: string,
|
|
31
30
|
): boolean | ValidationError[] {
|
|
32
31
|
if (!type) return true;
|
|
33
32
|
|
|
@@ -146,7 +145,7 @@ export default function validator(node: Node, config: Config) {
|
|
|
146
145
|
return errors;
|
|
147
146
|
}
|
|
148
147
|
|
|
149
|
-
if (schema.inline !=
|
|
148
|
+
if (schema.inline != null && node.inline !== schema.inline)
|
|
150
149
|
errors.push({
|
|
151
150
|
id: 'tag-placement-invalid',
|
|
152
151
|
level: 'critical',
|
|
@@ -178,6 +177,7 @@ export default function validator(node: Node, config: Config) {
|
|
|
178
177
|
}
|
|
179
178
|
|
|
180
179
|
for (let [key, value] of Object.entries(node.attributes)) {
|
|
180
|
+
// biome-ignore lint/suspicious/noPrototypeBuiltins: Object.hasOwn requires ES2022; Markdoc targets ES2021.
|
|
181
181
|
const attrib = Object.prototype.hasOwnProperty.call(attributes, key)
|
|
182
182
|
? attributes[key]
|
|
183
183
|
: undefined;
|
|
@@ -202,6 +202,7 @@ export default function validator(node: Node, config: Config) {
|
|
|
202
202
|
let variables = config.variables;
|
|
203
203
|
|
|
204
204
|
for (const key of value.path) {
|
|
205
|
+
// biome-ignore lint/suspicious/noPrototypeBuiltins: Object.hasOwn requires ES2022; Markdoc targets ES2021.
|
|
205
206
|
if (!Object.prototype.hasOwnProperty.call(variables, key)) {
|
|
206
207
|
missing = true;
|
|
207
208
|
break;
|
|
@@ -243,7 +244,7 @@ export default function validator(node: Node, config: Config) {
|
|
|
243
244
|
level: errorLevel || 'error',
|
|
244
245
|
message: `Attribute '${key}' must match one of ${displayMatches(
|
|
245
246
|
matches,
|
|
246
|
-
8
|
|
247
|
+
8,
|
|
247
248
|
)}. Got '${value}' instead.`,
|
|
248
249
|
});
|
|
249
250
|
|
|
@@ -263,6 +264,7 @@ export default function validator(node: Node, config: Config) {
|
|
|
263
264
|
for (const [key, { required }] of Object.entries(attributes))
|
|
264
265
|
if (
|
|
265
266
|
required &&
|
|
267
|
+
// biome-ignore lint/suspicious/noPrototypeBuiltins: Object.hasOwn requires ES2022; Markdoc targets ES2021.
|
|
266
268
|
(!Object.prototype.hasOwnProperty.call(node.attributes, key) ||
|
|
267
269
|
node.attributes[key] === undefined)
|
|
268
270
|
)
|
|
@@ -304,15 +306,42 @@ export default function validator(node: Node, config: Config) {
|
|
|
304
306
|
|
|
305
307
|
export function* walkWithParents(
|
|
306
308
|
node: Node,
|
|
307
|
-
parents: Node[] = []
|
|
309
|
+
parents: Node[] = [],
|
|
308
310
|
): Generator<[Node, Node[]]> {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
311
|
+
const stack: {
|
|
312
|
+
node: Node;
|
|
313
|
+
parents: Node[];
|
|
314
|
+
children: Node[];
|
|
315
|
+
index: number;
|
|
316
|
+
}[] = [];
|
|
317
|
+
let current: [Node, Node[]] = [node, parents];
|
|
318
|
+
|
|
319
|
+
while (true) {
|
|
320
|
+
const [currentNode, currentParents] = current;
|
|
321
|
+
yield current;
|
|
322
|
+
|
|
323
|
+
const children = [
|
|
324
|
+
...Object.values(currentNode.slots),
|
|
325
|
+
...currentNode.children,
|
|
326
|
+
];
|
|
327
|
+
if (children.length)
|
|
328
|
+
stack.push({
|
|
329
|
+
node: currentNode,
|
|
330
|
+
parents: currentParents,
|
|
331
|
+
children,
|
|
332
|
+
index: 0,
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
if (!stack.length) return;
|
|
336
|
+
const frame = stack[stack.length - 1];
|
|
337
|
+
// Copy when this child is reached: consumers can mutate yielded parent arrays.
|
|
338
|
+
current = [frame.children[frame.index++], [...frame.parents, frame.node]];
|
|
339
|
+
if (frame.index === frame.children.length) stack.pop();
|
|
340
|
+
}
|
|
312
341
|
}
|
|
313
342
|
|
|
314
343
|
function hasValidLocation(
|
|
315
|
-
error: ValidationError
|
|
344
|
+
error: ValidationError,
|
|
316
345
|
): error is ValidationError & { location: Location } {
|
|
317
346
|
const loc = error.location;
|
|
318
347
|
return (
|
|
@@ -325,7 +354,7 @@ function hasValidLocation(
|
|
|
325
354
|
|
|
326
355
|
function toValidateError(
|
|
327
356
|
node: { type: string; lines: number[]; location?: Location },
|
|
328
|
-
error: ValidationError
|
|
357
|
+
error: ValidationError,
|
|
329
358
|
): ValidateError {
|
|
330
359
|
if (hasValidLocation(error)) {
|
|
331
360
|
return {
|