naive-ui/demo/loaders/NaiveUIMdLoader.js

30 lines
946 B
JavaScript
Raw Normal View History

2019-10-10 22:38:29 +08:00
const marked = require('marked')
2019-12-22 15:31:04 +08:00
const renderer = require('./mdRenderer')
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;">
<div style="width: calc(100% - 148px); margin-right: 16px;">
${marked(content, {
renderer
})}
</div>
<div style="width: 132px;">
${parseMdAsAnchor(content)}
</div>
</div>
</component-documentation>
</template>`
2019-10-10 22:38:29 +08:00
}