Skip to content

Commit

Permalink
constrain
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Feb 6, 2025
1 parent 37da3dc commit 984be09
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 35 deletions.
78 changes: 44 additions & 34 deletions packages/fern-docs/mdx/src/plugins/rehype-expression-to-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,53 @@ import {
} from "../mdx-utils";
import { Hast, Mdast } from "../types";

export const rehypeExpressionToMd: Plugin<[], Hast.Root> = () => (ast) => {
visit(ast, (node) => {
/**
* Example:
* {<div>[Hello](https://example.com)</div>} -> {<div><a href="https://example.com">Hello</a></div>}
*/
if (isMdxExpression(node)) {
const estree = node.data?.estree;
if (!estree) {
return;
export const rehypeExpressionToMd: Plugin<
[{ mdxJsxElementAllowlist?: Record<string, string[]> }?],
Hast.Root
> =
({ mdxJsxElementAllowlist = {} } = {}) =>
(ast) => {
visit(ast, (node) => {
/**
* Example:
* {<div>[Hello](https://example.com)</div>} -> {<div><a href="https://example.com">Hello</a></div>}
*/
if (isMdxExpression(node)) {
const estree = node.data?.estree;
if (!estree) {
return;
}
replaceJsxTextToMarkdown(estree);
}
replaceJsxTextToMarkdown(estree);
}

/**
* Example:
* <Frame caption="[Hello](https://example.com)" /> -> <Frame caption={<a href="https://example.com">Hello</a>} />
*/
if (isMdxJsxElementHast(node)) {
node.attributes.forEach((attribute) => {
if (
isMdxJsxAttribute(attribute) &&
typeof attribute.value === "string"
) {
const expression = mdToEstree(attribute.value);
if (expression) {
attribute.value = {
type: "mdxJsxAttributeValueExpression",
value: attribute.value,
data: { estree: expression },
};
}
/**
* Example:
* <Frame caption="[Hello](https://example.com)" /> -> <Frame caption={<a href="https://example.com">Hello</a>} />
*/
if (isMdxJsxElementHast(node)) {
const allowlist = mdxJsxElementAllowlist[node.name ?? "Fragment"];
if (allowlist == null) {
return;
}
});
}
});
};
node.attributes.forEach((attribute) => {
if (
isMdxJsxAttribute(attribute) &&
allowlist.includes(attribute.name) &&
typeof attribute.value === "string"
) {
const expression = mdToEstree(attribute.value);
if (expression) {
attribute.value = {
type: "mdxJsxAttributeValueExpression",
value: attribute.value,
data: { estree: expression },
};
}
}
});
}
});
};

function replaceJsxTextToMarkdown(estree: Program) {
walk(estree, {
Expand Down
14 changes: 13 additions & 1 deletion packages/fern-docs/ui/src/mdx/bundlers/mdx-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,19 @@ async function serializeMdxImpl(

const rehypePlugins: PluggableList = [
rehypeSqueezeParagraphs,
rehypeExpressionToMd,
[
rehypeExpressionToMd,
{
mdxJsxElementAllowlist: {
Frame: ["caption"],
Tab: ["title"],
Card: ["title", "icon"],
Callout: ["title", "icon"],
Step: ["title"],
Accordion: ["title"],
},
},
],
rehypeMdxClassStyle,
[rehypeFiles, { replaceSrc }],
rehypeAcornErrorBoundary,
Expand Down
14 changes: 14 additions & 0 deletions packages/fern-docs/ui/src/mdx/bundlers/next-mdx-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@fern-docs/mdx";
import {
rehypeAcornErrorBoundary,
rehypeExpressionToMd,
rehypeMdxClassStyle,
rehypeSqueezeParagraphs,
remarkSanitizeAcorn,
Expand Down Expand Up @@ -58,6 +59,19 @@ function withDefaultMdxOptions(

const rehypePlugins: PluggableList = [
rehypeSqueezeParagraphs,
[
rehypeExpressionToMd,
{
mdxJsxElementAllowlist: {
Frame: ["caption"],
Tab: ["title"],
Card: ["title", "icon"],
Callout: ["title", "icon"],
Step: ["title"],
Accordion: ["title"],
},
},
],
rehypeMdxClassStyle,
[rehypeFiles, { replaceSrc }],
rehypeAcornErrorBoundary,
Expand Down

0 comments on commit 984be09

Please sign in to comment.