From c4b5c5f34af7524d20689f36979cf9491bc25a2c Mon Sep 17 00:00:00 2001 From: jlpereira Date: Tue, 15 Aug 2023 12:39:41 -0300 Subject: [PATCH 1/2] Replace internal vars in markdown --- src/plugins/markdown/index.js | 2 ++ src/plugins/markdown/relativeToRouter.js | 8 ++++++-- src/plugins/markdown/variableReplacement.js | 14 ++++++++++++++ vite.config.js | 10 ++++++++-- 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 src/plugins/markdown/index.js create mode 100644 src/plugins/markdown/variableReplacement.js diff --git a/src/plugins/markdown/index.js b/src/plugins/markdown/index.js new file mode 100644 index 0000000..3e71ce7 --- /dev/null +++ b/src/plugins/markdown/index.js @@ -0,0 +1,2 @@ +export * from './relativeToRouter' +export * from './variableReplacement' diff --git a/src/plugins/markdown/relativeToRouter.js b/src/plugins/markdown/relativeToRouter.js index 203676f..8fa9ebb 100644 --- a/src/plugins/markdown/relativeToRouter.js +++ b/src/plugins/markdown/relativeToRouter.js @@ -1,4 +1,4 @@ -export default (md) => { +export function relativeToRouterPlugin(md) { const scan = (state) => { state.tokens.forEach((tokens) => { if (tokens.type !== 'inline') { @@ -13,7 +13,11 @@ export default (md) => { } else if (inlineTokens[i].type === 'link_open') { const attrs = inlineTokens[i].attrs const href = attrs?.find((v) => v[0] === 'href') - if (href && !href[1].startsWith('http')) { + if ( + href && + !href[1].startsWith('http') && + !href[1].startsWith('mailto') + ) { inlineTokens[i].tag = 'router-link' inlineTokens[i].attrs = [['to', href[1]]] isRT = true diff --git a/src/plugins/markdown/variableReplacement.js b/src/plugins/markdown/variableReplacement.js new file mode 100644 index 0000000..84f6732 --- /dev/null +++ b/src/plugins/markdown/variableReplacement.js @@ -0,0 +1,14 @@ +export function variableReplacementPlugin(md, options) { + const regex = /{{\s*app:(.*?)\s*}}/g + const variables = options.variables || {} + + md.core.ruler.before('normalize', 'variable_replacement', (state) => { + state.src = state.src.replace(regex, (match, content) => { + if (Object.hasOwn(variables, content)) { + return variables[content] + } + + return match + }) + }) +} diff --git a/vite.config.js b/vite.config.js index 2040750..ea68340 100644 --- a/vite.config.js +++ b/vite.config.js @@ -4,7 +4,10 @@ import path from 'path' import Vue from '@vitejs/plugin-vue' import Markdown from 'vite-plugin-md' import markdownAnchor from 'markdown-it-anchor' -import markdownRelativeToRouter from './src/plugins/markdown/relativeToRouter.js' +import { + relativeToRouterPlugin, + variableReplacementPlugin +} from './src/plugins/markdown' import Pages from 'vite-plugin-pages' import './src/utils/globalVars' @@ -31,8 +34,11 @@ export default () => { '!container mx-auto p-4 sm:pl-0 sm:pr-0 prose dark:prose-invert box-border', markdownItSetup(md) { md.use(markdownAnchor) + md.use(variableReplacementPlugin, { + variables: { ...configuration } + }) }, - markdownItUses: [markdownRelativeToRouter] + markdownItUses: [relativeToRouterPlugin] }), Pages({ dirs: 'pages', From 01bdca4e073f03497ff50e33e777baa668837fbd Mon Sep 17 00:00:00 2001 From: jlpereira Date: Tue, 15 Aug 2023 12:45:27 -0300 Subject: [PATCH 2/2] Update README --- README.markdown | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 1f0a72e..0c8b0ab 100644 --- a/README.markdown +++ b/README.markdown @@ -92,7 +92,15 @@ name: 'Charles Darwin' # Welcome, {{ name }}! ``` -To use access TaxonPage internal variables that are provided in `config/*.yml` you can use the script tag in your markdown page and get them from the global object `__APP_ENV__` +To use TaxonPage internal variables in `config/*.yml`, you can either do so by adding the prefix {{ app:var_name }} or use the script tag in your markdown page and get them from the global object `__APP_ENV__` + +#### Prefix + +```markdown +# Welcome to {{ app:project_name }}! +``` + +#### Script tag ```javascript # Welcome to {{ project_name }}!