Skip to content

Commit

Permalink
fix prepare mdx content
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 29, 2024
1 parent 6af391e commit 25af7e3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Root as MdastRoot } from "mdast";
import { fromMarkdown } from "mdast-util-from-markdown";
import { gfmFromMarkdown } from "mdast-util-gfm";
import { mathFromMarkdown } from "mdast-util-math";
import { mdxFromMarkdown } from "mdast-util-mdx";
import { gfm } from "micromark-extension-gfm";
import { math } from "micromark-extension-math";
import { mdxjs } from "micromark-extension-mdxjs";
import { UnreachableCaseError } from "ts-essentials";
Expand All @@ -11,8 +13,8 @@ export function mdastFromMarkdown(content: string, format: "mdx" | "md" = "mdx")
return fromMarkdown(content);
} else if (format === "mdx") {
return fromMarkdown(content, {
extensions: [mdxjs(), math()],
mdastExtensions: [mdxFromMarkdown(), mathFromMarkdown()],
extensions: [mdxjs(), math(), gfm()],
mdastExtensions: [mdxFromMarkdown(), mathFromMarkdown(), gfmFromMarkdown()],
});
} else {
throw new UnreachableCaseError(format);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Root as MdastRoot } from "mdast";
import { gfmToMarkdown } from "mdast-util-gfm";
import { mathToMarkdown } from "mdast-util-math";
import { mdxToMarkdown } from "mdast-util-mdx";
import { toMarkdown } from "mdast-util-to-markdown";

export function mdastToMarkdown(mdast: MdastRoot): string {
return toMarkdown(mdast, {
extensions: [mdxToMarkdown(), mathToMarkdown()],
extensions: [mdxToMarkdown(), mathToMarkdown(), gfmToMarkdown()],
});
}
6 changes: 5 additions & 1 deletion packages/ui/fern-docs-mdx/src/mdast-utils/mdast-to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function one(value: unknown, includeImageAlt: boolean, includeHtml: boolean, pre
if ("children" in value) {
return (
all(value.children as Array<unknown>, includeImageAlt, includeHtml, preserveNewlines) +
(preserveNewlines && hasBreak(value) ? "\n" : "")
(preserveNewlines && hasBreak(value) ? "\n" : hasSpace(value) ? " " : "")
);
}
}
Expand All @@ -96,6 +96,10 @@ function hasBreak(value: Extract<Nodes, { children: unknown[] }>): boolean {
);
}

function hasSpace(value: Extract<Nodes, { children: unknown[] }>): boolean {
return hasBreak(value) || value.type === "tableCell";
}

/**
* Serialize a list of nodes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ Be sure to save the generated token - it won't be displayed after you leave the
`;
const result = prepareMdxContent(content);
expect(result.content).toMatchInlineSnapshot(`
"| Column 1 | Column 2 | Column 3 |
| -------- | -------- | -------- |
| Value 1 | Value 2 | Value 3 |"
"Column 1 Column 2 Column 3
Value 1 Value 2 Value 3"
`);
});
});

0 comments on commit 25af7e3

Please sign in to comment.