/
markdown.ts
68 lines (54 loc) · 2.1 KB
/
markdown.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 remarkFrontmatter from 'remark-frontmatter'
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,
}: {
mdString: string
options?: MarkdownOptions
}): T.Effect<OT.HasTracer & HasConsole, UnexpectedMarkdownError, string> =>
pipe(
T.gen(function* ($) {
// const matterResult = matter(mdString)
// Use remark to convert markdown into HTML string
// const processedContent = await remark().use(html).process(matterResult.content)
if (process.env['CL_FAST_MARKDOWN']) {
// NOTE `markdown-wasm` is an optional peer dependency
return yield* $(
T.tryPromise(async () => {
const { parse: parseWasm } = await import('markdown-wasm/dist/markdown.node.js')
return parseWasm(mdString)
}),
)
}
const builder = unified()
// parses out the frontmatter (which is needed for full-document parsing)
builder.use(remarkFrontmatter)
// parse markdown
builder.use(remarkParse as any)
if (options?.remarkPlugins) {
builder.use(options.remarkPlugins)
}
builder.use(remark2rehype)
if (options?.rehypePlugins) {
builder.use(options.rehypePlugins)
}
// rehype to html
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)}`
}