Files
supabase/apps/docs/lib/mdx/plugins/remarkRemoveTitle.ts
2024-07-23 09:55:41 +08:00

24 lines
586 B
TypeScript

import type { Parent } from 'mdast'
/**
* Removes the top heading from a MD file if
* it is the first node and it matches `title`.
*
* Useful when rendering title separately from MD
* and you need to remove the duplicate.
*/
export function removeTitle(title: string) {
return function transformer(root: Parent) {
const [firstNode] = root.children
if (firstNode?.type === 'heading') {
const [text] = firstNode.children
if (text?.type === 'text' && text.value === title) {
// Remove this node
root.children.splice(0, 1)
}
}
}
}