@markdoc/markdoc 0.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/LICENSE +21 -0
- package/README.md +75 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7961 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +7959 -0
- package/dist/index.mjs.map +7 -0
- package/dist/react.js +40 -0
- package/dist/react.js.map +7 -0
- package/dist/src/ast/base.d.ts +6 -0
- package/dist/src/ast/base.d.ts.map +1 -0
- package/dist/src/ast/function.d.ts +9 -0
- package/dist/src/ast/function.d.ts.map +1 -0
- package/dist/src/ast/index.d.ts +26 -0
- package/dist/src/ast/index.d.ts.map +1 -0
- package/dist/src/ast/node.d.ts +21 -0
- package/dist/src/ast/node.d.ts.map +1 -0
- package/dist/src/ast/tag.d.ts +9 -0
- package/dist/src/ast/tag.d.ts.map +1 -0
- package/dist/src/ast/variable.d.ts +8 -0
- package/dist/src/ast/variable.d.ts.map +1 -0
- package/dist/src/functions/index.d.ts +11 -0
- package/dist/src/functions/index.d.ts.map +1 -0
- package/dist/src/parser.d.ts +4 -0
- package/dist/src/parser.d.ts.map +1 -0
- package/dist/src/renderers/html.d.ts +3 -0
- package/dist/src/renderers/html.d.ts.map +1 -0
- package/dist/src/renderers/index.d.ts +10 -0
- package/dist/src/renderers/index.d.ts.map +1 -0
- package/dist/src/renderers/react/react.d.ts +11 -0
- package/dist/src/renderers/react/react.d.ts.map +1 -0
- package/dist/src/renderers/react/shared.d.ts +5 -0
- package/dist/src/renderers/react/shared.d.ts.map +1 -0
- package/dist/src/renderers/react/static.d.ts +3 -0
- package/dist/src/renderers/react/static.d.ts.map +1 -0
- package/dist/src/schema-types/class.d.ts +6 -0
- package/dist/src/schema-types/class.d.ts.map +1 -0
- package/dist/src/schema.d.ts +27 -0
- package/dist/src/schema.d.ts.map +1 -0
- package/dist/src/tags/conditional.d.ts +5 -0
- package/dist/src/tags/conditional.d.ts.map +1 -0
- package/dist/src/tags/index.d.ts +44 -0
- package/dist/src/tags/index.d.ts.map +1 -0
- package/dist/src/tags/partial.d.ts +3 -0
- package/dist/src/tags/partial.d.ts.map +1 -0
- package/dist/src/tags/table.d.ts +3 -0
- package/dist/src/tags/table.d.ts.map +1 -0
- package/dist/src/tokenizer/index.d.ts +10 -0
- package/dist/src/tokenizer/index.d.ts.map +1 -0
- package/dist/src/tokenizer/plugins/annotations.d.ts +3 -0
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -0
- package/dist/src/tokenizer/plugins/frontmatter.d.ts +3 -0
- package/dist/src/tokenizer/plugins/frontmatter.d.ts.map +1 -0
- package/dist/src/transformer.d.ts +12 -0
- package/dist/src/transformer.d.ts.map +1 -0
- package/dist/src/transforms/index.d.ts +4 -0
- package/dist/src/transforms/index.d.ts.map +1 -0
- package/dist/src/transforms/table.d.ts +3 -0
- package/dist/src/transforms/table.d.ts.map +1 -0
- package/dist/src/types.d.ts +88 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/utils.d.ts +6 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/validator.d.ts +6 -0
- package/dist/src/validator.d.ts.map +1 -0
- package/global.d.ts +11 -0
- package/index.ts +138 -0
- package/package.json +63 -0
- package/spec/support/matchers.d.ts +6 -0
- package/src/ast/base.ts +41 -0
- package/src/ast/function.ts +22 -0
- package/src/ast/index.ts +28 -0
- package/src/ast/node.test.ts +307 -0
- package/src/ast/node.ts +71 -0
- package/src/ast/tag.ts +19 -0
- package/src/ast/variable.test.ts +35 -0
- package/src/ast/variable.ts +17 -0
- package/src/functions/index.test.ts +34 -0
- package/src/functions/index.ts +45 -0
- package/src/grammar/tag.d.ts +23 -0
- package/src/grammar/tag.js +1836 -0
- package/src/grammar/tag.pegjs +170 -0
- package/src/grammar/tag.test.ts +364 -0
- package/src/parser.test.ts +597 -0
- package/src/parser.ts +158 -0
- package/src/renderers/html.test.ts +62 -0
- package/src/renderers/html.ts +46 -0
- package/src/renderers/index.ts +5 -0
- package/src/renderers/react/react.test.ts +204 -0
- package/src/renderers/react/react.ts +51 -0
- package/src/renderers/react/shared.ts +16 -0
- package/src/renderers/react/static.ts +52 -0
- package/src/schema-types/class.ts +23 -0
- package/src/schema.ts +233 -0
- package/src/tags/conditional.ts +45 -0
- package/src/tags/index.ts +10 -0
- package/src/tags/partial.ts +33 -0
- package/src/tags/table.ts +5 -0
- package/src/tokenizer/index.ts +21 -0
- package/src/tokenizer/plugins/annotations.test.ts +337 -0
- package/src/tokenizer/plugins/annotations.ts +133 -0
- package/src/tokenizer/plugins/frontmatter.test.ts +86 -0
- package/src/tokenizer/plugins/frontmatter.ts +39 -0
- package/src/transformer.ts +72 -0
- package/src/transforms/index.ts +2 -0
- package/src/transforms/table.ts +51 -0
- package/src/types.ts +142 -0
- package/src/utils.test.ts +173 -0
- package/src/utils.ts +116 -0
- package/src/validator.test.ts +298 -0
- package/src/validator.ts +233 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { findTagEnd, parseTags } from '../../utils';
|
|
2
|
+
import { parse, SyntaxError } from '../../grammar/tag';
|
|
3
|
+
import Variable from '../../ast/variable';
|
|
4
|
+
import Function from '../../ast/function';
|
|
5
|
+
|
|
6
|
+
import type { AttributeValue } from '../../types';
|
|
7
|
+
import type MarkdownIt from 'markdown-it/lib';
|
|
8
|
+
import type StateCore from 'markdown-it/lib/rules_core/state_core';
|
|
9
|
+
import type StateInline from 'markdown-it/lib/rules_inline/state_inline';
|
|
10
|
+
import type StateBlock from 'markdown-it/lib/rules_block/state_block';
|
|
11
|
+
import type Token from 'markdown-it/lib/token';
|
|
12
|
+
|
|
13
|
+
import { OPEN, CLOSE } from '../../utils';
|
|
14
|
+
|
|
15
|
+
function createToken(
|
|
16
|
+
state: StateBlock | StateInline,
|
|
17
|
+
content: string,
|
|
18
|
+
contentStart?: number
|
|
19
|
+
): Token {
|
|
20
|
+
try {
|
|
21
|
+
const { type, meta, nesting = 0 } = parse(content, { Variable, Function });
|
|
22
|
+
const token = state.push(type, '', nesting);
|
|
23
|
+
token.info = content;
|
|
24
|
+
token.meta = meta;
|
|
25
|
+
|
|
26
|
+
return token;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
29
|
+
|
|
30
|
+
const {
|
|
31
|
+
message,
|
|
32
|
+
location: { start, end },
|
|
33
|
+
} = error as SyntaxError;
|
|
34
|
+
const location = contentStart
|
|
35
|
+
? {
|
|
36
|
+
start: { offset: start.offset + contentStart },
|
|
37
|
+
end: { offset: end.offset + contentStart },
|
|
38
|
+
}
|
|
39
|
+
: null;
|
|
40
|
+
|
|
41
|
+
const token = state.push('error', '', 0);
|
|
42
|
+
token.meta = { error: { message, location } };
|
|
43
|
+
return token;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function block(
|
|
48
|
+
state: StateBlock,
|
|
49
|
+
startLine: number,
|
|
50
|
+
endLine: number,
|
|
51
|
+
silent: boolean
|
|
52
|
+
): boolean {
|
|
53
|
+
const start = state.bMarks[startLine] + state.tShift[startLine];
|
|
54
|
+
const finish = state.eMarks[startLine];
|
|
55
|
+
|
|
56
|
+
if (!state.src.startsWith(OPEN, start)) return false;
|
|
57
|
+
|
|
58
|
+
const tagEnd = findTagEnd(state.src, start);
|
|
59
|
+
const lastPossible = state.src.slice(0, finish).trim().length;
|
|
60
|
+
|
|
61
|
+
if (!tagEnd || tagEnd < lastPossible - CLOSE.length) return false;
|
|
62
|
+
|
|
63
|
+
const contentStart = start + OPEN.length;
|
|
64
|
+
const content = state.src.slice(contentStart, tagEnd).trim();
|
|
65
|
+
const lines = content.split('\n').length;
|
|
66
|
+
|
|
67
|
+
if (content[0] === '$') return false;
|
|
68
|
+
|
|
69
|
+
if (silent) return true;
|
|
70
|
+
|
|
71
|
+
const token = createToken(state, content, contentStart);
|
|
72
|
+
token.map = [startLine, startLine + lines];
|
|
73
|
+
state.line += lines;
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function inline(state: StateInline, silent: boolean): boolean {
|
|
78
|
+
if (!state.src.startsWith(OPEN, state.pos)) return false;
|
|
79
|
+
|
|
80
|
+
const tagEnd = findTagEnd(state.src, state.pos);
|
|
81
|
+
if (!tagEnd) return false;
|
|
82
|
+
|
|
83
|
+
const content = state.src.slice(state.pos + OPEN.length, tagEnd);
|
|
84
|
+
if (!silent) createToken(state, content.trim());
|
|
85
|
+
|
|
86
|
+
state.pos = tagEnd + CLOSE.length;
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function core(state: StateCore) {
|
|
91
|
+
let token: Token;
|
|
92
|
+
for (token of state.tokens) {
|
|
93
|
+
if (token.type !== 'fence') continue;
|
|
94
|
+
|
|
95
|
+
if (token.info.includes(OPEN)) {
|
|
96
|
+
const start = token.info.indexOf(OPEN);
|
|
97
|
+
const end = findTagEnd(token.info, start);
|
|
98
|
+
const content = token.info.slice(start + OPEN.length, end);
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
const { meta } = parse(content.trim(), { Variable, Function });
|
|
102
|
+
token.meta = meta;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
105
|
+
if (!token.errors) token.errors = [];
|
|
106
|
+
token.errors.push({
|
|
107
|
+
id: 'fence-tag-error',
|
|
108
|
+
level: 'error',
|
|
109
|
+
message: `Syntax error in fence tag: ${
|
|
110
|
+
(error as SyntaxError).message
|
|
111
|
+
}`,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (
|
|
117
|
+
token?.meta?.attributes?.find(
|
|
118
|
+
(attr: AttributeValue) => attr.name === 'process' && !attr.value
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
continue;
|
|
122
|
+
|
|
123
|
+
token.children = parseTags(token.content, token.map[0]);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export default function plugin(md: MarkdownIt /* options */) {
|
|
128
|
+
md.block.ruler.before('paragraph', 'annotations', block, {
|
|
129
|
+
alt: ['paragraph', 'blockquote'],
|
|
130
|
+
});
|
|
131
|
+
md.inline.ruler.push('containers', inline);
|
|
132
|
+
md.core.ruler.push('annotations', core);
|
|
133
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import Tokenizer from '..';
|
|
2
|
+
|
|
3
|
+
describe('MarkdownIt frontmatter plugin', function () {
|
|
4
|
+
const tokenizer = new Tokenizer();
|
|
5
|
+
|
|
6
|
+
function parse(example) {
|
|
7
|
+
const content = example.replace(/\n\s+/gm, '\n').trim();
|
|
8
|
+
return tokenizer.tokenize(content);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
it('parsing frontmatter', function () {
|
|
12
|
+
const example = parse(`
|
|
13
|
+
---
|
|
14
|
+
test: true
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
# Test
|
|
18
|
+
`);
|
|
19
|
+
|
|
20
|
+
expect(example.length).toEqual(4);
|
|
21
|
+
expect(example[0]).toDeepEqualSubset({
|
|
22
|
+
type: 'frontmatter',
|
|
23
|
+
content: 'test: true',
|
|
24
|
+
level: 0,
|
|
25
|
+
map: [0, 2],
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('parsing frontmatter only', function () {
|
|
30
|
+
const example = parse(`
|
|
31
|
+
---
|
|
32
|
+
test: true
|
|
33
|
+
foo: bar
|
|
34
|
+
---
|
|
35
|
+
`);
|
|
36
|
+
|
|
37
|
+
expect(example.length).toEqual(1);
|
|
38
|
+
expect(example[0]).toDeepEqualSubset({
|
|
39
|
+
type: 'frontmatter',
|
|
40
|
+
level: 0,
|
|
41
|
+
map: [0, 3],
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('spaces at end', function () {
|
|
46
|
+
const example = parse(`
|
|
47
|
+
---
|
|
48
|
+
test: true
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
# Test
|
|
52
|
+
`);
|
|
53
|
+
|
|
54
|
+
expect(example.length).toEqual(4);
|
|
55
|
+
expect(example[0]).toDeepEqualSubset({
|
|
56
|
+
type: 'frontmatter',
|
|
57
|
+
content: 'test: true',
|
|
58
|
+
level: 0,
|
|
59
|
+
map: [0, 2],
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('missing closing', function () {
|
|
64
|
+
const example = parse(`
|
|
65
|
+
---
|
|
66
|
+
test: true
|
|
67
|
+
|
|
68
|
+
# Test
|
|
69
|
+
`);
|
|
70
|
+
|
|
71
|
+
expect(example.length).toEqual(7);
|
|
72
|
+
expect(example[0].type).toEqual('hr');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('missing opening', function () {
|
|
76
|
+
const example = parse(`
|
|
77
|
+
test: true
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
# Test
|
|
81
|
+
`);
|
|
82
|
+
|
|
83
|
+
expect(example.length).toEqual(7);
|
|
84
|
+
expect(example[0].type).toEqual('paragraph_open');
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type MarkdownIt from 'markdown-it/lib';
|
|
2
|
+
import type StateBlock from 'markdown-it/lib/rules_block/state_block';
|
|
3
|
+
|
|
4
|
+
const fence = '---';
|
|
5
|
+
|
|
6
|
+
function getLine(state: StateBlock, n: number) {
|
|
7
|
+
return state.src.slice(state.bMarks[n], state.eMarks[n]).trim();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function findClose(state: StateBlock, endLine: number) {
|
|
11
|
+
for (let line = 1; line < endLine; line++)
|
|
12
|
+
if (getLine(state, line) === fence) return line;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function block(
|
|
16
|
+
state: StateBlock,
|
|
17
|
+
startLine: number,
|
|
18
|
+
endLine: number,
|
|
19
|
+
silent: boolean
|
|
20
|
+
): boolean {
|
|
21
|
+
if (startLine != 0 || getLine(state, 0) != fence) return false;
|
|
22
|
+
|
|
23
|
+
const close = findClose(state, endLine);
|
|
24
|
+
|
|
25
|
+
if (!close) return false;
|
|
26
|
+
if (silent) return true;
|
|
27
|
+
|
|
28
|
+
const token = state.push('frontmatter', '', 0);
|
|
29
|
+
token.content = state.src.slice(state.eMarks[0], state.bMarks[close]).trim();
|
|
30
|
+
token.map = [0, close];
|
|
31
|
+
token.hidden = true;
|
|
32
|
+
|
|
33
|
+
state.line = close + 1;
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export default function plugin(md: MarkdownIt /* options */) {
|
|
38
|
+
md.block.ruler.before('hr', 'frontmatter', block);
|
|
39
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import Tag from './ast/tag';
|
|
2
|
+
import { Class } from './schema-types/class';
|
|
3
|
+
import type {
|
|
4
|
+
Config,
|
|
5
|
+
Node,
|
|
6
|
+
NodeType,
|
|
7
|
+
Schema,
|
|
8
|
+
RenderableTreeNode,
|
|
9
|
+
RenderableTreeNodes,
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
type AttributesSchema = Schema['attributes'];
|
|
13
|
+
|
|
14
|
+
export const globalAttributes: AttributesSchema = {
|
|
15
|
+
class: { type: Class, render: true },
|
|
16
|
+
id: { type: String, render: true },
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export interface Transformer {
|
|
20
|
+
findSchema(node: Node, config: Config): Schema | undefined;
|
|
21
|
+
node(node: Node, config: Config): RenderableTreeNodes;
|
|
22
|
+
attributes(node: Node, config: Config): Record<string, any>;
|
|
23
|
+
children(node: Node, config: Config): RenderableTreeNode[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default {
|
|
27
|
+
findSchema(node: Node, { nodes = {}, tags = {} }: Config = {}) {
|
|
28
|
+
return node.tag ? tags[node.tag] : nodes[node.type as NodeType];
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
attributes(node: Node, config: Config = {}) {
|
|
32
|
+
const schema = this.findSchema(node, config) ?? {};
|
|
33
|
+
const output: Record<string, any> = {};
|
|
34
|
+
|
|
35
|
+
const attrs = { ...globalAttributes, ...schema.attributes };
|
|
36
|
+
for (const [key, attr] of Object.entries(attrs)) {
|
|
37
|
+
if (attr.render == false) continue;
|
|
38
|
+
|
|
39
|
+
const name = typeof attr.render === 'string' ? attr.render : key;
|
|
40
|
+
|
|
41
|
+
let value = node.attributes[key];
|
|
42
|
+
if (typeof attr.type === 'function') {
|
|
43
|
+
const instance: any = new attr.type();
|
|
44
|
+
if (instance.transform) {
|
|
45
|
+
value = instance.transform(value, config);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
value = value === undefined ? attr.default : value;
|
|
49
|
+
|
|
50
|
+
if (value === undefined) continue;
|
|
51
|
+
output[name] = value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return output;
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
children(node: Node, config: Config = {}) {
|
|
58
|
+
return node.children.flatMap((child) => this.node(child, config));
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
node(node: Node, config: Config = {}) {
|
|
62
|
+
const schema = this.findSchema(node, config) ?? {};
|
|
63
|
+
if (schema && schema.transform instanceof Function)
|
|
64
|
+
return schema.transform(node, config);
|
|
65
|
+
|
|
66
|
+
const children = this.children(node, config);
|
|
67
|
+
if (!schema || !schema.render) return children;
|
|
68
|
+
|
|
69
|
+
const attributes = this.attributes(node, config);
|
|
70
|
+
return new Tag(schema.render, attributes, children);
|
|
71
|
+
},
|
|
72
|
+
} as Transformer;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Ast from '../ast/index';
|
|
2
|
+
|
|
3
|
+
import type { Node } from '../types';
|
|
4
|
+
|
|
5
|
+
function convertToRow(node: Node, cellType = 'td') {
|
|
6
|
+
node.type = 'tr';
|
|
7
|
+
node.attributes = {};
|
|
8
|
+
|
|
9
|
+
for (const cell of node.children) cell.type = cellType;
|
|
10
|
+
|
|
11
|
+
return node;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default function transform(document: Node) {
|
|
15
|
+
for (const node of document.walk()) {
|
|
16
|
+
if (node.type !== 'tag' || node.tag !== 'table') continue;
|
|
17
|
+
|
|
18
|
+
const [first, ...rest] = node.children;
|
|
19
|
+
if (!first || first.type === 'table') continue;
|
|
20
|
+
|
|
21
|
+
const table = new Ast.Node('table', node.attributes, [
|
|
22
|
+
new Ast.Node('thead'),
|
|
23
|
+
new Ast.Node('tbody'),
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
const [thead, tbody] = table.children;
|
|
27
|
+
|
|
28
|
+
if (first.type === 'list') thead.push(convertToRow(first, 'th'));
|
|
29
|
+
|
|
30
|
+
for (const row of rest) {
|
|
31
|
+
// Convert lists to rows with special-case support for conditionals
|
|
32
|
+
// When a conditional is encountered, convert all of its top-level lists to rows
|
|
33
|
+
if (row.type === 'list') convertToRow(row);
|
|
34
|
+
else if (row.type === 'tag' && row.tag == 'if') {
|
|
35
|
+
const children = [];
|
|
36
|
+
|
|
37
|
+
for (const child of row.children) {
|
|
38
|
+
// Replace children and skip HRs in order to support conditionals with multiple rows
|
|
39
|
+
if (child.type === 'hr') continue;
|
|
40
|
+
if (child.type === 'list') convertToRow(child);
|
|
41
|
+
children.push(child);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
row.children = children;
|
|
45
|
+
} else continue;
|
|
46
|
+
tbody.push(row);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
node.children = [table];
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type Node from './ast/node';
|
|
2
|
+
import type Function from './ast/function';
|
|
3
|
+
import type Tag from './ast/tag';
|
|
4
|
+
|
|
5
|
+
export type { default as Function } from './ast/function';
|
|
6
|
+
export type { default as Node } from './ast/node';
|
|
7
|
+
export type { default as Tag } from './ast/tag';
|
|
8
|
+
|
|
9
|
+
export interface AstType {
|
|
10
|
+
readonly $$mdtype: 'Function' | 'Node' | 'Variable';
|
|
11
|
+
resolve(config: Config): any;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type AttributeValue = {
|
|
15
|
+
type: string;
|
|
16
|
+
name: string;
|
|
17
|
+
value: any;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type Config = Readonly<ConfigType>;
|
|
21
|
+
|
|
22
|
+
export type ConfigType = Partial<{
|
|
23
|
+
nodes: Partial<Record<NodeType, Schema>>;
|
|
24
|
+
tags: Record<string, Schema>;
|
|
25
|
+
variables: Record<string, any>;
|
|
26
|
+
functions: Record<string, ConfigFunction>;
|
|
27
|
+
partials: Record<string, any>;
|
|
28
|
+
validation?: {
|
|
29
|
+
validateFunctions?: boolean;
|
|
30
|
+
};
|
|
31
|
+
}>;
|
|
32
|
+
|
|
33
|
+
export type ConfigFunction = {
|
|
34
|
+
returns?: ValidationType | ValidationType[];
|
|
35
|
+
parameters?: Record<string, SchemaAttribute>;
|
|
36
|
+
transform?(parameters: Record<string, any>, config: Config): any;
|
|
37
|
+
validate?(fn: Function, config: Config): ValidationError[];
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export interface CustomAttributeTypeInterface {
|
|
41
|
+
transform?(value: any, config: Config): Scalar;
|
|
42
|
+
validate?(value: any, config: Config): ValidationError[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface CustomAttributeType {
|
|
46
|
+
new (): CustomAttributeTypeInterface;
|
|
47
|
+
readonly prototype: CustomAttributeTypeInterface;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type Location = {
|
|
51
|
+
file?: string;
|
|
52
|
+
start: LocationEdge;
|
|
53
|
+
end: LocationEdge;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type LocationEdge = {
|
|
57
|
+
line: number;
|
|
58
|
+
character?: number;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type NodeType =
|
|
62
|
+
| 'document'
|
|
63
|
+
| 'heading'
|
|
64
|
+
| 'paragraph'
|
|
65
|
+
| 'blockquote'
|
|
66
|
+
| 'hr'
|
|
67
|
+
| 'image'
|
|
68
|
+
| 'fence'
|
|
69
|
+
| 'tag'
|
|
70
|
+
| 'list'
|
|
71
|
+
| 'item'
|
|
72
|
+
| 'table'
|
|
73
|
+
| 'thead'
|
|
74
|
+
| 'tbody'
|
|
75
|
+
| 'tr'
|
|
76
|
+
| 'td'
|
|
77
|
+
| 'th'
|
|
78
|
+
| 'inline'
|
|
79
|
+
| 'strong'
|
|
80
|
+
| 'em'
|
|
81
|
+
| 's'
|
|
82
|
+
| 'link'
|
|
83
|
+
| 'code'
|
|
84
|
+
| 'text'
|
|
85
|
+
| 'hardbreak'
|
|
86
|
+
| 'softbreak';
|
|
87
|
+
|
|
88
|
+
export type Primitive = null | boolean | number | string;
|
|
89
|
+
|
|
90
|
+
export type RenderableTreeNode = Tag | string | null;
|
|
91
|
+
export type RenderableTreeNodes = RenderableTreeNode | RenderableTreeNode[];
|
|
92
|
+
|
|
93
|
+
export type Scalar = Primitive | Scalar[] | { [key: string]: Scalar };
|
|
94
|
+
|
|
95
|
+
export type Schema<C extends Config = Config, R = string> = {
|
|
96
|
+
render?: R;
|
|
97
|
+
children?: string[];
|
|
98
|
+
attributes?: Record<string, SchemaAttribute>;
|
|
99
|
+
selfClosing?: boolean;
|
|
100
|
+
transform?(node: Node, config: C): RenderableTreeNodes;
|
|
101
|
+
validate?(node: Node, config: C): ValidationError[];
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export type SchemaAttribute = {
|
|
105
|
+
type?: ValidationType | ValidationType[];
|
|
106
|
+
render?: boolean | string;
|
|
107
|
+
default?: any;
|
|
108
|
+
required?: boolean;
|
|
109
|
+
matches?: SchemaMatches | ((config: Config) => SchemaMatches);
|
|
110
|
+
errorLevel?: ValidationError['level'];
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export type SchemaMatches = RegExp | string[] | null;
|
|
114
|
+
|
|
115
|
+
export type ValidationError = {
|
|
116
|
+
id: string;
|
|
117
|
+
level: 'debug' | 'info' | 'warning' | 'error' | 'critical';
|
|
118
|
+
message: string;
|
|
119
|
+
location?: Location;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export type ValidateError = {
|
|
123
|
+
type: string;
|
|
124
|
+
lines: number[];
|
|
125
|
+
location?: Location;
|
|
126
|
+
error: ValidationError;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export type ValidationType =
|
|
130
|
+
| CustomAttributeType
|
|
131
|
+
| typeof String
|
|
132
|
+
| typeof Number
|
|
133
|
+
| typeof Boolean
|
|
134
|
+
| typeof Object
|
|
135
|
+
| typeof Array
|
|
136
|
+
| 'String'
|
|
137
|
+
| 'Number'
|
|
138
|
+
| 'Boolean'
|
|
139
|
+
| 'Object'
|
|
140
|
+
| 'Array';
|
|
141
|
+
|
|
142
|
+
export type Value = AstType | Scalar;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { findTagEnd, parseTags } from '../src/utils';
|
|
2
|
+
|
|
3
|
+
describe('Templating', function () {
|
|
4
|
+
describe('parseTags', function () {
|
|
5
|
+
it('simple example', function () {
|
|
6
|
+
const example =
|
|
7
|
+
'this is a {% foo blah="asdf" %}test{% /foo %} of template parsing';
|
|
8
|
+
const output = parseTags(example);
|
|
9
|
+
const expected = [
|
|
10
|
+
{ type: 'text', content: 'this is a ', start: 0, end: 9 },
|
|
11
|
+
{
|
|
12
|
+
type: 'tag_open',
|
|
13
|
+
info: '{% foo blah="asdf" %}',
|
|
14
|
+
start: 10,
|
|
15
|
+
end: 30,
|
|
16
|
+
nesting: 1,
|
|
17
|
+
meta: {
|
|
18
|
+
tag: 'foo',
|
|
19
|
+
attributes: [{ name: 'blah', type: 'attribute', value: 'asdf' }],
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
{ type: 'text', content: 'test', start: 31, end: 34 },
|
|
23
|
+
{
|
|
24
|
+
type: 'tag_close',
|
|
25
|
+
nesting: -1,
|
|
26
|
+
meta: { tag: 'foo' },
|
|
27
|
+
info: '{% /foo %}',
|
|
28
|
+
start: 35,
|
|
29
|
+
end: 44,
|
|
30
|
+
},
|
|
31
|
+
{ type: 'text', content: ' of template parsing', start: 45, end: 64 },
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
expect(output).toDeepEqualSubset(expected);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('findTagEnd', function () {
|
|
39
|
+
describe('inline tags', function () {
|
|
40
|
+
it('in a heading', function () {
|
|
41
|
+
const example = `# Testing {% #foo.bar baz=1 %}`;
|
|
42
|
+
const end = findTagEnd(example, 0);
|
|
43
|
+
expect(end).toEqual(28);
|
|
44
|
+
expect(example[end]).toEqual('%');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('with string', function () {
|
|
48
|
+
const example = `# Testing {% #foo.bar baz="example" test=true %}`;
|
|
49
|
+
const end = findTagEnd(example, 0);
|
|
50
|
+
expect(end).toEqual(46);
|
|
51
|
+
expect(example[end]).toEqual('%');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('with object literal attribute value', function () {
|
|
55
|
+
const example = `# Testing {% #foo.bar baz={test: 1, foo: {test: "asdf{"}} %}`;
|
|
56
|
+
const end = findTagEnd(example, 0);
|
|
57
|
+
expect(end).toEqual(58);
|
|
58
|
+
expect(example[end]).toEqual('%');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('in a simple container', function () {
|
|
62
|
+
const end = findTagEnd('{% foo %}');
|
|
63
|
+
expect(end).toEqual(7);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('in a container with shortcuts', function () {
|
|
67
|
+
const end = findTagEnd('{% foo .bar.baz#test %}');
|
|
68
|
+
expect(end).toEqual(21);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('in a container with a string attribute', function () {
|
|
72
|
+
const end = findTagEnd('{% foo test="this is a test" %}');
|
|
73
|
+
expect(end).toEqual(29);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('for an invalid container', function () {
|
|
77
|
+
const end = findTagEnd('{% foo .bar#baz');
|
|
78
|
+
expect(end).toBeUndefined;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('in a complex container', function () {
|
|
82
|
+
const end = findTagEnd(
|
|
83
|
+
'{% #foo .bar .baz test="this} is \\"{test}\\" a test" %} this is a test'
|
|
84
|
+
);
|
|
85
|
+
expect(end).toEqual(52);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('multiline tags', function () {
|
|
90
|
+
it('simple', function () {
|
|
91
|
+
const example = `
|
|
92
|
+
{% test #foo.bar
|
|
93
|
+
baz=1 %}
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
const end = findTagEnd(example, 0);
|
|
97
|
+
expect(end).toEqual(46);
|
|
98
|
+
expect(example[end]).toEqual('%');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('with string', function () {
|
|
102
|
+
const example = `
|
|
103
|
+
{% test #foo.bar
|
|
104
|
+
baz="this is a test"
|
|
105
|
+
example=1 %}
|
|
106
|
+
`;
|
|
107
|
+
|
|
108
|
+
const end = findTagEnd(example, 0);
|
|
109
|
+
expect(end).toEqual(85);
|
|
110
|
+
expect(example[end]).toEqual('%');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('with string and escaped quote', function () {
|
|
114
|
+
const example = `
|
|
115
|
+
{% test #foo.bar
|
|
116
|
+
baz="this \\"is a test"
|
|
117
|
+
example=1 %}
|
|
118
|
+
`;
|
|
119
|
+
|
|
120
|
+
const end = findTagEnd(example, 0);
|
|
121
|
+
expect(end).toEqual(87);
|
|
122
|
+
expect(example[end]).toEqual('%');
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('with string that has an opening brace', function () {
|
|
126
|
+
const example = `
|
|
127
|
+
{% test #foo.bar
|
|
128
|
+
baz="this {is a test"
|
|
129
|
+
example=1 %}
|
|
130
|
+
`;
|
|
131
|
+
|
|
132
|
+
const end = findTagEnd(example, 0);
|
|
133
|
+
expect(end).toEqual(86);
|
|
134
|
+
expect(example[end]).toEqual('%');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('with string that has escapes and braces', function () {
|
|
138
|
+
const example = `
|
|
139
|
+
{% test #foo.bar
|
|
140
|
+
baz="th\\"is {is a \\\\te\\"st"
|
|
141
|
+
example=1 %}
|
|
142
|
+
`;
|
|
143
|
+
|
|
144
|
+
const end = findTagEnd(example, 0);
|
|
145
|
+
expect(end).toEqual(92);
|
|
146
|
+
expect(example[end]).toEqual('%');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('with an object literal attribute value', function () {
|
|
150
|
+
const example = `
|
|
151
|
+
{% test #foo.bar
|
|
152
|
+
foo={testing: "this } is a test", bar: {baz: 1}}
|
|
153
|
+
example=1 another="test}" %}
|
|
154
|
+
`;
|
|
155
|
+
|
|
156
|
+
const end = findTagEnd(example, 0);
|
|
157
|
+
expect(end).toEqual(129);
|
|
158
|
+
expect(example[end]).toEqual('%');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('with an invalid object literal attribute value', function () {
|
|
162
|
+
const example = `
|
|
163
|
+
{test #foo.bar
|
|
164
|
+
foo={testing: "this } is a test", bar: {baz: 1}
|
|
165
|
+
example=1 another="test}"}
|
|
166
|
+
`;
|
|
167
|
+
|
|
168
|
+
const end = findTagEnd(example, 0, '{', '}');
|
|
169
|
+
expect(end).toEqual(undefined);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
});
|