@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
package/index.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import Tokenizer from './src/tokenizer';
|
|
2
|
+
import parser from './src/parser';
|
|
3
|
+
import Node from './src/ast/node';
|
|
4
|
+
import Tag from './src/ast/tag';
|
|
5
|
+
import Ast from './src/ast/index';
|
|
6
|
+
import * as nodes from './src/schema';
|
|
7
|
+
import tags from './src/tags/index';
|
|
8
|
+
import functions from './src/functions/index';
|
|
9
|
+
import renderers from './src/renderers/index';
|
|
10
|
+
import transformer from './src/transformer';
|
|
11
|
+
import validator from './src/validator';
|
|
12
|
+
import { parseTags } from './src/utils';
|
|
13
|
+
import transforms from './src/transforms/index';
|
|
14
|
+
|
|
15
|
+
import type Token from 'markdown-it/lib/token';
|
|
16
|
+
import type { Config, RenderableTreeNode, ValidateError } from './src/types';
|
|
17
|
+
|
|
18
|
+
export * from './src/types';
|
|
19
|
+
|
|
20
|
+
const tokenizer = new Tokenizer();
|
|
21
|
+
|
|
22
|
+
function mergeConfig(config: Config = {}): Config {
|
|
23
|
+
return {
|
|
24
|
+
...config,
|
|
25
|
+
tags: {
|
|
26
|
+
...tags,
|
|
27
|
+
...config.tags,
|
|
28
|
+
},
|
|
29
|
+
nodes: {
|
|
30
|
+
...nodes,
|
|
31
|
+
...config.nodes,
|
|
32
|
+
},
|
|
33
|
+
functions: {
|
|
34
|
+
...functions,
|
|
35
|
+
...config.functions,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function parse(content: string | Token[], file?: string): Node {
|
|
41
|
+
if (typeof content === 'string') content = tokenizer.tokenize(content);
|
|
42
|
+
return parser(content, file);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function resolve<C extends Config = Config>(
|
|
46
|
+
content: Node,
|
|
47
|
+
config: C
|
|
48
|
+
): Node;
|
|
49
|
+
export function resolve<C extends Config = Config>(
|
|
50
|
+
content: Node[],
|
|
51
|
+
config: C
|
|
52
|
+
): Node[];
|
|
53
|
+
export function resolve<C extends Config = Config>(
|
|
54
|
+
content: any,
|
|
55
|
+
config: C
|
|
56
|
+
): any {
|
|
57
|
+
if (Array.isArray(content))
|
|
58
|
+
return content.flatMap((child) => child.resolve(config));
|
|
59
|
+
|
|
60
|
+
return content.resolve(config);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function transform<C extends Config = Config>(
|
|
64
|
+
node: Node,
|
|
65
|
+
config?: C
|
|
66
|
+
): RenderableTreeNode;
|
|
67
|
+
export function transform<C extends Config = Config>(
|
|
68
|
+
nodes: Node[],
|
|
69
|
+
config?: C
|
|
70
|
+
): RenderableTreeNode[];
|
|
71
|
+
export function transform<C extends Config = Config>(
|
|
72
|
+
nodes: any,
|
|
73
|
+
options?: C
|
|
74
|
+
): any {
|
|
75
|
+
const config = mergeConfig(options);
|
|
76
|
+
const content = resolve(nodes, config);
|
|
77
|
+
|
|
78
|
+
if (Array.isArray(content))
|
|
79
|
+
return content.flatMap((child) => child.transform(config));
|
|
80
|
+
return content.transform(config);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function validate<C extends Config = Config>(
|
|
84
|
+
content: Node,
|
|
85
|
+
options?: C
|
|
86
|
+
): ValidateError[] {
|
|
87
|
+
const config = mergeConfig(options);
|
|
88
|
+
|
|
89
|
+
const output = [];
|
|
90
|
+
for (const node of [content, ...content.walk()]) {
|
|
91
|
+
const { type, lines, location } = node;
|
|
92
|
+
const errors = validator(node, config);
|
|
93
|
+
|
|
94
|
+
for (const error of errors) output.push({ type, lines, location, error });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return output;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function createElement(
|
|
101
|
+
name: string | { key?: string | number },
|
|
102
|
+
attributes = {},
|
|
103
|
+
...children: any[]
|
|
104
|
+
) {
|
|
105
|
+
return { name, attributes, children };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default {
|
|
109
|
+
nodes,
|
|
110
|
+
tags,
|
|
111
|
+
functions,
|
|
112
|
+
renderers,
|
|
113
|
+
transforms,
|
|
114
|
+
Ast,
|
|
115
|
+
Tag,
|
|
116
|
+
Tokenizer,
|
|
117
|
+
parseTags,
|
|
118
|
+
transformer,
|
|
119
|
+
validator,
|
|
120
|
+
parse,
|
|
121
|
+
transform,
|
|
122
|
+
validate,
|
|
123
|
+
createElement,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export {
|
|
127
|
+
nodes,
|
|
128
|
+
tags,
|
|
129
|
+
functions,
|
|
130
|
+
transforms,
|
|
131
|
+
renderers,
|
|
132
|
+
Ast,
|
|
133
|
+
Tag,
|
|
134
|
+
Tokenizer,
|
|
135
|
+
parseTags,
|
|
136
|
+
transformer,
|
|
137
|
+
validator,
|
|
138
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@markdoc/markdoc",
|
|
3
|
+
"author": "Ryan Paul",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "A text markup language for documentation",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.mjs",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"index.ts",
|
|
11
|
+
"**/*.d.ts",
|
|
12
|
+
"src",
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "node -r ts-eager/register ./node_modules/.bin/jasmine",
|
|
19
|
+
"marktest": "node -r ts-eager/register ${INSPECT:+--inspect-brk} spec/marktest/index.ts spec/marktest/tests.yaml",
|
|
20
|
+
"build": "npm run build:common && npm run build:types",
|
|
21
|
+
"build:common": "node build.js",
|
|
22
|
+
"build:types": "tsc --emitDeclarationOnly --outDir dist",
|
|
23
|
+
"lint": "eslint .",
|
|
24
|
+
"grammar": "pegjs -o src/grammar/tag.js src/grammar/tag.pegjs && prettier --write src/grammar/tag.js",
|
|
25
|
+
"prettier": "prettier --write index.ts src spec",
|
|
26
|
+
"prettier:check": "prettier --check index.ts src spec",
|
|
27
|
+
"type:check": "tsc --noEmit",
|
|
28
|
+
"typedoc": "typedoc index.ts --includeVersion --readme none --excludePrivate --excludeExternals --excludeInternal --sort source-order --out types",
|
|
29
|
+
"postinstall": "patch-package"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=14.7.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/jasmine": "^3.10.2",
|
|
36
|
+
"@types/markdown-it": "^12.2.3",
|
|
37
|
+
"@types/node": "^16.11.12",
|
|
38
|
+
"@types/react": "^17.0.38",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^5.12.0",
|
|
40
|
+
"@typescript-eslint/parser": "^5.12.0",
|
|
41
|
+
"deep-assert": "0.3.0",
|
|
42
|
+
"diff": "^4.0.2",
|
|
43
|
+
"esbuild": "0.13.3",
|
|
44
|
+
"eslint": "^8.9.0",
|
|
45
|
+
"jasmine": "3.5.0",
|
|
46
|
+
"markdown-it": "12.3.2",
|
|
47
|
+
"patch-package": "^6.4.7",
|
|
48
|
+
"pegjs": "0.11.0-master.b7b87ea",
|
|
49
|
+
"prettier": "^2.5.1",
|
|
50
|
+
"ts-eager": "2.0.2",
|
|
51
|
+
"typedoc": "^0.22.13",
|
|
52
|
+
"typescript": "^4.5.4",
|
|
53
|
+
"yaml-js": "^0.2.3"
|
|
54
|
+
},
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "git+https://github.com/markdoc/markdoc.git"
|
|
58
|
+
},
|
|
59
|
+
"bugs": {
|
|
60
|
+
"url": "https://github.com/markdoc/markdoc/issues"
|
|
61
|
+
},
|
|
62
|
+
"homepage": "https://markdoc.io"
|
|
63
|
+
}
|
package/src/ast/base.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AstType,
|
|
3
|
+
Config,
|
|
4
|
+
Function as AstFunction,
|
|
5
|
+
Scalar,
|
|
6
|
+
} from '../types';
|
|
7
|
+
|
|
8
|
+
export function isAst(value?: any): value is AstType {
|
|
9
|
+
return !!value?.$$mdtype;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function isFunction(value?: any): value is AstFunction {
|
|
13
|
+
return !!(value?.$$mdtype === 'Function');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function* getAstValues(value: any): Generator<AstType, void, unknown> {
|
|
17
|
+
if (value == null || typeof value !== 'object') return;
|
|
18
|
+
|
|
19
|
+
if (Array.isArray(value)) for (const v of value) yield* getAstValues(v);
|
|
20
|
+
|
|
21
|
+
if (isAst(value)) yield value;
|
|
22
|
+
|
|
23
|
+
if (Object.getPrototypeOf(value) !== Object.prototype) return;
|
|
24
|
+
|
|
25
|
+
for (const v of Object.values(value)) yield* getAstValues(v);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function resolve(value: any, config: Config = {}): any {
|
|
29
|
+
if (value == null || typeof value !== 'object') return value;
|
|
30
|
+
|
|
31
|
+
if (Array.isArray(value)) return value.map((item) => resolve(item, config));
|
|
32
|
+
|
|
33
|
+
if (isAst(value) && value?.resolve instanceof Function)
|
|
34
|
+
return value.resolve(config);
|
|
35
|
+
|
|
36
|
+
if (Object.getPrototypeOf(value) !== Object.prototype) return value;
|
|
37
|
+
|
|
38
|
+
const output: Record<string, Scalar> = {};
|
|
39
|
+
for (const [k, v] of Object.entries(value)) output[k] = resolve(v, config);
|
|
40
|
+
return output;
|
|
41
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { resolve } from './base';
|
|
2
|
+
import type { Config, AstType } from '../types';
|
|
3
|
+
|
|
4
|
+
export default class Function implements AstType {
|
|
5
|
+
readonly $$mdtype = 'Function';
|
|
6
|
+
|
|
7
|
+
name;
|
|
8
|
+
parameters;
|
|
9
|
+
|
|
10
|
+
constructor(name: string, parameters: Record<string, any>) {
|
|
11
|
+
this.name = name;
|
|
12
|
+
this.parameters = parameters;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
resolve(config: Config = {}) {
|
|
16
|
+
const fn = config?.functions?.[this.name];
|
|
17
|
+
if (!fn) return null;
|
|
18
|
+
|
|
19
|
+
const parameters = resolve(this.parameters, config);
|
|
20
|
+
return fn.transform?.(parameters, config);
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/ast/index.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import Node from './node';
|
|
2
|
+
import Function from './function';
|
|
3
|
+
import Variable from './variable';
|
|
4
|
+
import * as base from './base';
|
|
5
|
+
|
|
6
|
+
import type { AstType } from '../types';
|
|
7
|
+
|
|
8
|
+
const AstTypes = {
|
|
9
|
+
Function,
|
|
10
|
+
Node,
|
|
11
|
+
Variable,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function reviver(_: string, value: AstType): any {
|
|
15
|
+
if (!value) return value;
|
|
16
|
+
const klass = AstTypes[value.$$mdtype] as any;
|
|
17
|
+
return klass ? Object.assign(new klass(), value) : value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function fromJSON(text: string): Node | Node[] {
|
|
21
|
+
return JSON.parse(text, reviver);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default {
|
|
25
|
+
...AstTypes,
|
|
26
|
+
...base,
|
|
27
|
+
fromJSON,
|
|
28
|
+
};
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import markdoc from '../../index';
|
|
3
|
+
import Node from './node';
|
|
4
|
+
import Variable from './variable';
|
|
5
|
+
|
|
6
|
+
import Tag from '../ast/tag';
|
|
7
|
+
|
|
8
|
+
describe('Node object', function () {
|
|
9
|
+
describe('traversal', function () {
|
|
10
|
+
it('with a simple document', function () {
|
|
11
|
+
const example = new Node('document', {}, [
|
|
12
|
+
new Node('heading', { level: 1 }, [
|
|
13
|
+
new Node('inline', {}, [
|
|
14
|
+
new Node('text', { content: 'This is a heading' }),
|
|
15
|
+
]),
|
|
16
|
+
]),
|
|
17
|
+
new Node('paragraph', {}, [
|
|
18
|
+
new Node('inline', {}, [
|
|
19
|
+
new Node('text', { content: 'This is a paragraph' }),
|
|
20
|
+
]),
|
|
21
|
+
]),
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const iter = example.walk();
|
|
25
|
+
expect(typeof iter[Symbol.iterator]).toEqual('function');
|
|
26
|
+
|
|
27
|
+
const output = [...iter];
|
|
28
|
+
expect(output.length).toEqual(6);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('processor rendering', function () {
|
|
34
|
+
function process(content, config = {}) {
|
|
35
|
+
return markdoc.transform(content, config);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('built-in nodes', function () {
|
|
39
|
+
it('a heading node', function () {
|
|
40
|
+
const example = new Node('heading', { level: 1 }, [
|
|
41
|
+
new Node('inline', {}, [
|
|
42
|
+
new Node('text', { content: 'This is a heading' }),
|
|
43
|
+
]),
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
const output = process(example);
|
|
47
|
+
expect(output).toDeepEqual(new Tag('h1', {}, ['This is a heading']));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('fenced code blocks', function () {
|
|
51
|
+
it('fenced code block with language', function () {
|
|
52
|
+
const example = new Node('fence', {
|
|
53
|
+
language: 'ruby',
|
|
54
|
+
content: 'test',
|
|
55
|
+
});
|
|
56
|
+
const output = process(example);
|
|
57
|
+
expect(output).toDeepEqual(
|
|
58
|
+
new Tag('pre', { 'data-language': 'ruby' }, ['test'])
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('render functions', function () {
|
|
65
|
+
it('with a simple render function', function () {
|
|
66
|
+
const example = new Node('foo', {});
|
|
67
|
+
|
|
68
|
+
const foo = {
|
|
69
|
+
transform() {
|
|
70
|
+
return new Tag('foo', {}, []);
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const output = process(example, { nodes: { foo } });
|
|
75
|
+
expect(output).toDeepEqual(new Tag('foo', {}, []));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('with a render function that renders attributes', function () {
|
|
79
|
+
const example = new Node('foo', { bar: 'baz', test: 100 });
|
|
80
|
+
const foo = {
|
|
81
|
+
attributes: {
|
|
82
|
+
bar: { type: String, render: true },
|
|
83
|
+
test: { type: Number, render: 'example' },
|
|
84
|
+
},
|
|
85
|
+
transform(node, config) {
|
|
86
|
+
return {
|
|
87
|
+
name: 'foo',
|
|
88
|
+
attributes: node.transformAttributes(config),
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const output = process(example, { nodes: { foo } });
|
|
94
|
+
expect(output).toDeepEqual({
|
|
95
|
+
name: 'foo',
|
|
96
|
+
attributes: { bar: 'baz', example: 100 },
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('tags', function () {
|
|
102
|
+
const tags = {
|
|
103
|
+
foo: {
|
|
104
|
+
render: 'foo',
|
|
105
|
+
attributes: {
|
|
106
|
+
bar: { type: 'String', render: true },
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
it('with a tag', function () {
|
|
112
|
+
const example = new Node(
|
|
113
|
+
'tag',
|
|
114
|
+
{ bar: 'baz' },
|
|
115
|
+
[new Node('inline', {}, [new Node('text', { content: 'test' })])],
|
|
116
|
+
'foo'
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const output = process(example, { tags });
|
|
120
|
+
expect(output).toDeepEqual(new Tag('foo', { bar: 'baz' }, ['test']));
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('with a non-existing tag', function () {
|
|
124
|
+
const example = new Node('container', { bar: 'baz' }, [
|
|
125
|
+
new Node('inline', {}, [new Node('text', { content: 'test' })]),
|
|
126
|
+
]);
|
|
127
|
+
|
|
128
|
+
// @ts-ignore test mutating node in userland
|
|
129
|
+
example.container = 'bar';
|
|
130
|
+
const output = process(example, { tags });
|
|
131
|
+
expect(output).toDeepEqual(['test']);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe('tag name', function () {
|
|
136
|
+
it('with a string value', function () {
|
|
137
|
+
const example = new Node('foo', {});
|
|
138
|
+
const foo = { render: 'foo' };
|
|
139
|
+
|
|
140
|
+
const output = process(example, { nodes: { foo } }) as Tag;
|
|
141
|
+
expect(output.name).toEqual('foo');
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('with no value', function () {
|
|
145
|
+
const example = new Node('foo', {}, [new Node('bar', {})]);
|
|
146
|
+
|
|
147
|
+
const output = process(example, {
|
|
148
|
+
nodes: { foo: {}, bar: { render: 'bar' } },
|
|
149
|
+
});
|
|
150
|
+
expect(output).toDeepEqual([new Tag('bar', {}, [])]);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe('attributes', function () {
|
|
155
|
+
it('with an id', function () {
|
|
156
|
+
const example = new Node('paragraph', { id: 'bar' });
|
|
157
|
+
const output = process(example);
|
|
158
|
+
expect(output).toDeepEqualSubset({
|
|
159
|
+
name: 'p',
|
|
160
|
+
attributes: { id: 'bar' },
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('with a class', function () {
|
|
165
|
+
const example = new Node('paragraph', {
|
|
166
|
+
class: { foo: true, bar: false },
|
|
167
|
+
});
|
|
168
|
+
const output = process(example);
|
|
169
|
+
expect(output).toDeepEqual(new Tag('p', { class: 'foo' }, []));
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('with boolean render attribute', function () {
|
|
173
|
+
const example = new Node('foo', { bar: 1, baz: 'test' });
|
|
174
|
+
const foo = {
|
|
175
|
+
render: 'foo',
|
|
176
|
+
attributes: {
|
|
177
|
+
bar: { type: Number, render: true },
|
|
178
|
+
baz: { type: String, render: true },
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const output = process(example, { nodes: { foo } });
|
|
183
|
+
expect(output).toDeepEqual(new Tag('foo', { bar: 1, baz: 'test' }));
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('with string render attribute', function () {
|
|
187
|
+
const example = new Node('foo', { bar: 1, baz: 'test' });
|
|
188
|
+
const foo = {
|
|
189
|
+
render: 'foo',
|
|
190
|
+
attributes: {
|
|
191
|
+
bar: { type: Number, render: 'data-bar' },
|
|
192
|
+
baz: { type: String, render: 'data-baz' },
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const output = process(example, { nodes: { foo } });
|
|
197
|
+
expect(output).toDeepEqual(
|
|
198
|
+
new Tag('foo', { 'data-bar': 1, 'data-baz': 'test' })
|
|
199
|
+
);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('with a non-rendered attribute', function () {
|
|
203
|
+
const example = new Node('foo', { bar: 1, baz: 'test' });
|
|
204
|
+
const foo = {
|
|
205
|
+
render: 'foo',
|
|
206
|
+
attributes: {
|
|
207
|
+
bar: { type: Number, render: true },
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const output = process(example, { nodes: { foo } }) as Tag;
|
|
212
|
+
expect(Object.keys(output.attributes).includes('baz')).toBeFalse();
|
|
213
|
+
expect(output).toDeepEqual(new Tag('foo', { bar: 1 }, []));
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('with a default attribute value', function () {
|
|
217
|
+
const example = new Node('foo', {});
|
|
218
|
+
const foo = {
|
|
219
|
+
render: 'foo',
|
|
220
|
+
attributes: {
|
|
221
|
+
bar: { type: Number, render: true, default: 10 },
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const output = process(example, { nodes: { foo } }) as Tag;
|
|
226
|
+
expect(output.attributes.bar).toEqual(10);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('with an overriden default attribute value', function () {
|
|
230
|
+
const example = new Node('foo', { bar: 1 });
|
|
231
|
+
const foo = {
|
|
232
|
+
render: 'foo',
|
|
233
|
+
attributes: {
|
|
234
|
+
bar: { type: Number, render: true, default: 10 },
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const output = process(example, { nodes: { foo } }) as Tag;
|
|
239
|
+
expect(output.attributes.bar).toEqual(1);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('variable nested in a hash', function () {
|
|
243
|
+
const bar = new Variable(['a', 'b', 'c']);
|
|
244
|
+
const example = new Node('foo', { bar });
|
|
245
|
+
const foo = {
|
|
246
|
+
render: 'foo',
|
|
247
|
+
attributes: {
|
|
248
|
+
bar: { type: String, render: true },
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const output = process(example, {
|
|
253
|
+
nodes: { foo },
|
|
254
|
+
variables: { a: { b: { c: 'example' } } },
|
|
255
|
+
});
|
|
256
|
+
expect(output).toDeepEqual(new Tag('foo', { bar: 'example' }, []));
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
describe('custom types', () => {
|
|
260
|
+
const cases = [
|
|
261
|
+
{
|
|
262
|
+
name: 'should support default values',
|
|
263
|
+
type: class NonNullString {
|
|
264
|
+
transform(value) {
|
|
265
|
+
return value || '';
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
attributes: undefined,
|
|
269
|
+
expectedValue: '',
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
name: 'should support transforms',
|
|
273
|
+
type: class UpperCaseString {
|
|
274
|
+
transform(value) {
|
|
275
|
+
return (value || '').toUpperCase();
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
value: 'test',
|
|
279
|
+
expectedValue: 'TEST',
|
|
280
|
+
},
|
|
281
|
+
];
|
|
282
|
+
|
|
283
|
+
cases.forEach((test) => {
|
|
284
|
+
it(test.name, () => {
|
|
285
|
+
const example = new Node('tag', { bar: test.value });
|
|
286
|
+
|
|
287
|
+
const config = {
|
|
288
|
+
nodes: {
|
|
289
|
+
tag: {
|
|
290
|
+
render: 'tag',
|
|
291
|
+
attributes: {
|
|
292
|
+
bar: {
|
|
293
|
+
render: true,
|
|
294
|
+
type: test.type,
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const output = process(example, config) as Tag;
|
|
302
|
+
expect(output.attributes.bar).toEqual(test.expectedValue);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
});
|
package/src/ast/node.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { resolve } from './base';
|
|
2
|
+
import transformer from '../transformer';
|
|
3
|
+
import type {
|
|
4
|
+
AstType,
|
|
5
|
+
Location,
|
|
6
|
+
Config,
|
|
7
|
+
RenderableTreeNode,
|
|
8
|
+
RenderableTreeNodes,
|
|
9
|
+
Schema,
|
|
10
|
+
ValidationError,
|
|
11
|
+
} from '../types';
|
|
12
|
+
|
|
13
|
+
export default class Node implements AstType {
|
|
14
|
+
readonly $$mdtype = 'Node';
|
|
15
|
+
|
|
16
|
+
attributes: Record<string, any>;
|
|
17
|
+
children: Node[];
|
|
18
|
+
errors: ValidationError[] = [];
|
|
19
|
+
lines: number[] = [];
|
|
20
|
+
type: string;
|
|
21
|
+
tag?: string;
|
|
22
|
+
|
|
23
|
+
inline = false;
|
|
24
|
+
location?: Location;
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
type = 'node',
|
|
28
|
+
attributes: Record<string, any> = {},
|
|
29
|
+
children: Node[] = [],
|
|
30
|
+
tag?: string
|
|
31
|
+
) {
|
|
32
|
+
this.attributes = attributes;
|
|
33
|
+
this.children = children;
|
|
34
|
+
this.type = type;
|
|
35
|
+
this.tag = tag;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
*walk(): Generator<Node, void, unknown> {
|
|
39
|
+
for (const child of this.children) {
|
|
40
|
+
yield child;
|
|
41
|
+
yield* child.walk();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
push(node: Node) {
|
|
46
|
+
this.children.push(node);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
resolve(config: Config = {}): Node {
|
|
50
|
+
return Object.assign(new Node(), this, {
|
|
51
|
+
children: this.children.map((child) => child.resolve(config)),
|
|
52
|
+
attributes: resolve(this.attributes, config),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
findSchema(config: Config = {}): Schema | undefined {
|
|
57
|
+
return transformer.findSchema(this, config);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
transformAttributes(config: Config = {}) {
|
|
61
|
+
return transformer.attributes(this, config);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
transformChildren(config: Config): RenderableTreeNode[] {
|
|
65
|
+
return transformer.children(this, config);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
transform(config: Config): RenderableTreeNodes {
|
|
69
|
+
return transformer.node(this, config);
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/ast/tag.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { RenderableTreeNode } from '../types';
|
|
2
|
+
|
|
3
|
+
export default class Tag {
|
|
4
|
+
readonly $$mdtype = 'Tag' as const;
|
|
5
|
+
|
|
6
|
+
name: string;
|
|
7
|
+
attributes: Record<string, any>;
|
|
8
|
+
children: RenderableTreeNode[];
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
name = 'div',
|
|
12
|
+
attributes = {},
|
|
13
|
+
children: RenderableTreeNode[] = []
|
|
14
|
+
) {
|
|
15
|
+
this.name = name;
|
|
16
|
+
this.attributes = attributes;
|
|
17
|
+
this.children = children;
|
|
18
|
+
}
|
|
19
|
+
}
|