Skip to content

Allowing config option to set which headers to extract #1111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/@vuepress/core/lib/prepare/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ module.exports = class AppContext {

async addPage (options) {
options.permalinkPattern = this.siteConfig.permalink
options.siteConfig = this.siteConfig
const page = new Page(options, this)
await page.process({
markdown: this.markdown,
Expand Down
10 changes: 8 additions & 2 deletions packages/@vuepress/core/lib/prepare/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ module.exports = class Page {
relative,
permalink,
frontmatter = {},
permalinkPattern
permalinkPattern,
siteConfig
}, context) {
this.title = title
this._meta = meta
Expand All @@ -54,6 +55,7 @@ module.exports = class Page {
this._permalink = permalink
this.frontmatter = frontmatter
this._permalinkPattern = permalinkPattern
this._siteConfig = siteConfig
this._context = context

if (relative) {
Expand Down Expand Up @@ -108,9 +110,13 @@ module.exports = class Page {
}

// headers
this.headersToExtract = ['h2', 'h3']
if (this._siteConfig.markdown && this._siteConfig.markdown.extractHeaders) {
this.headersToExtract = this._siteConfig.markdown.extractHeaders
}
const headers = extractHeaders(
this._strippedContent,
['h2', 'h3'],
this.headersToExtract,
markdown
)
if (headers.length) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@vuepress/core/lib/webpack/createBaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = function createBaseConfig ({
const inlineLimit = 10000

const config = new Config()
const markdownConfig = siteConfig.markdown

config
.mode(isProd && !env.isDebug ? 'production' : 'development')
Expand Down Expand Up @@ -116,7 +117,7 @@ module.exports = function createBaseConfig ({
mdRule
.use('markdown-loader')
.loader(require.resolve('@vuepress/markdown-loader'))
.options({ sourceDir, markdown })
.options({ sourceDir, markdown, markdownConfig })

config.module
.rule('pug')
Expand Down
7 changes: 6 additions & 1 deletion packages/@vuepress/markdown-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = function (src) {
const isServer = this.target === 'node'
const options = getOptions(this)
const { sourceDir } = options
const { markdownConfig } = options
let { markdown } = options
if (!markdown) {
markdown = md()
Expand All @@ -42,7 +43,11 @@ module.exports = function (src) {

if (!isProd && !isServer) {
const inferredTitle = inferTitle(frontmatter.data, frontmatter.content)
const headers = extractHeaders(content, ['h2', 'h3'], markdown)
let headersToExtract = ['h2', 'h3']
if (markdownConfig && markdownConfig.extractHeaders) {
headersToExtract = markdownConfig.extractHeaders
}
const headers = extractHeaders(content, headersToExtract, markdown)
Copy link
Member

@ulivz ulivz Jan 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember I have said that siteConfig shouldn't be passed to markdown-loader like a giant brick

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I just change this to pass in siteConfig.markdown instead of all of siteConfig?

delete frontmatter.content

// diff frontmatter and title, since they are not going to be part of the
Expand Down
11 changes: 11 additions & 0 deletions packages/docs/docs/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ module.exports = {
}
```

### Extract Headers
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.
You can override the headers it pulls out in your `markdown` options.
``` js
module.exports = {
markdown: {
extractHeaders: [ 'h2', 'h3', 'h4' ]
}
}
```

### Active Header Links

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:
Expand Down