-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into rohin/required-global-headers
- Loading branch information
Showing
9 changed files
with
134 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./rehype-acorn-error-boundary"; | ||
export * from "./rehype-mdx-class-style"; | ||
export * from "./rehype-squeeze-paragraphs"; | ||
export * from "./remark-inject-esm"; | ||
export * from "./remark-sanitize-acorn"; | ||
export * from "./remark-squeeze-paragraphs"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* A remark plugin to inject an ESM export into the MDX file. | ||
* | ||
* Inspired by https://github.com/remcohaszing/remark-mdx-frontmatter/blob/main/src/remark-mdx-frontmatter.ts | ||
*/ | ||
|
||
import type { Statement } from "estree"; | ||
import { name as isIdentifierName } from "estree-util-is-identifier-name"; | ||
import { valueToEstree } from "estree-util-value-to-estree"; | ||
import type { Root } from "mdast"; | ||
import type { Plugin } from "unified"; | ||
|
||
export interface RemarkInjectEsmOptions { | ||
scope: Record<string, unknown>; | ||
} | ||
|
||
/** | ||
* A remark plugin to inject scope of variables into the MDX file. | ||
* | ||
* @param options Optional options to configure the output. | ||
* @returns A unified transformer. | ||
*/ | ||
export const remarkInjectEsm: Plugin<[RemarkInjectEsmOptions?], Root> = ( | ||
{ scope } = { scope: {} } | ||
) => { | ||
const keys = Object.keys(scope); | ||
if (keys.some((key) => !isIdentifierName(key))) { | ||
throw new Error( | ||
`Scope keys should be valid identifiers, got: ${JSON.stringify(keys)}` | ||
); | ||
} | ||
|
||
return (ast) => { | ||
if (Object.keys(scope).length === 0) { | ||
return; | ||
} | ||
|
||
ast.children.unshift({ | ||
type: "mdxjsEsm", | ||
value: "", | ||
data: { | ||
estree: { | ||
type: "Program", | ||
sourceType: "module", | ||
body: Object.entries(scope).map( | ||
([key, value]): Statement => ({ | ||
type: "VariableDeclaration", | ||
kind: "const", | ||
declarations: [ | ||
{ | ||
type: "VariableDeclarator", | ||
id: { type: "Identifier", name: key }, | ||
init: valueToEstree(value, { preserveReferences: true }), | ||
}, | ||
], | ||
}) | ||
), | ||
}, | ||
}, | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.