@pagopa/io-app-design-system 6.0.6 → 6.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/lib/commonjs/components/badge/Badge.js +2 -2
- package/lib/commonjs/components/badge/Badge.js.map +1 -1
- package/lib/commonjs/components/badge/__test__/__snapshots__/badge.test.tsx.snap +2 -2
- package/lib/commonjs/components/buttons/IOButton/__test__/__snapshots__/IOButton.test.tsx.snap +3 -3
- package/lib/commonjs/components/buttons/__test__/__snapshots__/button.test.tsx.snap +6 -6
- package/lib/commonjs/components/index.js +30 -19
- package/lib/commonjs/components/index.js.map +1 -1
- package/lib/commonjs/components/markdown/CodeBlock.js +36 -0
- package/lib/commonjs/components/markdown/CodeBlock.js.map +1 -0
- package/lib/commonjs/components/markdown/IOMarkdown.js +71 -0
- package/lib/commonjs/components/markdown/IOMarkdown.js.map +1 -0
- package/lib/commonjs/components/markdown/IOMarkdownLite.js +22 -0
- package/lib/commonjs/components/markdown/IOMarkdownLite.js.map +1 -0
- package/lib/commonjs/components/markdown/ImageRenderer.js +53 -0
- package/lib/commonjs/components/markdown/ImageRenderer.js.map +1 -0
- package/lib/commonjs/components/markdown/index.js +20 -0
- package/lib/commonjs/components/markdown/index.js.map +1 -0
- package/lib/commonjs/components/markdown/parser.js +253 -0
- package/lib/commonjs/components/markdown/parser.js.map +1 -0
- package/lib/commonjs/components/markdown/rules.js +324 -0
- package/lib/commonjs/components/markdown/rules.js.map +1 -0
- package/lib/commonjs/components/markdown/types.js +6 -0
- package/lib/commonjs/components/markdown/types.js.map +1 -0
- package/lib/commonjs/components/markdown/utils.js +113 -0
- package/lib/commonjs/components/markdown/utils.js.map +1 -0
- package/lib/commonjs/components/modules/__test__/__snapshots__/ModuleNavigationAlt.test.tsx.snap +2 -2
- package/lib/commonjs/components/otpInput/OTPInput.js +41 -26
- package/lib/commonjs/components/otpInput/OTPInput.js.map +1 -1
- package/lib/commonjs/components/tag/Tag.js +2 -1
- package/lib/commonjs/components/tag/Tag.js.map +1 -1
- package/lib/commonjs/components/toast/ToastProvider.js +3 -3
- package/lib/commonjs/components/toast/ToastProvider.js.map +1 -1
- package/lib/commonjs/components/tooltip/Tooltip.js +3 -3
- package/lib/commonjs/components/tooltip/Tooltip.js.map +1 -1
- package/lib/commonjs/components/typography/BodySmall.js +6 -3
- package/lib/commonjs/components/typography/BodySmall.js.map +1 -1
- package/lib/commonjs/components/typography/IOText.js +2 -3
- package/lib/commonjs/components/typography/IOText.js.map +1 -1
- package/lib/commonjs/utils/pipe.js +29 -0
- package/lib/commonjs/utils/pipe.js.map +1 -0
- package/lib/commonjs/utils/throttle.js +19 -0
- package/lib/commonjs/utils/throttle.js.map +1 -0
- package/lib/module/components/badge/Badge.js +2 -2
- package/lib/module/components/badge/Badge.js.map +1 -1
- package/lib/module/components/badge/__test__/__snapshots__/badge.test.tsx.snap +2 -2
- package/lib/module/components/buttons/IOButton/__test__/__snapshots__/IOButton.test.tsx.snap +3 -3
- package/lib/module/components/buttons/__test__/__snapshots__/button.test.tsx.snap +6 -6
- package/lib/module/components/index.js +3 -2
- package/lib/module/components/index.js.map +1 -1
- package/lib/module/components/markdown/CodeBlock.js +31 -0
- package/lib/module/components/markdown/CodeBlock.js.map +1 -0
- package/lib/module/components/markdown/IOMarkdown.js +66 -0
- package/lib/module/components/markdown/IOMarkdown.js.map +1 -0
- package/lib/module/components/markdown/IOMarkdownLite.js +17 -0
- package/lib/module/components/markdown/IOMarkdownLite.js.map +1 -0
- package/lib/module/components/markdown/ImageRenderer.js +48 -0
- package/lib/module/components/markdown/ImageRenderer.js.map +1 -0
- package/lib/module/components/markdown/index.js +5 -0
- package/lib/module/components/markdown/index.js.map +1 -0
- package/lib/module/components/markdown/parser.js +246 -0
- package/lib/module/components/markdown/parser.js.map +1 -0
- package/lib/module/components/markdown/rules.js +319 -0
- package/lib/module/components/markdown/rules.js.map +1 -0
- package/lib/module/components/markdown/types.js +4 -0
- package/lib/module/components/markdown/types.js.map +1 -0
- package/lib/module/components/markdown/utils.js +103 -0
- package/lib/module/components/markdown/utils.js.map +1 -0
- package/lib/module/components/modules/__test__/__snapshots__/ModuleNavigationAlt.test.tsx.snap +2 -2
- package/lib/module/components/otpInput/OTPInput.js +42 -27
- package/lib/module/components/otpInput/OTPInput.js.map +1 -1
- package/lib/module/components/tag/Tag.js +2 -1
- package/lib/module/components/tag/Tag.js.map +1 -1
- package/lib/module/components/toast/ToastProvider.js +1 -1
- package/lib/module/components/toast/ToastProvider.js.map +1 -1
- package/lib/module/components/tooltip/Tooltip.js +3 -3
- package/lib/module/components/tooltip/Tooltip.js.map +1 -1
- package/lib/module/components/typography/BodySmall.js +5 -2
- package/lib/module/components/typography/BodySmall.js.map +1 -1
- package/lib/module/components/typography/IOText.js +3 -4
- package/lib/module/components/typography/IOText.js.map +1 -1
- package/lib/module/utils/pipe.js +25 -0
- package/lib/module/utils/pipe.js.map +1 -0
- package/lib/module/utils/throttle.js +14 -0
- package/lib/module/utils/throttle.js.map +1 -0
- package/lib/typescript/components/badge/Badge.d.ts.map +1 -1
- package/lib/typescript/components/index.d.ts +3 -2
- package/lib/typescript/components/index.d.ts.map +1 -1
- package/lib/typescript/components/markdown/CodeBlock.d.ts +10 -0
- package/lib/typescript/components/markdown/CodeBlock.d.ts.map +1 -0
- package/lib/typescript/components/markdown/IOMarkdown.d.ts +34 -0
- package/lib/typescript/components/markdown/IOMarkdown.d.ts.map +1 -0
- package/lib/typescript/components/markdown/IOMarkdownLite.d.ts +22 -0
- package/lib/typescript/components/markdown/IOMarkdownLite.d.ts.map +1 -0
- package/lib/typescript/components/markdown/ImageRenderer.d.ts +12 -0
- package/lib/typescript/components/markdown/ImageRenderer.d.ts.map +1 -0
- package/lib/typescript/components/markdown/index.d.ts +6 -0
- package/lib/typescript/components/markdown/index.d.ts.map +1 -0
- package/lib/typescript/components/markdown/parser.d.ts +17 -0
- package/lib/typescript/components/markdown/parser.d.ts.map +1 -0
- package/lib/typescript/components/markdown/rules.d.ts +6 -0
- package/lib/typescript/components/markdown/rules.d.ts.map +1 -0
- package/lib/typescript/components/markdown/types.d.ts +41 -0
- package/lib/typescript/components/markdown/types.d.ts.map +1 -0
- package/lib/typescript/components/markdown/utils.d.ts +27 -0
- package/lib/typescript/components/markdown/utils.d.ts.map +1 -0
- package/lib/typescript/components/otpInput/OTPInput.d.ts +4 -0
- package/lib/typescript/components/otpInput/OTPInput.d.ts.map +1 -1
- package/lib/typescript/components/tag/Tag.d.ts.map +1 -1
- package/lib/typescript/components/toast/ToastProvider.d.ts.map +1 -1
- package/lib/typescript/components/tooltip/Tooltip.d.ts.map +1 -1
- package/lib/typescript/components/typography/BodySmall.d.ts +2 -0
- package/lib/typescript/components/typography/BodySmall.d.ts.map +1 -1
- package/lib/typescript/components/typography/IOText.d.ts.map +1 -1
- package/lib/typescript/utils/pipe.d.ts +25 -0
- package/lib/typescript/utils/pipe.d.ts.map +1 -0
- package/lib/typescript/utils/throttle.d.ts +2 -0
- package/lib/typescript/utils/throttle.d.ts.map +1 -0
- package/package.json +3 -3
- package/src/components/badge/Badge.tsx +2 -2
- package/src/components/badge/__test__/__snapshots__/badge.test.tsx.snap +2 -2
- package/src/components/buttons/IOButton/__test__/__snapshots__/IOButton.test.tsx.snap +3 -3
- package/src/components/buttons/__test__/__snapshots__/button.test.tsx.snap +6 -6
- package/src/components/index.tsx +3 -2
- package/src/components/markdown/CodeBlock.tsx +32 -0
- package/src/components/markdown/IOMarkdown.tsx +110 -0
- package/src/components/markdown/IOMarkdownLite.tsx +27 -0
- package/src/components/markdown/ImageRenderer.tsx +52 -0
- package/src/components/markdown/index.ts +7 -0
- package/src/components/markdown/parser.ts +334 -0
- package/src/components/markdown/rules.tsx +366 -0
- package/src/components/markdown/types.ts +81 -0
- package/src/components/markdown/utils.ts +127 -0
- package/src/components/modules/__test__/__snapshots__/ModuleNavigationAlt.test.tsx.snap +2 -2
- package/src/components/otpInput/OTPInput.tsx +57 -32
- package/src/components/tag/Tag.tsx +2 -1
- package/src/components/toast/ToastProvider.tsx +1 -1
- package/src/components/tooltip/Tooltip.tsx +4 -3
- package/src/components/typography/BodySmall.tsx +5 -2
- package/src/components/typography/IOText.tsx +5 -15
- package/src/utils/pipe.ts +55 -0
- package/src/utils/throttle.ts +15 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import MarkdownIt, { Token } from "markdown-it";
|
|
2
|
+
import { pipe } from "../../utils/pipe";
|
|
3
|
+
import { MarkdownNode, MarkdownNodeType } from "./types";
|
|
4
|
+
|
|
5
|
+
/* Two markdown-it instances: lite (no HTML) and full (HTML enabled) */
|
|
6
|
+
const mdLite = MarkdownIt({ html: false, typographer: false, linkify: false });
|
|
7
|
+
const mdFull = MarkdownIt({ html: true, typographer: false, linkify: false });
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a zero-dependency key generator for the markdown AST.
|
|
11
|
+
*
|
|
12
|
+
* These keys are only used as local React render keys, so they do not need
|
|
13
|
+
* cryptographic randomness or an external package: a per-parse incrementing
|
|
14
|
+
* counter is sufficient for our needs.
|
|
15
|
+
*/
|
|
16
|
+
const createKeyFactory = () => {
|
|
17
|
+
// eslint-disable-next-line functional/no-let
|
|
18
|
+
let keyCounter = 0;
|
|
19
|
+
|
|
20
|
+
return (prefix: string) => `md_${prefix}_${keyCounter++}`;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Complete set of all supported node types.
|
|
25
|
+
*/
|
|
26
|
+
const ALL_TYPES = new Set<string>([
|
|
27
|
+
/* lite types */
|
|
28
|
+
"heading1",
|
|
29
|
+
"heading2",
|
|
30
|
+
"heading3",
|
|
31
|
+
"heading4",
|
|
32
|
+
"heading5",
|
|
33
|
+
"heading6",
|
|
34
|
+
"paragraph",
|
|
35
|
+
"text",
|
|
36
|
+
"strong",
|
|
37
|
+
"em",
|
|
38
|
+
"link",
|
|
39
|
+
"softbreak",
|
|
40
|
+
"hardbreak",
|
|
41
|
+
/* full types */
|
|
42
|
+
"bullet_list",
|
|
43
|
+
"ordered_list",
|
|
44
|
+
"list_item",
|
|
45
|
+
"blockquote",
|
|
46
|
+
"image",
|
|
47
|
+
"code_inline",
|
|
48
|
+
"fence",
|
|
49
|
+
"hr",
|
|
50
|
+
"html_block",
|
|
51
|
+
"html_inline"
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The types disabled when using IOMarkdownLite.
|
|
56
|
+
*/
|
|
57
|
+
export const LITE_DISABLED_TYPES: ReadonlyArray<MarkdownNodeType> = [
|
|
58
|
+
"heading1",
|
|
59
|
+
"heading2",
|
|
60
|
+
"heading3",
|
|
61
|
+
"heading4",
|
|
62
|
+
"heading5",
|
|
63
|
+
"heading6",
|
|
64
|
+
"bullet_list",
|
|
65
|
+
"ordered_list",
|
|
66
|
+
"list_item",
|
|
67
|
+
"blockquote",
|
|
68
|
+
"image",
|
|
69
|
+
"code_inline",
|
|
70
|
+
"fence",
|
|
71
|
+
"hr",
|
|
72
|
+
"html_block",
|
|
73
|
+
"html_inline"
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Maps a markdown-it token type to a MarkdownNodeType.
|
|
78
|
+
* Normalizes `*_open` / `*_close` suffixes and heading tags.
|
|
79
|
+
* Returns undefined for unsupported or disabled types.
|
|
80
|
+
*/
|
|
81
|
+
const getNodeType = (
|
|
82
|
+
token: Token,
|
|
83
|
+
enabledTypes: Set<string>
|
|
84
|
+
): MarkdownNodeType | undefined => {
|
|
85
|
+
const cleanedType = token.type.replace(/_open|_close/g, "");
|
|
86
|
+
|
|
87
|
+
const type =
|
|
88
|
+
cleanedType === "heading"
|
|
89
|
+
? `${cleanedType}${token.tag.slice(1)}`
|
|
90
|
+
: cleanedType;
|
|
91
|
+
|
|
92
|
+
return enabledTypes.has(type) ? (type as MarkdownNodeType) : undefined;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Flattens nested inline tokens into the parent token stream.
|
|
97
|
+
* markdown-it wraps inline content in `inline` tokens with children.
|
|
98
|
+
*/
|
|
99
|
+
const flattenInline = (tokens: ReadonlyArray<Token>): ReadonlyArray<Token> =>
|
|
100
|
+
tokens.reduce<ReadonlyArray<Token>>((acc, token) => {
|
|
101
|
+
if (
|
|
102
|
+
token.type === "inline" &&
|
|
103
|
+
token.children &&
|
|
104
|
+
token.children.length > 0
|
|
105
|
+
) {
|
|
106
|
+
return [...acc, ...flattenInline(token.children)];
|
|
107
|
+
}
|
|
108
|
+
return [...acc, token];
|
|
109
|
+
}, []);
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Converts a flat array of tokens into a hierarchical AST,
|
|
113
|
+
* skipping disabled/unsupported token types entirely.
|
|
114
|
+
*/
|
|
115
|
+
const tokensToAST = (
|
|
116
|
+
tokens: ReadonlyArray<Token>,
|
|
117
|
+
enabledTypes: Set<string>,
|
|
118
|
+
getKey: (prefix: string) => string
|
|
119
|
+
): Array<MarkdownNode> => {
|
|
120
|
+
if (!tokens || tokens.length === 0) {
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const parseFrom = (index: number): [Array<MarkdownNode>, number] => {
|
|
125
|
+
if (index >= tokens.length) {
|
|
126
|
+
return [[], index];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const token = tokens[index];
|
|
130
|
+
const nodeType = getNodeType(token, enabledTypes);
|
|
131
|
+
|
|
132
|
+
// Closing token — stop and return to caller
|
|
133
|
+
if (token.nesting === -1) {
|
|
134
|
+
return [[], index + 1];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Unsupported / disabled type — skip it
|
|
138
|
+
if (nodeType === undefined) {
|
|
139
|
+
if (token.nesting === 1) {
|
|
140
|
+
// Opening token: skip ahead to matching close
|
|
141
|
+
const findMatchingClose = (pos: number, depth: number): number =>
|
|
142
|
+
pos >= tokens.length || depth === 0
|
|
143
|
+
? pos
|
|
144
|
+
: findMatchingClose(pos + 1, depth + tokens[pos].nesting);
|
|
145
|
+
return parseFrom(findMatchingClose(index + 1, 1));
|
|
146
|
+
}
|
|
147
|
+
// Self-closing / inline token: skip single token
|
|
148
|
+
return parseFrom(index + 1);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Skip empty text nodes
|
|
152
|
+
if (nodeType === "text" && token.content === "") {
|
|
153
|
+
return parseFrom(index + 1);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const attributes = token.attrs?.reduce<Record<string, string>>(
|
|
157
|
+
(prev, [name, value]) => ({ ...prev, [name]: value }),
|
|
158
|
+
{}
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const node: MarkdownNode = {
|
|
162
|
+
type: nodeType,
|
|
163
|
+
key: getKey(nodeType),
|
|
164
|
+
content: token.content || undefined,
|
|
165
|
+
attributes: attributes || undefined,
|
|
166
|
+
children: [],
|
|
167
|
+
// Preserve ordered flag for lists
|
|
168
|
+
...(nodeType === "ordered_list" ? { ordered: true } : {}),
|
|
169
|
+
...(nodeType === "bullet_list" ? { ordered: false } : {}),
|
|
170
|
+
// Preserve image src and alt via attributes
|
|
171
|
+
...(nodeType === "image"
|
|
172
|
+
? {
|
|
173
|
+
attributes: {
|
|
174
|
+
...attributes,
|
|
175
|
+
src: token.attrGet?.("src") ?? attributes?.src ?? "",
|
|
176
|
+
alt: token.content ?? ""
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
: {})
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
if (token.nesting === 1) {
|
|
183
|
+
// Opening token — parse children
|
|
184
|
+
const [childNodes, nextIndex] = parseFrom(index + 1);
|
|
185
|
+
|
|
186
|
+
const nodeWithChildren: MarkdownNode = {
|
|
187
|
+
...node,
|
|
188
|
+
children: childNodes
|
|
189
|
+
};
|
|
190
|
+
const [restNodes, finalIndex] = parseFrom(nextIndex);
|
|
191
|
+
return [[nodeWithChildren, ...restNodes], finalIndex];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Self-closing / inline token (nesting === 0)
|
|
195
|
+
const [restNodes, finalIndex] = parseFrom(index + 1);
|
|
196
|
+
return [[node, ...restNodes], finalIndex];
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const [nodes] = parseFrom(0);
|
|
200
|
+
return nodes;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Lifts image nodes out of paragraph containers so they become
|
|
205
|
+
* top-level siblings. markdown-it always wraps images inside
|
|
206
|
+
* paragraphs; this post-processing step ensures the existing
|
|
207
|
+
* `imageRule` is actually invoked during rendering.
|
|
208
|
+
*
|
|
209
|
+
* - Paragraph with **only** image children → replaced by the images.
|
|
210
|
+
* - Paragraph with a **mix** of text and images → split into
|
|
211
|
+
* alternating paragraph (text run) and standalone image nodes.
|
|
212
|
+
* - Paragraphs without images → unchanged.
|
|
213
|
+
*/
|
|
214
|
+
const liftImages = (
|
|
215
|
+
nodes: ReadonlyArray<MarkdownNode>,
|
|
216
|
+
getKey: (prefix: string) => string
|
|
217
|
+
): Array<MarkdownNode> =>
|
|
218
|
+
nodes.flatMap(node => {
|
|
219
|
+
if (node.type !== "paragraph") {
|
|
220
|
+
return [node];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const hasImage = node.children.some(c => c.type === "image");
|
|
224
|
+
if (!hasImage) {
|
|
225
|
+
return [node];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Every child is an image → lift them all out
|
|
229
|
+
const allImages = node.children.every(c => c.type === "image");
|
|
230
|
+
if (allImages) {
|
|
231
|
+
// Return images as top-level nodes (they keep their own keys)
|
|
232
|
+
return [...node.children];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Mixed content: split children into text runs and standalone images.
|
|
236
|
+
// A single reduce accumulates finished nodes and the current text run;
|
|
237
|
+
// a trailing text run is flushed after the fold.
|
|
238
|
+
type Acc = {
|
|
239
|
+
readonly result: ReadonlyArray<MarkdownNode>;
|
|
240
|
+
readonly textRun: ReadonlyArray<MarkdownNode>;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const wrapTextRun = (run: ReadonlyArray<MarkdownNode>): MarkdownNode => ({
|
|
244
|
+
...node,
|
|
245
|
+
key: getKey("paragraph"),
|
|
246
|
+
children: run
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
const { result, textRun } = node.children.reduce<Acc>(
|
|
250
|
+
(acc, child) =>
|
|
251
|
+
child.type === "image"
|
|
252
|
+
? {
|
|
253
|
+
result: [
|
|
254
|
+
...acc.result,
|
|
255
|
+
...(acc.textRun.length > 0 ? [wrapTextRun(acc.textRun)] : []),
|
|
256
|
+
child
|
|
257
|
+
],
|
|
258
|
+
textRun: []
|
|
259
|
+
}
|
|
260
|
+
: { ...acc, textRun: [...acc.textRun, child] },
|
|
261
|
+
{ result: [], textRun: [] }
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
return textRun.length > 0 ? [...result, wrapTextRun(textRun)] : [...result];
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const annotateListDepth = (
|
|
268
|
+
nodes: ReadonlyArray<MarkdownNode>,
|
|
269
|
+
parentListDepth = 0
|
|
270
|
+
): Array<MarkdownNode> =>
|
|
271
|
+
nodes.map(node => {
|
|
272
|
+
const listDepth = parentListDepth;
|
|
273
|
+
const childListDepth =
|
|
274
|
+
node.type === "bullet_list" || node.type === "ordered_list"
|
|
275
|
+
? parentListDepth + 1
|
|
276
|
+
: parentListDepth;
|
|
277
|
+
|
|
278
|
+
return {
|
|
279
|
+
...node,
|
|
280
|
+
listDepth,
|
|
281
|
+
children: annotateListDepth(node.children, childListDepth)
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Computes the enabled types set from the full set minus disabled types.
|
|
287
|
+
*/
|
|
288
|
+
const getEnabledTypes = (
|
|
289
|
+
disabledTypes?: ReadonlyArray<string>
|
|
290
|
+
): Set<string> => {
|
|
291
|
+
if (!disabledTypes || disabledTypes.length === 0) {
|
|
292
|
+
return ALL_TYPES;
|
|
293
|
+
}
|
|
294
|
+
const disabled = new Set(disabledTypes);
|
|
295
|
+
return new Set([...ALL_TYPES].filter(t => !disabled.has(t)));
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Parses a markdown source string into an AST.
|
|
300
|
+
* @param source The markdown string.
|
|
301
|
+
* @param disabledTypes Node types to exclude from parsing.
|
|
302
|
+
* @returns Array of MarkdownNode.
|
|
303
|
+
*/
|
|
304
|
+
export const parse = (
|
|
305
|
+
source: string,
|
|
306
|
+
disabledTypes?: ReadonlyArray<MarkdownNodeType>
|
|
307
|
+
): Array<MarkdownNode> => {
|
|
308
|
+
const enabledTypes = getEnabledTypes(disabledTypes);
|
|
309
|
+
const needsHtml =
|
|
310
|
+
enabledTypes.has("html_block") || enabledTypes.has("html_inline");
|
|
311
|
+
const md = needsHtml ? mdFull : mdLite;
|
|
312
|
+
const getKey = createKeyFactory();
|
|
313
|
+
|
|
314
|
+
return pipe(
|
|
315
|
+
// 1. Tokenize the markdown source using markdown-it
|
|
316
|
+
md.parse(source, {}),
|
|
317
|
+
// 2. Unwrap nested inline tokens into a flat token stream
|
|
318
|
+
flattenInline,
|
|
319
|
+
// 3. Convert the flat token stream into a hierarchical AST
|
|
320
|
+
tokens => tokensToAST(tokens, enabledTypes, getKey),
|
|
321
|
+
// 4. Hoist image nodes out of paragraph wrappers so imageRule is invoked
|
|
322
|
+
nodes => liftImages(nodes, getKey),
|
|
323
|
+
// 5. Drop empty paragraphs left behind by disabled/lifted node types
|
|
324
|
+
nodes => nodes.filter(n => n.type !== "paragraph" || n.children.length > 0),
|
|
325
|
+
// 6. Annotate nodes with their list nesting depth for rendering
|
|
326
|
+
annotateListDepth
|
|
327
|
+
);
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Parses markdown with the lite subset of rules only.
|
|
332
|
+
*/
|
|
333
|
+
export const parseLite = (source: string): Array<MarkdownNode> =>
|
|
334
|
+
parse(source, LITE_DISABLED_TYPES);
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import React, { Fragment } from "react";
|
|
2
|
+
import { View } from "react-native";
|
|
3
|
+
import { Banner } from "../banner";
|
|
4
|
+
import { Divider, HSpacer, VSpacer } from "../layout";
|
|
5
|
+
import { Body } from "../typography/Body";
|
|
6
|
+
import { BodyMonospace } from "../typography/BodyMonospace";
|
|
7
|
+
import { H1 } from "../typography/H1";
|
|
8
|
+
import { H2 } from "../typography/H2";
|
|
9
|
+
import { H3 } from "../typography/H3";
|
|
10
|
+
import { H4 } from "../typography/H4";
|
|
11
|
+
import { H5 } from "../typography/H5";
|
|
12
|
+
import { H6 } from "../typography/H6";
|
|
13
|
+
import { IOText } from "../typography/IOText";
|
|
14
|
+
import { CodeBlock } from "./CodeBlock";
|
|
15
|
+
import { ImageRenderer } from "./ImageRenderer";
|
|
16
|
+
import type {
|
|
17
|
+
MarkdownNode,
|
|
18
|
+
MarkdownNodeType,
|
|
19
|
+
RenderContext,
|
|
20
|
+
RenderRule
|
|
21
|
+
} from "./types";
|
|
22
|
+
import {
|
|
23
|
+
collectRawText,
|
|
24
|
+
extractPictogramName,
|
|
25
|
+
getOrderedListMarker,
|
|
26
|
+
getUnorderedListBullet,
|
|
27
|
+
isBrTag,
|
|
28
|
+
stripPictogramPrefix
|
|
29
|
+
} from "./utils";
|
|
30
|
+
|
|
31
|
+
/* ─── Inline flattening (shared between heading and paragraph rendering) ─── */
|
|
32
|
+
|
|
33
|
+
type InlineStyle = {
|
|
34
|
+
bold: boolean;
|
|
35
|
+
italic: boolean;
|
|
36
|
+
link?: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type StyledSegment = {
|
|
40
|
+
key: string;
|
|
41
|
+
text: string;
|
|
42
|
+
style: InlineStyle;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Recursively walks inline AST nodes and produces flat styled segments
|
|
47
|
+
* with accumulated bold/italic/link state.
|
|
48
|
+
*/
|
|
49
|
+
const flattenInlineNodes = (
|
|
50
|
+
nodes: ReadonlyArray<MarkdownNode>,
|
|
51
|
+
inherited: InlineStyle
|
|
52
|
+
): Array<StyledSegment> =>
|
|
53
|
+
nodes.reduce<Array<StyledSegment>>((acc, node) => {
|
|
54
|
+
switch (node.type) {
|
|
55
|
+
case "text":
|
|
56
|
+
case "code_inline":
|
|
57
|
+
return [
|
|
58
|
+
...acc,
|
|
59
|
+
{ key: node.key, text: node.content ?? "", style: inherited }
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
case "softbreak":
|
|
63
|
+
case "hardbreak":
|
|
64
|
+
return [...acc, { key: node.key, text: "\n", style: inherited }];
|
|
65
|
+
|
|
66
|
+
case "strong":
|
|
67
|
+
return [
|
|
68
|
+
...acc,
|
|
69
|
+
...flattenInlineNodes(node.children, {
|
|
70
|
+
...inherited,
|
|
71
|
+
bold: true
|
|
72
|
+
})
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
case "em":
|
|
76
|
+
return [
|
|
77
|
+
...acc,
|
|
78
|
+
...flattenInlineNodes(node.children, {
|
|
79
|
+
...inherited,
|
|
80
|
+
italic: true
|
|
81
|
+
})
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
case "link": {
|
|
85
|
+
const href = node.attributes?.href;
|
|
86
|
+
return [
|
|
87
|
+
...acc,
|
|
88
|
+
...flattenInlineNodes(node.children, {
|
|
89
|
+
...inherited,
|
|
90
|
+
link: href
|
|
91
|
+
})
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
default:
|
|
96
|
+
return acc;
|
|
97
|
+
}
|
|
98
|
+
}, []);
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Renders a single styled segment as either a raw string
|
|
102
|
+
* or an IOText element with the appropriate props.
|
|
103
|
+
*/
|
|
104
|
+
const renderSegment = (
|
|
105
|
+
segment: StyledSegment,
|
|
106
|
+
context: RenderContext,
|
|
107
|
+
isCode?: boolean
|
|
108
|
+
): React.ReactNode => {
|
|
109
|
+
if (isCode) {
|
|
110
|
+
return <BodyMonospace key={segment.key}>{segment.text}</BodyMonospace>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const { bold, italic, link } = segment.style;
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<IOText
|
|
117
|
+
key={segment.key}
|
|
118
|
+
{...(bold ? { weight: "Semibold" } : {})}
|
|
119
|
+
{...(italic ? { fontStyle: "italic" } : {})}
|
|
120
|
+
{...(link
|
|
121
|
+
? {
|
|
122
|
+
color: context.linkColor,
|
|
123
|
+
onPress: () => context.onLinkPress?.(link),
|
|
124
|
+
accessibilityRole: "link" as const,
|
|
125
|
+
textStyle: { textDecorationLine: "underline" as const }
|
|
126
|
+
}
|
|
127
|
+
: {})}
|
|
128
|
+
size={context.fontSize}
|
|
129
|
+
lineHeight={context.lineHeight}
|
|
130
|
+
>
|
|
131
|
+
{segment.text}
|
|
132
|
+
</IOText>
|
|
133
|
+
);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/* ─── Heading component map ─── */
|
|
137
|
+
|
|
138
|
+
type HeadingComponent = typeof H1;
|
|
139
|
+
|
|
140
|
+
const headingComponentMap: Record<string, HeadingComponent> = {
|
|
141
|
+
heading1: H1,
|
|
142
|
+
heading2: H2,
|
|
143
|
+
heading3: H3,
|
|
144
|
+
heading4: H4,
|
|
145
|
+
heading5: H5,
|
|
146
|
+
heading6: H6
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/* ─── Block rendering helpers ─── */
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Renders a paragraph block by flattening inline children
|
|
153
|
+
* into styled segments.
|
|
154
|
+
*/
|
|
155
|
+
const renderParagraph = (
|
|
156
|
+
node: MarkdownNode,
|
|
157
|
+
context: RenderContext
|
|
158
|
+
): React.ReactNode => {
|
|
159
|
+
const segments = flattenInlineNodes(node.children, {
|
|
160
|
+
bold: false,
|
|
161
|
+
italic: false
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<Body key={node.key} style={{ textAlign: context.textAlign }}>
|
|
166
|
+
{segments.map(seg => {
|
|
167
|
+
const matchingNode = node.children.find(c => c.key === seg.key);
|
|
168
|
+
const isCode = matchingNode?.type === "code_inline";
|
|
169
|
+
return renderSegment(seg, context, isCode);
|
|
170
|
+
})}
|
|
171
|
+
</Body>
|
|
172
|
+
);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Creates a render rule for a heading level using the corresponding
|
|
177
|
+
* DS heading component (H1-H6). This ensures headings inherit
|
|
178
|
+
* dynamicTypeRamp, uppercase/letterSpacing (H5), legacy typeface (H6),
|
|
179
|
+
* and theme colors automatically.
|
|
180
|
+
*/
|
|
181
|
+
const makeHeadingRule =
|
|
182
|
+
(Heading: HeadingComponent): RenderRule =>
|
|
183
|
+
(node, _renderChildren, context) => {
|
|
184
|
+
const segments = flattenInlineNodes(node.children, {
|
|
185
|
+
bold: false,
|
|
186
|
+
italic: false
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<View key={node.key} accessibilityRole="header">
|
|
191
|
+
<Heading style={{ textAlign: context.textAlign }}>
|
|
192
|
+
{segments.map(seg => seg.text)}
|
|
193
|
+
</Heading>
|
|
194
|
+
</View>
|
|
195
|
+
);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/* ─── Default render rules ─── */
|
|
199
|
+
|
|
200
|
+
const paragraphRule: RenderRule = (node, _renderChildren, context) =>
|
|
201
|
+
renderParagraph(node, context);
|
|
202
|
+
|
|
203
|
+
const textRule: RenderRule = node => (
|
|
204
|
+
<Fragment key={node.key}>{node.content ?? ""}</Fragment>
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const strongRule: RenderRule = (node, renderChildren) => (
|
|
208
|
+
<IOText key={node.key} weight="Semibold">
|
|
209
|
+
{renderChildren(node.children)}
|
|
210
|
+
</IOText>
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
const emRule: RenderRule = (node, renderChildren) => (
|
|
214
|
+
<IOText key={node.key} fontStyle="italic">
|
|
215
|
+
{renderChildren(node.children)}
|
|
216
|
+
</IOText>
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
const linkRule: RenderRule = (node, renderChildren, context) => {
|
|
220
|
+
const href = node.attributes?.href;
|
|
221
|
+
return (
|
|
222
|
+
<IOText
|
|
223
|
+
key={node.key}
|
|
224
|
+
color={context.linkColor}
|
|
225
|
+
onPress={href ? () => context.onLinkPress?.(href) : undefined}
|
|
226
|
+
accessibilityRole="link"
|
|
227
|
+
textStyle={{ textDecorationLine: "underline" }}
|
|
228
|
+
>
|
|
229
|
+
{renderChildren(node.children)}
|
|
230
|
+
</IOText>
|
|
231
|
+
);
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const softbreakRule: RenderRule = node => (
|
|
235
|
+
<Fragment key={node.key}>{"\n"}</Fragment>
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
const hardbreakRule: RenderRule = node => (
|
|
239
|
+
<Fragment key={node.key}>{"\n"}</Fragment>
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
const bulletListRule: RenderRule = (node, renderChildren) => (
|
|
243
|
+
<View
|
|
244
|
+
key={node.key}
|
|
245
|
+
accessible={true}
|
|
246
|
+
accessibilityRole="list"
|
|
247
|
+
style={{ paddingLeft: 12 }}
|
|
248
|
+
>
|
|
249
|
+
<VSpacer size={8} />
|
|
250
|
+
{node.children.map(child => (
|
|
251
|
+
<View key={child.key} style={{ flexDirection: "row" }}>
|
|
252
|
+
<Body>{getUnorderedListBullet(node.listDepth ?? 0)}</Body>
|
|
253
|
+
<HSpacer size={8} />
|
|
254
|
+
<View style={{ flex: 1, flexShrink: 1 }}>
|
|
255
|
+
{renderChildren(child.children)}
|
|
256
|
+
</View>
|
|
257
|
+
</View>
|
|
258
|
+
))}
|
|
259
|
+
<VSpacer size={8} />
|
|
260
|
+
</View>
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const orderedListRule: RenderRule = (node, renderChildren) => (
|
|
264
|
+
<View
|
|
265
|
+
key={node.key}
|
|
266
|
+
accessible={true}
|
|
267
|
+
accessibilityRole="list"
|
|
268
|
+
style={{ paddingLeft: 12 }}
|
|
269
|
+
>
|
|
270
|
+
<VSpacer size={8} />
|
|
271
|
+
{node.children.map((child, i) => (
|
|
272
|
+
<View key={child.key} style={{ flexDirection: "row" }}>
|
|
273
|
+
<Body>{getOrderedListMarker(i + 1, node.listDepth ?? 0)}</Body>
|
|
274
|
+
<HSpacer size={8} />
|
|
275
|
+
<View style={{ flex: 1, flexShrink: 1 }}>
|
|
276
|
+
{renderChildren(child.children)}
|
|
277
|
+
</View>
|
|
278
|
+
</View>
|
|
279
|
+
))}
|
|
280
|
+
<VSpacer size={8} />
|
|
281
|
+
</View>
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
const listItemRule: RenderRule = (node, renderChildren) => (
|
|
285
|
+
<View key={node.key} style={{ flex: 1, flexShrink: 1 }}>
|
|
286
|
+
{renderChildren(node.children)}
|
|
287
|
+
</View>
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
const blockquoteRule: RenderRule = node => {
|
|
291
|
+
const allText = collectRawText(node);
|
|
292
|
+
const pictogramName = extractPictogramName(allText);
|
|
293
|
+
|
|
294
|
+
// Find the first heading child for the banner title
|
|
295
|
+
const headingNode = node.children.find(c => c.type.startsWith("heading"));
|
|
296
|
+
const title = headingNode ? collectRawText(headingNode).trim() : undefined;
|
|
297
|
+
|
|
298
|
+
// Collect content from paragraph children, stripping the pictogram pattern
|
|
299
|
+
const content = node.children
|
|
300
|
+
.filter(c => c.type === "paragraph")
|
|
301
|
+
.map(c => stripPictogramPrefix(collectRawText(c)).trim())
|
|
302
|
+
.filter(Boolean)
|
|
303
|
+
.join("\n");
|
|
304
|
+
|
|
305
|
+
return (
|
|
306
|
+
<Banner
|
|
307
|
+
key={node.key}
|
|
308
|
+
pictogramName={pictogramName}
|
|
309
|
+
color="neutral"
|
|
310
|
+
title={title}
|
|
311
|
+
content={content || undefined}
|
|
312
|
+
/>
|
|
313
|
+
);
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const imageRule: RenderRule = node => (
|
|
317
|
+
<View key={node.key} style={{ marginVertical: 16 }}>
|
|
318
|
+
<ImageRenderer node={node} />
|
|
319
|
+
</View>
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
const codeInlineRule: RenderRule = node => (
|
|
323
|
+
<BodyMonospace key={node.key}>{node.content ?? ""}</BodyMonospace>
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
const fenceRule: RenderRule = node => (
|
|
327
|
+
<CodeBlock key={node.key} content={(node.content ?? "").trimEnd()} />
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
const hrRule: RenderRule = node => <Divider key={node.key} />;
|
|
331
|
+
|
|
332
|
+
const htmlRule: RenderRule = node => {
|
|
333
|
+
if (node.content && isBrTag(node.content)) {
|
|
334
|
+
return <Fragment key={node.key}>{"\n"}</Fragment>;
|
|
335
|
+
}
|
|
336
|
+
return null;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* The complete set of default render rules for all supported node types.
|
|
341
|
+
*/
|
|
342
|
+
export const DEFAULT_RULES: Record<MarkdownNodeType, RenderRule> = {
|
|
343
|
+
heading1: makeHeadingRule(headingComponentMap.heading1),
|
|
344
|
+
heading2: makeHeadingRule(headingComponentMap.heading2),
|
|
345
|
+
heading3: makeHeadingRule(headingComponentMap.heading3),
|
|
346
|
+
heading4: makeHeadingRule(headingComponentMap.heading4),
|
|
347
|
+
heading5: makeHeadingRule(headingComponentMap.heading5),
|
|
348
|
+
heading6: makeHeadingRule(headingComponentMap.heading6),
|
|
349
|
+
paragraph: paragraphRule,
|
|
350
|
+
text: textRule,
|
|
351
|
+
strong: strongRule,
|
|
352
|
+
em: emRule,
|
|
353
|
+
link: linkRule,
|
|
354
|
+
softbreak: softbreakRule,
|
|
355
|
+
hardbreak: hardbreakRule,
|
|
356
|
+
bullet_list: bulletListRule,
|
|
357
|
+
ordered_list: orderedListRule,
|
|
358
|
+
list_item: listItemRule,
|
|
359
|
+
blockquote: blockquoteRule,
|
|
360
|
+
image: imageRule,
|
|
361
|
+
code_inline: codeInlineRule,
|
|
362
|
+
fence: fenceRule,
|
|
363
|
+
hr: hrRule,
|
|
364
|
+
html_block: htmlRule,
|
|
365
|
+
html_inline: htmlRule
|
|
366
|
+
};
|