@gild-ui/vue-markdown 0.0.0-stage → 0.1.0-alpha.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.
@@ -0,0 +1,530 @@
1
+ import { a as rootProps, i as declare, t as CodeBlock } from "./CodeBlock-C-5omHoJ.mjs";
2
+ import { Comment, defineComponent, h, isVNode } from "vue";
3
+ import { StyleSheet, styled } from "@gild-ui/vue";
4
+ import { View } from "@gild-ui/vue/View";
5
+ import { useTheme } from "@gild-ui/vue/useTheme";
6
+ import { background, border, layout, position, space, system } from "@soroush.tech/styled-system";
7
+ import { createShouldForwardProp, props } from "@soroush.tech/styled-system/should-forward-prop";
8
+ import { themeDefault } from "@gild-ui/core/theme";
9
+ import rehypeHighlight from "rehype-highlight";
10
+ import rehypeSlug from "rehype-slug";
11
+ import remarkGfm from "remark-gfm";
12
+ import remarkParse from "remark-parse";
13
+ import remarkRehype from "remark-rehype";
14
+ import { unified } from "unified";
15
+ import { find, html } from "property-information";
16
+ import { Typography } from "@gild-ui/vue/Typography";
17
+ // ../react-markdown/src/tree/parseMarkdown.ts
18
+ const processor = unified().use(remarkParse).use(remarkGfm).use(remarkRehype, { allowDangerousHtml: true }).use(rehypeSlug).use(rehypeHighlight);
19
+ function rawToText(parent) {
20
+ for (const [index, child] of parent.children.entries()) if (child.type === "raw") parent.children[index] = {
21
+ type: "text",
22
+ value: child.value
23
+ };
24
+ else if (child.type === "element") rawToText(child);
25
+ }
26
+ /** The tree of a markdown string. */
27
+ function parseMarkdown(source) {
28
+ const tree = processor.runSync(processor.parse(source), source);
29
+ rawToText(tree);
30
+ return tree;
31
+ }
32
+ // ../react-markdown/src/tree/elements.ts
33
+ /** The props of the `Typography` of each element that is text. */
34
+ const TEXT = {
35
+ h1: {
36
+ mt: 2,
37
+ variant: "h1",
38
+ color: "primary",
39
+ gutterBottom: true
40
+ },
41
+ h2: {
42
+ mt: 2,
43
+ variant: "h2",
44
+ color: "primary",
45
+ gutterBottom: true
46
+ },
47
+ h3: {
48
+ mt: 2,
49
+ variant: "h3",
50
+ color: "primary",
51
+ gutterBottom: true
52
+ },
53
+ h4: {
54
+ mt: 2,
55
+ variant: "h4",
56
+ gutterBottom: true
57
+ },
58
+ h5: {
59
+ mt: 2,
60
+ variant: "h5",
61
+ gutterBottom: true
62
+ },
63
+ h6: {
64
+ mt: 2,
65
+ variant: "h6",
66
+ gutterBottom: true
67
+ },
68
+ p: {
69
+ variant: "body1",
70
+ color: "secondary",
71
+ mb: 2,
72
+ lineHeight: "relaxed"
73
+ },
74
+ strong: {
75
+ variant: "inherit",
76
+ fontWeight: "extraBold"
77
+ },
78
+ em: {
79
+ variant: "inherit",
80
+ fontStyle: "italic"
81
+ },
82
+ li: {
83
+ color: "secondary",
84
+ lineHeight: "base",
85
+ variant: "body1",
86
+ gutterBottom: true
87
+ }
88
+ };
89
+ /** The props of the `View` of each element that is a box. */
90
+ const BOX = {
91
+ ul: {
92
+ pl: 3,
93
+ mb: 2
94
+ },
95
+ ol: {
96
+ pl: 3,
97
+ mb: 2
98
+ },
99
+ blockquote: {
100
+ pl: 3,
101
+ py: 1,
102
+ my: 2
103
+ },
104
+ hr: {
105
+ my: 2,
106
+ height: "1px",
107
+ bg: "grid",
108
+ width: "100%",
109
+ border: "none"
110
+ },
111
+ table: { my: 2 },
112
+ tableBody: {
113
+ bg: "terminal",
114
+ borderRadius: "md"
115
+ },
116
+ thead: {
117
+ bg: "grid",
118
+ color: "primary"
119
+ }
120
+ };
121
+ /** The props of the `Typography` of a code element: a block takes none of a chip's. */
122
+ const CODE = {
123
+ block: {
124
+ color: "initial",
125
+ variant: "inherit",
126
+ fontFamily: "mono",
127
+ bg: "transparent",
128
+ px: 0
129
+ },
130
+ inline: {
131
+ color: "initial",
132
+ variant: "inherit",
133
+ fontFamily: "mono",
134
+ bg: "paper",
135
+ px: 1
136
+ }
137
+ };
138
+ const isElement = (node) => node.type === "element";
139
+ /** The class names of an element. */
140
+ const classesOf = (node) => Array.isArray(node.properties.className) ? node.properties.className.map(String) : [];
141
+ /** The elements among the children of an element. */
142
+ const elementsOf = (node, tagName) => node.children.filter(isElement).filter((child) => tagName === void 0 || child.tagName === tagName);
143
+ const TABLE_PARTS = /* @__PURE__ */ new Set([
144
+ "table",
145
+ "thead",
146
+ "tbody",
147
+ "tfoot",
148
+ "tr"
149
+ ]);
150
+ /** What an element holds, as a renderer draws it: a part of a table holds its parts only. */
151
+ const childrenOf = (node) => TABLE_PARTS.has(node.tagName) ? elementsOf(node) : node.children;
152
+ /** The language a code element was fenced with (` ```ts ` is `ts`), if it was. */
153
+ const languageOf = (node) => classesOf(node).find((name) => name.startsWith("language-"))?.slice(9);
154
+ /** Whether a list item is a task of a task list (`- [ ] todo`). */
155
+ const isTask = (node) => classesOf(node).includes("task-list-item");
156
+ /** The alignment of a table cell, as its column was written (`:--`, `:-:`, `--:`). */
157
+ function alignOf(cell) {
158
+ const { align } = cell.properties;
159
+ return align === "left" || align === "center" || align === "right" ? align : void 0;
160
+ }
161
+ const SAFE_PROTOCOL = /^(?:https?|ircs?|mailto|xmpp)$/i;
162
+ /** A URL of the document, or `undefined` for one that is not safe (`javascript:`...). A relative one is safe. */
163
+ function safeUrl(value) {
164
+ if (typeof value !== "string") return void 0;
165
+ const colon = value.indexOf(":");
166
+ const before = (mark) => {
167
+ const at = value.indexOf(mark);
168
+ return at !== -1 && colon > at;
169
+ };
170
+ return colon === -1 || before("/") || before("?") || before("#") || SAFE_PROTOCOL.test(value.slice(0, colon)) ? value : void 0;
171
+ }
172
+ // src/Preview/parts.ts
173
+ const underlineStyles = {
174
+ always: { textDecoration: "underline" },
175
+ none: { textDecoration: "none" },
176
+ hover: {
177
+ textDecoration: "none",
178
+ position: "relative",
179
+ "&::after": {
180
+ content: "\"\"",
181
+ position: "absolute",
182
+ bottom: 0,
183
+ left: 0,
184
+ width: "100%",
185
+ height: "1px",
186
+ background: "currentColor",
187
+ transform: "scaleX(0)",
188
+ transformOrigin: "right",
189
+ transition: "transform 0.2s ease"
190
+ },
191
+ "&:hover::after": {
192
+ transform: "scaleX(1)",
193
+ transformOrigin: "left"
194
+ }
195
+ }
196
+ };
197
+ const Link = styled(Typography, {
198
+ name: "Link",
199
+ label: "Link"
200
+ })(({ underline = "always" }) => underlineStyles[underline], ({ theme }) => ({
201
+ "&:hover": { color: theme.palette[themeDefault(theme, "color", "primary")].main },
202
+ "&:focus-visible": {
203
+ outline: `2px solid ${theme.palette[themeDefault(theme, "color", "primary")].main}`,
204
+ outlineOffset: "4px",
205
+ borderRadius: theme.radii.sm
206
+ }
207
+ }));
208
+ const Quote = styled(View, {
209
+ name: "Quote",
210
+ label: "Quote"
211
+ })(({ theme }) => ({ borderLeft: `2px solid ${theme.border.primary}` }));
212
+ const imageSystem = system({
213
+ objectFit: { property: "objectFit" },
214
+ objectPosition: { property: "objectPosition" },
215
+ borderRadius: {
216
+ property: "borderRadius",
217
+ scale: "radii"
218
+ }
219
+ });
220
+ const Image = styled("img", {
221
+ name: "Image",
222
+ label: "image",
223
+ shouldForwardProp: createShouldForwardProp([
224
+ ...props,
225
+ "fallback",
226
+ "objectFit",
227
+ "objectPosition",
228
+ "borderRadius"
229
+ ]),
230
+ systemProps: [
231
+ layout,
232
+ space,
233
+ background,
234
+ position,
235
+ imageSystem
236
+ ]
237
+ })();
238
+ const colorSystem = system({
239
+ color: {
240
+ property: "color",
241
+ scale: "text"
242
+ },
243
+ bg: {
244
+ property: "backgroundColor",
245
+ scale: "background"
246
+ },
247
+ borderColor: {
248
+ property: "borderColor",
249
+ scale: "border"
250
+ }
251
+ });
252
+ const TableContainer = styled(View, {
253
+ name: "TableContainer",
254
+ label: "TableContainer"
255
+ })({
256
+ width: "100%",
257
+ overflowX: "auto"
258
+ });
259
+ const Table = styled("table", {
260
+ name: "Table",
261
+ label: "Table",
262
+ shouldForwardProp: createShouldForwardProp([...props]),
263
+ systemProps: [
264
+ space,
265
+ layout,
266
+ colorSystem,
267
+ border
268
+ ]
269
+ })({
270
+ width: "100%",
271
+ borderCollapse: "separate",
272
+ borderSpacing: 0,
273
+ overflow: "hidden"
274
+ });
275
+ const TableHead = styled("thead", {
276
+ name: "TableHead",
277
+ label: "TableHead",
278
+ shouldForwardProp: createShouldForwardProp([...props]),
279
+ systemProps: [colorSystem]
280
+ })();
281
+ const TableRow = styled("tr", {
282
+ name: "TableRow",
283
+ label: "TableRow",
284
+ shouldForwardProp: createShouldForwardProp([...props]),
285
+ systemProps: [
286
+ space,
287
+ border,
288
+ colorSystem
289
+ ]
290
+ })(({ theme }) => ({ "&:hover": {
291
+ backgroundColor: theme.palette[themeDefault(theme, "color", "primary")].light,
292
+ color: theme.palette[themeDefault(theme, "color", "primary")].contrastText
293
+ } }));
294
+ const TableCell = styled("td", {
295
+ name: "TableCell",
296
+ label: "TableCell",
297
+ shouldForwardProp: createShouldForwardProp([...props, "align"]),
298
+ systemProps: [
299
+ space,
300
+ colorSystem,
301
+ border
302
+ ]
303
+ })(({ theme, align = "inherit" }) => {
304
+ const size = theme.sizes[themeDefault(theme, "size", "md")];
305
+ return {
306
+ padding: `${theme.space[size.paddingTop]} ${theme.space[size.paddingRight]} ${theme.space[size.paddingBottom]} ${theme.space[size.paddingLeft]}`,
307
+ fontSize: theme.fontSizes[size.fontSize],
308
+ textAlign: align,
309
+ borderBottom: `1px solid ${theme.border.light}`
310
+ };
311
+ });
312
+ const Checkbox = styled("label", {
313
+ name: "Checkbox",
314
+ label: "Checkbox",
315
+ shouldForwardProp: createShouldForwardProp([...props]),
316
+ systemProps: [space]
317
+ })(({ theme }) => ({
318
+ display: "inline-flex",
319
+ alignItems: "center",
320
+ verticalAlign: "middle",
321
+ color: theme.palette.primary.main,
322
+ fontSize: "1.25em"
323
+ }));
324
+ const CheckboxInput = styled("input", {
325
+ name: "Checkbox",
326
+ slot: "input",
327
+ label: "CheckboxInput"
328
+ })({
329
+ position: "absolute",
330
+ opacity: 0,
331
+ width: 0,
332
+ height: 0
333
+ });
334
+ const CheckboxIcon = styled("span", {
335
+ name: "Checkbox",
336
+ slot: "icon",
337
+ label: "CheckboxIcon"
338
+ })({
339
+ display: "inline-flex",
340
+ "& .cb-checked, & .cb-indeterminate": { display: "none" },
341
+ "input:checked + &": {
342
+ "& .cb-unchecked": { display: "none" },
343
+ "& .cb-checked": { display: "inline-flex" }
344
+ }
345
+ });
346
+ const mark = (className, svg, ...shapes) => h("span", { class: className }, h("svg", {
347
+ viewBox: "0 0 18 18",
348
+ width: "1em",
349
+ height: "1em",
350
+ ...svg,
351
+ "aria-hidden": "true"
352
+ }, shapes));
353
+ /** The read-only checkbox of a task of a task list. */
354
+ const checkbox = (checked, attributes) => h(Checkbox, { ...attributes }, () => [h(CheckboxInput, {
355
+ type: "checkbox",
356
+ disabled: true,
357
+ "aria-label": "Task item",
358
+ checked
359
+ }), h(CheckboxIcon, null, () => [
360
+ mark("cb-unchecked", { fill: "none" }, h("rect", {
361
+ x: "1.5",
362
+ y: "1.5",
363
+ width: "15",
364
+ height: "15",
365
+ rx: "2.5",
366
+ stroke: "currentColor",
367
+ "stroke-width": "1.5"
368
+ })),
369
+ mark("cb-checked", {}, h("rect", {
370
+ width: "18",
371
+ height: "18",
372
+ rx: "3",
373
+ fill: "currentColor"
374
+ }), h("path", {
375
+ d: "M4.5 9L7.5 12L13.5 6",
376
+ stroke: "white",
377
+ "stroke-width": "1.8",
378
+ "stroke-linecap": "round",
379
+ "stroke-linejoin": "round",
380
+ fill: "none"
381
+ })),
382
+ mark("cb-indeterminate", {}, h("rect", {
383
+ width: "18",
384
+ height: "18",
385
+ rx: "3",
386
+ fill: "currentColor"
387
+ }), h("rect", {
388
+ x: "4.5",
389
+ y: "8.1",
390
+ width: "9",
391
+ height: "1.8",
392
+ rx: "0.9",
393
+ fill: "white"
394
+ }))
395
+ ])]);
396
+ // src/Preview/render.ts
397
+ const taskItem = StyleSheet.create({ item: { listStyleType: "none" } });
398
+ const isTextTag = (tag) => Object.hasOwn(TEXT, tag);
399
+ const isHeading = (tag) => /^h[1-6]$/.test(tag);
400
+ const isBoxTag = (tag) => tag === "ul" || tag === "ol";
401
+ /** The attributes of an element of the tree, as the element takes them. */
402
+ function attributesOf(node) {
403
+ return Object.fromEntries(Object.entries(node.properties).map(([name, value]) => {
404
+ return [find(html, name).attribute, Array.isArray(value) ? value.join(" ") : value];
405
+ }));
406
+ }
407
+ function renderElement(node, context) {
408
+ const { slotProps } = context;
409
+ const { tagName, properties } = node;
410
+ const children = () => childrenOf(node).map((child) => renderNode(child, context));
411
+ if (isTextTag(tagName)) {
412
+ const as = isHeading(tagName) || tagName === "p" ? void 0 : tagName;
413
+ return h(Typography, {
414
+ id: isHeading(tagName) ? properties.id : void 0,
415
+ as,
416
+ ...TEXT[tagName],
417
+ ...tagName === "li" && isTask(node) ? { style: taskItem.item } : {},
418
+ ...slotProps[tagName]
419
+ }, children);
420
+ }
421
+ if (isBoxTag(tagName)) return h(View, {
422
+ as: tagName,
423
+ ...BOX[tagName],
424
+ ...slotProps[tagName]
425
+ }, children);
426
+ switch (tagName) {
427
+ case "a": return h(Link, {
428
+ as: "a",
429
+ href: safeUrl(properties.href),
430
+ underline: "hover",
431
+ color: context.accent,
432
+ variant: "inherit",
433
+ ...slotProps.a
434
+ }, children);
435
+ case "blockquote": return h(Quote, {
436
+ as: "blockquote",
437
+ ...BOX.blockquote,
438
+ ...slotProps.blockquote
439
+ }, children);
440
+ case "input": return checkbox(Boolean(properties.checked), {
441
+ mr: 1,
442
+ ...slotProps.input
443
+ });
444
+ case "code": {
445
+ const isBlock = languageOf(node) !== void 0;
446
+ return h(Typography, {
447
+ as: "code",
448
+ class: classesOf(node),
449
+ ...CODE[isBlock ? "block" : "inline"],
450
+ display: isBlock ? "block" : "inline",
451
+ borderRadius: "sm",
452
+ ...slotProps.code
453
+ }, children);
454
+ }
455
+ case "pre": return h(CodeBlock, { ...slotProps.pre }, children);
456
+ case "table": return h(TableContainer, { my: BOX.table.my }, () => h(Table, {
457
+ ...BOX.tableBody,
458
+ ...slotProps.table
459
+ }, children));
460
+ case "thead": return h(TableHead, {
461
+ ...BOX.thead,
462
+ ...slotProps.thead
463
+ }, children);
464
+ case "tbody": return h("tbody", { ...slotProps.tbody }, children());
465
+ case "tr": return h(TableRow, { ...slotProps.tr }, children);
466
+ case "th": return h(TableCell, {
467
+ as: "th",
468
+ scope: "col",
469
+ align: alignOf(node),
470
+ ...slotProps.th
471
+ }, children);
472
+ case "td": return h(TableCell, {
473
+ align: alignOf(node),
474
+ ...slotProps.td
475
+ }, children);
476
+ case "hr": return h(View, {
477
+ as: "hr",
478
+ ...BOX.hr,
479
+ ...slotProps.hr
480
+ });
481
+ case "img": return h(Image, {
482
+ src: safeUrl(properties.src),
483
+ alt: properties.alt,
484
+ maxWidth: "100%",
485
+ ...slotProps.img
486
+ });
487
+ default: return h(tagName, attributesOf(node), children());
488
+ }
489
+ }
490
+ function renderNode(node, context) {
491
+ if (node.type === "text") return node.value;
492
+ return isElement(node) ? renderElement(node, context) : null;
493
+ }
494
+ /** The nodes of a tree as Vue nodes. */
495
+ const renderNodes = (nodes, context) => nodes.map((node) => renderNode(node, context));
496
+ // src/Preview/Preview.ts
497
+ const PreviewRoot = styled(View, {
498
+ name: "MarkdownPreview",
499
+ label: "MarkdownPreview"
500
+ })({
501
+ minWidth: 0,
502
+ maxWidth: "100%",
503
+ overflowWrap: "break-word"
504
+ });
505
+ /** The text of what a slot holds: the markdown source. */
506
+ function sourceOf(child) {
507
+ if (Array.isArray(child)) return child.map(sourceOf).join("");
508
+ if (typeof child === "string") return child;
509
+ if (!isVNode(child) || child.type === Comment) return "";
510
+ if (typeof child.children === "string") return child.children;
511
+ return Array.isArray(child.children) ? sourceOf(child.children) : "";
512
+ }
513
+ /** Renders a markdown string - the default slot - with every element drawn by a primitive of the design system. */
514
+ const Preview = defineComponent((props, { attrs, slots }) => {
515
+ const theme = useTheme();
516
+ return () => {
517
+ const tree = parseMarkdown(sourceOf(slots.default?.()));
518
+ const context = {
519
+ slotProps: props.slotProps ?? {},
520
+ accent: themeDefault(theme.value, "accentTextColor", "primary")
521
+ };
522
+ return h(PreviewRoot, rootProps(props, attrs), () => renderNodes(tree.children, context));
523
+ };
524
+ }, {
525
+ name: "MarkdownPreview",
526
+ inheritAttrs: false,
527
+ props: declare("slotProps", "style"),
528
+ slots: {}
529
+ });
530
+ export { Preview as t };