VuePress 当前在 webpack 管道中处理 markdown 文件。这个过程的要点是:
.md -> markdown-loader(VuePress 自定义) -> vue-loader -> ...
我将展示一个示例,说明如何使用 Nunjucks 渲染 markdown 模板在它被传递到 markdown 加载器进行编译。
为了预处理降价,我们需要在第一个 VuePress 加载器之前插入一个加载器。幸运的是,我们可以通过公开的插件/配置 API 做到这一点:
.vuepress/config.js
chainWebpack: config => {
config.module
.rule('md')
.test(/\.md$/)
.use(path.resolve(__dirname, './nunjucks'))
.loader(path.resolve(__dirname, './nunjucks'))
.end()
},
.vuepress/nunjucks.js
const nunjucks = require('nunjucks')
// Change start/end tokens so they don't collide with the Vue syntax
nunjucks.configure({
autoescape: false,
tags: {
blockStart: '{{{%',
blockEnd: '%}}}',
variableStart: '{{{',
variableEnd: '}}}',
commentStart: '{{{#',
commentEnd: '#}}}'
}
})
const config = {
apiVersion: 32,
}
module.exports = function(source) {
const rendered = nunjucks.renderString(source, config)
return rendered
}