@markdoc/markdoc 0.5.6 → 0.5.8
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 +15 -81
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +566 -459
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +561 -458
- package/dist/index.mjs.map +4 -4
- package/dist/react.js +16 -16
- package/dist/react.js.map +2 -2
- package/dist/src/ast/index.d.ts +3 -14
- package/dist/src/ast/index.d.ts.map +1 -1
- package/dist/src/formatter.d.ts +1 -1
- package/dist/src/formatter.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/tag.d.ts +1 -1
- package/dist/src/tag.d.ts.map +1 -1
- package/dist/src/tags/index.d.ts +5 -60
- package/dist/src/tags/index.d.ts.map +1 -1
- package/dist/src/tokenizer/index.d.ts +5 -0
- package/dist/src/tokenizer/index.d.ts.map +1 -1
- package/dist/src/tokenizer/plugins/link.d.ts +4 -0
- package/dist/src/tokenizer/plugins/link.d.ts.map +1 -0
- package/dist/src/transformer.d.ts +1 -1
- package/dist/src/transformer.d.ts.map +1 -1
- package/dist/src/transforms/table.d.ts.map +1 -1
- package/dist/src/types.d.ts +21 -21
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils.d.ts +1 -0
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/validator.d.ts +3 -23
- package/dist/src/validator.d.ts.map +1 -1
- package/package.json +19 -12
- package/spec/marktest/index.ts +136 -0
- package/src/tokenizer/index.ts +15 -0
- package/src/tokenizer/plugins/link.test.ts +96 -0
- package/src/tokenizer/plugins/link.ts +37 -0
- package/src/transforms/table.test.ts +20 -9
- package/src/transforms/table.ts +6 -13
- package/src/utils.test.ts +1 -1
- package/src/utils.ts +30 -0
- package/src/validator.ts +31 -5
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
import { diffJson, diffChars } from 'diff';
|
|
4
|
+
import yaml from 'yaml-js';
|
|
5
|
+
|
|
6
|
+
import markdoc from '../../index';
|
|
7
|
+
import React from './react-shim';
|
|
8
|
+
|
|
9
|
+
class Loader extends yaml.loader.Loader {
|
|
10
|
+
construct_mapping(node) {
|
|
11
|
+
return {
|
|
12
|
+
...super.construct_mapping(node),
|
|
13
|
+
$$lines: {
|
|
14
|
+
start: node.start_mark.line,
|
|
15
|
+
end: node.end_mark.line,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const tokenizer = new markdoc.Tokenizer({
|
|
22
|
+
allowIndentation: true,
|
|
23
|
+
allowComments: true,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
function parse(content: string, slots?: boolean, file?: string) {
|
|
27
|
+
const tokens = tokenizer.tokenize(content);
|
|
28
|
+
return markdoc.parse(tokens, { file, slots });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function stripLines(object) {
|
|
32
|
+
const removeLines = (key, value) => (key === '$$lines' ? undefined : value);
|
|
33
|
+
return JSON.parse(JSON.stringify(object, removeLines));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function render(code, config, dynamic) {
|
|
37
|
+
const partials = {};
|
|
38
|
+
for (const [file, content] of Object.entries(config.partials ?? {}))
|
|
39
|
+
partials[file] = parse(content as string, false, file);
|
|
40
|
+
|
|
41
|
+
const { react, reactStatic } = markdoc.renderers;
|
|
42
|
+
const transformed = markdoc.transform(code, { ...config, partials });
|
|
43
|
+
return dynamic ? react(transformed, React) : eval(reactStatic(transformed));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function checkMatch(diffs) {
|
|
47
|
+
return diffs.find((diff) => diff.added || diff.removed);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function run(
|
|
51
|
+
{ code = null, renderer = 'react', config = {}, expected = undefined } = {},
|
|
52
|
+
dynamic
|
|
53
|
+
) {
|
|
54
|
+
if (renderer === 'html') {
|
|
55
|
+
const transformed = markdoc.transform(code, config);
|
|
56
|
+
const output = markdoc.renderers.html(transformed);
|
|
57
|
+
const diff = diffChars((expected || '').trim(), (output || '').trim());
|
|
58
|
+
return checkMatch(diff) ? diff : false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const output = render(code, config, dynamic) || {};
|
|
62
|
+
const diff = diffJson(expected || {}, output.children || []);
|
|
63
|
+
return checkMatch(diff) ? diff : false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function formatter(filename, { $$lines: lines, name, code }, diff) {
|
|
67
|
+
const prettyDiff = diff
|
|
68
|
+
.flatMap((part) => [
|
|
69
|
+
part.added ? '\x1b[32m' : part.removed ? '\x1b[31m' : '\x1b[0m',
|
|
70
|
+
part.added || part.removed ? part.value.replace(/ /g, '⎵') : part.value,
|
|
71
|
+
])
|
|
72
|
+
.join('');
|
|
73
|
+
|
|
74
|
+
return [
|
|
75
|
+
`\x1b[31mFAILED:\x1b[0m ${filename}:${lines.start}`,
|
|
76
|
+
name,
|
|
77
|
+
code,
|
|
78
|
+
prettyDiff,
|
|
79
|
+
].join('\n\n');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function formatValidation(filename, test, validation) {
|
|
83
|
+
const { $$lines: lines, name, code } = test;
|
|
84
|
+
|
|
85
|
+
let output = '';
|
|
86
|
+
for (const {
|
|
87
|
+
lines,
|
|
88
|
+
error: { message },
|
|
89
|
+
} of validation)
|
|
90
|
+
output += `${lines ? lines[0] : '?'}:${message}\n`;
|
|
91
|
+
|
|
92
|
+
return [
|
|
93
|
+
`\x1b[31mINVALID:\x1b[0m ${filename}:${lines.start}`,
|
|
94
|
+
name,
|
|
95
|
+
code,
|
|
96
|
+
output,
|
|
97
|
+
].join('\n\n');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
(async () => {
|
|
101
|
+
const [filename, line] = process.argv.slice(2).join('').split(':');
|
|
102
|
+
const path = resolve(filename);
|
|
103
|
+
const file = await fs.readFile(path);
|
|
104
|
+
const tests = yaml.load(file, Loader);
|
|
105
|
+
|
|
106
|
+
let exitCode = 0;
|
|
107
|
+
for (const test of tests) {
|
|
108
|
+
const code = parse(test.code || '', test.slots);
|
|
109
|
+
|
|
110
|
+
const { start, end } = test.$$lines;
|
|
111
|
+
if (line && (Number(line) - 1 < start || Number(line) - 1 > end)) continue;
|
|
112
|
+
|
|
113
|
+
const validation = markdoc.validate(code, test.config);
|
|
114
|
+
|
|
115
|
+
let result;
|
|
116
|
+
if (test.expectedError) {
|
|
117
|
+
const output = validation.map((v) => v.error.message).join('\n');
|
|
118
|
+
const diff = diffChars(
|
|
119
|
+
(test.expectedError || '').trim(),
|
|
120
|
+
(output || '').trim()
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
result = checkMatch(diff) ? diff : false;
|
|
124
|
+
} else {
|
|
125
|
+
if (validation.length && test.validation != false)
|
|
126
|
+
console.log(formatValidation(path, test, validation));
|
|
127
|
+
|
|
128
|
+
result = run({ ...stripLines(test), code }, true);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!result) continue;
|
|
132
|
+
console.log(formatter(path, test, result));
|
|
133
|
+
exitCode = 1;
|
|
134
|
+
}
|
|
135
|
+
process.exit(exitCode);
|
|
136
|
+
})();
|
package/src/tokenizer/index.ts
CHANGED
|
@@ -2,8 +2,11 @@ import MarkdownIt from 'markdown-it/lib';
|
|
|
2
2
|
import annotations from './plugins/annotations';
|
|
3
3
|
import frontmatter from './plugins/frontmatter';
|
|
4
4
|
import comments from './plugins/comments';
|
|
5
|
+
import link from './plugins/link';
|
|
5
6
|
import type Token from 'markdown-it/lib/token';
|
|
6
7
|
|
|
8
|
+
export type LinkPluginOptions = { validatedProtocols: string[] };
|
|
9
|
+
|
|
7
10
|
export default class Tokenizer {
|
|
8
11
|
private parser: MarkdownIt;
|
|
9
12
|
|
|
@@ -11,11 +14,14 @@ export default class Tokenizer {
|
|
|
11
14
|
config: MarkdownIt.Options & {
|
|
12
15
|
allowIndentation?: boolean;
|
|
13
16
|
allowComments?: boolean;
|
|
17
|
+
allowLinkValidation?: boolean;
|
|
18
|
+
linkValidationOptions?: LinkPluginOptions;
|
|
14
19
|
} = {}
|
|
15
20
|
) {
|
|
16
21
|
this.parser = new MarkdownIt(config);
|
|
17
22
|
this.parser.use(annotations, 'annotations', {});
|
|
18
23
|
this.parser.use(frontmatter, 'frontmatter', {});
|
|
24
|
+
|
|
19
25
|
this.parser.disable([
|
|
20
26
|
'lheading',
|
|
21
27
|
// Disable indented `code_block` support https://spec.commonmark.org/0.30/#indented-code-block
|
|
@@ -23,6 +29,15 @@ export default class Tokenizer {
|
|
|
23
29
|
]);
|
|
24
30
|
|
|
25
31
|
if (config.allowComments) this.parser.use(comments, 'comments', {});
|
|
32
|
+
if (config.allowLinkValidation) {
|
|
33
|
+
// Set http and https as the default protocols to validate
|
|
34
|
+
this.parser.use(
|
|
35
|
+
link,
|
|
36
|
+
config.linkValidationOptions ?? {
|
|
37
|
+
validatedProtocols: ['http', 'https'],
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
}
|
|
26
41
|
}
|
|
27
42
|
|
|
28
43
|
tokenize(content: string): Token[] {
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import Tokenizer from '..';
|
|
2
|
+
|
|
3
|
+
describe('MarkdownIt Link plugin', function () {
|
|
4
|
+
function parse(
|
|
5
|
+
example: string,
|
|
6
|
+
tokenizer: Tokenizer = new Tokenizer({ allowLinkValidation: true })
|
|
7
|
+
) {
|
|
8
|
+
const content = example.replace(/\n\s+/gm, '\n').trim();
|
|
9
|
+
return tokenizer.tokenize(content);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function getInlineError(tokens) {
|
|
13
|
+
return tokens.find((token) => token.type === 'inline')?.errors?.[0];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
it('accepts valid link urls and markdoc tag in one paragraph', function () {
|
|
17
|
+
const tokens = parse(`The link is https://example.com. {% tag /%})`);
|
|
18
|
+
expect(getInlineError(tokens)).toBeUndefined();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('accepts raw tag content in markdown link format', function () {
|
|
22
|
+
const tokens = parse(`[Link]({% tag %})`);
|
|
23
|
+
expect(getInlineError(tokens)).toBeUndefined();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('rejects plain link url with tags', function () {
|
|
27
|
+
const tokens = parse(`https://example.com/{% tag %}content{% /tag %})`);
|
|
28
|
+
expect(getInlineError(tokens)).toDeepEqualSubset({
|
|
29
|
+
id: 'href-format-invalid',
|
|
30
|
+
level: 'error',
|
|
31
|
+
message:
|
|
32
|
+
"The 'href' format cannot contain Markdoc tag or variable. URLs must be static strings.",
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('rejects URL with parenthesis in link', function () {
|
|
37
|
+
const tokens = parse(
|
|
38
|
+
`https://en.wikipedia.org/wiki/Exam_(disambiguation){% tag /%}`
|
|
39
|
+
);
|
|
40
|
+
expect(getInlineError(tokens)).toDeepEqualSubset({
|
|
41
|
+
id: 'href-format-invalid',
|
|
42
|
+
level: 'error',
|
|
43
|
+
message:
|
|
44
|
+
"The 'href' format cannot contain Markdoc tag or variable. URLs must be static strings.",
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('rejects variables in markdown link urls', function () {
|
|
49
|
+
const tokens = parse(`[Link](https://{% $variable.custom_value %})`);
|
|
50
|
+
expect(getInlineError(tokens)).toDeepEqualSubset({
|
|
51
|
+
id: 'href-format-invalid',
|
|
52
|
+
level: 'error',
|
|
53
|
+
message:
|
|
54
|
+
"The 'href' format cannot contain Markdoc tag or variable. URLs must be static strings.",
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('rejects self-closing tags in markdown link urls', function () {
|
|
59
|
+
const tokens = parse(`[Link](https://example.com/{% tag /%})`);
|
|
60
|
+
expect(getInlineError(tokens)).toDeepEqualSubset({
|
|
61
|
+
id: 'href-format-invalid',
|
|
62
|
+
level: 'error',
|
|
63
|
+
message:
|
|
64
|
+
"The 'href' format cannot contain Markdoc tag or variable. URLs must be static strings.",
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('rejects non self-closing tags in markdown link urls', function () {
|
|
69
|
+
const tokens = parse(
|
|
70
|
+
`[Link](https://example.com/{% tag %}content{% /tag %})`
|
|
71
|
+
);
|
|
72
|
+
expect(getInlineError(tokens)).toDeepEqualSubset({
|
|
73
|
+
id: 'href-format-invalid',
|
|
74
|
+
level: 'error',
|
|
75
|
+
message:
|
|
76
|
+
"The 'href' format cannot contain Markdoc tag or variable. URLs must be static strings.",
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('rejects custom protocols defined in the config with markdoc variable', function () {
|
|
81
|
+
const tokenizer = new Tokenizer({
|
|
82
|
+
allowLinkValidation: true,
|
|
83
|
+
linkValidationOptions: { validatedProtocols: ['vscode'] },
|
|
84
|
+
});
|
|
85
|
+
const tokens = parse(
|
|
86
|
+
`[Link](vscode://{% $variable.custom_value %})`,
|
|
87
|
+
tokenizer
|
|
88
|
+
);
|
|
89
|
+
expect(getInlineError(tokens)).toDeepEqualSubset({
|
|
90
|
+
id: 'href-format-invalid',
|
|
91
|
+
level: 'error',
|
|
92
|
+
message:
|
|
93
|
+
"The 'href' format cannot contain Markdoc tag or variable. URLs must be static strings.",
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type MarkdownIt from 'markdown-it/lib';
|
|
2
|
+
import type StateCore from 'markdown-it/lib/rules_core/state_core';
|
|
3
|
+
import type Token from 'markdown-it/lib/token';
|
|
4
|
+
import type { ValidationError } from '../../types';
|
|
5
|
+
|
|
6
|
+
import { containsMarkdocTagInUrl } from '../../utils';
|
|
7
|
+
import { LinkPluginOptions } from '..';
|
|
8
|
+
|
|
9
|
+
const INVALID_HREF_MESSAGE =
|
|
10
|
+
"The 'href' format cannot contain Markdoc tag or variable. URLs must be static strings.";
|
|
11
|
+
|
|
12
|
+
type TokenWithErrors = Token & { errors?: ValidationError[] };
|
|
13
|
+
|
|
14
|
+
function pushHrefError(token: Token) {
|
|
15
|
+
const tokenWithErrors = token as TokenWithErrors;
|
|
16
|
+
if (!tokenWithErrors.errors) tokenWithErrors.errors = [];
|
|
17
|
+
tokenWithErrors.errors.push({
|
|
18
|
+
id: 'href-format-invalid',
|
|
19
|
+
level: 'error',
|
|
20
|
+
message: INVALID_HREF_MESSAGE,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function core(state: StateCore, options: LinkPluginOptions) {
|
|
25
|
+
let token: Token;
|
|
26
|
+
for (token of state.tokens) {
|
|
27
|
+
if (token.type !== 'inline' || typeof token.content !== 'string') continue;
|
|
28
|
+
|
|
29
|
+
if (containsMarkdocTagInUrl(token.content, options.validatedProtocols)) {
|
|
30
|
+
pushHrefError(token);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default function plugin(md: MarkdownIt, options: LinkPluginOptions) {
|
|
36
|
+
md.core.ruler.push('link_url_validation', (state) => core(state, options));
|
|
37
|
+
}
|
|
@@ -23,8 +23,8 @@ This is invalid non-conditional content at the row level.
|
|
|
23
23
|
(e) => e.error.id === 'table-syntax'
|
|
24
24
|
);
|
|
25
25
|
|
|
26
|
-
// One error for the paragraph inside the conditional
|
|
27
|
-
// one for the bare paragraph
|
|
26
|
+
// One error for the paragraph inside the conditional,
|
|
27
|
+
// one for the bare paragraph at the row level
|
|
28
28
|
expect(tableSyntaxErrors.length).toBe(2);
|
|
29
29
|
for (const err of tableSyntaxErrors) {
|
|
30
30
|
expect(err.error.level).toBe('critical');
|
|
@@ -32,18 +32,21 @@ This is invalid non-conditional content at the row level.
|
|
|
32
32
|
expect(err.error.message).toContain('indented');
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
//
|
|
35
|
+
// Errors should point to the actual invalid content, not the table
|
|
36
36
|
const conditionalError = tableSyntaxErrors.find(
|
|
37
|
-
(e) => e.
|
|
37
|
+
(e) => e.location?.start.line === 7
|
|
38
38
|
);
|
|
39
|
-
expect(conditionalError).toBeDefined();
|
|
40
|
-
expect(conditionalError?.location?.start.line).toBeGreaterThan(0);
|
|
41
|
-
|
|
42
|
-
// The row-level error should point to the table tag (paragraph is discarded)
|
|
43
39
|
const rowLevelError = tableSyntaxErrors.find(
|
|
44
|
-
(e) => e.location?.start.line ===
|
|
40
|
+
(e) => e.location?.start.line === 9
|
|
45
41
|
);
|
|
42
|
+
expect(conditionalError).toBeDefined();
|
|
43
|
+
expect(conditionalError?.location?.start.line).toBe(7);
|
|
44
|
+
expect(conditionalError?.location?.end.line).toBe(8);
|
|
45
|
+
expect(conditionalError?.lines).toEqual([7, 8]);
|
|
46
46
|
expect(rowLevelError).toBeDefined();
|
|
47
|
+
expect(rowLevelError?.location?.start.line).toBe(9);
|
|
48
|
+
expect(rowLevelError?.location?.end.line).toBe(10);
|
|
49
|
+
expect(rowLevelError?.lines).toEqual([9, 10]);
|
|
47
50
|
});
|
|
48
51
|
|
|
49
52
|
it('does not produce errors for valid conditional rows', function () {
|
|
@@ -174,6 +177,10 @@ This is not a valid row
|
|
|
174
177
|
expect(tableSyntaxErrors.length).toBe(1);
|
|
175
178
|
expect(tableSyntaxErrors[0].error.level).toBe('critical');
|
|
176
179
|
expect(tableSyntaxErrors[0].error.message).toContain('tag callout');
|
|
180
|
+
// Error should point to the callout tag, not the if tag or table
|
|
181
|
+
expect(tableSyntaxErrors[0].location?.start.line).toBe(5);
|
|
182
|
+
expect(tableSyntaxErrors[0].location?.end.line).toBe(6);
|
|
183
|
+
expect(tableSyntaxErrors[0].lines).toEqual([5, 6]);
|
|
177
184
|
});
|
|
178
185
|
|
|
179
186
|
it('produces an error for non-conditional tags at the row level of a table', function () {
|
|
@@ -198,5 +205,9 @@ This is not a valid row
|
|
|
198
205
|
expect(tableSyntaxErrors.length).toBe(1);
|
|
199
206
|
expect(tableSyntaxErrors[0].error.level).toBe('critical');
|
|
200
207
|
expect(tableSyntaxErrors[0].error.message).toContain('tag');
|
|
208
|
+
// Error should point to the callout tag, not the table
|
|
209
|
+
expect(tableSyntaxErrors[0].location?.start.line).toBe(4);
|
|
210
|
+
expect(tableSyntaxErrors[0].location?.end.line).toBe(5);
|
|
211
|
+
expect(tableSyntaxErrors[0].lines).toEqual([4, 5]);
|
|
201
212
|
});
|
|
202
213
|
});
|
package/src/transforms/table.ts
CHANGED
|
@@ -23,19 +23,14 @@ function isComment(node: Node) {
|
|
|
23
23
|
);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
function unexpectedNodeError({
|
|
27
|
-
type,
|
|
28
|
-
tag,
|
|
29
|
-
}: {
|
|
30
|
-
type: NodeType;
|
|
31
|
-
tag?: string;
|
|
32
|
-
}): ValidationError {
|
|
26
|
+
function unexpectedNodeError(node: Node): ValidationError {
|
|
33
27
|
return {
|
|
34
28
|
id: 'table-syntax',
|
|
35
29
|
level: 'critical',
|
|
36
|
-
message: `Found ${type}${
|
|
37
|
-
tag ? ` ${tag}` : ''
|
|
30
|
+
message: `Found ${node.type}${
|
|
31
|
+
node.tag ? ` ${node.tag}` : ''
|
|
38
32
|
} where a list was expected. Make sure all content inside table cells is indented.`,
|
|
33
|
+
location: node.location,
|
|
39
34
|
};
|
|
40
35
|
}
|
|
41
36
|
|
|
@@ -76,9 +71,7 @@ export default function transform(
|
|
|
76
71
|
) {
|
|
77
72
|
// Allow structural tags: else, nested conditionals, and comments
|
|
78
73
|
} else {
|
|
79
|
-
row.errors.push(
|
|
80
|
-
unexpectedNodeError({ type: child.type, tag: child.tag })
|
|
81
|
-
);
|
|
74
|
+
row.errors.push(unexpectedNodeError(child));
|
|
82
75
|
continue;
|
|
83
76
|
}
|
|
84
77
|
children.push(child);
|
|
@@ -86,7 +79,7 @@ export default function transform(
|
|
|
86
79
|
|
|
87
80
|
row.children = children;
|
|
88
81
|
} else if (row.type !== 'hr' && !isComment(row)) {
|
|
89
|
-
node.errors.push(unexpectedNodeError(
|
|
82
|
+
node.errors.push(unexpectedNodeError(row));
|
|
90
83
|
continue;
|
|
91
84
|
} else continue;
|
|
92
85
|
tbody.push(row);
|
package/src/utils.test.ts
CHANGED
package/src/utils.ts
CHANGED
|
@@ -51,6 +51,36 @@ export function findTagEnd(content: string, start = 0) {
|
|
|
51
51
|
return null;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
export function containsMarkdocTagInUrl(
|
|
55
|
+
content: string,
|
|
56
|
+
allowedProtocols: string[]
|
|
57
|
+
) {
|
|
58
|
+
for (let pos = 0; pos < content.length; pos++) {
|
|
59
|
+
if (!content.startsWith(OPEN, pos)) continue;
|
|
60
|
+
|
|
61
|
+
const end = findTagEnd(content, pos);
|
|
62
|
+
if (end === null) {
|
|
63
|
+
// If we cannot find the closing tag, we skip over it
|
|
64
|
+
pos += OPEN.length - 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Locate the start of the content from the tag start (stop tracing the starting position when it hits the first whitespace)
|
|
69
|
+
let start = pos;
|
|
70
|
+
while (start > 0 && !/\s/.test(content[start - 1])) start--;
|
|
71
|
+
|
|
72
|
+
// Check if the content contains the URL.
|
|
73
|
+
for (const protocol of allowedProtocols) {
|
|
74
|
+
if (content.slice(start, pos).includes(`${protocol}://`)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
54
84
|
function parseTag(content: string, line: number, contentStart: number) {
|
|
55
85
|
try {
|
|
56
86
|
return parse(content, { Variable, Function });
|
package/src/validator.ts
CHANGED
|
@@ -3,10 +3,12 @@ import Ast from './ast/index';
|
|
|
3
3
|
import { isPromise } from './utils';
|
|
4
4
|
|
|
5
5
|
import type {
|
|
6
|
+
Location,
|
|
6
7
|
Node,
|
|
7
8
|
Function,
|
|
8
9
|
Config,
|
|
9
10
|
SchemaAttribute,
|
|
11
|
+
ValidateError,
|
|
10
12
|
ValidationError,
|
|
11
13
|
Value,
|
|
12
14
|
} from './types';
|
|
@@ -303,9 +305,35 @@ export function* walkWithParents(
|
|
|
303
305
|
yield* walkWithParents(child, [...parents, node]);
|
|
304
306
|
}
|
|
305
307
|
|
|
308
|
+
function hasValidLocation(
|
|
309
|
+
error: ValidationError
|
|
310
|
+
): error is ValidationError & { location: Location } {
|
|
311
|
+
const loc = error.location;
|
|
312
|
+
return (
|
|
313
|
+
loc != null &&
|
|
314
|
+
typeof loc.start?.line === 'number' &&
|
|
315
|
+
typeof loc.end?.line === 'number' &&
|
|
316
|
+
(loc.file === undefined || typeof loc.file === 'string')
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function toValidateError(
|
|
321
|
+
node: { type: string; lines: number[]; location?: Location },
|
|
322
|
+
error: ValidationError
|
|
323
|
+
): ValidateError {
|
|
324
|
+
if (hasValidLocation(error)) {
|
|
325
|
+
return {
|
|
326
|
+
type: node.type,
|
|
327
|
+
lines: [error.location.start.line, error.location.end.line],
|
|
328
|
+
location: { file: node.location?.file, ...error.location },
|
|
329
|
+
error,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
return { type: node.type, lines: node.lines, location: node.location, error };
|
|
333
|
+
}
|
|
334
|
+
|
|
306
335
|
export function validateTree(content: Node, config: Config) {
|
|
307
336
|
const output = [...walkWithParents(content)].map(([node, parents]) => {
|
|
308
|
-
const { type, lines, location } = node;
|
|
309
337
|
const updatedConfig = {
|
|
310
338
|
...config,
|
|
311
339
|
validation: { ...config.validation, parents },
|
|
@@ -313,12 +341,10 @@ export function validateTree(content: Node, config: Config) {
|
|
|
313
341
|
const errors = validator(node, updatedConfig);
|
|
314
342
|
|
|
315
343
|
if (isPromise(errors)) {
|
|
316
|
-
return errors.then((e) =>
|
|
317
|
-
e.map((error) => ({ type, lines, location, error }))
|
|
318
|
-
);
|
|
344
|
+
return errors.then((e) => e.map((error) => toValidateError(node, error)));
|
|
319
345
|
}
|
|
320
346
|
|
|
321
|
-
return errors.map((error) => (
|
|
347
|
+
return errors.map((error) => toValidateError(node, error));
|
|
322
348
|
});
|
|
323
349
|
|
|
324
350
|
if (output.some(isPromise)) {
|