naive-ui/build/loaders/naive-ui-md-loader.js

72 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-10-10 22:38:29 +08:00
const marked = require('marked')
const createRenderer = require('./md-renderer')
2019-12-22 23:19:08 +08:00
const renderer = createRenderer()
const projectPath = require('./project-path')
2019-10-10 22:38:29 +08:00
2020-02-13 19:14:30 +08:00
function parseMdAsAnchor (tokens) {
2019-12-22 15:31:04 +08:00
const titles = tokens.filter(token => token.type === 'heading' && token.depth === 2).map(token => token.text)
const linkTags = titles.map(title => {
const href = title.replace(/ /g, '-')
return `<n-anchor-link title="${title}" href="#${href}"/>`
})
2020-03-05 22:31:20 +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) {
const componentsIndex = tokens.findIndex(token => token.type === 'code' && token.lang === 'component')
let components = []
if (~componentsIndex) {
components = tokens[componentsIndex].text
components = components.split('\n').map(component => component.trim()).filter(component => component.length)
tokens.splice(componentsIndex, 1)
}
return components
}
module.exports = function (content, path) {
const relativeURL = path.replace(projectPath + '/', '')
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)
2020-03-19 13:26:06 +08:00
const titleIndex = tokens.findIndex(token => token.type === 'heading' && token.depth === 1)
2020-11-03 15:10:29 +08:00
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
.map(component => `import ${component} from './${component}'`)
.join('\n')
2020-03-19 13:26:06 +08:00
let mdContent = marked.parser(tokens, { renderer })
if (titleText) {
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>
${
showAnchor ? `
2020-02-22 20:25:10 +08:00
<div style="width: 144px;">
2020-02-13 19:14:30 +08:00
${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
}