45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
import { visit } from 'unist-util-visit';
|
|
|
|
export default function calloutDirective() {
|
|
return (tree) => {
|
|
visit(tree, (node) => {
|
|
// Handle textDirective, leafDirective, and containerDirective callouts
|
|
if (
|
|
node.type === 'textDirective' ||
|
|
node.type === 'leafDirective' ||
|
|
node.type === 'containerDirective'
|
|
) {
|
|
const name = node.name;
|
|
|
|
// Only handle known callout types
|
|
const validTypes = ['note', 'tip', 'warning', 'danger'];
|
|
if (!validTypes.includes(name)) return;
|
|
|
|
// Extract attributes (e.g., title="...")
|
|
const attrs = Object.entries(node.attributes ?? {}).map(([name, value]) => ({
|
|
type: 'mdxJsxAttribute',
|
|
name,
|
|
value,
|
|
}));
|
|
|
|
// Add type attribute
|
|
attrs.push({
|
|
type: 'mdxJsxAttribute',
|
|
name: 'type',
|
|
value: name,
|
|
});
|
|
|
|
// Build children from node's children
|
|
const children = node.children || [];
|
|
|
|
// Transform to mdxJsxFlowElement
|
|
node.type = 'mdxJsxFlowElement';
|
|
node.name = 'Callout';
|
|
node.attributes = attrs;
|
|
node.children = children;
|
|
node.data = { hName: 'Callout', hProperties: {} };
|
|
}
|
|
});
|
|
};
|
|
}
|