File tree 3 files changed +73
-4
lines changed
3 files changed +73
-4
lines changed Original file line number Diff line number Diff line change 1
1
import { noop } from '../../util/core' ;
2
2
import { on } from '../../util/dom' ;
3
- import { parseQuery , cleanPath , replaceSlug } from '../util' ;
3
+ import { parseQuery , cleanPath , replaceSlug , endsWith } from '../util' ;
4
4
import { History } from './base' ;
5
5
6
6
function replaceHash ( path ) {
7
7
const i = location . href . indexOf ( '#' ) ;
8
8
location . replace ( location . href . slice ( 0 , i >= 0 ? i : 0 ) + '#' + path ) ;
9
9
}
10
-
11
10
export class HashHistory extends History {
12
11
constructor ( config ) {
13
12
super ( config ) ;
@@ -18,7 +17,15 @@ export class HashHistory extends History {
18
17
const path = window . location . pathname || '' ;
19
18
const base = this . config . basePath ;
20
19
21
- return / ^ ( \/ | h t t p s ? : ) / g. test ( base ) ? base : cleanPath ( path + '/' + base ) ;
20
+ // This handles the case where Docsify is served off an
21
+ // explicit file path, i.e.`/base/index.html#/blah`. This
22
+ // prevents the `/index.html` part of the URI from being
23
+ // remove during routing.
24
+ // See here: https://github.com/docsifyjs/docsify/pull/1372
25
+ const basePath = endsWith ( path , '.html' )
26
+ ? path + '#/' + base
27
+ : path + '/' + base ;
28
+ return / ^ ( \/ | h t t p s ? : ) / g. test ( base ) ? base : cleanPath ( basePath ) ;
22
29
}
23
30
24
31
getCurrentPath ( ) {
Original file line number Diff line number Diff line change @@ -76,10 +76,44 @@ export const resolvePath = cached(path => {
76
76
return '/' + resolved . join ( '/' ) ;
77
77
} ) ;
78
78
79
+ /**
80
+ * Normalises the URI path to handle the case where Docsify is
81
+ * hosted off explicit files, i.e. /index.html. This function
82
+ * eliminates any path segments that contain `#` fragments.
83
+ *
84
+ * This is used to map browser URIs to markdown file sources.
85
+ *
86
+ * For example:
87
+ *
88
+ * http://example.org/base/index.html#/blah
89
+ *
90
+ * would be mapped to:
91
+ *
92
+ * http://example.org/base/blah.md.
93
+ *
94
+ * See here for more information:
95
+ *
96
+ * https://github.com/docsifyjs/docsify/pull/1372
97
+ *
98
+ * @param {string } path The URI path to normalise
99
+ * @return {string } { path, query }
100
+ */
101
+
102
+ function normaliseFragment ( path ) {
103
+ return path
104
+ . split ( '/' )
105
+ . filter ( p => p . indexOf ( '#' ) === - 1 )
106
+ . join ( '/' ) ;
107
+ }
108
+
79
109
export function getPath ( ...args ) {
80
- return cleanPath ( args . join ( '/' ) ) ;
110
+ return cleanPath ( args . map ( normaliseFragment ) . join ( '/' ) ) ;
81
111
}
82
112
83
113
export const replaceSlug = cached ( path => {
84
114
return path . replace ( '#' , '?id=' ) ;
85
115
} ) ;
116
+
117
+ export function endsWith ( str , suffix ) {
118
+ return str . indexOf ( suffix , str . length - suffix . length ) !== - 1 ;
119
+ }
Original file line number Diff line number Diff line change
1
+ const docsifyInit = require ( '../helpers/docsify-init' ) ;
2
+
3
+ describe ( `Index file hosting` , function ( ) {
4
+ const sharedOptions = {
5
+ config : {
6
+ basePath : `${ TEST_HOST } /docs/index.html#/` ,
7
+ } ,
8
+ testURL : `${ TEST_HOST } /docs/index.html#/` ,
9
+ } ;
10
+
11
+ test ( 'should serve from index file' , async ( ) => {
12
+ await docsifyInit ( sharedOptions ) ;
13
+
14
+ await expect ( page ) . toHaveText (
15
+ '#main' ,
16
+ 'A magical documentation site generator'
17
+ ) ;
18
+ expect ( page . url ( ) ) . toMatch ( / i n d e x \. h t m l # \/ $ / ) ;
19
+ } ) ;
20
+
21
+ test ( 'should use index file links in sidebar from index file hosting' , async ( ) => {
22
+ await docsifyInit ( sharedOptions ) ;
23
+
24
+ await page . click ( 'a[href="#/quickstart"]' ) ;
25
+ await expect ( page ) . toHaveText ( '#main' , 'Quick start' ) ;
26
+ expect ( page . url ( ) ) . toMatch ( / i n d e x \. h t m l # \/ q u i c k s t a r t $ / ) ;
27
+ } ) ;
28
+ } ) ;
You can’t perform that action at this time.
0 commit comments