-
Notifications
You must be signed in to change notification settings - Fork 170
/
markdown.ts
59 lines (53 loc) · 2.01 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
import type { Aleph, LoadInput, LoadOutput, ResolveResult, Plugin } from '../types.d.ts'
import marked from 'https://esm.sh/marked@2.0.1'
import { safeLoadFront } from 'https://esm.sh/yaml-front-matter@4.1.1'
import util from '../shared/util.ts'
export const test = /\.(md|markdown)$/i
export const markdownResovler = (specifier: string): ResolveResult => {
let pagePath = util.trimPrefix(specifier.replace(/\.(md|markdown)$/i, ''), '/pages')
let isIndex = pagePath.endsWith('/index')
if (isIndex) {
pagePath = util.trimSuffix(pagePath, '/index')
if (pagePath === '') {
pagePath = '/'
}
}
return { asPage: { path: pagePath, isIndex } }
}
export const markdownLoader = async ({ specifier }: LoadInput, aleph: Aleph): Promise<LoadOutput> => {
const { framework } = aleph.config
const { content } = await aleph.fetchModule(specifier)
const { __content, ...meta } = safeLoadFront((new TextDecoder).decode(content))
const html = marked.parse(__content)
const props = {
id: util.isString(meta.id) ? meta.id : undefined,
className: util.isString(meta.className) ? meta.className : undefined,
style: util.isPlainObject(meta.style) ? meta.style : undefined,
}
if (framework === 'react') {
return {
code: [
`import { createElement } from 'https://esm.sh/react'`,
`import HTMLPage from 'https://deno.land/x/aleph/framework/react/components/HTMLPage.ts'`,
`export default function MarkdownPage(props) {`,
` return createElement(HTMLPage, {`,
` ...${JSON.stringify(props)},`,
` ...props,`,
` html: ${JSON.stringify(html)}`,
` })`,
`}`,
`MarkdownPage.meta = ${JSON.stringify(meta)}`,
].join('\n')
}
}
throw new Error(`markdown-loader: don't support framework '${framework}'`)
}
export default (): Plugin => {
return {
name: 'markdown-loader',
setup: aleph => {
aleph.onResolve(test, markdownResovler)
aleph.onLoad(test, input => markdownLoader(input, aleph))
}
}
}