Skip to content

Commit

Permalink
feat($markdown): markdown plugin (close: #585) (#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma authored and ulivz committed Mar 12, 2019
1 parent 3b14375 commit 9734a58
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 38 deletions.
6 changes: 3 additions & 3 deletions packages/@vuepress/core/lib/node/plugin-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/

const instantiateOption = require('./override/instantiateOption')
const { flattenPlugin, normalizePluginsConfig } = require('./util')
const { flattenPlugin } = require('./util')
const { PLUGIN_OPTION_MAP } = require('./constants')
const {
moduleResolver: { getPluginResolver },
datatypes: { assertTypes, isPlainObject },
logger, chalk
logger, chalk, normalizeConfig
} = require('@vuepress/shared-utils')

/**
Expand Down Expand Up @@ -136,7 +136,7 @@ module.exports = class PluginAPI {
*/

useByPluginsConfig (pluginsConfig) {
pluginsConfig = normalizePluginsConfig(pluginsConfig)
pluginsConfig = normalizeConfig(pluginsConfig)
pluginsConfig.forEach(([pluginRaw, pluginOptions]) => {
this.use(pluginRaw, pluginOptions)
})
Expand Down
31 changes: 0 additions & 31 deletions packages/@vuepress/core/lib/node/plugin-api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,3 @@ exports.flattenPlugin = function (
$$options: pluginOptions /* used for test */
})
}

/**
* Normalize plugins config in `.vuepress/config.js`
*
* @param pluginsConfig
*/

exports.normalizePluginsConfig = function (pluginsConfig) {
const { valid, warnMsg } = assertTypes(pluginsConfig, [Object, Array])
if (!valid) {
if (pluginsConfig !== undefined) {
logger.warn(
`[${chalk.gray('config')}] `
+ `Invalid value for "plugin" field : ${warnMsg}`
)
}
pluginsConfig = []
return pluginsConfig
}

if (Array.isArray(pluginsConfig)) {
pluginsConfig = pluginsConfig.map(item => {
return Array.isArray(item) ? item : [item]
})
} else if (typeof pluginsConfig === 'object') {
pluginsConfig = Object.keys(pluginsConfig).map(item => {
return [item, pluginsConfig[item]]
})
}
return pluginsConfig
}
19 changes: 18 additions & 1 deletion packages/@vuepress/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const snippetPlugin = require('./lib/snippet')
const tocPlugin = require('./lib/tableOfContents')
const emojiPlugin = require('markdown-it-emoji')
const anchorPlugin = require('markdown-it-anchor')
const { slugify: _slugify, logger, chalk, hash } = require('@vuepress/shared-utils')
const {
slugify: _slugify,
logger, chalk, hash, normalizeConfig,
moduleResolver: { getMarkdownItResolver }
} = require('@vuepress/shared-utils')

