Implement plugin to convert gitbook rich-quotes to custom containers

This commit is contained in:
Oleg Kalachev
2022-04-12 01:01:34 +04:00
parent 4190353569
commit 3b3b5b6a89
2 changed files with 39 additions and 1 deletions

View File

@@ -44,6 +44,7 @@ module.exports = {
},
plugins: [
'@vuepress/plugin-search',
'vuepress-plugin-copy-code2'
'vuepress-plugin-copy-code2',
require('./rich-quotes')
]
}

View File

@@ -0,0 +1,37 @@
// Plugin to convert GitBook rich quotes to custom containers
const types = {
info: 'tip',
note: 'tip',
tag: 'tip',
comment: 'tip',
hint: 'tip',
success: 'tip',
warning: 'warning',
caution: 'warning',
danger: 'danger',
quote: 'tip'
}
function replace(src) {
return src.replace(/^> \*\*(.*?)\*\* (.*\n(>.*\n)*)/gm, function (match, type, text) {
text = text.replace(/^>/gm, '');
return `::: ${types[type.toLowerCase()]}\n${text}\n:::`;
});
}
module.exports = {
name: 'vuepress-plugin-rich-quotes',
extendsMarkdown: (md) => {
var _render = md.render;
// TODO: a rough hack to replace rich quotes
// TODO: use proper plugin api
md.render = function(src, env) {
src = replace(src);
return _render.call(md, src, env);
}
},
};