@markdoc/markdoc 0.1.13 → 0.2.1
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 +9 -7
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +45 -29
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +45 -29
- package/dist/index.mjs.map +3 -3
- package/dist/react.js +16 -5
- package/dist/react.js.map +3 -3
- package/dist/src/formatter.d.ts.map +1 -1
- package/dist/src/renderers/html.d.ts.map +1 -1
- package/dist/src/renderers/react/react.d.ts +1 -1
- 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 -0
- package/dist/src/tag.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/transformer.d.ts.map +1 -1
- package/dist/src/types.d.ts +1 -1
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils.d.ts +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/index.ts +3 -3
- package/package.json +10 -3
- package/src/formatter.test.ts +70 -7
- package/src/formatter.ts +41 -12
- package/src/renderers/html.test.ts +10 -6
- package/src/renderers/html.ts +3 -2
- package/src/renderers/react/react.test.ts +5 -12
- package/src/renderers/react/react.ts +21 -4
- package/src/renderers/react/static.ts +21 -3
- package/src/tag.test.ts +17 -0
- package/src/tag.ts +4 -0
- package/src/tags/conditional.ts +11 -2
- package/src/transformer.ts +12 -2
- package/src/types.ts +1 -1
- package/src/utils.ts +2 -2
- package/dist/src/renderers/react/shared.d.ts +0 -5
- package/dist/src/renderers/react/shared.d.ts.map +0 -1
- package/src/renderers/react/shared.ts +0 -16
package/dist/react.js
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// src/tag.ts
|
|
2
|
+
var Tag = class {
|
|
3
|
+
constructor(name = "div", attributes = {}, children = []) {
|
|
4
|
+
this.$$mdtype = "Tag";
|
|
5
|
+
this.name = name;
|
|
6
|
+
this.attributes = attributes;
|
|
7
|
+
this.children = children;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
Tag.isTag = (tag) => {
|
|
11
|
+
return !!(tag?.$$mdtype === "Tag");
|
|
12
|
+
};
|
|
5
13
|
|
|
6
14
|
// src/renderers/react/react.ts
|
|
15
|
+
function tagName(name, components) {
|
|
16
|
+
return typeof name !== "string" ? name : name[0] !== name[0].toUpperCase() ? name : components instanceof Function ? components(name) : components[name];
|
|
17
|
+
}
|
|
7
18
|
function dynamic(node, React, { components = {} } = {}) {
|
|
8
19
|
function deepRender(value) {
|
|
9
20
|
if (value == null || typeof value !== "object")
|
|
@@ -22,7 +33,7 @@ function dynamic(node, React, { components = {} } = {}) {
|
|
|
22
33
|
function render(node2) {
|
|
23
34
|
if (Array.isArray(node2))
|
|
24
35
|
return React.createElement(React.Fragment, null, ...node2.map(render));
|
|
25
|
-
if (node2 === null || typeof node2 !== "object")
|
|
36
|
+
if (node2 === null || typeof node2 !== "object" || !Tag.isTag(node2))
|
|
26
37
|
return node2;
|
|
27
38
|
const {
|
|
28
39
|
name,
|
package/dist/react.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/
|
|
4
|
-
"sourcesContent": ["import type {
|
|
5
|
-
"mappings": ";
|
|
3
|
+
"sources": ["../src/tag.ts", "../src/renderers/react/react.ts"],
|
|
4
|
+
"sourcesContent": ["import type { RenderableTreeNode } from './types';\n\nexport default class Tag<\n N extends string = string,\n A extends Record<string, any> = Record<string, any>\n> {\n readonly $$mdtype = 'Tag' as const;\n\n static isTag = (tag: any): tag is Tag => {\n return !!(tag?.$$mdtype === 'Tag');\n };\n\n name: N;\n attributes: A;\n children: RenderableTreeNode[];\n\n constructor(\n name = 'div' as N,\n attributes = {} as A,\n children: RenderableTreeNode[] = []\n ) {\n this.name = name;\n this.attributes = attributes;\n this.children = children;\n }\n}\n", "import Tag from '../../tag';\nimport { RenderableTreeNodes, Scalar } from '../../types';\n\nimport type { createElement, ComponentType, Fragment, ReactNode } from 'react';\n\ntype ReactShape = Readonly<{\n createElement: typeof createElement;\n Fragment: typeof Fragment;\n}>;\n\ntype Component = ComponentType<unknown>;\n\nfunction tagName(\n name: string,\n components: Record<string, Component> | ((string: string) => Component)\n): string | Component {\n return typeof name !== 'string'\n ? name // This can be an object, e.g. when React.forwardRef is used\n : name[0] !== name[0].toUpperCase()\n ? name\n : components instanceof Function\n ? components(name)\n : components[name];\n}\n\nexport default function dynamic(\n node: RenderableTreeNodes,\n React: ReactShape,\n { components = {} } = {}\n) {\n function deepRender(value: any): any {\n if (value == null || typeof value !== 'object') return value;\n\n if (Array.isArray(value)) return value.map((item) => deepRender(item));\n\n if (value.$$mdtype === 'Tag') return render(value);\n\n if (typeof value !== 'object') return value;\n\n const output: Record<string, Scalar> = {};\n for (const [k, v] of Object.entries(value)) output[k] = deepRender(v);\n return output;\n }\n\n function render(node: RenderableTreeNodes): ReactNode {\n if (Array.isArray(node))\n return React.createElement(React.Fragment, null, ...node.map(render));\n\n if (node === null || typeof node !== 'object' || !Tag.isTag(node))\n return node;\n\n const {\n name,\n attributes: { class: className, ...attrs } = {},\n children = [],\n } = node;\n\n if (className) attrs.className = className;\n\n return React.createElement(\n tagName(name, components),\n Object.keys(attrs).length == 0 ? null : deepRender(attrs),\n ...children.map(render)\n );\n }\n\n return render(node);\n}\n"],
|
|
5
|
+
"mappings": ";AAEA,gBAGE;AAAA,EAWA,YACE,OAAO,OACP,aAAa,IACb,WAAiC,IACjC;AAdO,oBAAW;AAelB,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,WAAW;AAAA;AAAA;AAfX,AANT,IAMS,QAAQ,C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AAG;IACb,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;
|
|
1
|
+
{"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../../src/formatter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAA4B,IAAI,EAAE,KAAK,EAAY,MAAM,SAAS,CAAC;AAE/E,aAAK,OAAO,GAAG;IACb,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AA2aF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAIlE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../../../src/renderers/html.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../../../src/renderers/html.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAsBpD,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,IAAI,EAAE,mBAAmB,GAAG,MAAM,CAuBhE"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { RenderableTreeNodes } from '../../types';
|
|
1
2
|
import type { createElement, Fragment, ReactNode } from 'react';
|
|
2
|
-
import type { RenderableTreeNodes } from '../../types';
|
|
3
3
|
declare type ReactShape = Readonly<{
|
|
4
4
|
createElement: typeof createElement;
|
|
5
5
|
Fragment: typeof Fragment;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/react.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/react.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAU,MAAM,aAAa,CAAC;AAE1D,OAAO,KAAK,EAAE,aAAa,EAAiB,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAE/E,aAAK,UAAU,GAAG,QAAQ,CAAC;IACzB,aAAa,EAAE,OAAO,aAAa,CAAC;IACpC,QAAQ,EAAE,OAAO,QAAQ,CAAC;CAC3B,CAAC,CAAC;AAiBH,MAAM,CAAC,OAAO,UAAU,OAAO,CAC7B,IAAI,EAAE,mBAAmB,EACzB,KAAK,EAAE,UAAU,EACjB,EAAE,UAAe,EAAE;;CAAK,aAuCzB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"static.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/static.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"static.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/static.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,mBAAmB,EAAE,MAAM,aAAa,CAAC;AA6DtE,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,IAAI,EAAE,mBAAmB,GAAG,MAAM,CAOrE"}
|
package/dist/src/tag.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { RenderableTreeNode } from './types';
|
|
2
2
|
export default class Tag<N extends string = string, A extends Record<string, any> = Record<string, any>> {
|
|
3
3
|
readonly $$mdtype: "Tag";
|
|
4
|
+
static isTag: (tag: any) => tag is Tag<string, Record<string, any>>;
|
|
4
5
|
name: N;
|
|
5
6
|
attributes: A;
|
|
6
7
|
children: RenderableTreeNode[];
|
package/dist/src/tag.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tag.d.ts","sourceRoot":"","sources":["../../src/tag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD,MAAM,CAAC,OAAO,OAAO,GAAG,CACtB,CAAC,SAAS,MAAM,GAAG,MAAM,EACzB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAEnD,QAAQ,CAAC,QAAQ,QAAkB;IAEnC,IAAI,EAAE,CAAC,CAAC;IACR,UAAU,EAAE,CAAC,CAAC;IACd,QAAQ,EAAE,kBAAkB,EAAE,CAAC;gBAG7B,IAAI,IAAa,EACjB,UAAU,IAAU,EACpB,QAAQ,GAAE,kBAAkB,EAAO;CAMtC"}
|
|
1
|
+
{"version":3,"file":"tag.d.ts","sourceRoot":"","sources":["../../src/tag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD,MAAM,CAAC,OAAO,OAAO,GAAG,CACtB,CAAC,SAAS,MAAM,GAAG,MAAM,EACzB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAEnD,QAAQ,CAAC,QAAQ,QAAkB;IAEnC,MAAM,CAAC,KAAK,QAAS,GAAG,6CAEtB;IAEF,IAAI,EAAE,CAAC,CAAC;IACR,UAAU,EAAE,CAAC,CAAC;IACd,QAAQ,EAAE,kBAAkB,EAAE,CAAC;gBAG7B,IAAI,IAAa,EACjB,UAAU,IAAU,EACpB,QAAQ,GAAE,kBAAkB,EAAO;CAMtC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conditional.d.ts","sourceRoot":"","sources":["../../../src/tags/conditional.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"conditional.d.ts","sourceRoot":"","sources":["../../../src/tags/conditional.ts"],"names":[],"mappings":"AAEA,OAAO,EAKL,MAAM,EAEP,MAAM,UAAU,CAAC;AAIlB,wBAAgB,MAAM,CAAC,KAAK,EAAE,GAAG,WAEhC;AAmBD,eAAO,MAAM,KAAK,EAAE,MAmBnB,CAAC;AAEF,eAAO,MAAM,OAAO,EAAE,MAKrB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../../src/transformer.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../../src/transformer.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAMV,MAAM,EACN,WAAW,EACZ,MAAM,SAAS,CAAC;AAEjB,aAAK,gBAAgB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAE7C,eAAO,MAAM,gBAAgB,EAAE,gBAG9B,CAAC;;AAEF,wBA2DiB"}
|
package/dist/src/types.d.ts
CHANGED
|
@@ -51,7 +51,7 @@ export declare type LocationEdge = {
|
|
|
51
51
|
};
|
|
52
52
|
export declare type NodeType = 'blockquote' | 'code' | 'comment' | 'document' | 'em' | 'error' | 'fence' | 'hardbreak' | 'heading' | 'hr' | 'image' | 'inline' | 'item' | 'link' | 'list' | 'node' | 'paragraph' | 's' | 'softbreak' | 'strong' | 'table' | 'tag' | 'tbody' | 'td' | 'text' | 'th' | 'thead' | 'tr';
|
|
53
53
|
export declare type Primitive = null | boolean | number | string;
|
|
54
|
-
export declare type RenderableTreeNode = Tag |
|
|
54
|
+
export declare type RenderableTreeNode = Tag | Scalar;
|
|
55
55
|
export declare type RenderableTreeNodes = RenderableTreeNode | RenderableTreeNode[];
|
|
56
56
|
export declare type Scalar = Primitive | Scalar[] | {
|
|
57
57
|
[key: string]: Scalar;
|
package/dist/src/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types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O,CAAC;AAEZ,oBAAY,KAAK,GAAG,OAAO,GAAG,MAAM,CAAC"}
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type Token from 'markdown-it/lib/token';
|
|
|
2
2
|
export declare const OPEN = "{%";
|
|
3
3
|
export declare const CLOSE = "%}";
|
|
4
4
|
export declare const IDENTIFIER_REGEX: RegExp;
|
|
5
|
-
export declare function isIdentifier(s:
|
|
5
|
+
export declare function isIdentifier(s: any): s is string;
|
|
6
6
|
export declare function isPromise(a: any): a is Promise<any>;
|
|
7
7
|
export declare function findTagEnd(content: string, start?: number): number | null;
|
|
8
8
|
export declare function parseTags(content: string, firstLine?: number): Token[];
|
package/dist/src/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,MAAM,uBAAuB,CAAC;AAQ/C,eAAO,MAAM,IAAI,OAAO,CAAC;AACzB,eAAO,MAAM,KAAK,OAAO,CAAC;AAE1B,eAAO,MAAM,gBAAgB,QAAqB,CAAC;AAEnD,wBAAgB,YAAY,CAAC,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,MAAM,uBAAuB,CAAC;AAQ/C,eAAO,MAAM,IAAI,OAAO,CAAC;AACzB,eAAO,MAAM,KAAK,OAAO,CAAC;AAE1B,eAAO,MAAM,gBAAgB,QAAqB,CAAC;AAEnD,wBAAgB,YAAY,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,MAAM,CAEhD;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,CAEnD;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,SAAI,iBA0BpD;AAoBD,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,SAAI,GAAG,KAAK,EAAE,CA+DjE"}
|
package/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Ast from './src/ast';
|
|
2
|
-
import
|
|
2
|
+
import format from './src/formatter';
|
|
3
3
|
import functions from './src/functions';
|
|
4
4
|
import parser from './src/parser';
|
|
5
5
|
import * as nodes from './src/schema';
|
|
@@ -134,7 +134,7 @@ export {
|
|
|
134
134
|
transformer,
|
|
135
135
|
validator,
|
|
136
136
|
truthy,
|
|
137
|
-
|
|
137
|
+
format,
|
|
138
138
|
};
|
|
139
139
|
|
|
140
140
|
export default class Markdoc {
|
|
@@ -155,7 +155,7 @@ export default class Markdoc {
|
|
|
155
155
|
static validate = validate;
|
|
156
156
|
static createElement = createElement;
|
|
157
157
|
static truthy = truthy;
|
|
158
|
-
static
|
|
158
|
+
static format = format;
|
|
159
159
|
|
|
160
160
|
config;
|
|
161
161
|
constructor(config: Config) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markdoc/markdoc",
|
|
3
3
|
"author": "Ryan Paul",
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"description": "A text markup language for documentation",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.mjs",
|
|
@@ -32,9 +32,16 @@
|
|
|
32
32
|
"engines": {
|
|
33
33
|
"node": ">=14.7.0"
|
|
34
34
|
},
|
|
35
|
+
"optionalDependencies": {
|
|
36
|
+
"@types/markdown-it": "12.2.3"
|
|
37
|
+
},
|
|
35
38
|
"peerDependencies": {
|
|
36
|
-
"@types/
|
|
37
|
-
"
|
|
39
|
+
"@types/react": "*",
|
|
40
|
+
"react": "*"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"@types/react": {"optional": true},
|
|
44
|
+
"react": {"optional": true}
|
|
38
45
|
},
|
|
39
46
|
"devDependencies": {
|
|
40
47
|
"@types/jasmine": "^3.10.2",
|
package/src/formatter.test.ts
CHANGED
|
@@ -133,7 +133,38 @@ subtitle: Subtitle
|
|
|
133
133
|
---
|
|
134
134
|
|
|
135
135
|
`;
|
|
136
|
-
|
|
136
|
+
stable(source);
|
|
137
|
+
stable(source);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('escape markdown content', () => {
|
|
141
|
+
const source = `
|
|
142
|
+
\\* Asterisk
|
|
143
|
+
|
|
144
|
+
**/docs/\\***
|
|
145
|
+
|
|
146
|
+
~~**a \\_sentence\\_ with \\_underscores**~~
|
|
147
|
+
|
|
148
|
+
- Item with [brackets]
|
|
149
|
+
|
|
150
|
+
\`\`\`
|
|
151
|
+
\\*\\_[\\[]
|
|
152
|
+
\`\`\`
|
|
153
|
+
|
|
154
|
+
{% table %}
|
|
155
|
+
- **[Link](https://example.com?q=()**
|
|
156
|
+
- **[Link](https://example.com?q=\\()**
|
|
157
|
+
- 
|
|
158
|
+
- 
|
|
159
|
+
{% /table %}
|
|
160
|
+
|
|
161
|
+
paragraph 1
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
paragraph 2
|
|
166
|
+
`;
|
|
167
|
+
|
|
137
168
|
stable(source);
|
|
138
169
|
});
|
|
139
170
|
|
|
@@ -164,7 +195,7 @@ subtitle: Subtitle
|
|
|
164
195
|
X
|
|
165
196
|
{% /if %}
|
|
166
197
|
{% $user.name %}
|
|
167
|
-
{% key x=$user.name
|
|
198
|
+
{% key x=$user.name y=$flag z=$array[5] /%}
|
|
168
199
|
`;
|
|
169
200
|
const expected = `
|
|
170
201
|
{% tag "complex primary" /%}
|
|
@@ -175,7 +206,7 @@ X
|
|
|
175
206
|
|
|
176
207
|
{% $user.name %}
|
|
177
208
|
|
|
178
|
-
{% key x=$user.name
|
|
209
|
+
{% key x=$user.name y=$flag z=$array[5] /%}
|
|
179
210
|
`;
|
|
180
211
|
|
|
181
212
|
check(source, expected);
|
|
@@ -251,7 +282,7 @@ Yes!
|
|
|
251
282
|
it('long inline tags', () => {
|
|
252
283
|
const source = `{% button type="button" href="https://example.com/a-very-long-inline-tag" %}A very long inline tag{% /button %}
|
|
253
284
|
`;
|
|
254
|
-
|
|
285
|
+
stable(source);
|
|
255
286
|
|
|
256
287
|
const inlineParent = `### {% image src="/src" alt="A very long alt text to test if the tag wraps or not" /%}
|
|
257
288
|
`;
|
|
@@ -262,7 +293,7 @@ Yes!
|
|
|
262
293
|
const source = `
|
|
263
294
|
{% tag a=true b="My very long text well over 80 characters in total" c=123456789 d=false /%}
|
|
264
295
|
`;
|
|
265
|
-
|
|
296
|
+
stable(source, { maxTagOpeningWidth: Infinity });
|
|
266
297
|
});
|
|
267
298
|
|
|
268
299
|
it('nested tags — allowIndentation: true', () => {
|
|
@@ -469,7 +500,7 @@ Yes!
|
|
|
469
500
|
{% /list %}
|
|
470
501
|
`;
|
|
471
502
|
|
|
472
|
-
|
|
503
|
+
stable(source);
|
|
473
504
|
});
|
|
474
505
|
|
|
475
506
|
it('complicated nested lists', () => {
|
|
@@ -604,7 +635,7 @@ Yes!
|
|
|
604
635
|
{% /tab %}
|
|
605
636
|
`;
|
|
606
637
|
|
|
607
|
-
|
|
638
|
+
stable(source);
|
|
608
639
|
});
|
|
609
640
|
|
|
610
641
|
it('fences with no language', () => {
|
|
@@ -622,4 +653,36 @@ Package.json
|
|
|
622
653
|
|
|
623
654
|
check(source, expected);
|
|
624
655
|
});
|
|
656
|
+
|
|
657
|
+
it('complex lists', () => {
|
|
658
|
+
const source = `
|
|
659
|
+
* **One {% colspan=1 %}**
|
|
660
|
+
* **Two {% colspan=2 %}**
|
|
661
|
+
* **Three {% colspan=3 %}**
|
|
662
|
+
`;
|
|
663
|
+
|
|
664
|
+
const expected = `
|
|
665
|
+
- **One**{% colspan=1 %}
|
|
666
|
+
- **Two**{% colspan=2 %}
|
|
667
|
+
- **Three**{% colspan=3 %}
|
|
668
|
+
`;
|
|
669
|
+
|
|
670
|
+
check(source, expected);
|
|
671
|
+
stable(expected);
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
it('nested fences', () => {
|
|
675
|
+
const source = `
|
|
676
|
+
${'`'.repeat(4)}
|
|
677
|
+
|
|
678
|
+
${'`'.repeat(3)}
|
|
679
|
+
Fence within a fence
|
|
680
|
+
${'`'.repeat(3)}
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
${'`'.repeat(4)}
|
|
684
|
+
`;
|
|
685
|
+
|
|
686
|
+
stable(source);
|
|
687
|
+
});
|
|
625
688
|
});
|
package/src/formatter.ts
CHANGED
|
@@ -29,6 +29,10 @@ function* formatChildren(a: Node, options: Options) {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
function* formatInline(g: Generator<string>) {
|
|
33
|
+
yield [...g].join('').trim();
|
|
34
|
+
}
|
|
35
|
+
|
|
32
36
|
function* formatTableRow(items: Array<string>) {
|
|
33
37
|
yield `| ${items.join(' | ')} |`;
|
|
34
38
|
}
|
|
@@ -93,7 +97,8 @@ function* formatVariable(v: Variable) {
|
|
|
93
97
|
yield v.path
|
|
94
98
|
.map((p, i) => {
|
|
95
99
|
if (i === 0) return p;
|
|
96
|
-
if (isIdentifier(
|
|
100
|
+
if (isIdentifier(p)) return '.' + p;
|
|
101
|
+
if (typeof p === 'number') return `[${p}]`;
|
|
97
102
|
return `["${p}"]`;
|
|
98
103
|
})
|
|
99
104
|
.join('');
|
|
@@ -117,6 +122,14 @@ function* trimStart(g: Generator<string>) {
|
|
|
117
122
|
yield* g;
|
|
118
123
|
}
|
|
119
124
|
|
|
125
|
+
function* escapeMarkdownCharacters(s: string, characters: RegExp) {
|
|
126
|
+
yield s
|
|
127
|
+
.replace(characters, '\\$&')
|
|
128
|
+
// TODO keep as entity in the AST?
|
|
129
|
+
// Non-breaking space (0xA0)
|
|
130
|
+
.replace(new RegExp('\xa0', 'g'), ' ');
|
|
131
|
+
}
|
|
132
|
+
|
|
120
133
|
function* formatNode(n: Node, o: Options = {}) {
|
|
121
134
|
const no = { ...o, parent: n };
|
|
122
135
|
const indent = SPACE.repeat(no.indent || 0);
|
|
@@ -157,7 +170,9 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
157
170
|
yield* formatValue(n.attributes.alt, no);
|
|
158
171
|
yield ']';
|
|
159
172
|
yield '(';
|
|
160
|
-
yield*
|
|
173
|
+
yield* typeof n.attributes.src === 'string'
|
|
174
|
+
? escapeMarkdownCharacters(n.attributes.src, /[()]/)
|
|
175
|
+
: formatValue(n.attributes.src, no);
|
|
161
176
|
if (n.attributes.title) {
|
|
162
177
|
yield SPACE + `"${n.attributes.title}"`;
|
|
163
178
|
}
|
|
@@ -169,7 +184,9 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
169
184
|
yield* formatChildren(n, no);
|
|
170
185
|
yield ']';
|
|
171
186
|
yield '(';
|
|
172
|
-
yield*
|
|
187
|
+
yield* typeof n.attributes.href === 'string'
|
|
188
|
+
? escapeMarkdownCharacters(n.attributes.href, /[()]/g)
|
|
189
|
+
: formatValue(n.attributes.href, no);
|
|
173
190
|
if (n.attributes.title) {
|
|
174
191
|
yield SPACE + `"${n.attributes.title}"`;
|
|
175
192
|
}
|
|
@@ -177,9 +194,12 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
177
194
|
break;
|
|
178
195
|
}
|
|
179
196
|
case 'text': {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
197
|
+
const { content } = n.attributes;
|
|
198
|
+
if (Ast.isAst(content)) yield OPEN + SPACE;
|
|
199
|
+
yield* typeof content === 'string'
|
|
200
|
+
? escapeMarkdownCharacters(content, /[_*~]/g)
|
|
201
|
+
: formatValue(content, no);
|
|
202
|
+
if (Ast.isAst(content)) yield SPACE + CLOSE;
|
|
183
203
|
break;
|
|
184
204
|
}
|
|
185
205
|
case 'blockquote': {
|
|
@@ -199,7 +219,16 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
199
219
|
case 'fence': {
|
|
200
220
|
yield NL;
|
|
201
221
|
yield indent;
|
|
202
|
-
|
|
222
|
+
|
|
223
|
+
const innerFence = n.attributes.content.match(/`{3,}/g) || [];
|
|
224
|
+
|
|
225
|
+
const innerFenceLength = innerFence
|
|
226
|
+
.map((s: string) => s.length)
|
|
227
|
+
.reduce(max, 0);
|
|
228
|
+
|
|
229
|
+
const boundary = '`'.repeat(innerFenceLength ? innerFenceLength + 1 : 3);
|
|
230
|
+
|
|
231
|
+
yield boundary;
|
|
203
232
|
if (n.attributes.language) yield n.attributes.language;
|
|
204
233
|
if (n.annotations.length) yield SPACE;
|
|
205
234
|
yield* formatAnnotations(n);
|
|
@@ -207,7 +236,7 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
207
236
|
yield indent;
|
|
208
237
|
// TODO use formatChildren once we can differentiate inline from block tags within fences
|
|
209
238
|
yield n.attributes.content.split(NL).join(NL + indent); // yield* formatChildren(n, no);
|
|
210
|
-
yield
|
|
239
|
+
yield boundary;
|
|
211
240
|
yield NL;
|
|
212
241
|
break;
|
|
213
242
|
}
|
|
@@ -263,25 +292,25 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
263
292
|
}
|
|
264
293
|
case 'strong': {
|
|
265
294
|
yield '**';
|
|
266
|
-
yield* formatChildren(n, no);
|
|
295
|
+
yield* formatInline(formatChildren(n, no));
|
|
267
296
|
yield '**';
|
|
268
297
|
break;
|
|
269
298
|
}
|
|
270
299
|
case 'em': {
|
|
271
300
|
yield '_';
|
|
272
|
-
yield* formatChildren(n, no);
|
|
301
|
+
yield* formatInline(formatChildren(n, no));
|
|
273
302
|
yield '_';
|
|
274
303
|
break;
|
|
275
304
|
}
|
|
276
305
|
case 'code': {
|
|
277
306
|
yield '`';
|
|
278
|
-
yield* formatValue(n.attributes.content, no);
|
|
307
|
+
yield* formatInline(formatValue(n.attributes.content, no));
|
|
279
308
|
yield '`';
|
|
280
309
|
break;
|
|
281
310
|
}
|
|
282
311
|
case 's': {
|
|
283
312
|
yield '~~';
|
|
284
|
-
yield* formatChildren(n, no);
|
|
313
|
+
yield* formatInline(formatChildren(n, no));
|
|
285
314
|
yield '~~';
|
|
286
315
|
break;
|
|
287
316
|
}
|
|
@@ -1,39 +1,43 @@
|
|
|
1
1
|
import render from './html';
|
|
2
2
|
import { RenderableTreeNode } from '../types';
|
|
3
|
+
import Tag from '../tag';
|
|
3
4
|
|
|
4
5
|
function tag(
|
|
5
6
|
name: string,
|
|
6
7
|
attributes: Record<string, any> = {},
|
|
7
8
|
children: RenderableTreeNode[] = []
|
|
8
9
|
) {
|
|
9
|
-
return
|
|
10
|
+
return new Tag(name, attributes, children);
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
describe('HTML renderer', function () {
|
|
13
14
|
it('rendering a tag', function () {
|
|
14
|
-
const example = render(tag('h1',
|
|
15
|
+
const example = render(tag('h1', undefined, ['test'])).trim();
|
|
15
16
|
expect(example).toEqual('<h1>test</h1>');
|
|
16
17
|
});
|
|
17
18
|
|
|
18
19
|
it('rendering string child nodes', function () {
|
|
19
|
-
const example = tag('h1',
|
|
20
|
+
const example = tag('h1', undefined, ['test ', '1']);
|
|
20
21
|
expect(render(example)).toEqual('<h1>test 1</h1>');
|
|
21
22
|
});
|
|
22
23
|
|
|
23
24
|
it('rendering nested tags', function () {
|
|
24
|
-
const example = tag('div',
|
|
25
|
+
const example = tag('div', undefined, [tag('p', undefined, ['test'])]);
|
|
25
26
|
|
|
26
27
|
expect(render(example)).toEqual('<div><p>test</p></div>');
|
|
27
28
|
});
|
|
28
29
|
|
|
29
30
|
it('rendering parallel tags', function () {
|
|
30
|
-
const example = [
|
|
31
|
+
const example = [
|
|
32
|
+
tag('p', undefined, ['foo']),
|
|
33
|
+
tag('p', undefined, ['bar']),
|
|
34
|
+
];
|
|
31
35
|
|
|
32
36
|
expect(render(example)).toEqual('<p>foo</p><p>bar</p>');
|
|
33
37
|
});
|
|
34
38
|
|
|
35
39
|
it('rendering a tag with an invalid child', function () {
|
|
36
|
-
const example = tag('div',
|
|
40
|
+
const example = tag('div', undefined, ['test', { foo: 'bar' }]);
|
|
37
41
|
|
|
38
42
|
expect(render(example)).toEqual('<div>test</div>');
|
|
39
43
|
});
|
package/src/renderers/html.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import MarkdownIt from 'markdown-it';
|
|
2
|
+
import Tag from '../tag';
|
|
2
3
|
import type { RenderableTreeNodes } from '../types';
|
|
3
4
|
const { escapeHtml } = MarkdownIt().utils;
|
|
4
5
|
|
|
@@ -23,11 +24,11 @@ const voidElements = new Set([
|
|
|
23
24
|
|
|
24
25
|
export default function render(node: RenderableTreeNodes): string {
|
|
25
26
|
if (typeof node === 'string' || typeof node === 'number')
|
|
26
|
-
return escapeHtml(node);
|
|
27
|
+
return escapeHtml(String(node));
|
|
27
28
|
|
|
28
29
|
if (Array.isArray(node)) return node.map(render).join('');
|
|
29
30
|
|
|
30
|
-
if (node === null || typeof node !== 'object') return '';
|
|
31
|
+
if (node === null || typeof node !== 'object' || !Tag.isTag(node)) return '';
|
|
31
32
|
|
|
32
33
|
const { name, attributes, children = [] } = node;
|
|
33
34
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import dynamic from './react';
|
|
2
2
|
import renderStatic from './static';
|
|
3
|
+
import Tag from '../../tag';
|
|
3
4
|
|
|
4
5
|
const React = {
|
|
5
6
|
Fragment: 'fragment',
|
|
@@ -52,7 +53,7 @@ describe('React dynamic renderer', function () {
|
|
|
52
53
|
|
|
53
54
|
it('rendering an external component', function () {
|
|
54
55
|
const components = { Foo: 'bar' };
|
|
55
|
-
const example =
|
|
56
|
+
const example = new Tag('Foo', undefined, ['test']);
|
|
56
57
|
const output = dynamic(example, React, { components });
|
|
57
58
|
expect(output).toDeepEqualSubset({
|
|
58
59
|
name: 'bar',
|
|
@@ -73,11 +74,7 @@ describe('React dynamic renderer', function () {
|
|
|
73
74
|
});
|
|
74
75
|
|
|
75
76
|
it('with a class attribute', function () {
|
|
76
|
-
const example = {
|
|
77
|
-
name: 'h1',
|
|
78
|
-
attributes: { class: 'foo bar' },
|
|
79
|
-
children: ['test'],
|
|
80
|
-
};
|
|
77
|
+
const example = new Tag('h1', { class: 'foo bar' }, ['test']);
|
|
81
78
|
|
|
82
79
|
const output = dynamic(example, React);
|
|
83
80
|
expect(output).toDeepEqualSubset({
|
|
@@ -101,11 +98,7 @@ describe('React dynamic renderer', function () {
|
|
|
101
98
|
|
|
102
99
|
describe('rendering built-in nodes', function () {
|
|
103
100
|
it('rendering a fenced code block', function () {
|
|
104
|
-
const example = {
|
|
105
|
-
name: 'pre',
|
|
106
|
-
attributes: { class: 'code code-ruby' },
|
|
107
|
-
children: ['test'],
|
|
108
|
-
};
|
|
101
|
+
const example = new Tag('pre', { class: 'code code-ruby' }, ['test']);
|
|
109
102
|
|
|
110
103
|
const output = dynamic(example, React);
|
|
111
104
|
expect(output).toDeepEqual({
|
|
@@ -164,7 +157,7 @@ describe('React static renderer', function () {
|
|
|
164
157
|
|
|
165
158
|
it('rendering an external component', function () {
|
|
166
159
|
const components = { Foo: 'bar' };
|
|
167
|
-
const example =
|
|
160
|
+
const example = new Tag('Foo', undefined, ['test']);
|
|
168
161
|
const code = renderStatic(example);
|
|
169
162
|
const output = eval(code)({ components });
|
|
170
163
|
|