import rehype from 'rehype' ; /**
 * Custom Rehype Parser types as rehype's are lacking
 */ export type RehypeNode = { type : 'element' | 'text' | 'comment' ; tagName : string ; properties ? : { className ? : string [ ] ; } ; children ? : RehypeNode [ ] ; value ? : string ; } ; export type RehypeRootNode = { type : 'root' ; children ? : RehypeNode [ ] ; } ; param code param indexes param className export function updateNodeClass ( code , indexes , className ) { const ast = parseCode ( code ) ; const selectedNode = getSelectedNode ( ast , indexes ) ; if ( selectedNode . properties && className ) { selectedNode . properties . className = className . split ( ' ' ) ; } else if ( ! className && selectedNode . properties . className ) { delete selectedNode . properties . className ; } const newCode = rehype ( ) . stringify ( ast ) ; return newCode ; } export function parseCode ( code ) { const ast = rehype ( ) . data ( 'settings' , { fragment : true , } ) . parse ( code ) ; return ast as RehypeNode ; } function getSelectedNode ( rootNode , indexes ) { const selectedNode = indexes . reduce ( ( acc , index ) => { const children = acc . children . filter ( ( child ) => child . type === 'element' ) ; return children [ index ] ; } , rootNode ) ; return selectedNode ; }