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'
3
9
4
10
const cached = { }
5
11
@@ -14,10 +20,10 @@ function getAlias (path, alias, last) {
14
20
: path
15
21
}
16
22
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 )
19
25
? path
20
- : / \/ $ / g. test ( path ) ? `${ path } README.md ` : `${ path } .md `
26
+ : / \/ $ / g. test ( path ) ? `${ path } README${ ext } ` : `${ path } ${ ext } `
21
27
}
22
28
23
29
export class History {
@@ -34,10 +40,11 @@ export class History {
34
40
35
41
const { config } = this
36
42
const base = this . getBasePath ( )
43
+ const ext = typeof config . ext !== 'string' ? '.md' : config . ext
37
44
38
45
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
41
48
path = isAbsolutePath ( path ) ? path : getPath ( base , path )
42
49
43
50
if ( isRelative ) {
@@ -57,5 +64,20 @@ export class History {
57
64
58
65
parse ( ) { }
59
66
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
+ }
61
83
}
0 commit comments