/**
* Create markdown by config.
Expand All @@ -29,11 +33,14 @@ module.exports = (markdown = {}) => {
externalLinks,
anchor,
toc,
plugins,
lineNumbers,
beforeInstantiate,
afterInstantiate
} = markdown

const resolver = getMarkdownItResolver()

// allow user config slugify
const slugify = markdown.slugify || _slugify

Expand Down Expand Up @@ -100,6 +107,16 @@ module.exports = (markdown = {}) => {

const md = config.toMd(require('markdown-it'), markdown)

const pluginsConfig = normalizeConfig(plugins || [])
pluginsConfig.forEach(([pluginRaw, pluginOptions]) => {
const plugin = resolver.resolve(pluginRaw)
if (plugin.entry) {
md.use(plugin.entry, pluginOptions)
} else {
// TODO: error handling
}
})

afterInstantiate && afterInstantiate(md)

// override parse to allow cache
Expand Down
2 changes: 2 additions & 0 deletions packages/@vuepress/shared-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as isIndexFile from './isIndexFile'
import logger from './logger'
import * as moduleLoader from './moduleLoader'
import * as moduleResolver from './moduleResolver'
import normalizeConfig from './normalizeConfig'
import * as parseEmojis from './parseEmojis'
import parseFrontmatter from './parseFrontmatter'
import parseHeaders from './parseHeaders'
Expand Down Expand Up @@ -50,6 +51,7 @@ export {
logger,
moduleLoader,
moduleResolver,
normalizeConfig,
parseEmojis,
parseFrontmatter,
parseHeaders,
Expand Down
14 changes: 11 additions & 3 deletions packages/@vuepress/shared-utils/src/moduleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ class ModuleResolver {
this.allowedTypes = allowedTypes
this.load = load
this.cwd = cwd || process.cwd()
this.nonScopePrefix = `${org}-${type}-`
this.scopePrefix = `@${org}/${type}-`
if (org) {
this.nonScopePrefix = `${org}-${type}-`
this.scopePrefix = `@${org}/${type}-`
} else {
this.nonScopePrefix = `${type}-`
}
this.typePrefixLength = type.length + 1
/* - */
this.prefixSlicePosition = this.typePrefixLength + org.length + 1
Expand Down Expand Up @@ -197,7 +201,7 @@ class ModuleResolver {
const pkg = resolveScopePackage(req)
if (pkg) {
// speicial handling for default org.
if (pkg.org === this.org) {
if (this.org && pkg.org === this.org) {
shortcut = pkg.name.startsWith(`${this.type}-`)
? pkg.name.slice(this.typePrefixLength)
: pkg.name
Expand Down Expand Up @@ -265,6 +269,10 @@ export function resolveScopePackage (name: string) {
}
}

export const getMarkdownItResolver = (cwd: string) => new ModuleResolver(
'markdown-it', '', [String, Function], true /* load module */, cwd
)

export const getPluginResolver = (cwd: string): ModuleResolver => new ModuleResolver(
'plugin', 'vuepress', [String, Function, Object], true /* load module */, cwd
)
Expand Down
28 changes: 28 additions & 0 deletions packages/@vuepress/shared-utils/src/normalizeConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { assertTypes } from './datatypes'
import logger from './logger'
import chalk from 'chalk'

export default function normalizeConfig (pluginsConfig: any) {
const { valid, warnMsg } = assertTypes(pluginsConfig, [Object, Array])
if (!valid) {
if (pluginsConfig !== undefined) {
logger.warn(
`[${chalk.gray('config')}] `
+ `Invalid value for "plugin" field : ${warnMsg}`
)
}
pluginsConfig = []
return pluginsConfig
}

if (Array.isArray(pluginsConfig)) {
pluginsConfig = pluginsConfig.map(item => {
return Array.isArray(item) ? item : [item]
})
} else if (typeof pluginsConfig === 'object') {
pluginsConfig = Object.keys(pluginsConfig).map(item => {
return [item, pluginsConfig[item]]
})
}
return pluginsConfig
}
32 changes: 32 additions & 0 deletions packages/docs/docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,38 @@ This attribute will control the behaviour of `[[TOC]]`. It contains the followin

We also provide a [global component TOC](../guide/using-vue.md#toc) which allows for more free control by passing props directly to `<TOC>`.

### markdown.plugins

You can install any markdown-it plugins through `markdown.plugins` option. It is similar with [using VuePress plugins](../plugin/using-a-plugin.html#using-a-plugin). You can either use Babel style or object style. The `markdown-it-` prefix is optional and can omit in the list.

``` js
module.exports = {
markdown: {
plugins: [
'@org/foo', // equals to @org/markdown-it-foo if exists
['markdown-it-bar', {
// provide options here
}]
]
}
}
```

or

``` js
module.exports = {
markdown: {
plugins: {
'@org/foo': {}
'markdown-it-bar': {
// provide options here
}
}
}
}
```

### markdown.extendMarkdown

- Type: `Function`
Expand Down
32 changes: 32 additions & 0 deletions packages/docs/docs/zh/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,38 @@ VuePress 提供了一种添加额外样式的简便方法。你可以创建一

此外,我们还提供了[全局组件 TOC](../guide/using-vue.md#toc),可以通过直接向 `<TOC>` 传递属性实现更加自由的控制。

### markdown.plugins

你可以使用 `markdown.plugins` 来安装 markdown-it 插件。它的使用方法与[安装一个 VuePress 插件](../plugin/using-a-plugin.html#using-a-plugin)类似。你可以使用 Babel 语法或对象语法。`markdown-it-` 前缀同样是可以忽略的。

``` js
module.exports = {
markdown: {
plugins: [
'@org/foo', // 等价于 @org/markdown-it-foo,如果对应的包存在
['markdown-it-bar', {
// 提供你的选项
}]
]
}
}
```

or

``` js
module.exports = {
markdown: {
plugins: {
'@org/foo': {}
'markdown-it-bar': {
// 提供你的选项
}
}
}
}
```

### markdown.extendMarkdown

- 类型: `Function`
Expand Down

0 comments on commit 9734a58

Please sign in to comment.