@mdx-js/language-service
Version:
MDX support for Volar
34 lines (29 loc) • 830 B
JavaScript
/**
* @import {VirtualCodePlugin} from './plugin.js'
*/
/**
* @param {unknown} options
* @returns {VirtualCodePlugin}
*/
export function rehypeMdxTitle(options) {
const opts = typeof options === 'object' && options !== null ? options : {}
const maxRank = 'maxRank' in opts ? Number(opts.maxRank) : 1
const name = 'name' in opts ? String(opts.name) : 'title'
return () => {
let hasTitle = false
return {
visit(node) {
hasTitle ||= node.type === 'heading' && node.depth <= maxRank
},
finalize() {
return (
'/** Generated by [rehype-mdx-title](https://github.com/remcohaszing/rehype-mdx-title) */\nexport const ' +
name +
' = /** @type {string' +
(hasTitle ? '' : ' | undefined') +
"} */ ('')"
)
}
}
}
}