File tree Expand file tree Collapse file tree 3 files changed +73
-4
lines changed Expand file tree Collapse file tree 3 files changed +73
-4
lines changed Original file line number Diff line number Diff line change 11import { noop } from '../../util/core' ;
22import { on } from '../../util/dom' ;
3- import { parseQuery , cleanPath , replaceSlug } from '../util' ;
3+ import { parseQuery , cleanPath , replaceSlug , endsWith } from '../util' ;
44import { History } from './base' ;
55
66function replaceHash ( path ) {
77 const i = location . href . indexOf ( '#' ) ;
88 location . replace ( location . href . slice ( 0 , i >= 0 ? i : 0 ) + '#' + path ) ;
99}
10-
1110export class HashHistory extends History {
1211 constructor ( config ) {
1312 super ( config ) ;
@@ -18,7 +17,15 @@ export class HashHistory extends History {
1817 const path = window . location . pathname || '' ;
1918 const base = this . config . basePath ;
2019
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 ) ;
2229 }
2330
2431 getCurrentPath ( ) {
Original file line number Diff line number Diff line change @@ -76,10 +76,44 @@ export const resolvePath = cached(path => {
7676 return '/' + resolved . join ( '/' ) ;
7777} ) ;
7878
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+
79109export function getPath ( ...args ) {
80- return cleanPath ( args . join ( '/' ) ) ;
110+ return cleanPath ( args . map ( normaliseFragment ) . join ( '/' ) ) ;
81111}
82112
83113export const replaceSlug = cached ( path => {
84114 return path . replace ( '#' , '?id=' ) ;
85115} ) ;
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