naive-ui/demo/loaders/NaiveUIMdLoader.js

31 lines
973 B
JavaScript
Raw Normal View History

2019-10-10 22:38:29 +08:00
const marked = require('marked')
2019-12-22 23:19:08 +08:00
const createRenderer = require('./mdRenderer')
const renderer = createRenderer()
2019-10-10 22:38:29 +08:00
2019-12-22 15:31:04 +08:00
function parseMdAsAnchor (content) {
const tokens = marked.lexer(content)
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}"/>`
})
return `<n-anchor :top="24" position="absolute" affix style="width: 132px;">${linkTags.join('\n')}</n-anchor>`
2019-10-10 22:38:29 +08:00
}
module.exports = function (content) {
2019-12-22 15:31:04 +08:00
return `<template>
<component-documentation>
<div style="display: flex; flex-wrap: nowrap;">
2019-12-22 15:56:22 +08:00
<div style="width: calc(100% - 184px); margin-right: 24x;">
2019-12-22 15:31:04 +08:00
${marked(content, {
2019-12-23 21:40:45 +08:00
renderer
})}
2019-12-22 15:31:04 +08:00
</div>
2019-12-22 15:56:22 +08:00
<div style="width: 160px;">
2019-12-22 15:31:04 +08:00
${parseMdAsAnchor(content)}
</div>
</div>
</component-documentation>
</template>`
2019-10-10 22:38:29 +08:00
}