import { Node , NodeWalker , NodeWalkingStep } from "commonmark" ; export function getNodeText ( root ) { let text = "" ; walkMarkdown ( root , node => { if ( node . literal ) { text += node . literal ; } } ) ; return text ; } export function walkMarkdown ( root , callback ) { const walker = root . walker ( ) ; while ( true ) { const event = nextStep ( walker ) ; if ( event === null ) { break ; } const { node } = event ; if ( event . entering ) { callback ( node ) ; } } } export function createNodeId ( node ) { return encodeURIComponent ( getNodeText ( node ) . toLowerCase ( ) . split ( /[^A-Za-z0-9]+/g ) . filter ( x => x !== "" ) . join ( "-" ) ) ; } export function nextStep ( walker ) { return walker . next ( ) as NodeWalkingStep | null ; }