Skip to content

Commit db16389

Browse files
authoredNov 10, 2020
feat($markdown): make page suffix configurable (close #2452) (#2674)
1 parent 970b434 commit db16389

File tree

7 files changed

+58
-9
lines changed

7 files changed

+58
-9
lines changed
 

‎packages/@vuepress/markdown/__tests__/link.spec.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ import { Md } from './util'
22
import link from '../lib/link.js'
33
import { dataReturnable } from '../index.js'
44

5-
const mdL = Md().use(link, {
5+
const EXTERNAL_ATTRS = {
66
target: '_blank',
77
rel: 'noopener noreferrer'
8-
})
8+
}
9+
10+
const setup = ({ externalAttrs = EXTERNAL_ATTRS, suffix } = {}) => {
11+
const mdL = Md().use(link, EXTERNAL_ATTRS, suffix)
12+
dataReturnable(mdL)
913

10-
dataReturnable(mdL)
14+
return mdL
15+
}
16+
17+
const mdL = setup()
1118

1219
const internalLinkAsserts = {
1320
// START absolute path usage
@@ -69,6 +76,21 @@ describe('link', () => {
6976
expect(html).toMatchSnapshot()
7077
}
7178
})
79+
80+
test('with custom page suffix should render links correctly', () => {
81+
const suffix = '/'
82+
const mdLSuffix = setup({ suffix })
83+
84+
for (const before in internalLinkAsserts) {
85+
const input = `[${before}](${before})`
86+
const output = mdLSuffix.render(input)
87+
const after = getCompiledLink(output)
88+
const value = internalLinkAsserts[before]
89+
const isHtmlLink = value === before
90+
const expected = isHtmlLink ? value : value.replace('.html', suffix)
91+
expect(after).toBe(expected)
92+
}
93+
})
7294
})
7395

7496
function getCompiledLink (output) {

‎packages/@vuepress/markdown/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
module.exports = (markdown = {}) => {
3232
const {
3333
externalLinks,
34+
pageSuffix,
3435
anchor,
3536
toc,
3637
plugins,
@@ -73,7 +74,7 @@ module.exports = (markdown = {}) => {
7374
.use(convertRouterLinkPlugin, [Object.assign({
7475
target: '_blank',
7576
rel: 'noopener noreferrer'
76-
}, externalLinks)])
77+
}, externalLinks), pageSuffix])
7778
.end()
7879

7980
.plugin(PLUGINS.HOIST_SCRIPT_STYLE)

‎packages/@vuepress/markdown/lib/link.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const url = require('url')
66

77
const indexRE = /(^|.*\/)(index|readme).md(#?.*)$/i
88

9-
module.exports = (md, externalAttrs) => {
9+
module.exports = (md, externalAttrs, pageSuffix = '.html') => {
1010
let hasOpenRouterLink = false
1111
let hasOpenExternalLink = false
1212

@@ -28,13 +28,13 @@ module.exports = (md, externalAttrs) => {
2828
}
2929
} else if (isSourceLink) {
3030
hasOpenRouterLink = true
31-
tokens[idx] = toRouterLink(token, link, relativePath)
31+
tokens[idx] = toRouterLink(token, link, relativePath, pageSuffix)
3232
}
3333
}
3434
return self.renderToken(tokens, idx, options)
3535
}
3636

37-
function toRouterLink (token, link, relativePath) {
37+
function toRouterLink (token, link, relativePath, suffix) {
3838
link[0] = 'to'
3939
let to = link[1]
4040

@@ -55,8 +55,8 @@ module.exports = (md, externalAttrs) => {
5555
to = path + hash
5656
} else {
5757
to = to
58-
.replace(/\.md$/, '.html')
59-
.replace(/\.md(#.*)$/, '.html$1')
58+
.replace(/\.md$/, suffix)
59+
.replace(/\.md(#.*)$/, `${suffix}$1`)
6060
}
6161

6262
// markdown-it encodes the uri

‎packages/docs/docs/config/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ Function for transforming [header](../miscellaneous/glossary.md#headers) texts i
277277

278278
Options for [markdown-it-anchor](https://github.com/valeriangalliat/markdown-it-anchor). (Note: prefer `markdown.slugify` to customize header ids.)
279279

280+
### markdown.pageSuffix
281+
282+
- Type: `string`
283+
- Default: `.html`
284+
285+
Option to customize internal links to be compatible when using the [vuepress-plugin-clean-urls](https://vuepress.github.io/en/plugins/clean-urls/).
286+
280287
### markdown.externalLinks
281288

282289
- Type: `Object`

‎packages/docs/docs/guide/markdown.md

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ VuePress supports redirecting to clean links. If a link `/foo` is not found, Vue
4343
Regardless of whether the permalink and clean-urls plugins are used, your relative path should be defined by the current file structure. In the above example, even though you set the path of `/foo/one.md` to `/foo/one/`, you should still access `/foo/two.md` via `./two.md`.
4444
:::
4545

46+
### Page Suffix
47+
48+
Pages and internal links get generated with the `.html` suffix by default.
49+
50+
You can customize this by setting [config.markdown.pageSuffix](../config/README.md#markdown-pagesuffix).
51+
4652
### External Links
4753

4854
Outbound links automatically get `target="_blank" rel="noopener noreferrer"`:

‎packages/docs/docs/zh/config/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ VuePress 提供了一种添加额外样式的简便方法。你可以创建一
273273

274274
[markdown-it-anchor](https://github.com/valeriangalliat/markdown-it-anchor) 的选项。
275275

276+
### markdown.pageSuffix
277+
278+
- 类型: `string`
279+
- 默认值: `.html`
280+
281+
Option to customize internal links to be compatible when using the [vuepress-plugin-clean-urls](https://vuepress.github.io/en/plugins/clean-urls/).
282+
276283
### markdown.externalLinks
277284

278285
- 类型: `Object`

‎packages/docs/docs/zh/guide/markdown.md

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ VuePress 支持重定向到干净链接。如果一个链接 `/foo` 找不到,
4343
无论是否使用了 permalink 和 clean-urls 插件,你的相对路径都应该依赖于当前的文件结构来定义。在上面的例子中,即使你将 `/foo/one.md` 的路径设为了 `/foo/one/`,你依然应该通过 `./two.md` 来访问 `/foo/two.md`
4444
:::
4545

46+
### Page Suffix
47+
48+
Pages and internal links get generated with the `.html` suffix by default.
49+
50+
You can customize this by setting [config.markdown.pageSuffix](../config/README.md#markdown-pagesuffix).
51+
4652
### 外部链接
4753

4854
外部的链接将会被自动地设置为 `target="_blank" rel="noopener noreferrer"`:

0 commit comments

Comments
 (0)
Please sign in to comment.