Skip to content

Define Headers to Parse during Page Prep - resolve #984 #1004

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 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -270,6 +270,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
6 changes: 4 additions & 2 deletions packages/@vuepress/core/lib/prepare/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ module.exports = class Page {
relative,
permalink,
frontmatter = {},
permalinkPattern
permalinkPattern,
siteConfig
}) {
this.title = title
this._meta = meta
Expand All @@ -41,6 +42,7 @@ module.exports = class Page {
this._permalink = permalink
this.frontmatter = frontmatter
this._permalinkPattern = permalinkPattern
this._siteConfig = siteConfig

if (relative) {
this.regularPath = encodeURI(fileToPath(relative))
Expand Down Expand Up @@ -89,7 +91,7 @@ module.exports = class Page {
// headers
const headers = extractHeaders(
this._strippedContent,
['h2', 'h3'],
this._siteConfig.themeConfig.extractHeaders || ['h2', 'h3'],
markdown
)
if (headers.length) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/markdown-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function (src) {

if (!isProd && !isServer) {
const inferredTitle = inferTitle(frontmatter.data, frontmatter.content)
const headers = extractHeaders(content, ['h2', 'h3'], markdown)
const headers = extractHeaders(content, options.extractHeaders || ['h2', 'h3'], markdown)
delete frontmatter.content

// diff frontmatter and title, since they are not going to be part of the
Expand Down
24 changes: 19 additions & 5 deletions packages/docs/docs/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ 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 `themeConfig` options.

``` js
module.exports = {
themeConfig: {
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 Expand Up @@ -329,7 +343,7 @@ module.exports = {
```

::: warning Note
Unlike the [built-in search](#built-in-search) engine which works out of the box, [Algolia DocSearch](https://community.algolia.com/docsearch/) requires you to submit your site to them for indexing before it starts working.
Unlike the [built-in search](#built-in-search) engine which works out of the box, [Algolia DocSearch](https://community.algolia.com/docsearch/) requires you to submit your site to them for indexing before it starts working.
:::

For more options, refer to [Algolia DocSearch's documentation](https://github.com/algolia/docsearch#docsearch-options).
Expand Down Expand Up @@ -373,10 +387,10 @@ module.exports = {
themeConfig: {
serviceWorker: {
updatePopup: true // Boolean | Object, default to undefined.
// If set to true, the default text config will be:
// updatePopup: {
// message: "New content is available.",
// buttonText: "Refresh"
// If set to true, the default text config will be:
// updatePopup: {
// message: "New content is available.",
// buttonText: "Refresh"
// }
}
}
Expand Down