2020-08-13 11:47:56 +08:00
|
|
|
import { resolve, relative, join } from 'path'
|
|
|
|
import vfile from 'vfile'
|
|
|
|
import remark from 'remark'
|
|
|
|
import slug from 'remark-slug'
|
|
|
|
import frontmatter from 'remark-frontmatter'
|
|
|
|
import shortcodes from 'remark-shortcodes'
|
|
|
|
import remarkToRehype from 'remark-rehype'
|
|
|
|
import raw from 'rehype-raw'
|
|
|
|
import html from 'rehype-stringify'
|
2021-09-25 15:17:57 +08:00
|
|
|
import highlight from 'rehype-highlight'
|
2020-08-13 11:47:56 +08:00
|
|
|
import etpl from 'etpl'
|
|
|
|
import { readFileSync, writeFileSync, replaceExtSync } from './util'
|
|
|
|
import demo from './remark-demo'
|
|
|
|
import ref from './remark-ref'
|
2021-10-20 01:11:27 +08:00
|
|
|
import anchor from './remark-anchor'
|
2020-08-13 11:47:56 +08:00
|
|
|
import details from './remark-details'
|
|
|
|
import custom from './remark-custom'
|
|
|
|
import extractFrontmatter from './remark-extract-frontmatter'
|
|
|
|
import rehypePreviewImg from './rehype-preview-img'
|
|
|
|
import rehypeLink from './rehype-link'
|
2021-09-25 15:17:57 +08:00
|
|
|
import rehypeScoped from './rehype-scoped'
|
2020-08-13 11:47:56 +08:00
|
|
|
import rehypeDemo from './rehype-demo'
|
2021-09-25 15:17:57 +08:00
|
|
|
import rehypePre from './rehype-pre'
|
2020-08-13 11:47:56 +08:00
|
|
|
import { add } from './deps'
|
|
|
|
import { vue } from './language'
|
|
|
|
|
|
|
|
const DOCS_DIR = resolve(__dirname, '../docs')
|
|
|
|
const PAGES_DIR = resolve(__dirname, '../../pages')
|
|
|
|
const PAGE_TPL = readFileSync(resolve(__dirname, '../templates/page.etpl'))
|
|
|
|
|
|
|
|
const renderPage = etpl.compile(PAGE_TPL)
|
|
|
|
|
|
|
|
const md = remark()
|
2021-10-20 01:11:27 +08:00
|
|
|
.use(anchor)
|
2020-08-13 11:47:56 +08:00
|
|
|
.use(custom)
|
|
|
|
.use(details)
|
|
|
|
.use(ref)
|
|
|
|
.use(frontmatter)
|
|
|
|
.use(shortcodes)
|
|
|
|
.use(demo)
|
|
|
|
.use(extractFrontmatter)
|
|
|
|
.use(slug)
|
|
|
|
.use(remarkToRehype, { allowDangerousHTML: true })
|
|
|
|
.use(raw)
|
|
|
|
.use(rehypePreviewImg)
|
|
|
|
.use(rehypeLink)
|
2021-09-25 15:17:57 +08:00
|
|
|
.use(rehypeScoped)
|
2020-08-13 11:47:56 +08:00
|
|
|
.use(rehypeDemo)
|
2021-09-25 15:17:57 +08:00
|
|
|
.use(highlight, { languages: { vue } })
|
|
|
|
.use(rehypePre)
|
2020-08-13 11:47:56 +08:00
|
|
|
.use(html, { allowDangerousHTML: true })
|
|
|
|
|
|
|
|
export function render (contents, path, data = {}) {
|
|
|
|
return md.processSync(vfile({ contents, path, data }))
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderFile (file) {
|
|
|
|
return render(readFileSync(file), file)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderDocToPage (file) {
|
|
|
|
let src = resolve(DOCS_DIR, file)
|
|
|
|
let dest = resolve(PAGES_DIR, replaceExtSync(file, 'vue'))
|
|
|
|
let { contents, data } = renderFile(src, dest)
|
2021-09-25 15:17:57 +08:00
|
|
|
let {
|
|
|
|
demos = {},
|
|
|
|
components = {},
|
|
|
|
meta = {},
|
|
|
|
deps = {},
|
|
|
|
hasAlert = false
|
|
|
|
} = data
|
2020-08-13 11:47:56 +08:00
|
|
|
|
|
|
|
Object.keys(deps || {}).forEach(dep => {
|
|
|
|
add({ [dep]: { [src]: true } })
|
|
|
|
})
|
|
|
|
|
|
|
|
let { layout, style } = meta
|
|
|
|
let componentList = Object.keys(components)
|
|
|
|
let demoList = Object.keys(demos)
|
|
|
|
let result = renderPage({
|
2021-09-25 15:17:57 +08:00
|
|
|
content: (contents || '')
|
|
|
|
.replace(/\n{3,}/g, '\n\n')
|
|
|
|
.replace(/\{/g, '{')
|
|
|
|
.replace(/\}/g, '}')
|
|
|
|
.replace(/v-pre="true"/g, 'v-pre')
|
|
|
|
.replace(/data-markdown="true"/g, 'data-markdown'),
|
2020-08-13 11:47:56 +08:00
|
|
|
demos: demoList.map(name => {
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
src: join('@/components/demos', relative(DOCS_DIR, demos[name].path))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
components: componentList,
|
2021-08-25 17:00:13 +08:00
|
|
|
alert: hasAlert,
|
|
|
|
boilerplate: demoList.length || componentList.length || hasAlert,
|
2020-08-13 11:47:56 +08:00
|
|
|
layout,
|
|
|
|
style: style || 'post'
|
|
|
|
})
|
|
|
|
|
|
|
|
writeFileSync(dest, result)
|
|
|
|
}
|