From 75f7613a2cc0f1ab908802414258ea7c6338c8c9 Mon Sep 17 00:00:00 2001 From: Brian Retterer Date: Sun, 16 Dec 2018 17:27:20 -0500 Subject: [PATCH 1/3] Allowing config option to set which headers to extract --- packages/@vuepress/core/lib/prepare/AppContext.js | 1 + packages/@vuepress/core/lib/prepare/Page.js | 10 ++++++++-- .../@vuepress/core/lib/webpack/createBaseConfig.js | 2 +- packages/@vuepress/markdown-loader/index.js | 7 ++++++- packages/docs/docs/theme/default-theme-config.md | 11 +++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/@vuepress/core/lib/prepare/AppContext.js b/packages/@vuepress/core/lib/prepare/AppContext.js index c1176a154b..235fbb5851 100644 --- a/packages/@vuepress/core/lib/prepare/AppContext.js +++ b/packages/@vuepress/core/lib/prepare/AppContext.js @@ -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, diff --git a/packages/@vuepress/core/lib/prepare/Page.js b/packages/@vuepress/core/lib/prepare/Page.js index 04b7178a50..62851c15f0 100644 --- a/packages/@vuepress/core/lib/prepare/Page.js +++ b/packages/@vuepress/core/lib/prepare/Page.js @@ -45,7 +45,8 @@ module.exports = class Page { relative, permalink, frontmatter = {}, - permalinkPattern + permalinkPattern, + siteConfig }, context) { this.title = title this._meta = meta @@ -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) { @@ -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) { diff --git a/packages/@vuepress/core/lib/webpack/createBaseConfig.js b/packages/@vuepress/core/lib/webpack/createBaseConfig.js index c04aae63d5..7e49022d80 100644 --- a/packages/@vuepress/core/lib/webpack/createBaseConfig.js +++ b/packages/@vuepress/core/lib/webpack/createBaseConfig.js @@ -116,7 +116,7 @@ module.exports = function createBaseConfig ({ mdRule .use('markdown-loader') .loader(require.resolve('@vuepress/markdown-loader')) - .options({ sourceDir, markdown }) + .options({ sourceDir, markdown, siteConfig }) config.module .rule('pug') diff --git a/packages/@vuepress/markdown-loader/index.js b/packages/@vuepress/markdown-loader/index.js index f4fd02ab9b..8b57fd8fb0 100644 --- a/packages/@vuepress/markdown-loader/index.js +++ b/packages/@vuepress/markdown-loader/index.js @@ -22,6 +22,7 @@ module.exports = function (src) { const isServer = this.target === 'node' const options = getOptions(this) const { sourceDir } = options + const { siteConfig } = options let { markdown } = options if (!markdown) { markdown = md() @@ -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 (siteConfig.markdown && siteConfig.markdown.extractHeaders) { + headersToExtract = siteConfig.markdown.extractHeaders + } + const headers = extractHeaders(content, headersToExtract, markdown) delete frontmatter.content // diff frontmatter and title, since they are not going to be part of the diff --git a/packages/docs/docs/theme/default-theme-config.md b/packages/docs/docs/theme/default-theme-config.md index e921d46ce2..71c5fda210 100644 --- a/packages/docs/docs/theme/default-theme-config.md +++ b/packages/docs/docs/theme/default-theme-config.md @@ -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: From 733e9afad4ceca5f7e74464b8010662981288ebd Mon Sep 17 00:00:00 2001 From: Brian Retterer Date: Thu, 17 Jan 2019 22:26:23 -0500 Subject: [PATCH 2/3] Passes in only siteConfig.markdown into markdown-loader --- packages/@vuepress/core/lib/webpack/createBaseConfig.js | 3 ++- packages/@vuepress/markdown-loader/index.js | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/@vuepress/core/lib/webpack/createBaseConfig.js b/packages/@vuepress/core/lib/webpack/createBaseConfig.js index 7e49022d80..c9eeafc80b 100644 --- a/packages/@vuepress/core/lib/webpack/createBaseConfig.js +++ b/packages/@vuepress/core/lib/webpack/createBaseConfig.js @@ -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') @@ -116,7 +117,7 @@ module.exports = function createBaseConfig ({ mdRule .use('markdown-loader') .loader(require.resolve('@vuepress/markdown-loader')) - .options({ sourceDir, markdown, siteConfig }) + .options({ sourceDir, markdown, markdownConfig }) config.module .rule('pug') diff --git a/packages/@vuepress/markdown-loader/index.js b/packages/@vuepress/markdown-loader/index.js index 8b57fd8fb0..4661476bc1 100644 --- a/packages/@vuepress/markdown-loader/index.js +++ b/packages/@vuepress/markdown-loader/index.js @@ -22,7 +22,7 @@ module.exports = function (src) { const isServer = this.target === 'node' const options = getOptions(this) const { sourceDir } = options - const { siteConfig } = options + const { markdownConfig } = options let { markdown } = options if (!markdown) { markdown = md() @@ -44,8 +44,8 @@ module.exports = function (src) { if (!isProd && !isServer) { const inferredTitle = inferTitle(frontmatter.data, frontmatter.content) let headersToExtract = ['h2', 'h3'] - if (siteConfig.markdown && siteConfig.markdown.extractHeaders) { - headersToExtract = siteConfig.markdown.extractHeaders + if (markdownConfig && markdownConfig.extractHeaders) { + headersToExtract = markdownConfig.extractHeaders } const headers = extractHeaders(content, headersToExtract, markdown) delete frontmatter.content From cf5d845e1c47d6eebd8461cb04b12c6317b28934 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Tue, 30 Jul 2019 17:31:01 +0000 Subject: [PATCH 3/3] feat: markdown.extractHeaders option - specify the headers of extracted headers --- packages/@vuepress/core/lib/prepare/AppContext.js | 1 + packages/@vuepress/core/lib/prepare/Page.js | 13 +++++-------- .../@vuepress/core/lib/webpack/createBaseConfig.js | 4 ++-- packages/@vuepress/markdown-loader/index.js | 9 ++------- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/packages/@vuepress/core/lib/prepare/AppContext.js b/packages/@vuepress/core/lib/prepare/AppContext.js index 235fbb5851..b95823201a 100644 --- a/packages/@vuepress/core/lib/prepare/AppContext.js +++ b/packages/@vuepress/core/lib/prepare/AppContext.js @@ -73,6 +73,7 @@ module.exports = class AppContext { this.base = this.siteConfig.base || '/' this.themeConfig = this.siteConfig.themeConfig || {} + this.extractHeaders = this.siteConfig.markdown && this.siteConfig.markdown.extractHeaders const rawOutDir = this.cliOptions.dest || this.siteConfig.dest this.outDir = rawOutDir diff --git a/packages/@vuepress/core/lib/prepare/Page.js b/packages/@vuepress/core/lib/prepare/Page.js index 62851c15f0..a0d4dd3786 100644 --- a/packages/@vuepress/core/lib/prepare/Page.js +++ b/packages/@vuepress/core/lib/prepare/Page.js @@ -46,7 +46,7 @@ module.exports = class Page { permalink, frontmatter = {}, permalinkPattern, - siteConfig + extractHeaders = ['h2', 'h3'] }, context) { this.title = title this._meta = meta @@ -55,7 +55,7 @@ module.exports = class Page { this._permalink = permalink this.frontmatter = frontmatter this._permalinkPattern = permalinkPattern - this._siteConfig = siteConfig + this._extractHeaders = extractHeaders this._context = context if (relative) { @@ -109,16 +109,13 @@ module.exports = class Page { this.title = title } - // headers - this.headersToExtract = ['h2', 'h3'] - if (this._siteConfig.markdown && this._siteConfig.markdown.extractHeaders) { - this.headersToExtract = this._siteConfig.markdown.extractHeaders - } + // extract headers const headers = extractHeaders( this._strippedContent, - this.headersToExtract, + this._extractHeaders, markdown ) + if (headers.length) { this.headers = headers } diff --git a/packages/@vuepress/core/lib/webpack/createBaseConfig.js b/packages/@vuepress/core/lib/webpack/createBaseConfig.js index c9eeafc80b..7a840e615c 100644 --- a/packages/@vuepress/core/lib/webpack/createBaseConfig.js +++ b/packages/@vuepress/core/lib/webpack/createBaseConfig.js @@ -33,7 +33,7 @@ module.exports = function createBaseConfig ({ const inlineLimit = 10000 const config = new Config() - const markdownConfig = siteConfig.markdown + const extractHeaders = siteConfig.markdown && siteConfig.markdown.extractHeaders config .mode(isProd && !env.isDebug ? 'production' : 'development') @@ -117,7 +117,7 @@ module.exports = function createBaseConfig ({ mdRule .use('markdown-loader') .loader(require.resolve('@vuepress/markdown-loader')) - .options({ sourceDir, markdown, markdownConfig }) + .options({ sourceDir, markdown, extractHeaders }) config.module .rule('pug') diff --git a/packages/@vuepress/markdown-loader/index.js b/packages/@vuepress/markdown-loader/index.js index 4661476bc1..3be7c68c9e 100644 --- a/packages/@vuepress/markdown-loader/index.js +++ b/packages/@vuepress/markdown-loader/index.js @@ -21,8 +21,7 @@ module.exports = function (src) { const isProd = process.env.NODE_ENV === 'production' const isServer = this.target === 'node' const options = getOptions(this) - const { sourceDir } = options - const { markdownConfig } = options + const { sourceDir, extractHeaders: extractHeadersPattern = ['h2', 'h3'] } = options let { markdown } = options if (!markdown) { markdown = md() @@ -43,11 +42,7 @@ module.exports = function (src) { if (!isProd && !isServer) { const inferredTitle = inferTitle(frontmatter.data, frontmatter.content) - let headersToExtract = ['h2', 'h3'] - if (markdownConfig && markdownConfig.extractHeaders) { - headersToExtract = markdownConfig.extractHeaders - } - const headers = extractHeaders(content, headersToExtract, markdown) + const headers = extractHeaders(content, extractHeadersPattern, markdown) delete frontmatter.content // diff frontmatter and title, since they are not going to be part of the