From 3cec536c1f3d5d027ee16cd0629f84461e565096 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 28 Jun 2021 10:49:38 -0400 Subject: [PATCH] fix: automatically escape vite user defined variables in markdown --- src/node/markdownToVue.ts | 28 +++++++++++++++++++++------- src/node/plugin.ts | 23 ++++++++++++++++------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 5c49ce2fd76d..36ca9ff8c154 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -11,17 +11,18 @@ import chalk from 'chalk' const debug = require('debug')('vitepress:md') const cache = new LRUCache({ max: 1024 }) -interface MarkdownCompileResult { +export interface MarkdownCompileResult { vueSrc: string pageData: PageData deadLinks: string[] } export function createMarkdownToVueRenderFn( - root: string, srcDir: string, options: MarkdownOptions = {}, - pages: string[] + pages: string[], + userDefines: Record | undefined, + isBuild = false ) { const md = createMarkdownRenderer(srcDir, options) pages = pages.map((p) => slash(p.replace(/\.md$/, ''))) @@ -44,10 +45,23 @@ export function createMarkdownToVueRenderFn( const { content, data: frontmatter } = matter(src) let { html, data } = md.render(content) - // avoid env variables being replaced by vite - html = html - .replace(/import\.meta/g, 'import.meta') - .replace(/process\.env/g, 'process.env') + if (isBuild) { + // avoid env variables being replaced by vite + html = html + .replace(/\bimport\.meta/g, 'import.meta') + .replace(/\bprocess\.env/g, 'process.env') + + // also avoid replacing vite user defines + if (userDefines) { + const regex = new RegExp( + `\\b(${Object.keys(userDefines) + .map((key) => key.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')) + .join('|')})`, + 'g' + ) + html = html.replace(regex, (_) => `${_[0]}${_.slice(1)}`) + } + } // validate data.links const deadLinks = [] diff --git a/src/node/plugin.ts b/src/node/plugin.ts index a68475614021..ee8692328971 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -1,7 +1,10 @@ import path from 'path' import { mergeConfig, Plugin, ResolvedConfig } from 'vite' import { SiteConfig, resolveSiteData } from './config' -import { createMarkdownToVueRenderFn } from './markdownToVue' +import { + createMarkdownToVueRenderFn, + MarkdownCompileResult +} from './markdownToVue' import { APP_PATH, SITE_DATA_REQUEST_PATH } from './alias' import createVuePlugin from '@vitejs/plugin-vue' import { slash } from './utils/slash' @@ -38,12 +41,11 @@ export function createVitePressPlugin( ssr = false, pageToHashMap?: Record ): Plugin[] { - const markdownToVue = createMarkdownToVueRenderFn( - root, - srcDir, - markdown, - pages - ) + let markdownToVue: ( + src: string, + file: string, + publicDir: string + ) => MarkdownCompileResult const vuePlugin = createVuePlugin({ include: [/\.vue$/, /\.md$/], @@ -59,6 +61,13 @@ export function createVitePressPlugin( configResolved(resolvedConfig) { config = resolvedConfig + markdownToVue = createMarkdownToVueRenderFn( + srcDir, + markdown, + pages, + config.define, + config.command === 'build' + ) }, config() {