From 3b3b5b6a895439aa997259ca42549ea3cd421049 Mon Sep 17 00:00:00 2001 From: Oleg Kalachev Date: Tue, 12 Apr 2022 01:01:34 +0400 Subject: [PATCH] Implement plugin to convert gitbook rich-quotes to custom containers --- docs/.vuepress/config.js | 3 ++- docs/.vuepress/rich-quotes.js | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 docs/.vuepress/rich-quotes.js diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index b1070940..173d538c 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -44,6 +44,7 @@ module.exports = { }, plugins: [ '@vuepress/plugin-search', - 'vuepress-plugin-copy-code2' + 'vuepress-plugin-copy-code2', + require('./rich-quotes') ] } diff --git a/docs/.vuepress/rich-quotes.js b/docs/.vuepress/rich-quotes.js new file mode 100644 index 00000000..ed942e70 --- /dev/null +++ b/docs/.vuepress/rich-quotes.js @@ -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); + } + }, + +};