Core: Improve default MDX attribute indexing strategy for remarkStructure

This commit is contained in:
Fuma Nama
2025-05-06 18:09:07 +08:00
parent 50db8746b5
commit 79e75c31b4
2 changed files with 23 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
Improve default MDX attribute indexing strategy for `remarkStructure`
@@ -1,5 +1,5 @@
import Slugger from 'github-slugger';
import type { Root, Nodes } from 'mdast';
import type { Nodes, Root } from 'mdast';
import { remark } from 'remark';
import remarkGfm from 'remark-gfm';
import type { PluggableList, Transformer } from 'unified';
@@ -8,6 +8,7 @@ import { flattenNode } from './remark-utils';
import type {
MdxJsxAttribute,
MdxJsxExpressionAttribute,
MdxJsxFlowElement,
} from 'mdast-util-mdx-jsx';
interface Heading {
@@ -36,10 +37,16 @@ export interface StructureOptions {
*/
types?: string[] | ((node: Nodes) => boolean);
/**
* A list of indexable MDX attributes, either:
*
* - an array of attribute names.
* - a function that determines if attribute should be indexed.
*/
allowedMdxAttributes?:
| string[]
| ((
node: Nodes,
node: MdxJsxFlowElement,
attribute: MdxJsxAttribute | MdxJsxExpressionAttribute,
) => boolean);
}
@@ -68,7 +75,11 @@ export function remarkStructure({
'tableCell',
'mdxJsxFlowElement',
],
allowedMdxAttributes = () => true,
allowedMdxAttributes = (node) => {
if (!node.name) return false;
return ['TypeTable', 'Callout'].includes(node.name);
},
}: StructureOptions = {}): Transformer<Root, Root> {
if (Array.isArray(allowedMdxAttributes)) {
const arr = allowedMdxAttributes;
@@ -138,11 +149,11 @@ export function remarkStructure({
content: element.name,
},
...element.attributes.flatMap((attribute) => {
const valueStr =
const value =
typeof attribute.value === 'string'
? attribute.value
: attribute.value?.value;
if (!valueStr) return [];
if (!value || value.length === 0) return [];
if (
allowedMdxAttributes &&
!allowedMdxAttributes(element, attribute)
@@ -153,8 +164,8 @@ export function remarkStructure({
heading: lastHeading,
content:
attribute.type === 'mdxJsxAttribute'
? `${attribute.name}: ${valueStr}`
: valueStr,
? `${attribute.name}: ${value}`
: value,
};
}),
);