Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit 4959769

Browse files
authored
fix: tables in markdown preview (#1013)
1 parent 94b2142 commit 4959769

File tree

8 files changed

+679
-292
lines changed

8 files changed

+679
-292
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
},
3333
"workspaces": [
3434
"packages/*"
35-
]
35+
],
36+
"dependencies": {}
3637
}

packages/core/package.json

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
"@emotion/react": "11.10.6",
6868
"@emotion/styled": "11.10.6",
6969
"@lezer/common": "1.0.2",
70-
"@mdx-js/mdx": "2.3.0",
71-
"@mdx-js/react": "2.3.0",
70+
"@mdx-js/mdx": "3.0.0",
71+
"@mdx-js/react": "3.0.0",
7272
"@mui/base": "5.0.0-beta.14",
7373
"@mui/material": "5.11.16",
7474
"@mui/system": "5.11.16",
@@ -120,15 +120,15 @@
120120
"jwt-decode": "3.1.2",
121121
"localforage": "1.10.0",
122122
"lodash": "4.17.21",
123-
"mdast-util-gfm-footnote": "1.0.2",
124-
"mdast-util-gfm-strikethrough": "1.0.3",
125-
"mdast-util-gfm-table": "1.0.7",
126-
"mdast-util-gfm-task-list-item": "1.0.2",
127-
"micromark-extension-gfm-footnote": "1.1.0",
128-
"micromark-extension-gfm-strikethrough": "1.0.5",
129-
"micromark-extension-gfm-table": "1.0.5",
130-
"micromark-extension-gfm-task-list-item": "1.0.4",
131-
"micromark-util-combine-extensions": "1.0.0",
123+
"mdast-util-gfm-footnote": "2.0.0",
124+
"mdast-util-gfm-strikethrough": "2.0.0",
125+
"mdast-util-gfm-table": "2.0.0",
126+
"mdast-util-gfm-task-list-item": "2.0.0",
127+
"micromark-extension-gfm-footnote": "2.0.0",
128+
"micromark-extension-gfm-strikethrough": "2.0.0",
129+
"micromark-extension-gfm-table": "2.0.0",
130+
"micromark-extension-gfm-task-list-item": "2.0.1",
131+
"micromark-util-combine-extensions": "2.0.0",
132132
"minimatch": "9.0.0",
133133
"moment": "2.29.4",
134134
"node-polyglot": "2.5.0",
@@ -142,7 +142,7 @@
142142
"react-dom": "18.2.0",
143143
"react-frame-component": "5.2.6",
144144
"react-is": "18.2.0",
145-
"react-markdown": "8.0.7",
145+
"react-markdown": "9.0.1",
146146
"react-polyglot": "0.7.2",
147147
"react-redux": "8.0.5",
148148
"react-router-dom": "6.10.0",
@@ -153,9 +153,9 @@
153153
"react-waypoint": "10.3.0",
154154
"react-window": "1.8.9",
155155
"remark-gfm": "4.0.0",
156-
"remark-html": "15.0.2",
157-
"remark-mdx": "2.3.0",
158-
"remark-parse": "10.0.1",
156+
"remark-html": "16.0.1",
157+
"remark-mdx": "3.0.0",
158+
"remark-parse": "11.0.0",
159159
"sanitize-filename": "1.6.3",
160160
"scheduler": "0.23.0",
161161
"semaphore": "1.1.0",
@@ -166,15 +166,15 @@
166166
"stream-browserify": "3.0.0",
167167
"styled-components": "5.3.10",
168168
"symbol-observable": "4.0.0",
169-
"unified": "10.1.2",
170-
"unist-util-visit": "4.1.2",
169+
"unified": "11.0.4",
170+
"unist-util-visit": "5.0.0",
171171
"url": "0.11.0",
172172
"url-join": "5.0.0",
173173
"uuid": "9.0.0",
174174
"validate-color": "2.2.4",
175-
"vfile": "5.3.7",
176-
"vfile-message": "3.1.4",
177-
"vfile-statistics": "2.0.1",
175+
"vfile": "6.0.1",
176+
"vfile-message": "4.0.2",
177+
"vfile-statistics": "3.0.0",
178178
"what-input": "5.2.12",
179179
"what-the-diff": "0.6.0",
180180
"yaml": "2.2.2"
@@ -218,6 +218,7 @@
218218
"@types/react-virtualized-auto-sizer": "1.0.1",
219219
"@types/react-window": "1.8.5",
220220
"@types/styled-components": "5.1.26",
221+
"@types/unist": "3.0.2",
221222
"@types/url-join": "4.0.1",
222223
"@types/uuid": "9.0.1",
223224
"@typescript-eslint/eslint-plugin": "5.59.1",

packages/core/src/widgets/markdown/plate/hooks/useMarkdownToSlate.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { getShortcodes } from '../../../../lib/registry';
88
import gfm from '../serialization/gfm';
99
import toSlatePlugin from '../serialization/slate/toSlatePlugin';
1010

11+
import type { ProcessCallback } from 'unified';
12+
import type { VFile } from 'vfile';
1113
import type { ShortcodeConfig } from '../../../../interface';
1214
import type { MdValue } from '../plateTypes';
1315

@@ -22,19 +24,22 @@ export const markdownToSlate = async (
2224
{ useMdx, shortcodeConfigs }: UseMarkdownToSlateOptions,
2325
) => {
2426
return new Promise<MdValue>(resolve => {
27+
const callback: ProcessCallback<VFile> = (err, file) => {
28+
if (err) {
29+
console.error(err);
30+
return;
31+
}
32+
resolve(file?.result as MdValue);
33+
return undefined;
34+
};
35+
2536
unified()
2637
.use(markdown)
2738
.use(gfm)
2839
// eslint-disable-next-line @typescript-eslint/no-empty-function
2940
.use(useMdx ? mdx : () => {})
3041
.use(toSlatePlugin({ shortcodeConfigs: shortcodeConfigs ?? getShortcodes(), useMdx }))
31-
.process(markdownValue, (err, file) => {
32-
if (err) {
33-
console.error(err);
34-
return;
35-
}
36-
resolve(file?.result as MdValue);
37-
});
42+
.process(markdownValue, callback);
3843
});
3944
};
4045

