diff --git a/docs/.vuepress/config.ts b/docs/.vuepress/config.ts index c8997415f5..8a54963d5f 100644 --- a/docs/.vuepress/config.ts +++ b/docs/.vuepress/config.ts @@ -71,6 +71,11 @@ export default defineUserConfig({ return importPath }, }, + image: { + figure: true, + mark: true, + size: true, + }, }, extendsMarkdown: (md) => { @@ -91,11 +96,7 @@ export default defineUserConfig({ json: true, rss: true, }), - markdownImagePlugin({ - figure: true, - mark: true, - size: true, - }), + markdownImagePlugin(), markdownMathPlugin(), redirectPlugin({ switchLocale: 'modal', diff --git a/docs/plugins/markdown-image.md b/docs/plugins/markdown-image.md index 74e9387799..b1187cb3b2 100644 --- a/docs/plugins/markdown-image.md +++ b/docs/plugins/markdown-image.md @@ -14,8 +14,9 @@ npm i -D @vuepress/plugin-markdown-image@next import { markdownImagePlugin } from '@vuepress/plugin-markdown-image' export default { - plugins: [ - markdownImagePlugin({ + markdown: { + // config the plugin + image: { // Enable figure figure: true, // Enable image lazyload @@ -24,7 +25,11 @@ export default { mark: true, // Enable image size size: true, - }), + }, + }, + plugins: [ + // apply the plugin + markdownImagePlugin(), ], } ``` @@ -111,6 +116,8 @@ If the image is standalone in a line, wrapped or not wrapped by link, it will be ## Options +You can set these options under `markdown.image` in your vuepress config file, or pass them to `markdownImagePlugin` directly. The latter has higher priority. + ### figure - Type: `boolean` diff --git a/docs/plugins/markdown-math.md b/docs/plugins/markdown-math.md index 4bd337bb43..1542279d79 100644 --- a/docs/plugins/markdown-math.md +++ b/docs/plugins/markdown-math.md @@ -20,10 +20,15 @@ npm i -D katex import { markdownMathPlugin } from '@vuepress/plugin-markdown-math' export default { - plugins: [ - markdownMathPlugin({ + markdown: { + // config the plugin + math: { // options - }), + }, + }, + plugins: [ + // apply the plugin + markdownMathPlugin(), ], } ``` @@ -92,6 +97,8 @@ Mathjax: ## Options +You can set these options under `markdown.math` in your vuepress config file, or pass them to `markdownMathPlugin` directly. The latter has higher priority. + ### type - Type: `'katex' | 'mathjax'` diff --git a/docs/zh/plugins/markdown-image.md b/docs/zh/plugins/markdown-image.md index 47edabba7e..d57085a915 100644 --- a/docs/zh/plugins/markdown-image.md +++ b/docs/zh/plugins/markdown-image.md @@ -14,8 +14,9 @@ npm i -D @vuepress/plugin-markdown-image@next import { markdownImagePlugin } from '@vuepress/plugin-markdown-image' export default { - plugins: [ - markdownImagePlugin({ + markdown: { + // 配置插件 + image: { // 启用 figure figure: true, // 启用图片懒加载 @@ -24,7 +25,11 @@ export default { mark: true, // 启用图片大小 size: true, - }), + }, + }, + plugins: [ + // 应用插件 + markdownImagePlugin(), ], } ``` @@ -111,6 +116,8 @@ interface ImageMarkOptions { ## 配置项 +你可以在 VuePress 配置文件中的 `markdown.image` 字段中配置此插件,也可以在调用时传入配置,后者具有更高优先级。 + ### figure - 类型:`boolean` diff --git a/docs/zh/plugins/markdown-math.md b/docs/zh/plugins/markdown-math.md index a0bd2c4670..85ccacb52a 100644 --- a/docs/zh/plugins/markdown-math.md +++ b/docs/zh/plugins/markdown-math.md @@ -20,10 +20,15 @@ npm i -D katex import { markdownMathPlugin } from '@vuepress/plugin-markdown-math' export default { - plugins: [ - markdownMathPlugin({ + markdown: { + // 配置插件 + math: { // 选项 - }), + }, + }, + plugins: [ + // 应用插件 + markdownMathPlugin(), ], } ``` @@ -92,6 +97,8 @@ Mathjax: ## 配置项 +你可以在 VuePress 配置文件中的 `markdown.math` 字段中配置此插件,也可以在调用时传入配置,后者具有更高优先级。 + ### 类型 - 类型:`'katex' | 'mathjax'` diff --git a/plugins/plugin-markdown-image/package.json b/plugins/plugin-markdown-image/package.json index 2118e17a0e..55b2b453cc 100644 --- a/plugins/plugin-markdown-image/package.json +++ b/plugins/plugin-markdown-image/package.json @@ -32,6 +32,7 @@ "exports": { ".": "./lib/node/index.js", "./client/*": "./lib/client/*", + "./types": "./lib/types.d.ts", "./package.json": "./package.json" }, "main": "./lib/node/index.js", @@ -40,7 +41,8 @@ "lib" ], "scripts": { - "build": "tsc -b tsconfig.build.json", + "build": "tsc -b ./tsconfig.build.json", + "copy": "cpx \"src/types.d.ts\" lib", "clean": "rimraf --glob ./lib ./*.tsbuildinfo", "style": "sass src:lib --no-source-map" }, diff --git a/plugins/plugin-markdown-image/src/node/index.ts b/plugins/plugin-markdown-image/src/node/index.ts index 0307142a61..480e66176b 100644 --- a/plugins/plugin-markdown-image/src/node/index.ts +++ b/plugins/plugin-markdown-image/src/node/index.ts @@ -1 +1,2 @@ export * from './markdownImagePlugin.js' +export * from './options.js' diff --git a/plugins/plugin-markdown-image/src/node/markdownImagePlugin.ts b/plugins/plugin-markdown-image/src/node/markdownImagePlugin.ts index f0c2ab0c6d..7fdb05d5f2 100644 --- a/plugins/plugin-markdown-image/src/node/markdownImagePlugin.ts +++ b/plugins/plugin-markdown-image/src/node/markdownImagePlugin.ts @@ -8,20 +8,24 @@ import type { MarkdownImagePluginOptions } from './options.js' import { prepareClientConfigFile } from './prepare/index.js' import { PLUGIN_NAME } from './utils.js' -export const markdownImagePlugin = ( - options: MarkdownImagePluginOptions, -): Plugin => ({ - name: PLUGIN_NAME, +export const markdownImagePlugin = + (pluginOptions?: MarkdownImagePluginOptions): Plugin => + (app) => { + const options = pluginOptions ?? app.options.markdown.image ?? {} - extendsMarkdown: (md) => { - const { mark } = options + return { + name: PLUGIN_NAME, - if (options.figure) md.use(figure) - if (options.lazyload) md.use(imgLazyload) - if (options.obsidianSize) md.use(obsidianImageSize) - if (options.size) md.use(imgSize) - if (mark) md.use(imgMark, isPlainObject(mark) ? mark : {}) - }, + extendsMarkdown: (md) => { + const { mark } = options - clientConfigFile: (app) => prepareClientConfigFile(app, options), -}) + if (options.figure) md.use(figure) + if (options.lazyload) md.use(imgLazyload) + if (options.obsidianSize) md.use(obsidianImageSize) + if (options.size) md.use(imgSize) + if (mark) md.use(imgMark, isPlainObject(mark) ? mark : {}) + }, + + clientConfigFile: (app) => prepareClientConfigFile(app, options), + } + } diff --git a/plugins/plugin-markdown-image/src/types.d.ts b/plugins/plugin-markdown-image/src/types.d.ts new file mode 100644 index 0000000000..75c1afbdb0 --- /dev/null +++ b/plugins/plugin-markdown-image/src/types.d.ts @@ -0,0 +1,7 @@ +import type { MarkdownImagePluginOptions } from './node/options.js' + +declare module 'vuepress/markdown' { + export interface MarkdownOptions { + image?: MarkdownImagePluginOptions + } +} diff --git a/plugins/plugin-markdown-image/tsconfig.build.json b/plugins/plugin-markdown-image/tsconfig.build.json index 458814cb0c..065d759558 100644 --- a/plugins/plugin-markdown-image/tsconfig.build.json +++ b/plugins/plugin-markdown-image/tsconfig.build.json @@ -2,7 +2,10 @@ "extends": "../../tsconfig.build.json", "compilerOptions": { "rootDir": "./src", - "outDir": "./lib" + "outDir": "./lib", + "paths": { + "@vuepress/markdown": ["./src/types.d.ts"] + } }, "include": ["./src"] } diff --git a/plugins/plugin-markdown-math/package.json b/plugins/plugin-markdown-math/package.json index 56e15ec557..0999e60055 100644 --- a/plugins/plugin-markdown-math/package.json +++ b/plugins/plugin-markdown-math/package.json @@ -27,6 +27,7 @@ "type": "module", "exports": { ".": "./lib/node/index.js", + "./types": "./lib/types.d.ts", "./package.json": "./package.json" }, "main": "./lib/node/index.js", @@ -37,6 +38,7 @@ "scripts": { "build": "tsc -b tsconfig.build.json", "clean": "rimraf --glob ./lib ./*.tsbuildinfo", + "copy": "cpx \"src/types.d.ts\" lib", "style": "sass src:lib --no-source-map" }, "dependencies": { diff --git a/plugins/plugin-markdown-math/src/node/index.ts b/plugins/plugin-markdown-math/src/node/index.ts index 59fa63f67b..123a77b62d 100644 --- a/plugins/plugin-markdown-math/src/node/index.ts +++ b/plugins/plugin-markdown-math/src/node/index.ts @@ -1 +1,2 @@ export * from './markdownMathPlugin.js' +export * from './options.js' diff --git a/plugins/plugin-markdown-math/src/node/markdownMathPlugin.ts b/plugins/plugin-markdown-math/src/node/markdownMathPlugin.ts index a111d1c206..35867676de 100644 --- a/plugins/plugin-markdown-math/src/node/markdownMathPlugin.ts +++ b/plugins/plugin-markdown-math/src/node/markdownMathPlugin.ts @@ -13,105 +13,112 @@ import { } from './prepare/index.js' import { PLUGIN_NAME } from './utils.js' -export const markdownMathPlugin = ({ - type, - ...options -}: MarkdownMathPluginOptions = {}): Plugin => { - const isMathjaxInstalled = getInstalledStatus('mathjax-full', import.meta.url) - const isKatexInstalled = getInstalledStatus('katex', import.meta.url) +export const markdownMathPlugin = + (pluginOptions?: MarkdownMathPluginOptions): Plugin => + (app) => { + const { type, ...options } = + pluginOptions ?? app.options.markdown.math ?? {} - const mathRenderer = - type === 'katex' && isKatexInstalled - ? 'katex' - : type === 'mathjax' && isMathjaxInstalled - ? 'mathjax' - : isMathjaxInstalled + const isMathjaxInstalled = getInstalledStatus( + 'mathjax-full', + import.meta.url, + ) + const isKatexInstalled = getInstalledStatus('katex', import.meta.url) + + const mathRenderer = + type === 'katex' && isKatexInstalled + ? 'katex' + : type === 'mathjax' && isMathjaxInstalled ? 'mathjax' - : isKatexInstalled - ? 'katex' - : null + : isMathjaxInstalled + ? 'mathjax' + : isKatexInstalled + ? 'katex' + : null - if (!mathRenderer || (type && mathRenderer !== type)) { - const packages = { katex: 'katex', mathjax: 'mathjax-full' } - logger.error( - !mathRenderer - ? 'No math renderer found, please install mathjax-full or katex' - : `type is "${type}", but "${packages[type!]}" is not installed`, - ) + if (!mathRenderer || (type && mathRenderer !== type)) { + const packages = { katex: 'katex', mathjax: 'mathjax-full' } + logger.error( + !mathRenderer + ? 'No math renderer found, please install mathjax-full or katex' + : `type is "${type}", but "${packages[type!]}" is not installed`, + ) - return { - name: PLUGIN_NAME, + return { + name: PLUGIN_NAME, + } } - } - const mathjaxInstance = - mathRenderer === 'mathjax' - ? createMathjaxInstance({ - ...(options as MarkdownMathjaxPluginOptions), - transformer: (content: string) => - content.replace(/^ + content.replace(/^ { - if (mathRenderer === 'mathjax') { - addCustomElement(bundlerOptions, app, /^mjx-/) - } - }, + extendsBundlerOptions: (bundlerOptions, app) => { + if (mathRenderer === 'mathjax') { + addCustomElement(bundlerOptions, app, /^mjx-/) + } + }, - extendsMarkdown: (md) => { - if (mathRenderer === 'mathjax') { - md.use(mathjax, mathjaxInstance!) - // Reset mathjax style in each render - md.use((md) => { - const originalRender = md.render.bind(md) + extendsMarkdown: (md) => { + if (mathRenderer === 'mathjax') { + md.use(mathjax, mathjaxInstance!) + // Reset mathjax style in each render + md.use((md) => { + const originalRender = md.render.bind(md) - md.render = (src: string, env: unknown): string => { - const result = originalRender(src, env) + md.render = (src: string, env: unknown): string => { + const result = originalRender(src, env) - mathjaxInstance!.reset() + mathjaxInstance!.reset() - return result - } - }) - } else { - md.use(katex, { - logger: (errorCode, errorMsg, token, { filePathRelative }) => { - // Ignore this error - if (errorCode === 'newLineInDisplayMode') return + return result + } + }) + } else { + md.use(katex, { + logger: (errorCode, errorMsg, token, { filePathRelative }) => { + // Ignore this error + if (errorCode === 'newLineInDisplayMode') return - if (errorCode === 'unicodeTextInMathMode') - logger.warn( - `Found unicode character ${token.text} inside tex${ - filePathRelative ? ` in ${colors.cyan(filePathRelative)}` : '' - }. You should use ${colors.magenta(`\\text{${token.text}}`)}`, - ) - else - logger.warn( - `${errorMsg}.${ - filePathRelative - ? `\nFound in ${colors.cyan(filePathRelative)}` - : '' - }`, - ) - }, - ...options, - transformer: (content: string) => - content.replace(/^(<[a-z]+ )/g, '$1v-pre '), - }) - } - }, + if (errorCode === 'unicodeTextInMathMode') + logger.warn( + `Found unicode character ${token.text} inside tex${ + filePathRelative + ? ` in ${colors.cyan(filePathRelative)}` + : '' + }. You should use ${colors.magenta(`\\text{${token.text}}`)}`, + ) + else + logger.warn( + `${errorMsg}.${ + filePathRelative + ? `\nFound in ${colors.cyan(filePathRelative)}` + : '' + }`, + ) + }, + ...options, + transformer: (content: string) => + content.replace(/^(<[a-z]+ )/g, '$1v-pre '), + }) + } + }, - onPrepared: async (app) => { - if (mathRenderer === 'mathjax') { - await prepareMathjaxStyle(app, mathjaxInstance!) - } - }, + onPrepared: async (app) => { + if (mathRenderer === 'mathjax') { + await prepareMathjaxStyle(app, mathjaxInstance!) + } + }, - clientConfigFile: (app) => - prepareClientConfigFile(app, mathRenderer, options), + clientConfigFile: (app) => + prepareClientConfigFile(app, mathRenderer, options), + } } -} diff --git a/plugins/plugin-markdown-math/src/types.d.ts b/plugins/plugin-markdown-math/src/types.d.ts new file mode 100644 index 0000000000..dfacf0665d --- /dev/null +++ b/plugins/plugin-markdown-math/src/types.d.ts @@ -0,0 +1,7 @@ +import type { MarkdownMathPluginOptions } from './node/options.js' + +declare module 'vuepress/markdown' { + export interface MarkdownOptions { + math?: MarkdownMathPluginOptions + } +} diff --git a/plugins/plugin-markdown-math/tsconfig.build.json b/plugins/plugin-markdown-math/tsconfig.build.json index 458814cb0c..065d759558 100644 --- a/plugins/plugin-markdown-math/tsconfig.build.json +++ b/plugins/plugin-markdown-math/tsconfig.build.json @@ -2,7 +2,10 @@ "extends": "../../tsconfig.build.json", "compilerOptions": { "rootDir": "./src", - "outDir": "./lib" + "outDir": "./lib", + "paths": { + "@vuepress/markdown": ["./src/types.d.ts"] + } }, "include": ["./src"] }