@markdoc/markdoc 0.1.13 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +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.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
|
@@ -1,12 +1,28 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import Tag from '../../tag';
|
|
2
|
+
import { RenderableTreeNodes, Scalar } from '../../types';
|
|
3
|
+
|
|
4
|
+
import type { createElement, ComponentType, Fragment, ReactNode } from 'react';
|
|
4
5
|
|
|
5
6
|
type ReactShape = Readonly<{
|
|
6
7
|
createElement: typeof createElement;
|
|
7
8
|
Fragment: typeof Fragment;
|
|
8
9
|
}>;
|
|
9
10
|
|
|
11
|
+
type Component = ComponentType<unknown>;
|
|
12
|
+
|
|
13
|
+
function tagName(
|
|
14
|
+
name: string,
|
|
15
|
+
components: Record<string, Component> | ((string: string) => Component)
|
|
16
|
+
): string | Component {
|
|
17
|
+
return typeof name !== 'string'
|
|
18
|
+
? name // This can be an object, e.g. when React.forwardRef is used
|
|
19
|
+
: name[0] !== name[0].toUpperCase()
|
|
20
|
+
? name
|
|
21
|
+
: components instanceof Function
|
|
22
|
+
? components(name)
|
|
23
|
+
: components[name];
|
|
24
|
+
}
|
|
25
|
+
|
|
10
26
|
export default function dynamic(
|
|
11
27
|
node: RenderableTreeNodes,
|
|
12
28
|
React: ReactShape,
|
|
@@ -30,7 +46,8 @@ export default function dynamic(
|
|
|
30
46
|
if (Array.isArray(node))
|
|
31
47
|
return React.createElement(React.Fragment, null, ...node.map(render));
|
|
32
48
|
|
|
33
|
-
if (node === null || typeof node !== 'object'
|
|
49
|
+
if (node === null || typeof node !== 'object' || !Tag.isTag(node))
|
|
50
|
+
return node;
|
|
34
51
|
|
|
35
52
|
const {
|
|
36
53
|
name,
|
|
@@ -1,5 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import Tag from '../../tag';
|
|
2
|
+
import { RenderableTreeNode, RenderableTreeNodes } from '../../types';
|
|
3
|
+
|
|
4
|
+
import type { ComponentType } from 'react';
|
|
5
|
+
|
|
6
|
+
type Component = ComponentType<unknown>;
|
|
7
|
+
|
|
8
|
+
function tagName(
|
|
9
|
+
name: string,
|
|
10
|
+
components: Record<string, Component> | ((string: string) => Component)
|
|
11
|
+
): string | Component {
|
|
12
|
+
return typeof name !== 'string'
|
|
13
|
+
? 'Fragment'
|
|
14
|
+
: name[0] !== name[0].toUpperCase()
|
|
15
|
+
? name
|
|
16
|
+
: components instanceof Function
|
|
17
|
+
? components(name)
|
|
18
|
+
: components[name];
|
|
19
|
+
}
|
|
3
20
|
|
|
4
21
|
function renderArray(children: RenderableTreeNode[]): string {
|
|
5
22
|
return children.map(render).join(', ');
|
|
@@ -26,7 +43,8 @@ function render(node: RenderableTreeNodes): string {
|
|
|
26
43
|
if (Array.isArray(node))
|
|
27
44
|
return `React.createElement(React.Fragment, null, ${renderArray(node)})`;
|
|
28
45
|
|
|
29
|
-
if (node === null || typeof node !== 'object'
|
|
46
|
+
if (node === null || typeof node !== 'object' || !Tag.isTag(node))
|
|
47
|
+
return JSON.stringify(node);
|
|
30
48
|
|
|
31
49
|
const {
|
|
32
50
|
name,
|
package/src/tag.ts
CHANGED
package/src/tags/conditional.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { isPromise } from '../utils';
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import {
|
|
4
|
+
MaybePromise,
|
|
5
|
+
Node,
|
|
6
|
+
RenderableTreeNode,
|
|
7
|
+
RenderableTreeNodes,
|
|
8
|
+
Schema,
|
|
9
|
+
Value,
|
|
10
|
+
} from '../types';
|
|
4
11
|
|
|
5
12
|
type Condition = { condition: Value; children: Node[] };
|
|
6
13
|
|
|
@@ -34,7 +41,9 @@ export const tagIf: Schema = {
|
|
|
34
41
|
const conditions = renderConditions(node);
|
|
35
42
|
for (const { condition, children } of conditions)
|
|
36
43
|
if (truthy(condition)) {
|
|
37
|
-
const nodes = children.flatMap(
|
|
44
|
+
const nodes = children.flatMap<MaybePromise<RenderableTreeNodes>>(
|
|
45
|
+
(child) => child.transform(config)
|
|
46
|
+
);
|
|
38
47
|
if (nodes.some(isPromise)) {
|
|
39
48
|
return Promise.all(nodes).then((nodes) => nodes.flat());
|
|
40
49
|
}
|
package/src/transformer.ts
CHANGED
|
@@ -2,7 +2,15 @@ import Tag from './tag';
|
|
|
2
2
|
import { Class } from './schema-types/class';
|
|
3
3
|
import { Id } from './schema-types/id';
|
|
4
4
|
import { isPromise } from './utils';
|
|
5
|
-
import type {
|
|
5
|
+
import type {
|
|
6
|
+
Config,
|
|
7
|
+
MaybePromise,
|
|
8
|
+
Node,
|
|
9
|
+
NodeType,
|
|
10
|
+
RenderableTreeNodes,
|
|
11
|
+
Schema,
|
|
12
|
+
Transformer,
|
|
13
|
+
} from './types';
|
|
6
14
|
|
|
7
15
|
type AttributesSchema = Schema['attributes'];
|
|
8
16
|
|
|
@@ -43,7 +51,9 @@ export default {
|
|
|
43
51
|
},
|
|
44
52
|
|
|
45
53
|
children(node: Node, config: Config = {}) {
|
|
46
|
-
const children = node.children.flatMap(
|
|
54
|
+
const children = node.children.flatMap<MaybePromise<RenderableTreeNodes>>(
|
|
55
|
+
(child) => this.node(child, config)
|
|
56
|
+
);
|
|
47
57
|
if (children.some(isPromise)) {
|
|
48
58
|
return Promise.all(children);
|
|
49
59
|
}
|
package/src/types.ts
CHANGED
|
@@ -93,7 +93,7 @@ export type NodeType =
|
|
|
93
93
|
|
|
94
94
|
export type Primitive = null | boolean | number | string;
|
|
95
95
|
|
|
96
|
-
export type RenderableTreeNode = Tag |
|
|
96
|
+
export type RenderableTreeNode = Tag | Scalar;
|
|
97
97
|
export type RenderableTreeNodes = RenderableTreeNode | RenderableTreeNode[];
|
|
98
98
|
|
|
99
99
|
export type Scalar = Primitive | Scalar[] | { [key: string]: Scalar };
|
package/src/utils.ts
CHANGED
|
@@ -15,8 +15,8 @@ export const CLOSE = '%}';
|
|
|
15
15
|
|
|
16
16
|
export const IDENTIFIER_REGEX = /^[a-zA-Z0-9_-]+$/;
|
|
17
17
|
|
|
18
|
-
export function isIdentifier(s:
|
|
19
|
-
return IDENTIFIER_REGEX.test(s);
|
|
18
|
+
export function isIdentifier(s: any): s is string {
|
|
19
|
+
return typeof s === 'string' && IDENTIFIER_REGEX.test(s);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export function isPromise(a: any): a is Promise<any> {
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { ComponentType } from 'react';
|
|
2
|
-
declare type Component = ComponentType<unknown>;
|
|
3
|
-
export declare function tagName(name: string, components: Record<string, Component> | ((string: string) => Component)): string | Component;
|
|
4
|
-
export {};
|
|
5
|
-
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/shared.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAE3C,aAAK,SAAS,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;AAExC,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,KAAK,SAAS,CAAC,GACtE,MAAM,GAAG,SAAS,CAQpB"}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { ComponentType } from 'react';
|
|
2
|
-
|
|
3
|
-
type Component = ComponentType<unknown>;
|
|
4
|
-
|
|
5
|
-
export function tagName(
|
|
6
|
-
name: string,
|
|
7
|
-
components: Record<string, Component> | ((string: string) => Component)
|
|
8
|
-
): string | Component {
|
|
9
|
-
return typeof name !== 'string'
|
|
10
|
-
? 'Fragment'
|
|
11
|
-
: name[0] !== name[0].toUpperCase()
|
|
12
|
-
? name
|
|
13
|
-
: components instanceof Function
|
|
14
|
-
? components(name)
|
|
15
|
-
: components[name];
|
|
16
|
-
}
|