packages/core/src/widgets/markdown/plate/hooks/useMdx.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { evaluate } from '@mdx-js/mdx';
22
import * as provider from '@mdx-js/react';
33
import { useCallback, useEffect, useState } from 'react';
44
import * as runtime from 'react/jsx-runtime';
5-
import remarkGfm from 'remark-gfm';
65
import { VFile } from 'vfile';
76
import { VFileMessage } from 'vfile-message';
87

8+
import useDebounce from '@staticcms/core/lib/hooks/useDebounce';
99
import useDebouncedCallback from '@staticcms/core/lib/hooks/useDebouncedCallback';
10+
import remarkGfm from '../serialization/gfm';
1011
import flattenListItemParagraphs from '../serialization/slate/flattenListItemParagraphs';
11-
import useDebounce from '@staticcms/core/lib/hooks/useDebounce';
12+
13+
import type { Compatible } from 'vfile';
1214

1315
export interface UseMdxState {
1416
file: VFile | null;
@@ -33,7 +35,7 @@ export default function useMdx(
3335
};
3436

3537
try {
36-
file.result = (await evaluate(file, options)).default;
38+
file.result = (await evaluate(file as Readonly<Compatible>, options)).default;
3739
} catch (error) {
3840
const message = error instanceof VFileMessage ? error : new VFileMessage(String(error));
3941

packages/core/src/widgets/markdown/plate/serialization/gfm.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,32 @@ import type { Plugin, Processor } from 'unified';
2020
function gfmFromMarkdown() {
2121
return [
2222
gfmFootnoteFromMarkdown(),
23-
gfmStrikethroughFromMarkdown,
24-
gfmTableFromMarkdown,
25-
gfmTaskListItemFromMarkdown,
23+
gfmStrikethroughFromMarkdown(),
24+
gfmTableFromMarkdown(),
25+
gfmTaskListItemFromMarkdown(),
2626
];
2727
}
2828

2929
function gfmToMarkdown() {
3030
return {
3131
extensions: [
3232
gfmFootnoteToMarkdown(),
33-
gfmStrikethroughToMarkdown,
33+
gfmStrikethroughToMarkdown(),
3434
gfmTableToMarkdown({}),
35-
gfmTaskListItemToMarkdown,
35+
gfmTaskListItemToMarkdown(),
3636
],
3737
};
3838
}
3939

4040
function gfm() {
41-
return combineExtensions([gfmFootnote(), gfmStrikethrough({}), gfmTable, gfmTaskListItem]);
41+
return combineExtensions([gfmFootnote(), gfmStrikethrough({}), gfmTable(), gfmTaskListItem()]);
42+
}
43+
44+
declare module 'unified' {
45+
interface Data {
46+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
47+
toMarkdownExtensions?: Array<any>;
48+
}
4249
}
4350

4451
/**
@@ -47,15 +54,13 @@ function gfm() {
4754
const remarkGfm: Plugin<void[], Root> = function (this: Processor) {
4855
const data = this.data();
4956

50-
add('micromarkExtensions', gfm());
51-
add('fromMarkdownExtensions', gfmFromMarkdown());
52-
add('toMarkdownExtensions', gfmToMarkdown());
57+
const micromarkExtensions = data.micromarkExtensions || (data.micromarkExtensions = []);
58+
const fromMarkdownExtensions = data.fromMarkdownExtensions || (data.fromMarkdownExtensions = []);
59+
const toMarkdownExtensions = data.toMarkdownExtensions || (data.toMarkdownExtensions = []);
5360

54-
function add(field: string, value: unknown) {
55-
const list = (data[field] ? data[field] : (data[field] = [])) as unknown[];
56-
57-
list.push(value);
58-
}
61+
micromarkExtensions.push(gfm());
62+
fromMarkdownExtensions.push(gfmFromMarkdown());
63+
toMarkdownExtensions.push(gfmToMarkdown());
5964
};
6065

6166
export default remarkGfm;

packages/core/src/widgets/markdown/plate/serialization/slate/flattenListItemParagraphs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { visit } from 'unist-util-visit';
22

33
import type { Plugin } from 'unified';
4-
import type { Parent, Node, VisitorResult } from 'unist-util-visit/lib';
4+
import type { Node, Parent } from 'unist';
5+
import type { VisitorResult } from 'unist-util-visit';
56

67
const flattenListItemParagraphs: Plugin = function () {
78
return ast => {

packages/core/src/widgets/markdown/plate/serialization/slate/toSlatePlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import transform from './deserializeMarkdown';
22

33
import type { ShortcodeConfig } from '@staticcms/core/interface';
4-
import type { Plugin } from 'unified';
4+
import type { Compiler, Plugin } from 'unified';
55
import type { MdastNode } from './ast-types';
66

77
export interface ToSlatePluginOptions {
@@ -19,7 +19,7 @@ export const slateCompiler =
1919

2020
const toSlatePlugin = (options: ToSlatePluginOptions): Plugin =>
2121
function () {
22-
this.Compiler = slateCompiler(options);
22+
this.compiler = slateCompiler(options) as unknown as Compiler;
2323
};
2424

2525
export default toSlatePlugin;

0 commit comments

Comments
 (0)