|
1 | 1 | import _ from "lodash"; |
2 | 2 | import { unified } from "unified"; |
3 | 3 | import remarkParse from "remark-parse"; |
4 | | -import remarkMdx from "remark-mdx"; |
5 | 4 | import remarkFrontmatter from "remark-frontmatter"; |
6 | 5 | import remarkGfm from "remark-gfm"; |
7 | 6 | import remarkStringify from "remark-stringify"; |
8 | 7 | import remarkMdxFrontmatter from "remark-mdx-frontmatter"; |
9 | 8 | import { VFile } from "vfile"; |
10 | | -import { Root } from "mdast"; |
| 9 | +import { Root, RootContent, RootContentMap } from "mdast"; |
11 | 10 | import { ILoader } from "./_types"; |
12 | 11 | import { createLoader } from "./_utils"; |
13 | 12 |
|
14 | | -const parser = unified() |
15 | | - .use(remarkParse) |
16 | | - .use(remarkMdx) |
17 | | - .use(remarkFrontmatter, ["yaml"]) |
18 | | - .use(remarkMdxFrontmatter) |
19 | | - .use(remarkGfm); |
20 | | - |
21 | | -const serializer = unified() |
22 | | - .use(remarkStringify) |
23 | | - .use(remarkMdx) |
24 | | - .use(remarkFrontmatter, ["yaml"]) |
25 | | - .use(remarkMdxFrontmatter) |
26 | | - .use(remarkGfm); |
| 13 | +const parser = unified().use(remarkParse).use(remarkFrontmatter, ["yaml"]).use(remarkGfm); |
| 14 | +const serializer = unified().use(remarkStringify).use(remarkFrontmatter, ["yaml"]).use(remarkGfm); |
27 | 15 |
|
28 | 16 | export function createMdxFormatLoader(): ILoader<string, Record<string, any>> { |
| 17 | + const skippedTypes: (keyof RootContentMap | "root")[] = ["code", "inlineCode"]; |
29 | 18 | return createLoader({ |
30 | 19 | async pull(locale, input) { |
31 | 20 | const file = new VFile(input); |
32 | 21 | const ast = parser.parse(file); |
33 | | - return JSON.parse(JSON.stringify(ast)); |
| 22 | + |
| 23 | + const result = _.cloneDeep(ast); |
| 24 | + |
| 25 | + traverseMdast(result, (node) => { |
| 26 | + if (skippedTypes.includes(node.type)) { |
| 27 | + if ("value" in node) { |
| 28 | + node.value = ""; |
| 29 | + } |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + return result; |
34 | 34 | }, |
35 | 35 |
|
36 | | - async push(locale, data) { |
37 | | - const ast = data as unknown as Root; |
38 | | - const content = String(serializer.stringify(ast)); |
39 | | - return content; |
| 36 | + async push(locale, data, originalInput, originalLocale, pullInput, pullOutput) { |
| 37 | + const file = new VFile(originalInput); |
| 38 | + const ast = parser.parse(file); |
| 39 | + |
| 40 | + const result = _.cloneDeep(ast); |
| 41 | + |
| 42 | + traverseMdast(result, (node, indexPath) => { |
| 43 | + if ("value" in node) { |
| 44 | + const incomingValue = findNodeByIndexPath(data, indexPath); |
| 45 | + if (incomingValue && "value" in incomingValue && !_.isEmpty(incomingValue.value)) { |
| 46 | + node.value = incomingValue.value; |
| 47 | + } |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + return String(serializer.stringify(result)); |
40 | 52 | }, |
41 | 53 | }); |
42 | 54 | } |
@@ -73,3 +85,35 @@ export function createMdxStructureLoader(): ILoader<Record<string, any>, Record< |
73 | 85 | }, |
74 | 86 | }); |
75 | 87 | } |
| 88 | + |
| 89 | +function traverseMdast( |
| 90 | + ast: Root | RootContent, |
| 91 | + visitor: (node: Root | RootContent, path: number[]) => void, |
| 92 | + indexPath: number[] = [], |
| 93 | +) { |
| 94 | + visitor(ast, indexPath); |
| 95 | + |
| 96 | + if ("children" in ast && Array.isArray(ast.children)) { |
| 97 | + for (let i = 0; i < ast.children.length; i++) { |
| 98 | + traverseMdast(ast.children[i], visitor, [...indexPath, i]); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +function findNodeByIndexPath(ast: Root | RootContent, indexPath: number[]): Root | RootContent | null { |
| 104 | + let result: Root | RootContent | null = null; |
| 105 | + |
| 106 | + const stringifiedIndexPath = indexPath.join("."); |
| 107 | + traverseMdast(ast, (node, path) => { |
| 108 | + if (result) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + const currentStringifiedPath = path.join("."); |
| 113 | + if (currentStringifiedPath === stringifiedIndexPath) { |
| 114 | + result = node; |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + return result; |
| 119 | +} |
0 commit comments