Skip to content

Commit 37b7166

Browse files
Merge branch 'develop' into fix-search-ie11
2 parents b4d9c31 + 759ffac commit 37b7166

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

src/core/router/history/hash.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { noop } from '../../util/core';
22
import { on } from '../../util/dom';
3-
import { parseQuery, cleanPath, replaceSlug } from '../util';
3+
import { parseQuery, cleanPath, replaceSlug, endsWith } from '../util';
44
import { History } from './base';
55

66
function replaceHash(path) {
77
const i = location.href.indexOf('#');
88
location.replace(location.href.slice(0, i >= 0 ? i : 0) + '#' + path);
99
}
10-
1110
export 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 /^(\/|https?:)/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 /^(\/|https?:)/g.test(base) ? base : cleanPath(basePath);
2229
}
2330

2431
getCurrentPath() {

src/core/router/util.js

+35-1
Original file line numberDiff line numberDiff 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+
79109
export function getPath(...args) {
80-
return cleanPath(args.join('/'));
110+
return cleanPath(args.map(normaliseFragment).join('/'));
81111
}
82112

83113
export 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+
}

test/e2e/index-file.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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(/index\.html#\/$/);
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(/index\.html#\/quickstart$/);
27+
});
28+
});

0 commit comments

Comments
 (0)