|
| 1 | +import _ from "lodash"; |
| 2 | +import { unified } from "unified"; |
| 3 | +import remarkParse from "remark-parse"; |
| 4 | +import remarkMdx from "remark-mdx"; |
| 5 | +import remarkFrontmatter from "remark-frontmatter"; |
| 6 | +import remarkGfm from "remark-gfm"; |
| 7 | +import remarkStringify from "remark-stringify"; |
| 8 | +import remarkMdxFrontmatter from "remark-mdx-frontmatter"; |
| 9 | +import { VFile } from "vfile"; |
| 10 | +import { Root } from "mdast"; |
| 11 | +import { ILoader } from "./_types"; |
| 12 | +import { composeLoaders, createLoader } from "./_utils"; |
| 13 | + |
| 14 | +// Define MDX node types |
| 15 | +type MdxNode = { |
| 16 | + type: string; |
| 17 | + [key: string]: any; |
| 18 | +}; |
| 19 | + |
| 20 | +type MdxTree = { |
| 21 | + type: "root"; |
| 22 | + children: MdxNode[]; |
| 23 | +}; |
| 24 | + |
| 25 | +export function createMdxFormatLoader(): ILoader<string, Record<string, any>> { |
| 26 | + // Create a unified processor for MDX parsing with all required plugins |
| 27 | + const parser = unified() |
| 28 | + .use(remarkParse) |
| 29 | + .use(remarkMdx) |
| 30 | + .use(remarkFrontmatter, ["yaml"]) |
| 31 | + .use(remarkMdxFrontmatter) |
| 32 | + .use(remarkGfm); |
| 33 | + |
| 34 | + // Create a unified processor for MDX serialization |
| 35 | + const serializer = unified() |
| 36 | + .use(remarkStringify) |
| 37 | + .use(remarkMdx) |
| 38 | + .use(remarkFrontmatter, ["yaml"]) |
| 39 | + .use(remarkMdxFrontmatter) |
| 40 | + .use(remarkGfm); |
| 41 | + |
| 42 | + return createLoader({ |
| 43 | + async pull(locale, input) { |
| 44 | + // Parse the MDX content into an AST |
| 45 | + const file = new VFile(input); |
| 46 | + const ast = parser.parse(file); |
| 47 | + |
| 48 | + // Instead of returning the AST directly, convert it to a plain object |
| 49 | + // This ensures compatibility with the ILoader interface |
| 50 | + return JSON.parse(JSON.stringify(ast)); |
| 51 | + }, |
| 52 | + |
| 53 | + async push(locale, data) { |
| 54 | + // Recreate an AST from the plain object |
| 55 | + const ast = data as unknown as Root; |
| 56 | + |
| 57 | + // Serialize the AST back to MDX content |
| 58 | + const file = serializer.stringify(ast); |
| 59 | + return String(file); |
| 60 | + }, |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +export function createMdxStructureLoader(): ILoader<Record<string, any>, Record<string, string>> { |
| 65 | + return createLoader({ |
| 66 | + async pull(locale, input) { |
| 67 | + const result = _.chain(input) |
| 68 | + .pickBy((value, key) => key.endsWith("/value")) |
| 69 | + .value(); |
| 70 | + |
| 71 | + return result; |
| 72 | + }, |
| 73 | + async push(locale, data, originalInput) { |
| 74 | + const result = _.merge({}, originalInput, data); |
| 75 | + |
| 76 | + return result; |
| 77 | + }, |
| 78 | + }); |
| 79 | +} |
0 commit comments