1- import { getPath , isAbsolutePath } from '../util'
2- import { noop } from '../../util/core'
1+ import {
2+ getPath ,
3+ isAbsolutePath ,
4+ stringifyQuery ,
5+ cleanPath ,
6+ replaceSlug
7+ } from '../util'
8+ import { noop , merge } from '../../util/core'
39
410const cached = { }
511
@@ -14,10 +20,10 @@ function getAlias (path, alias, last) {
1420 : path
1521}
1622
17- function getFileName ( path ) {
18- return / \. ( m d | h t m l ) $ / g . test ( path )
23+ function getFileName ( path , ext ) {
24+ return new RegExp ( `\\.( ${ ext . replace ( / ^ \. / , '' ) } |html)$` , 'g' ) . test ( path )
1925 ? path
20- : / \/ $ / g. test ( path ) ? `${ path } README.md ` : `${ path } .md `
26+ : / \/ $ / g. test ( path ) ? `${ path } README${ ext } ` : `${ path } ${ ext } `
2127}
2228
2329export class History {
@@ -34,10 +40,11 @@ export class History {
3440
3541 const { config } = this
3642 const base = this . getBasePath ( )
43+ const ext = typeof config . ext !== 'string' ? '.md' : config . ext
3744
3845 path = config . alias ? getAlias ( path , config . alias ) : path
39- path = getFileName ( path )
40- path = path === ' /README.md' ? config . homepage || path : path
46+ path = getFileName ( path , ext )
47+ path = path === ` /README${ ext } ` ? config . homepage || path : path
4148 path = isAbsolutePath ( path ) ? path : getPath ( base , path )
4249
4350 if ( isRelative ) {
@@ -57,5 +64,20 @@ export class History {
5764
5865 parse ( ) { }
5966
60- toURL ( ) { }
67+ toURL ( path , params , currentRoute ) {
68+ const local = currentRoute && path [ 0 ] === '#'
69+ const route = this . parse ( replaceSlug ( path ) )
70+
71+ route . query = merge ( { } , route . query , params )
72+ path = route . path + stringifyQuery ( route . query )
73+ path = path . replace ( / \. m d ( \? ) | \. m d $ / , '$1' )
74+
75+ if ( local ) {
76+ const idIndex = currentRoute . indexOf ( '?' )
77+ path =
78+ ( idIndex > 0 ? currentRoute . substr ( 0 , idIndex ) : currentRoute ) + path
79+ }
80+
81+ return cleanPath ( '/' + path )
82+ }
6183}
0 commit comments