Skip to content

Commit ad0ff72

Browse files
shigmaulivz
authored andcommitted
feat: plugin container (#1381)
1 parent 7d2c065 commit ad0ff72

File tree

11 files changed

+270
-62
lines changed

11 files changed

+270
-62
lines changed

packages/@vuepress/markdown/index.js

-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const lineNumbersPlugin = require('./lib/lineNumbers')
1414
const componentPlugin = require('./lib/component')
1515
const hoistScriptStylePlugin = require('./lib/hoist')
1616
const convertRouterLinkPlugin = require('./lib/link')
17-
const containersPlugin = require('./lib/containers')
1817
const markdownSlotsContainersPlugin = require('./lib/markdownSlotsContainers')
1918
const snippetPlugin = require('./lib/snippet')
2019
const emojiPlugin = require('markdown-it-emoji')
@@ -75,10 +74,6 @@ module.exports = (markdown = {}) => {
7574
.use(hoistScriptStylePlugin)
7675
.end()
7776

78-
.plugin(PLUGINS.CONTAINERS)
79-
.use(containersPlugin)
80-
.end()
81-
8277
.plugin(PLUGINS.MARKDOWN_SLOTS_CONTAINERS)
8378
.use(markdownSlotsContainersPlugin)
8479
.end()

packages/@vuepress/markdown/lib/containers.js

-33
This file was deleted.

packages/@vuepress/markdown/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"markdown-it": "^8.4.1",
2525
"markdown-it-anchor": "^5.0.2",
2626
"markdown-it-chain": "^1.3.0",
27-
"markdown-it-container": "^2.0.0",
2827
"markdown-it-emoji": "^1.4.0",
2928
"markdown-it-table-of-contents": "^0.4.0",
3029
"prismjs": "^1.13.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__tests__
2+
__mocks__
3+
.temp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @vuepress/plugin-container
2+
3+
> markdown container plugin for vuepress
4+
5+
See [documentation](https://vuepress.vuejs.org/plugin/official/plugin-container.html).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const container = require('markdown-it-container')
2+
3+
function call (target, ...args) {
4+
if (typeof target === 'function') {
5+
return target(...args)
6+
} else {
7+
return target
8+
}
9+
}
10+
11+
module.exports = (options, context) => ({
12+
multiple: true,
13+
14+
extendMarkdown (md) {
15+
const {
16+
validate,
17+
marker,
18+
before,
19+
after,
20+
type = '',
21+
defaultTitle = type.toUpperCase()
22+
} = options
23+
if (!type) return
24+
25+
let { render } = options
26+
if (!render) {
27+
if (before !== undefined && after !== undefined) {
28+
render = (tokens, index) => {
29+
const token = tokens[index]
30+
return token.nesting === 1 ? call(before, token) : call(after, token)
31+
}
32+
} else {
33+
render = (tokens, index) => {
34+
const token = tokens[index]
35+
let title = token.info.trim().slice(type.length).trim() || defaultTitle
36+
if (title) title = `<p class="custom-block-title">${title}</p>`
37+
if (token.nesting === 1) {
38+
return `<div class="${type} custom-block">${title}\n`
39+
} else {
40+
return `</div>\n`
41+
}
42+
}
43+
}
44+
}
45+
46+
md.use(container, type, { render, validate, marker })
47+
}
48+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@vuepress/plugin-container",
3+
"version": "1.0.0-alpha.40",
4+
"description": "markdown container plugin for vuepress",
5+
"main": "index.js",
6+
"publishConfig": {
7+
"access": "public"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/vuejs/vuepress.git",
12+
"directory": "packages/@vuepress/plugin-container"
13+
},
14+
"keywords": [
15+
"documentation",
16+
"vue",
17+
"vuepress",
18+
"generator"
19+
],
20+
"dependencies": {
21+
"markdown-it-container": "^2.0.0"
22+
},
23+
"author": "Shigma <[email protected]>",
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/vuejs/vuepress/issues"
27+
},
28+
"homepage": "https://github.com/vuejs/vuepress/packages/@vuepress/plugin-container#readme"
29+
}

packages/@vuepress/theme-default/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ module.exports = (options, ctx) => ({
2020
plugins: [
2121
'@vuepress/active-header-links',
2222
'@vuepress/search',
23-
'@vuepress/plugin-nprogress'
23+
'@vuepress/plugin-nprogress',
24+
['@vuepress/container', { type: 'tip' }],
25+
['@vuepress/container', { type: 'warning' }],
26+
['@vuepress/container', { type: 'danger' }],
27+
['@vuepress/container', {
28+
type: 'v-pre',
29+
before: '<div v-pre>\n',
30+
after: '</div>\n'
31+
}]
2432
]
2533
})

packages/docs/docs/.vuepress/config.js

+16-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const container = require('markdown-it-container')
1+
const { fs, path } = require('@vuepress/shared-utils')
22

33
module.exports = ctx => ({
44
dest: '../../vuepress',
@@ -74,14 +74,17 @@ module.exports = ctx => ({
7474
['@vuepress/google-analytics', {
7575
ga: 'UA-128189152-1'
7676
}],
77+
['@vuepress/container', {
78+
type: 'vue',
79+
before: '<pre class="vue-container"><code>',
80+
after: '</code></pre>',
81+
}],
82+
['@vuepress/container', {
83+
type: 'upgrade',
84+
before: ({ info }) => `<UpgradePath title="${info.trim().slice(7).trim()}">`,
85+
after: '</UpgradePath>',
86+
}],
7787
],
78-
extendMarkdown (md) {
79-
md.use(container, 'upgrade', {
80-
render: (tokens, idx) => tokens[idx].nesting === 1
81-
? `<UpgradePath title="${tokens[idx].info.trim().slice('upgrade'.length).trim()}">`
82-
: '</UpgradePath>'
83-
})
84-
},
8588
})
8689

8790
function getGuideSidebar (groupA, groupB) {
@@ -114,6 +117,10 @@ function getGuideSidebar (groupA, groupB) {
114117
]
115118
}
116119

120+
const officalPlugins = fs
121+
.readdirSync(path.resolve(__dirname, '../plugin/official'))
122+
.map(filename => 'official/' + filename.slice(0, -3))
123+
117124
function getPluginSidebar (pluginTitle, pluginIntro, officialPluginTitle) {
118125
return [
119126
{
@@ -131,20 +138,7 @@ function getPluginSidebar (pluginTitle, pluginIntro, officialPluginTitle) {
131138
{
132139
title: officialPluginTitle,
133140
collapsable: false,
134-
children: [
135-
'official/plugin-search',
136-
'official/plugin-active-header-links',
137-
'official/plugin-pwa',
138-
'official/plugin-blog',
139-
'official/plugin-pagination',
140-
'official/plugin-google-analytics',
141-
'official/plugin-i18n-ui',
142-
'official/plugin-last-updated',
143-
'official/plugin-medium-zoom',
144-
'official/plugin-back-to-top',
145-
'official/plugin-register-components',
146-
'official/plugin-clean-urls'
147-
]
141+
children: officalPlugins,
148142
}
149143
]
150144
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: container
3+
metaTitle: A plugin of registering markdown containers | VuePress
4+
---
5+
6+
# [@vuepress/plugin-container](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/plugin-container)
7+
8+
> A plugin of registering markdown containers
9+
10+
## Install
11+
12+
```bash
13+
yarn add -D @vuepress/plugin-container
14+
# OR npm install -D @vuepress/plugin-container
15+
```
16+
17+
## Usage
18+
19+
```javascript
20+
module.exports = {
21+
plugins: ['@vuepress/container']
22+
}
23+
```
24+
25+
## Options
26+
27+
### type
28+
29+
- Type: `string`
30+
- This is a required option.
31+
32+
The type for the container. For example, if `type` was set to `foo`, only the following syntax will be parsed as a container:
33+
34+
```md
35+
::: foo bar
36+
write something here ~
37+
:::
38+
```
39+
40+
### defaultTitle
41+
42+
- Type: `string`
43+
- Default: the upper case of `type`
44+
45+
The default title for the container. If no title was provided, `defaultTitle` will be showed as the title of the container.
46+
47+
### before
48+
49+
- Type: `string | Function`
50+
- Default: `undefined`
51+
52+
String to be placed before the block. If specified as a function, a argument `token` will be passed to it. If specified, it will override `defaultTitle`.
53+
54+
### after
55+
56+
- Type: `string | Function`
57+
- Default: `undefined`
58+
59+
String to be placed after the block. If specified as a function, a argument `token` will be passed to it. If specified, it will override `defaultTitle`.
60+
61+
### validate
62+
63+
- Type: `Function`
64+
- Default: `undefined`
65+
66+
A function to validate tail after opening marker, should return `true` on success.
67+
68+
### render
69+
70+
- Type: `Function`
71+
- Default: `undefined`
72+
73+
The renderer function for opening/closing tokens. If specified, it will override `before`, `after` and `defaultTitle`.
74+
75+
### marker
76+
77+
- Type: `string`
78+
- Default: `':'`
79+
80+
The character to use in delimiter.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: container
3+
metaTitle: Markdown 容器插件 | VuePress
4+
---
5+
6+
# [@vuepress/plugin-container](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/plugin-container)
7+
8+
> Markdown 容器插件
9+
10+
## 安装
11+
12+
```bash
13+
yarn add -D @vuepress/plugin-container
14+
# OR npm install -D @vuepress/plugin-container
15+
```
16+
17+
## 使用
18+
19+
```javascript
20+
module.exports = {
21+
plugins: ['@vuepress/container']
22+
}
23+
```
24+
25+
## 选项
26+
27+
### type
28+
29+
- 类型: `string`
30+
- 这是一个必需的选项
31+
32+
容器的类型。举个例子,如果 `type` 被设置为 `foo`,则仅有下面的语法会被视为对应的容器:
33+
34+
```md
35+
::: foo bar
36+
随便写点啥 ~
37+
:::
38+
```
39+
40+
### defaultTitle
41+
42+
- 类型: `string`
43+
- 默认值: `type` 的大写形式
44+
45+
容器的默认标题。如果没有提供标题,则会使用 `defaultTitle` 作为容器的标题。
46+
47+
### before
48+
49+
- 类型: `string | Function`
50+
- 默认值: `undefined`
51+
52+
要插入在容器前的 HTML。如果设置为一个函数,将传入当前的 `token` 作为第一个参数。如果设置了这个值,它将覆盖 `defaultTitle` 的效果。
53+
54+
### after
55+
56+
- 类型: `string | Function`
57+
- 默认值: `undefined`
58+
59+
要插入在容器后的 HTML。如果设置为一个函数,将传入当前的 `token` 作为第一个参数。如果设置了这个值,它将覆盖 `defaultTitle` 的效果。
60+
61+
### validate
62+
63+
- 类型: `Function`
64+
- 默认值: `undefined`
65+
66+
一个用于判定容器是否结束的函数。当认定容器范围结束时应返回一个 `true`
67+
68+
### render
69+
70+
- 类型: `Function`
71+
- 默认值: `undefined`
72+
73+
容器开头和结束 token 的渲染函数。如果设置了这个值,它将覆盖 `before`, `after``defaultTitle` 的效果。
74+
75+
### marker
76+
77+
- 类型: `string`
78+
- 默认值: `':'`
79+
80+
用于分隔符的字符。

0 commit comments

Comments
 (0)