From 939e23b3bffae396c3329e475f5264c397ce4449 Mon Sep 17 00:00:00 2001 From: Ricardo Gladwell Date: Sat, 12 Sep 2020 13:14:48 +0100 Subject: [PATCH] Fix: Cannot serve off `/.../index.html` Docsify must be hosted on a server that supports a default directory index (i.e. maps `/.../` -> `/.../index.html`). Some platforms do not support this, however. For example, HTML apps hosted on the popular game/software platform, Itch.io. This change supports hosting Docsify off an explicit path file, such as `/index.html`. It does this by: 1. Adding handling for paths like `index.html#/blah`, and 2. Normalising paths with fragments back to markdown paths For example, `http://example.org/index.html#/blah` would be mapped to `http://example.org/blah.md`. This fixes: https://github.com/docsifyjs/docsify/issues/427 --- src/core/router/history/hash.js | 5 ++++- src/core/router/util.js | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/core/router/history/hash.js b/src/core/router/history/hash.js index caaea4571..847445400 100644 --- a/src/core/router/history/hash.js +++ b/src/core/router/history/hash.js @@ -18,7 +18,10 @@ export class HashHistory extends History { const path = window.location.pathname || ''; const base = this.config.basePath; - return /^(\/|https?:)/g.test(base) ? base : cleanPath(path + '/' + base); + const basePath = path.endsWith('.html') + ? path + '#/' + base + : path + '/' + base; + return /^(\/|https?:)/g.test(base) ? base : cleanPath(basePath); } getCurrentPath() { diff --git a/src/core/router/util.js b/src/core/router/util.js index fc3e2f79d..c26b8d06c 100644 --- a/src/core/router/util.js +++ b/src/core/router/util.js @@ -76,8 +76,15 @@ export const resolvePath = cached(path => { return '/' + resolved.join('/'); }); +function normaliseFragment(path) { + return path + .split('/') + .filter(p => !p.includes('#')) + .join('/'); +} + export function getPath(...args) { - return cleanPath(args.join('/')); + return cleanPath(args.map(normaliseFragment).join('/')); } export const replaceSlug = cached(path => {