Skip to content

Commit d2fef5d

Browse files
kefranabgulivz
authored andcommittedOct 21, 2019
feat($markdown): extractHeaders option (close: #1903) (#1945)
1 parent cd72acc commit d2fef5d

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed
 

‎packages/@vuepress/core/lib/node/App.js

+1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ module.exports = class App {
344344

345345
async addPage (options) {
346346
options.permalinkPattern = this.siteConfig.permalink
347+
options.extractHeaders = this.siteConfig.markdown && this.siteConfig.markdown.extractHeaders
347348
const page = new Page(options, this)
348349
await page.process({
349350
markdown: this.markdown,

‎packages/@vuepress/core/lib/node/Page.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ module.exports = class Page {
4545
relative,
4646
permalink,
4747
frontmatter = {},
48-
permalinkPattern
48+
permalinkPattern,
49+
extractHeaders = ['h2', 'h3']
4950
}, context) {
5051
this.title = title
5152
this._meta = meta
@@ -54,6 +55,7 @@ module.exports = class Page {
5455
this._permalink = permalink
5556
this.frontmatter = frontmatter
5657
this._permalinkPattern = permalinkPattern
58+
this._extractHeaders = extractHeaders
5759
this._context = context
5860

5961
if (relative) {
@@ -111,12 +113,13 @@ module.exports = class Page {
111113
this.title = title
112114
}
113115

114-
// headers
116+
// extract headers
115117
const headers = extractHeaders(
116118
this._strippedContent,
117-
['h2', 'h3'],
119+
this._extractHeaders,
118120
markdown
119121
)
122+
120123
if (headers.length) {
121124
this.headers = headers
122125
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = function createBaseConfig (context, isServer) {
3434
const inlineLimit = 10000
3535

3636
const config = new Config()
37+
const extractHeaders = siteConfig.markdown && siteConfig.markdown.extractHeaders
3738

3839
config
3940
.mode(isProd && !env.isDebug ? 'production' : 'development')
@@ -118,7 +119,7 @@ module.exports = function createBaseConfig (context, isServer) {
118119
mdRule
119120
.use('markdown-loader')
120121
.loader(require.resolve('@vuepress/markdown-loader'))
121-
.options({ sourceDir, markdown })
122+
.options({ sourceDir, markdown, extractHeaders })
122123

123124
config.module
124125
.rule('pug')

‎packages/@vuepress/markdown-loader/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function (src) {
2222
const isServer = this.target === 'node'
2323
const options = getOptions(this)
2424
const loader = Object.create(this)
25-
const { sourceDir } = options
25+
const { sourceDir, extractHeaders: extractHeadersPattern = ['h2', 'h3'] } = options
2626
let { markdown } = options
2727
if (!markdown) {
2828
markdown = md()
@@ -43,7 +43,7 @@ module.exports = function (src) {
4343

4444
if (!isProd && !isServer) {
4545
const inferredTitle = inferTitle(frontmatter.data, frontmatter.content)
46-
const headers = extractHeaders(content, ['h2', 'h3'], markdown)
46+
const headers = extractHeaders(content, extractHeadersPattern, markdown)
4747
delete frontmatter.content
4848

4949
// diff frontmatter and title, since they are not going to be part of the

‎packages/@vuepress/theme-default/components/SidebarLink.vue

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default {
5454
}
5555
}
5656
57-
function renderLink (h, to, text, active) {
57+
function renderLink (h, to, text, active, level) {
5858
return h('router-link', {
5959
props: {
6060
to,
@@ -64,6 +64,9 @@ function renderLink (h, to, text, active) {
6464
class: {
6565
active,
6666
'sidebar-link': true
67+
},
68+
style: {
69+
'padding-left': level + 'rem'
Has conversations. Original line has conversations.
6770
}
6871
}, text)
6972
}
@@ -73,7 +76,7 @@ function renderChildren (h, children, path, route, maxDepth, depth = 1) {
7376
return h('ul', { class: 'sidebar-sub-headers' }, children.map(c => {
7477
const active = isActive(route, path + '#' + c.slug)
7578
return h('li', { class: 'sidebar-sub-header' }, [
76-
renderLink(h, path + '#' + c.slug, c.title, active),
79+
renderLink(h, path + '#' + c.slug, c.title, active, c.level - 1),
7780
renderChildren(h, c.children, path, route, maxDepth, depth + 1)
7881
])
7982
}))

‎packages/docs/docs/theme/default-theme-config.md

+11
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ module.exports = {
169169
}
170170
```
171171

172+
### Extract Headers
173+
While preparing the page, headers are extracted from the Markdown file and stored in `this.$page.headers`. By default, VuePress will extract `h2` and `h3` elements for you.
174+
You can override the headers it pulls out in your `markdown` options.
175+
``` js
176+
module.exports = {
177+
markdown: {
178+
extractHeaders: [ 'h2', 'h3', 'h4' ]
179+
}
180+
}
181+
```
182+
172183
### Active Header Links
173184

174185
By default, the nested header links and the hash in the URL are updated as the user scrolls to view the different sections of the page. This behavior can be disabled with the following theme config:

0 commit comments

Comments
 (0)
Please sign in to comment.