import MarkdownIt from 'markdown-it' ; import { parseHeader } from '../utils/parseHeader' ; import { highlight } from './plugins/highlight' ; import { slugify } from './plugins/slugify' ; import { highlightLinePlugin } from './plugins/highlightLines' ; import { lineNumberPlugin } from './plugins/lineNumbers' ; import { componentPlugin } from './plugins/component' ; import { containerPlugin } from './plugins/containers' ; import { snippetPlugin } from './plugins/snippet' ; import { hoistPlugin } from './plugins/hoist' ; import { preWrapperPlugin } from './plugins/preWrapper' ; import { linkPlugin } from './plugins/link' ; import { headingPlugin } from './plugins/headings' ; import { Header } from '../shared' ; import anchor from 'markdown-it-anchor' ; import attrs from 'markdown-it-attrs' ; import emoji from 'markdown-it-emoji' ; import toc from 'markdown-it-table-of-contents' ; export interface MarkdownOptions extends MarkdownIt . Options { lineNumbers ? : boolean ; config ? : ( md : MarkdownIt ) => void ; anchor ? : { permalink ? : anchor . AnchorOptions [ 'permalink' ] ; } ; attrs ? : { leftDelimiter ? : string ; rightDelimiter ? : string ; allowedAttributes ? : string [ ] ; } ; toc ? : any ; externalLinks ? : Record < string , string > ; } export interface MarkdownParsedData { hoistedTags ? : string [ ] ; links ? : string [ ] ; headers ? : Header [ ] ; } export interface MarkdownRenderer extends MarkdownIt { __path : string ; __relativePath : string ; __data : MarkdownParsedData ; } export type { Header } ; export const createMarkdownRenderer = ( srcDir , options = { } ) : MarkdownRenderer => { const md = MarkdownIt ( { html : true , linkify : true , highlight , ... options } ) as MarkdownRenderer ; md . use ( componentPlugin ) . use ( highlightLinePlugin ) . use ( preWrapperPlugin ) . use ( snippetPlugin , srcDir ) . use ( hoistPlugin ) . use ( containerPlugin ) . use ( headingPlugin ) . use ( linkPlugin , { target : '_blank' , rel : 'noopener noreferrer' , ... options . externalLinks } ) . use ( attrs , options . attrs ) . use ( anchor , { slugify , permalink : anchor . permalink . ariaHidden ( { } ) , ... options . anchor } ) . use ( toc , { slugify , includeLevel : [ 2 , 3 ] , format : parseHeader , ... options . toc } ) . use ( emoji ) ; if ( options . config ) { options . config ( md ) ; } if ( options . lineNumbers ) { md . use ( lineNumberPlugin ) ; } const originalRender = md . render ; md . render = ( ... args ) => { md . __data = { } ; return originalRender . call ( md , ... args ) ; } ; return md ; } ;