import { errorToString } from '@contentlayer/utils' ; import type { HasConsole } from '@contentlayer/utils/effect' ; import { OT , pipe , T , Tagged } from '@contentlayer/utils/effect' ; import rehypeStringify from 'rehype-stringify' ; import remarkParse from 'remark-parse' ; import remark2rehype from 'remark-rehype' ; import { unified } from 'unified' ; import type { MarkdownOptions } from './plugin.js' ; export const markdownToHtml = ( { mdString , options , } ) : T . Effect < OT . HasTracer & HasConsole , UnexpectedMarkdownError , string > => pipe ( T . gen ( function * ( $ ) { if ( process . env [ 'CL_FAST_MARKDOWN' ] ) { return yield * $ ( T . tryPromise ( async ( ) => { const { parse : parseWasm } = await import ( 'markdown-wasm/dist/markdown.node.js' ) ; return parseWasm ( mdString ) ; } ) ) ; } const builder = unified ( ) . use ( remarkParse as any ) ; if ( options ?. remarkPlugins ) { builder . use ( options . remarkPlugins ) ; } builder . use ( remark2rehype ) ; if ( options ?. rehypePlugins ) { builder . use ( options . rehypePlugins ) ; } builder . use ( rehypeStringify as any ) ; const res = yield * $ ( T . tryPromise ( ( ) => builder . process ( mdString ) ) ) ; return res . toString ( ) ; } ) , T . catchAllDefect ( T . fail ) , T . mapError ( ( error ) => new UnexpectedMarkdownError ( { error } ) ) , OT . withSpan ( '@contentlayer/core/markdown:markdownToHtml' ) ) ; export class UnexpectedMarkdownError extends Tagged ( 'UnexpectedMarkdownError' ) < { readonly error : unknown ; } > { toString = ( ) => `UnexpectedMarkdownError: ${ errorToString ( this . error ) }` ; }