Skip to content

Commit 53c84b3

Browse files
committed
refactor($core): separate markdown and markdown-loader from core
1 parent c3cf73f commit 53c84b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+283
-145
lines changed

packages/@vuepress/core/lib/dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
1717
const DevLogPlugin = require('./webpack/DevLogPlugin')
1818
const createClientConfig = require('./webpack/createClientConfig')
1919
const { applyUserWebpackConfig } = require('./util/index')
20-
const { frontmatterEmitter } = require('./webpack/markdownLoader')
20+
const { frontmatterEmitter } = require('@vuepress/markdown-loader')
2121

2222
logger.wait('\nExtracting site metadata...')
2323
const options = await prepare(sourceDir, cliOptions, false /* isProd */)

packages/@vuepress/core/lib/prepare/AppContext.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
const path = require('path')
8-
const createMarkdown = require('../markdown/index')
8+
const createMarkdown = require('./createMarkdown')
99
const loadConfig = require('./loadConfig')
1010
const loadTheme = require('./loadTheme')
1111
const { fs, logger, chalk, globby, sort, datatypes: { isFunction }} = require('@vuepress/shared-utils')
@@ -69,7 +69,7 @@ module.exports = class AppContext {
6969
this.resolveTemplates()
7070
await this.resolveTheme()
7171
this.resolvePlugins()
72-
this.markdown = createMarkdown(this.siteConfig.markdown, this.pluginAPI)
72+
this.markdown = createMarkdown(this)
7373

7474
await this.resolvePages()
7575
await Promise.all(
@@ -79,7 +79,6 @@ module.exports = class AppContext {
7979
)
8080

8181
await this.pluginAPI.options.ready.apply()
82-
this.pluginAPI.options.extendMarkdown.syncApply(this.markdown)
8382
await this.pluginAPI.options.clientDynamicModules.apply(this)
8483
await this.pluginAPI.options.globalUIComponents.apply(this)
8584
await this.pluginAPI.options.enhanceAppFiles.apply(this)

packages/@vuepress/core/lib/prepare/Page.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66

77
const path = require('path')
8-
const slugify = require('../markdown/slugify')
9-
const { inferTitle, inferDate, extractHeaders, DATE_RE } = require('../util/index')
10-
const { fs, fileToPath, parseFrontmatter, getPermalink } = require('@vuepress/shared-utils')
8+
const slugify = require('../../../markdown/lib/slugify')
9+
const { inferDate, DATE_RE } = require('../util/index')
10+
const { extractHeaders, fs, fileToPath, parseFrontmatter, getPermalink, inferTitle } = require('@vuepress/shared-utils')
1111

1212
/**
1313
* Expose Page class.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const createMarkdown = require('@vuepress/markdown')
8+
9+
/**
10+
* Expose createMarkdown.
11+
*/
12+
13+
module.exports = function (ctx) {
14+
const { markdown: markdownConfig = {}} = ctx.siteConfig
15+
const { chainMarkdown, extendMarkdown } = markdownConfig
16+
17+
const beforeInstantiate = config => {
18+
chainMarkdown && chainMarkdown(config)
19+
ctx.pluginAPI.options.chainMarkdown.syncApply(config)
20+
}
21+
22+
const afterInstantiate = md => {
23+
extendMarkdown && extendMarkdown(md)
24+
ctx.pluginAPI.options.extendMarkdown.syncApply(md)
25+
}
26+
27+
return createMarkdown(
28+
Object.assign(markdownConfig, {
29+
beforeInstantiate,
30+
afterInstantiate
31+
})
32+
)
33+
}

packages/@vuepress/core/lib/util/index.js

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
'use strict'
22

3-
/**
4-
* Module dependencies.
5-
*/
6-
7-
const { deeplyParseHeaders } = require('@vuepress/shared-utils')
8-
93
/**
104
* Normalize head tag config.
115
*
@@ -49,65 +43,6 @@ exports.applyUserWebpackConfig = function (userConfig, config, isServer) {
4943
return config
5044
}
5145

52-
/**
53-
* Infer a page's title via frontmatter and content.
54-
*
55-
* @param frontmatter
56-
* @param strippedContent
57-
* @returns {*}
58-
*/
59-
60-
exports.inferTitle = function (frontmatter, strippedContent) {
61-
if (frontmatter.home) {
62-
return 'Home'
63-
}
64-
if (frontmatter.title) {
65-
return deeplyParseHeaders(frontmatter.title)
66-
}
67-
const match = strippedContent.trim().match(/^#+\s+(.*)/)
68-
if (match) {
69-
return deeplyParseHeaders(match[1])
70-
}
71-
}
72-
73-
/**
74-
* Extract heeaders from markdown source content.
75-
*
76-
* @param {string} content
77-
* @param {array} include
78-
* @param {object} md
79-
* @returns {array}
80-
*/
81-
82-
const LRU = require('lru-cache')
83-
const cache = LRU({ max: 1000 })
84-
85-
exports.extractHeaders = function (content, include = [], md) {
86-
const key = content + include.join(',')
87-
const hit = cache.get(key)
88-
if (hit) {
89-
return hit
90-
}
91-
92-
const tokens = md.parse(content, {})
93-
94-
const res = []
95-
tokens.forEach((t, i) => {
96-
if (t.type === 'heading_open' && include.includes(t.tag)) {
97-
const title = tokens[i + 1].content
98-
const slug = t.attrs.find(([name]) => name === 'id')[1]
99-
res.push({
100-
level: parseInt(t.tag.slice(1), 10),
101-
title: deeplyParseHeaders(title),
102-
slug: slug || md.slugify(title)
103-
})
104-
}
105-
})
106-
107-
cache.set(key, res)
108-
return res
109-
}
110-
11146
/**
11247
* Infer date.
11348
*

packages/@vuepress/core/lib/webpack/createBaseConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ module.exports = function createBaseConfig ({
136136

137137
mdRule
138138
.use('markdown-loader')
139-
.loader(require.resolve('./markdownLoader'))
139+
.loader(require.resolve('@vuepress/markdown-loader'))
140140
.options({ sourceDir, markdown })
141141

142142
config.module

packages/@vuepress/core/package.json

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"dependencies": {
2222
"@babel/core": "7.0.0-beta.47",
2323
"@vue/babel-preset-app": "3.0.0-beta.11",
24+
"@vuepress/markdown": "^1.0.0",
25+
"@vuepress/markdown-loader": "^1.0.0",
2426
"@vuepress/shared-utils": "^1.0.0",
2527
"@vuepress/plugin-last-updated": "^1.0.0",
2628
"@vuepress/plugin-register-components": "^1.0.0",
@@ -33,8 +35,6 @@
3335
"copy-webpack-plugin": "^4.5.1",
3436
"cross-spawn": "^6.0.5",
3537
"css-loader": "^0.28.11",
36-
"diacritics": "^1.3.0",
37-
"escape-html": "^1.0.3",
3838
"file-loader": "^1.1.11",
3939
"globby": "^8.0.1",
4040
"gray-matter": "^4.0.1",
@@ -43,18 +43,11 @@
4343
"koa-mount": "^3.0.0",
4444
"koa-range": "^0.3.0",
4545
"koa-static": "^4.0.2",
46-
"loader-utils": "^1.1.0",
4746
"lru-cache": "^4.1.2",
48-
"markdown-it": "^8.4.1",
49-
"markdown-it-anchor": "^5.0.2",
50-
"markdown-it-container": "^2.0.0",
51-
"markdown-it-emoji": "^1.4.0",
52-
"markdown-it-table-of-contents": "^0.4.0",
5347
"mini-css-extract-plugin": "^0.4.1",
5448
"optimize-css-assets-webpack-plugin": "^4.0.0",
5549
"portfinder": "^1.0.13",
5650
"postcss-loader": "^2.1.5",
57-
"prismjs": "^1.13.0",
5851
"toml": "^2.3.3",
5952
"url-loader": "^1.0.1",
6053
"vue": "^2.5.16",
@@ -67,8 +60,7 @@
6760
"webpack-chain": "^4.6.0",
6861
"webpack-merge": "^4.1.2",
6962
"webpack-serve": "^1.0.2",
70-
"webpackbar": "^2.6.1",
71-
"markdown-it-chain": "^1.2.0"
63+
"webpackbar": "^2.6.1"
7264
},
7365
"engines": {
7466
"node": ">=8"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__tests__
2+
__mocks__
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# @vuepress/markdown-loader
2+
3+
> markdown-loader for vuepress
4+
5+
## Usage
6+
7+
```js
8+
const rule = config.module
9+
.rule('markdown')
10+
.test(/\.md$/)
11+
12+
rule
13+
.use('vue-loader')
14+
.loader('vue-loader')
15+
.options({ /* ... */ })
16+
17+
rule
18+
.use('markdown-loader')
19+
.loader(require.resolve('@vuepress/markdown-loader'))
20+
.options({
21+
markdown: /* instance created by @vuepress/markdown */,
22+
sourceDir: /* root source directory of your docs */,
23+
})
24+
```

packages/@vuepress/core/lib/webpack/markdownLoader.js renamed to packages/@vuepress/markdown-loader/index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
* Module dependencies.
55
*/
66

7-
const fs = require('fs')
87
const path = require('path')
9-
const hash = require('hash-sum')
108
const { EventEmitter } = require('events')
119
const { getOptions } = require('loader-utils')
12-
const { inferTitle, extractHeaders } = require('../util/index')
13-
const { parseFrontmatter } = require('@vuepress/shared-utils')
10+
const { fs, hash, parseFrontmatter, inferTitle, extractHeaders } = require('@vuepress/shared-utils')
1411
const LRU = require('lru-cache')
12+
const md = require('@vuepress/markdown')
1513

1614
const cache = LRU({ max: 1000 })
1715
const devCache = LRU({ max: 1000 })
@@ -23,7 +21,12 @@ const devCache = LRU({ max: 1000 })
2321
module.exports = function (src) {
2422
const isProd = process.env.NODE_ENV === 'production'
2523
const isServer = this.target === 'node'
26-
const { markdown, sourceDir } = getOptions(this)
24+
const options = getOptions(this)
25+
const { sourceDir } = options
26+
let { markdown } = options
27+
if (!markdown) {
28+
markdown = md()
29+
}
2730

2831
// we implement a manual cache here because this loader is chained before
2932
// vue-loader, and will be applied on the same file multiple times when
@@ -69,18 +72,23 @@ module.exports = function (src) {
6972
// check if relative links are valid
7073
links && links.forEach(link => {
7174
link = decodeURIComponent(link)
75+
7276
const shortname = link
7377
.replace(/#.*$/, '')
7478
.replace(/\.html$/, '.md')
79+
7580
const filename = shortname
7681
.replace(/\/$/, '/README.md')
7782
.replace(/^\//, sourceDir + '/')
83+
7884
const altname = shortname
7985
.replace(/\/$/, '/index.md')
8086
.replace(/^\//, sourceDir + '/')
87+
8188
const dir = path.dirname(this.resourcePath)
8289
const file = path.resolve(dir, filename)
8390
const altfile = altname !== filename ? path.resolve(dir, altname) : null
91+
8492
if (!fs.existsSync(file) && (!altfile || !fs.existsSync(altfile))) {
8593
this.emitWarning(
8694
new Error(
@@ -95,7 +103,6 @@ module.exports = function (src) {
95103
`<template>\n` +
96104
`<div class="content">${html}</div>\n` +
97105
`</template>\n` +
98-
`<script>export default { props: ['target'] }</script>` +
99106
(hoistedTags || []).join('\n')
100107
)
101108
cache.set(key, res)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@vuepress/markdown-loader",
3+
"version": "1.0.0",
4+
"description": "markdown-loader 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+
},
13+
"keywords": [
14+
"documentation",
15+
"vue",
16+
"vuepress",
17+
"generator"
18+
],
19+
"dependencies": {
20+
"@vuepress/markdown": "1.0.0",
21+
"loader-utils": "^1.1.0"
22+
},
23+
"author": "Evan You",
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/vuejs/vuepress/issues"
27+
},
28+
"homepage": "https://github.com/vuejs/vuepress/packages/@vuepress/markdown-loader#readme"
29+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__tests__
2+
__mocks__

packages/@vuepress/markdown/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @vuepress/markdown
2+
3+
> markdown for vuepress

packages/@vuepress/core/__test__/markdown/containers.spec.js renamed to packages/@vuepress/markdown/__tests__/containers.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Md, getFragment } from './util'
2-
import containers from '../../lib/markdown/containers.js'
2+
import containers from '../lib/containers.js'
33

44
const mdC = Md().use(containers)
55

packages/@vuepress/core/__test__/markdown/highlight.spec.js renamed to packages/@vuepress/markdown/__tests__/highlight.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Md, getFragment } from './util'
2-
import highlight from '../../lib/markdown/highlight.js'
2+
import highlight from '../lib/highlight.js'
33

44
const md = Md()
55
const mdH = Md().set({ highlight })

packages/@vuepress/core/__test__/markdown/highlightLines.spec.js renamed to packages/@vuepress/markdown/__tests__/highlightLines.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Md, getFragment } from './util'
2-
import highlightLines from '../../lib/markdown/highlightLines.js'
2+
import highlightLines from '../lib/highlightLines.js'
33

44
const md = Md()
55
const mdH = Md().use(highlightLines)

packages/@vuepress/core/__test__/markdown/hoist.spec.js renamed to packages/@vuepress/markdown/__tests__/hoist.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Md, getFragment } from './util'
2-
import hoist from '../../lib/markdown/hoist.js'
3-
import { dataReturnable } from '../../lib/markdown/index.js'
2+
import hoist from '../lib/hoist.js'
3+
import { dataReturnable } from '../lib/index.js'
44

55
const md = Md().set({ html: true })
66
const mdH = Md().set({ html: true }).use(hoist)

packages/@vuepress/core/__test__/markdown/lineNumers.spec.js renamed to packages/@vuepress/markdown/__tests__/lineNumers.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Md, getFragment } from './util'
2-
import preWrapper from '../../lib/markdown/preWrapper.js'
3-
import lineNumbers from '../../lib/markdown/lineNumbers.js'
4-
import highlightLines from '../../lib/markdown/highlightLines.js'
2+
import preWrapper from '../lib/preWrapper.js'
3+
import lineNumbers from '../lib/lineNumbers.js'
4+
import highlightLines from '../lib/highlightLines.js'
55

66
// lineNumbers must be chained after preWrapper.
77
// since lineNumbers needs to add extra stateful class to its block wrapper.

packages/@vuepress/core/__test__/markdown/link.spec.js renamed to packages/@vuepress/markdown/__tests__/link.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Md } from './util'
2-
import link from '../../lib/markdown/link.js'
3-
import { dataReturnable } from '../../lib/markdown/index.js'
2+
import link from '../lib/link.js'
3+
import { dataReturnable } from '../lib/index.js'
44

55
const mdL = Md().use(link, {
66
target: '_blank',

0 commit comments

Comments
 (0)