2019-10-10 22:38:29 +08:00
|
|
|
const marked = require('marked')
|
2020-11-12 13:30:04 +08:00
|
|
|
const createRenderer = require('./md-renderer')
|
2019-12-22 23:19:08 +08:00
|
|
|
const renderer = createRenderer()
|
2020-09-07 14:03:37 +08:00
|
|
|
const projectPath = require('./project-path')
|
2019-10-10 22:38:29 +08:00
|
|
|
|
2020-02-13 19:14:30 +08:00
|
|
|
function parseMdAsAnchor (tokens) {
|
2021-03-15 15:25:11 +08:00
|
|
|
const titles = tokens
|
|
|
|
.filter((token) => token.type === 'heading' && token.depth === 2)
|
|
|
|
.map((token) => token.text)
|
|
|
|
const linkTags = titles.map((title) => {
|
2019-12-22 15:31:04 +08:00
|
|
|
const href = title.replace(/ /g, '-')
|
|
|
|
return `<n-anchor-link title="${title}" href="#${href}"/>`
|
|
|
|
})
|
2021-03-15 15:25:11 +08:00
|
|
|
return `<n-anchor ignore-gap :top="32" position="absolute" affix style="width: 144px;">${linkTags.join(
|
|
|
|
'\n'
|
|
|
|
)}</n-anchor>`
|
2019-10-10 22:38:29 +08:00
|
|
|
}
|
|
|
|
|
2020-02-13 19:14:30 +08:00
|
|
|
function parseComponents (tokens) {
|
2021-03-15 15:25:11 +08:00
|
|
|
const componentsIndex = tokens.findIndex(
|
|
|
|
(token) => token.type === 'code' && token.lang === 'component'
|
|
|
|
)
|
2020-02-13 19:14:30 +08:00
|
|
|
let components = []
|
|
|
|
if (~componentsIndex) {
|
|
|
|
components = tokens[componentsIndex].text
|
2021-03-15 15:25:11 +08:00
|
|
|
components = components
|
|
|
|
.split('\n')
|
|
|
|
.map((component) => component.trim())
|
|
|
|
.filter((component) => component.length)
|
2020-02-13 19:14:30 +08:00
|
|
|
tokens.splice(componentsIndex, 1)
|
|
|
|
}
|
|
|
|
return components
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:03:02 +08:00
|
|
|
module.exports = function (content, path) {
|
|
|
|
const relativeURL = path.replace(projectPath + '/', '')
|
2020-08-19 23:30:04 +08:00
|
|
|
|
|
|
|
const showAnchor = !!~content.search('<!--anchor:on-->')
|
|
|
|
// for marked bug https://github.com/markedjs/marked/issues/1047
|
|
|
|
content = content.replace(/\n#/g, '\n\n#')
|
2020-02-13 19:14:30 +08:00
|
|
|
const tokens = marked.lexer(content)
|
2021-03-15 15:25:11 +08:00
|
|
|
const titleIndex = tokens.findIndex(
|
|
|
|
(token) => token.type === 'heading' && token.depth === 1
|
|
|
|
)
|
|
|
|
const titleText =
|
|
|
|
titleIndex > -1 ? JSON.stringify(tokens[titleIndex].text) : null
|
2020-02-13 19:14:30 +08:00
|
|
|
const anchor = parseMdAsAnchor(tokens)
|
|
|
|
const components = parseComponents(tokens)
|
|
|
|
const importStatements = components
|
2021-03-15 15:25:11 +08:00
|
|
|
.map((component) => `import ${component} from './${component}'`)
|
2020-02-13 19:14:30 +08:00
|
|
|
.join('\n')
|
2020-03-19 13:26:06 +08:00
|
|
|
let mdContent = marked.parser(tokens, { renderer })
|
|
|
|
if (titleText) {
|
2020-10-26 15:03:02 +08:00
|
|
|
const githubButton = `<edit-on-github-header relative-url="${relativeURL}" text=${titleText}></edit-on-github-header>`
|
2020-03-19 13:26:06 +08:00
|
|
|
const titleReg = /(<n-h1[^>]*>)(.*?)(<\/n-h1>)/
|
2020-09-04 01:06:39 +08:00
|
|
|
mdContent = mdContent.replace(titleReg, `${githubButton}`)
|
2020-03-16 13:01:13 +08:00
|
|
|
}
|
2020-03-19 13:26:06 +08:00
|
|
|
return `
|
2020-03-16 13:01:13 +08:00
|
|
|
<template>
|
2019-12-22 15:31:04 +08:00
|
|
|
<component-documentation>
|
|
|
|
<div style="display: flex; flex-wrap: nowrap;">
|
2020-02-22 20:25:10 +08:00
|
|
|
<div style="width: calc(100% - 180px); margin-right: 36px;">
|
2020-03-19 13:26:06 +08:00
|
|
|
${mdContent}
|
2019-12-22 15:31:04 +08:00
|
|
|
</div>
|
2021-03-15 15:25:11 +08:00
|
|
|
${showAnchor ? `<div style="width: 144px;">${anchor}</div>` : ''}
|
2019-12-22 15:31:04 +08:00
|
|
|
</div>
|
|
|
|
</component-documentation>
|
2020-02-13 19:14:30 +08:00
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
${importStatements}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
${components.join(',\n')}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>`
|
2019-10-10 22:38:29 +08:00
|
|
|
}
|