Skip to content

Commit

Permalink
Rename to optimize.ignoreComponentNames in MDX (#10884)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Apr 26, 2024
1 parent 2abfc66 commit d03c9a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-oranges-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/mdx": major
---

Renames the `optimize.customComponentNames` option to `optimize.ignoreComponentNames` to better reflect its usecase. Its behaviour is not changed and should continue to work as before.
12 changes: 6 additions & 6 deletions packages/integrations/mdx/src/rehype-optimize-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { toHtml } from 'hast-util-to-html';
type Node = any;

export interface OptimizeOptions {
customComponentNames?: string[];
ignoreComponentNames?: string[];
}

const exportConstComponentsRe = /export\s+const\s+components\s*=/;
Expand All @@ -21,10 +21,10 @@ export function rehypeOptimizeStatic(options?: OptimizeOptions) {
return (tree: any) => {
// A set of non-static components to avoid collapsing when walking the tree
// as they need to be preserved as JSX to be rendered dynamically.
const customComponentNames = new Set<string>(options?.customComponentNames);
const ignoreComponentNames = new Set<string>(options?.ignoreComponentNames);

// Find `export const components = { ... }` and get it's object's keys to be
// populated into `customComponentNames`. This configuration is used to render
// populated into `ignoreComponentNames`. This configuration is used to render
// some HTML elements as custom components, and we also want to avoid collapsing them.
for (const child of tree.children) {
if (child.type === 'mdxjsEsm' && exportConstComponentsRe.test(child.value)) {
Expand All @@ -34,7 +34,7 @@ export function rehypeOptimizeStatic(options?: OptimizeOptions) {
for (const objectPropertyNode of objectPropertyNodes) {
const componentName = objectPropertyNode.key?.name ?? objectPropertyNode.key?.value;
if (componentName) {
customComponentNames.add(componentName);
ignoreComponentNames.add(componentName);
}
}
}
Expand All @@ -53,10 +53,10 @@ export function rehypeOptimizeStatic(options?: OptimizeOptions) {
if (key != null && key !== 'children') return SKIP;

// @ts-expect-error read tagName naively
const isCustomComponent = node.tagName && customComponentNames.has(node.tagName);
const isNodeIgnored = node.tagName && ignoreComponentNames.has(node.tagName);
// For nodes that can't be optimized, eliminate all elements in the
// `elementStack` from the `allPossibleElements` set.
if (node.type.startsWith('mdx') || isCustomComponent) {
if (node.type.startsWith('mdx') || isNodeIgnored) {
for (const el of elementStack) {
allPossibleElements.delete(el);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import mdx from '@astrojs/mdx';
export default {
integrations: [mdx({
optimize: {
customComponentNames: ['strong']
ignoreComponentNames: ['strong']
}
})]
}

0 comments on commit d03c9a9

Please sign in to comment.