Skip to content

Commit a8c7fdd

Browse files
committed
fix(markdown): remove site base from internal links (close #58)
1 parent 35b8d3f commit a8c7fdd

File tree

2 files changed

+137
-6
lines changed

2 files changed

+137
-6
lines changed

packages/@vuepress/markdown/__tests__/plugins/linksPlugin.spec.ts

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,133 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
399399
},
400400
])
401401
})
402+
it('should not conflict with base', () => {
403+
const md = MarkdownIt({ html: true }).use(linksPlugin)
404+
const env: MarkdownEnv = {
405+
base: '/path/',
406+
filePathRelative: 'path/to/file.md',
407+
}
408+
409+
const rendered = md.render(source, env)
410+
411+
expect(rendered).toEqual(
412+
[
413+
'<RouterLink to="/path/to/foo.html">foo1</RouterLink>',
414+
'<RouterLink to="/path/to/foo.html#hash">foo2</RouterLink>',
415+
'<RouterLink to="/path/to/foo.html">foo3</RouterLink>',
416+
'<RouterLink to="/path/bar.html">bar1</RouterLink>',
417+
'<RouterLink to="/path/bar.html#hash">bar2</RouterLink>',
418+
'<RouterLink to="/path/bar.html">bar3</RouterLink>',
419+
'<RouterLink to="/path/to/foo/bar.html">foobar1</RouterLink>',
420+
'<RouterLink to="/path/to/foo/bar.html#hash">foobar2</RouterLink>',
421+
'<RouterLink to="/path/foo/bar.html">foobar3</RouterLink>',
422+
'<RouterLink to="/path/foo/bar.html#hash">foobar4</RouterLink>',
423+
'<RouterLink to="/path/to/">index1</RouterLink>',
424+
'<RouterLink to="/path/to/#hash">index2</RouterLink>',
425+
'<RouterLink to="/path/to/">index3</RouterLink>',
426+
'<RouterLink to="/path/">index4</RouterLink>',
427+
'<RouterLink to="/path/foo/bar/">index5</RouterLink>',
428+
'<RouterLink to="/path/to/">readme1</RouterLink>',
429+
'<RouterLink to="/path/#hash">readme2</RouterLink>',
430+
'<RouterLink to="/path/foo/bar/">readme3</RouterLink>',
431+
]
432+
.map((a) => `<p>${a}</p>`)
433+
.join('\n') + '\n'
434+
)
435+
436+
expect(env.links).toEqual([
437+
{
438+
raw: 'foo.md',
439+
relative: 'path/to/foo.md',
440+
absolute: '/path/path/to/foo.md',
441+
},
442+
{
443+
raw: 'foo.md#hash',
444+
relative: 'path/to/foo.md',
445+
absolute: '/path/path/to/foo.md',
446+
},
447+
{
448+
raw: './foo.md',
449+
relative: 'path/to/foo.md',
450+
absolute: '/path/path/to/foo.md',
451+
},
452+
{
453+
raw: '../bar.md',
454+
relative: 'path/bar.md',
455+
absolute: '/path/path/bar.md',
456+
},
457+
{
458+
raw: '../bar.md#hash',
459+
relative: 'path/bar.md',
460+
absolute: '/path/path/bar.md',
461+
},
462+
{
463+
raw: './../bar.md',
464+
relative: 'path/bar.md',
465+
absolute: '/path/path/bar.md',
466+
},
467+
{
468+
raw: 'foo/bar.md',
469+
relative: 'path/to/foo/bar.md',
470+
absolute: '/path/path/to/foo/bar.md',
471+
},
472+
{
473+
raw: 'foo/bar.md#hash',
474+
relative: 'path/to/foo/bar.md',
475+
absolute: '/path/path/to/foo/bar.md',
476+
},
477+
{
478+
raw: '../foo/bar.md',
479+
relative: 'path/foo/bar.md',
480+
absolute: '/path/path/foo/bar.md',
481+
},
482+
{
483+
raw: '../foo/bar.md#hash',
484+
relative: 'path/foo/bar.md',
485+
absolute: '/path/path/foo/bar.md',
486+
},
487+
{
488+
raw: 'index.md',
489+
relative: 'path/to/index.md',
490+
absolute: '/path/path/to/index.md',
491+
},
492+
{
493+
raw: 'index.md#hash',
494+
relative: 'path/to/index.md',
495+
absolute: '/path/path/to/index.md',
496+
},
497+
{
498+
raw: './index.md',
499+
relative: 'path/to/index.md',
500+
absolute: '/path/path/to/index.md',
501+
},
502+
{
503+
raw: '../index.md',
504+
relative: 'path/index.md',
505+
absolute: '/path/path/index.md',
506+
},
507+
{
508+
raw: '../foo/bar/index.md',
509+
relative: 'path/foo/bar/index.md',
510+
absolute: '/path/path/foo/bar/index.md',
511+
},
512+
{
513+
raw: 'readme.md',
514+
relative: 'path/to/readme.md',
515+
absolute: '/path/path/to/readme.md',
516+
},
517+
{
518+
raw: '../readme.md#hash',
519+
relative: 'path/readme.md',
520+
absolute: '/path/path/readme.md',
521+
},
522+
{
523+
raw: '../foo/bar/readme.md',
524+
relative: 'path/foo/bar/readme.md',
525+
absolute: '/path/path/foo/bar/readme.md',
526+
},
527+
])
528+
})
402529
})
403530

404531
describe('absolute links', () => {
@@ -407,7 +534,7 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
407534
'[html](/base/path/to/index.html)',
408535
].join('\n\n')
409536

410-
it('should resolve to relative paths correctly', () => {
537+
it('should resolve to internal links correctly', () => {
411538
const md = MarkdownIt({ html: true }).use(linksPlugin)
412539
const env: MarkdownEnv = {
413540
base: '/base/',
@@ -417,8 +544,8 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
417544

418545
expect(rendered).toEqual(
419546
[
420-
'<RouterLink to="/base/path/to/">md</RouterLink>',
421-
'<RouterLink to="/base/path/to/index.html">html</RouterLink>',
547+
'<RouterLink to="/path/to/">md</RouterLink>',
548+
'<RouterLink to="/path/to/index.html">html</RouterLink>',
422549
]
423550
.map((a) => `<p>${a}</p>`)
424551
.join('\n') + '\n'

packages/@vuepress/markdown/src/plugins/linksPlugin/linksPlugin.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
9292
filePathRelative
9393
)
9494

95-
// normalize markdown file path
95+
// normalize markdown file path to route path
9696
//
97-
// './foo/bar.md' => './foo/bar.html'
98-
// './foo/index.md' => './foo/'
97+
// we are removing the `base` from absolute path because it should not be
98+
// passed to `<RouterLink>`
99+
//
100+
// '/foo/index.md' => '/foo/'
101+
// '/foo/bar.md' => '/foo/bar.html'
99102
const normalizedPath = absolutePath
103+
.replace(new RegExp(`^${base}`), '/')
100104
.replace(/(^|\/)(README|index).md$/i, '$1')
101105
.replace(/\.md$/, '.html')
102106

0 commit comments

Comments
 (0)