Skip to content
Merged
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
45 changes: 20 additions & 25 deletions docs/plugins/markdown/markdown-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ Register markdown custom containers in your VuePress site.

This plugin simplifies the use of [markdown-it-container](https://github.com/markdown-it/markdown-it-container), but also retains its original capabilities.

The [Custom Containers](../../themes/default/markdown.md#custom-containers) of default theme is powered by this plugin.

## Usage

```bash
Expand Down Expand Up @@ -51,6 +49,7 @@ This plugin can be used multiple times to support different types of containers.
### type

- Type: `string`
- Required: Yes

- Details:

Expand All @@ -61,6 +60,7 @@ This plugin can be used multiple times to support different types of containers.
### locales

- Type: `Record<string, { defaultInfo: string }>`
- Default: `{}`

- Details:

Expand Down Expand Up @@ -88,21 +88,18 @@ export default {
}
```

- Also see:
- Reference:
- [Guide > I18n](https://vuejs.press/guide/i18n.html)

### before

- Type: `(info: string) => string`

- Default:

<!-- prettier-ignore-start -->
```ts
(info: string): string =>
`<div class="custom-container ${type}">${info ? `<p class="custom-container-title">${info}</p>` : ''}\n`
```
<!-- prettier-ignore-end -->
```ts
;(info: string): string =>
`<div class="custom-container ${type}">${info ? `<p class="custom-container-title">${info}</p>` : ''}\n`
```

- Details:

Expand All @@ -115,14 +112,11 @@ export default {
### after

- Type: `(info: string) => string`

- Default:

<!-- prettier-ignore-start -->
```ts
(): string => '</div>\n'
```
<!-- prettier-ignore-end -->
```ts
;(): string => '</div>\n'
```

- Details:

Expand All @@ -136,15 +130,15 @@ export default {

- Type:

```ts
type MarkdownItContainerRenderFunction = (
tokens: Token[],
index: number,
options: unknown,
env: MarkdownEnv,
self: Renderer,
) => string
```
```ts
type MarkdownItContainerRenderFunction = (
tokens: Token[],
index: number,
options: unknown,
env: MarkdownEnv,
self: Renderer,
) => string
```

- Details:

Expand All @@ -163,6 +157,7 @@ type MarkdownItContainerRenderFunction = (
### marker

- Type: `string`
- Default: `':'`

- Details:

Expand Down
64 changes: 30 additions & 34 deletions docs/zh/plugins/markdown/markdown-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ icon: package

该插件简化了 [markdown-it-container](https://github.com/markdown-it/markdown-it-container) 的使用方法,但同时也保留了其原本的能力。

默认主题的 [自定义容器](../../themes/default/markdown.md#自定义容器) 就是由该插件支持的。

## 使用方法

```bash
Expand Down Expand Up @@ -40,7 +38,7 @@ export default {

- `type` 是必需的,应通过 [type](#type) 配置项来指定。
- `info` 是可选的,其默认值可以通过 [locales](#locales) 的 `defaultInfo` 配置项来指定。
- `content` 可是任何合法的 Markdown 内容。
- `content` 可以是任何合法的 Markdown 内容。

::: tip
该插件可以被多次使用,以便支持不同类型的容器。
Expand All @@ -50,7 +48,8 @@ export default {

### type

- 类型: `string`
- 类型:`string`
- 必填:是

- 详情:

Expand All @@ -60,13 +59,14 @@ export default {

### locales

- 类型: `Record<string, { defaultInfo: string }>`
- 类型:`Record<string, { defaultInfo: string }>`
- 默认值:`{}`

- 详情:

容器在不同 locales 下的默认 `info`
容器在不同 locales 下的默认 `info`。

如果没有指定该配置项,默认 `info` 会使用大写的 [type](#type)
如果没有指定该配置项,默认 `info` 会使用大写的 [type](#type)。

- 示例:

Expand All @@ -93,16 +93,13 @@ export default {

### before

- 类型: `(info: string) => string`

- 类型:`(info: string) => string`
- 默认值:

<!-- prettier-ignore-start -->
```ts
(info: string): string =>
`<div class="custom-container ${type}">${info ? `<p class="custom-container-title">${info}</p>` : ''}\n`
```
<!-- prettier-ignore-end -->
```ts
;(info: string): string =>
`<div class="custom-container ${type}">${info ? `<p class="custom-container-title">${info}</p>` : ''}\n`
```

- 详情:

Expand All @@ -114,15 +111,12 @@ export default {

### after

- 类型: `(info: string) => string`

- 类型:`(info: string) => string`
- 默认值:

<!-- prettier-ignore-start -->
```ts
(): string => '</div>\n'
```
<!-- prettier-ignore-end -->
```ts
;(): string => '</div>\n'
```

- 详情:

Expand All @@ -136,33 +130,35 @@ export default {

- 类型:

```ts
type MarkdownItContainerRenderFunction = (
tokens: Token[],
index: number,
options: unknown,
env: MarkdownEnv,
self: Renderer,
) => string
```
```ts
type MarkdownItContainerRenderFunction = (
tokens: Token[],
index: number,
options: unknown,
env: MarkdownEnv,
self: Renderer,
) => string
```

- 详情:

[markdown-it-container](https://github.com/markdown-it/markdown-it-container#api) 的 `render` 配置项。

该插件使用了一个默认的 `render` 函数。但如果你指定了该配置项,那么默认的 `render` 函数就会被替换掉,此时 [locales](#locales)[before](#before) 和 [after](#after) 配置项都会被忽略。
该插件使用了一个默认的 `render` 函数。但如果你指定了该配置项,那么默认的 `render` 函数就会被替换掉,此时 [locales](#locales)[before](#before) 和 [after](#after) 配置项都会被忽略。

### validate

- 类型: `(params: string) => boolean`
- 类型:`(params: string) => boolean`

- 详情:

[markdown-it-container](https://github.com/markdown-it/markdown-it-container#api) 的 `validate` 配置项。

### marker

- 类型: `string`
- 类型:`string`

- 默认值:`':'`

- 详情:

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './markdownContainerPlugin.js'
export type * from './options.js'
Original file line number Diff line number Diff line change
@@ -1,90 +1,33 @@
import type Renderer from 'markdown-it/lib/renderer.mjs'
import type Token from 'markdown-it/lib/token.mjs'
// eslint-disable-next-line import/no-rename-default
import container from 'markdown-it-container'
import type { Plugin, PluginObject } from 'vuepress/core'
import type { MarkdownEnv } from 'vuepress/markdown'
import type { LocaleConfig } from 'vuepress/shared'
import { ensureLeadingSlash, resolveLocalePath } from 'vuepress/shared'
import { colors, logger } from 'vuepress/utils'
import type {
MarkdownContainerPluginOptions,
RenderPlaceFunction,
} from './options.js'

/**
* Options for markdown-it-container
* Create markdown container plugin
*
* 创建 markdown 容器插件
*
* @param options - Plugin options / 插件配置项
*
* @example
* ```ts
* import { markdownContainerPlugin } from '@vuepress/plugin-markdown-container'
*
* markdownContainerPlugin({
* type: 'tip',
* locales: {
* '/': { defaultInfo: 'TIP' },
* '/zh/': { defaultInfo: '提示' },
* },
* })
* ```
*/
export interface MarkdownItContainerOptions {
/**
* The marker of the container syntax
*
* @default ':'
* @see https://github.com/markdown-it/markdown-it-container#api
*/
marker?: string

/**
* Renderer function for opening / closing tokens
*
* @see https://github.com/markdown-it/markdown-it-container#api
*/
render?: MarkdownItContainerRenderFunction

/**
* Function to validate tail after opening marker, should return `true` on success
*/
validate?: (params: string) => boolean
}

export type MarkdownItContainerRenderFunction = (
tokens: Token[],
index: number,
options: unknown,
env: MarkdownEnv,
self: Renderer,
) => string

export type RenderPlaceFunction = (info: string) => string

/**
* Options for @vuepress/plugin-markdown-container
*/
export interface MarkdownContainerPluginOptions
extends MarkdownItContainerOptions {
/**
* The type of the container
*
* It would be used as the `name` of the container
*
* @see https://github.com/markdown-it/markdown-it-container#api
*/
type: string

/**
* Locales config for container
*/
locales?: LocaleConfig<{
/**
* Default info of the container
*
* If this option is not specified, the default info will fallback to the
* uppercase of the `type` option
*/
defaultInfo: string
}>

/**
* A function to render the starting tag of the container.
*
* This option will not take effect if you don't specify the `after` option.
*/
before?: RenderPlaceFunction

/**
* A function to render the ending tag of the container.
*
* This option will not take effect if you don't specify the `before` option.
*/
after?: RenderPlaceFunction
}

export const markdownContainerPlugin = ({
// plugin options
type,
Expand Down
Loading
Loading