Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support markdown options in markdown plugins #145

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export default defineUserConfig({
return importPath
},
},
image: {
figure: true,
mark: true,
size: true,
},
},

extendsMarkdown: (md) => {
Expand All @@ -91,11 +96,7 @@ export default defineUserConfig({
json: true,
rss: true,
}),
markdownImagePlugin({
figure: true,
mark: true,
size: true,
}),
markdownImagePlugin(),
markdownMathPlugin(),
redirectPlugin({
switchLocale: 'modal',
Expand Down
13 changes: 10 additions & 3 deletions docs/plugins/markdown-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +25,11 @@ export default {
mark: true,
// Enable image size
size: true,
}),
},
},
plugins: [
// apply the plugin
markdownImagePlugin(),
],
}
```
Expand Down Expand Up @@ -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`
Expand Down
13 changes: 10 additions & 3 deletions docs/plugins/markdown-math.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
}
```
Expand Down Expand Up @@ -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'`
Expand Down
13 changes: 10 additions & 3 deletions docs/zh/plugins/markdown-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// 启用图片懒加载
Expand All @@ -24,7 +25,11 @@ export default {
mark: true,
// 启用图片大小
size: true,
}),
},
},
plugins: [
// 应用插件
markdownImagePlugin(),
],
}
```
Expand Down Expand Up @@ -111,6 +116,8 @@ interface ImageMarkOptions {

## 配置项

你可以在 VuePress 配置文件中的 `markdown.image` 字段中配置此插件,也可以在调用时传入配置,后者具有更高优先级。

### figure

- 类型:`boolean`
Expand Down
13 changes: 10 additions & 3 deletions docs/zh/plugins/markdown-math.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ npm i -D katex
import { markdownMathPlugin } from '@vuepress/plugin-markdown-math'

export default {
plugins: [
markdownMathPlugin({
markdown: {
// 配置插件
math: {
// 选项
}),
},
},
plugins: [
// 应用插件
markdownMathPlugin(),
],
}
```
Expand Down Expand Up @@ -92,6 +97,8 @@ Mathjax:

## 配置项

你可以在 VuePress 配置文件中的 `markdown.math` 字段中配置此插件,也可以在调用时传入配置,后者具有更高优先级。

### 类型

- 类型:`'katex' | 'mathjax'`
Expand Down
4 changes: 3 additions & 1 deletion plugins/plugin-markdown-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions plugins/plugin-markdown-image/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './markdownImagePlugin.js'
export * from './options.js'
32 changes: 18 additions & 14 deletions plugins/plugin-markdown-image/src/node/markdownImagePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
7 changes: 7 additions & 0 deletions plugins/plugin-markdown-image/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MarkdownImagePluginOptions } from './node/options.js'

declare module 'vuepress/markdown' {
export interface MarkdownOptions {
image?: MarkdownImagePluginOptions
}
}
5 changes: 4 additions & 1 deletion plugins/plugin-markdown-image/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./lib"
"outDir": "./lib",
"paths": {
"@vuepress/markdown": ["./src/types.d.ts"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be verbose to require this config. What about putting the declare module clause inside options.ts?

}
},
"include": ["./src"]
}
2 changes: 2 additions & 0 deletions plugins/plugin-markdown-math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions plugins/plugin-markdown-math/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './markdownMathPlugin.js'
export * from './options.js'
Loading
Loading