@player-ui/markdown-plugin 0.14.1 → 0.15.0-next.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/dist/MarkdownPlugin.native.js +45 -26
- package/dist/MarkdownPlugin.native.js.map +1 -1
- package/dist/cjs/index.cjs +39 -31
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.legacy-esm.js +39 -31
- package/dist/index.mjs +39 -31
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/index.test.ts +97 -0
- package/src/index.ts +18 -11
- package/src/types.ts +16 -0
- package/src/utils/markdownParser.ts +32 -24
- package/types/types.d.ts +12 -0
- package/types/utils/markdownParser.d.ts +2 -3
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { Node, ParseObjectOptions } from "@player-ui/player";
|
|
2
2
|
import { NodeType } from "@player-ui/player";
|
|
3
|
-
import type { Asset } from "@player-ui/types";
|
|
4
3
|
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
5
|
-
import type { Mappers } from "../types";
|
|
4
|
+
import type { Mappers, MarkdownAsset } from "../types";
|
|
6
5
|
import { transformers } from "./transformers";
|
|
7
6
|
|
|
8
7
|
/**
|
|
@@ -16,7 +15,7 @@ export function parseAssetMarkdownContent({
|
|
|
16
15
|
/**
|
|
17
16
|
* Asset to be parsed
|
|
18
17
|
*/
|
|
19
|
-
asset:
|
|
18
|
+
asset: MarkdownAsset;
|
|
20
19
|
/**
|
|
21
20
|
* Mappers record of AST Node to Player Content
|
|
22
21
|
*
|
|
@@ -32,36 +31,45 @@ export function parseAssetMarkdownContent({
|
|
|
32
31
|
options?: ParseObjectOptions,
|
|
33
32
|
) => Node.Node | null;
|
|
34
33
|
}): Node.Node | null {
|
|
35
|
-
const
|
|
36
|
-
const
|
|
34
|
+
const input = asset.value ?? "";
|
|
35
|
+
const { children } = fromMarkdown(input);
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
// No markdown content: return an empty text asset
|
|
38
|
+
if (children.length === 0) {
|
|
39
|
+
const empty = mappers.text({ originalAsset: asset, value: "" });
|
|
40
|
+
return parser?.(empty, NodeType.Asset) || null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Map all children to their transformed content
|
|
44
|
+
const value = children
|
|
45
|
+
.map((node) => {
|
|
46
|
+
const transformer = transformers[node.type];
|
|
47
|
+
if (!transformer) {
|
|
48
|
+
if (mappers.null) {
|
|
49
|
+
return mappers?.null({ originalAsset: asset });
|
|
50
|
+
} else {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
41
54
|
return transformer({
|
|
42
|
-
astNode: node
|
|
55
|
+
astNode: node,
|
|
43
56
|
asset,
|
|
44
57
|
mappers,
|
|
45
58
|
transformers,
|
|
46
59
|
});
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
const collection = mappers.collection({
|
|
50
|
-
originalAsset: asset,
|
|
51
|
-
value,
|
|
52
|
-
});
|
|
60
|
+
})
|
|
61
|
+
.filter((x) => x !== null);
|
|
53
62
|
|
|
54
|
-
|
|
63
|
+
// If only one item, return it directly; otherwise wrap in collection
|
|
64
|
+
if (value.length === 1) {
|
|
65
|
+
const [first] = value;
|
|
66
|
+
return parser?.(first!, NodeType.Asset) || null;
|
|
55
67
|
}
|
|
56
68
|
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
astNode: children[0] as any,
|
|
61
|
-
asset,
|
|
62
|
-
mappers,
|
|
63
|
-
transformers,
|
|
69
|
+
const collection = mappers.collection({
|
|
70
|
+
originalAsset: asset,
|
|
71
|
+
value,
|
|
64
72
|
});
|
|
65
73
|
|
|
66
|
-
return parser?.(
|
|
74
|
+
return parser?.(collection, NodeType.Asset) || null;
|
|
67
75
|
}
|
package/types/types.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { Asset } from "@player-ui/types";
|
|
2
|
+
/**
|
|
3
|
+
* A markdown asset authored in flows.
|
|
4
|
+
* The value may be undefined or a string.
|
|
5
|
+
*/
|
|
6
|
+
export interface MarkdownAsset extends Asset<"markdown"> {
|
|
7
|
+
value?: string;
|
|
8
|
+
}
|
|
2
9
|
export interface BaseArgs {
|
|
3
10
|
/**
|
|
4
11
|
* Unparsed Asset
|
|
@@ -11,6 +18,7 @@ export type LiteralMapper<T extends object = object> = (args: {
|
|
|
11
18
|
*/
|
|
12
19
|
value: string;
|
|
13
20
|
} & BaseArgs & T) => Asset;
|
|
21
|
+
export type NullMapper<T extends object = object> = (args: BaseArgs & T) => Asset;
|
|
14
22
|
export type CompositeMapper<T extends object = object> = (args: {
|
|
15
23
|
/**
|
|
16
24
|
* array of assets resulted from the recursion over the AST node children
|
|
@@ -109,6 +117,10 @@ export interface Mappers {
|
|
|
109
117
|
* list item markdown (e.g. - item)
|
|
110
118
|
*/
|
|
111
119
|
listItem?: CompositeMapper;
|
|
120
|
+
/**
|
|
121
|
+
* Optional mapper for when nothing comes back from the mapping
|
|
122
|
+
*/
|
|
123
|
+
null?: NullMapper;
|
|
112
124
|
}
|
|
113
125
|
export type Transformer<T = any> = (args: {
|
|
114
126
|
/**
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Node, ParseObjectOptions } from "@player-ui/player";
|
|
2
|
-
import type {
|
|
3
|
-
import type { Mappers } from "../types";
|
|
2
|
+
import type { Mappers, MarkdownAsset } from "../types";
|
|
4
3
|
/**
|
|
5
4
|
* Parses markdown content using a provided mappers record.
|
|
6
5
|
*/
|
|
@@ -8,7 +7,7 @@ export declare function parseAssetMarkdownContent({ asset, mappers, parser, }: {
|
|
|
8
7
|
/**
|
|
9
8
|
* Asset to be parsed
|
|
10
9
|
*/
|
|
11
|
-
asset:
|
|
10
|
+
asset: MarkdownAsset;
|
|
12
11
|
/**
|
|
13
12
|
* Mappers record of AST Node to Player Content
|
|
14
13
|
*
|