From 31654f12eccd3e8353e7e94cd20f895a1918cc9f Mon Sep 17 00:00:00 2001 From: Sylvain Brocard Date: Thu, 6 Dec 2018 15:30:58 +0100 Subject: [PATCH 1/2] feat: allows relative path, fixed #590 --- docs/configuration.md | 40 +++++++++++++++++++++++++++++++++ src/core/config.js | 3 ++- src/core/router/history/base.js | 7 +++++- src/core/router/util.js | 14 ++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 830748f..ba48475 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -150,6 +150,46 @@ window.$docsify = { }; ``` +## relativePath + +- Type: `Boolean` +- Default: `false` + +If **true** links are relative to the current context. + +For example, the directory structure is as follows: + +```text +. +└── docs + ├── README.md + ├── guide.md + └── zh-cn + ├── README.md + ├── guide.md + └── config + └── example.md +``` + +With relative path **enabled** and current URL `http://domain.com/zh-cn/README`, given links will resolve to: + +```text +guide.md => http://domain.com/zh-cn/guide +config/example.md => http://domain.com/zh-cn/config/example +../README.md => http://domain.com/README +/README.md => http://domain.com/README +``` + +```js +window.$docsify = { + // Relative path enabled + relativePath: true, + + // Relative path disabled (default value) + relativePath: false +}; +``` + ## coverpage - Type: `Boolean|String|String[]|Object` diff --git a/src/core/config.js b/src/core/config.js index 0a8763a..a1386b2 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -25,7 +25,8 @@ export default function () { formatUpdated: '', externalLinkTarget: '_blank', routerMode: 'hash', - noCompileLinks: [] + noCompileLinks: [], + relativePath: false }, window.$docsify ) diff --git a/src/core/router/history/base.js b/src/core/router/history/base.js index 0f9900d..c58a1f9 100644 --- a/src/core/router/history/base.js +++ b/src/core/router/history/base.js @@ -3,7 +3,8 @@ import { isAbsolutePath, stringifyQuery, cleanPath, - replaceSlug + replaceSlug, + resolvePath } from '../util' import {noop, merge} from '../../util/core' @@ -76,6 +77,10 @@ export class History { (idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path } + if (this.config.relativePath && !path.startsWith('/')) { + const currentDir = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1) + return cleanPath(resolvePath(currentDir + path)) + } return cleanPath('/' + path) } } diff --git a/src/core/router/util.js b/src/core/router/util.js index 217461f..2ed88c5 100644 --- a/src/core/router/util.js +++ b/src/core/router/util.js @@ -53,6 +53,20 @@ export const cleanPath = cached(path => { return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/') }) +export const resolvePath = cached(path => { + const segments = path.replace(/^\//, '').split('/') + let resolved = [] + for (let i = 0, len = segments.length; i < len; i++) { + const segment = segments[i] + if (segment === '..') { + resolved.pop() + } else if (segment !== '.') { + resolved.push(segment) + } + } + return '/' + resolved.join('/') +}) + export function getPath(...args) { return cleanPath(args.join('/')) } From ca8ae5071725515f7aa2c7d4ce24150adbc31ed0 Mon Sep 17 00:00:00 2001 From: Sylvain Brocard Date: Mon, 25 Mar 2019 17:20:09 +0100 Subject: [PATCH 2/2] IE compatibility and add unit tests --- src/core/router/history/base.js | 6 ++-- test/unit/base.js | 62 +++++++++++++++++++++++++++++++++ test/unit/util.js | 30 ++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 test/unit/base.js create mode 100644 test/unit/util.js diff --git a/src/core/router/history/base.js b/src/core/router/history/base.js index c58a1f9..7ea763d 100644 --- a/src/core/router/history/base.js +++ b/src/core/router/history/base.js @@ -74,11 +74,11 @@ export class History { if (local) { const idIndex = currentRoute.indexOf('?') path = - (idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path + (idIndex > 0 ? currentRoute.substring(0, idIndex) : currentRoute) + path } - if (this.config.relativePath && !path.startsWith('/')) { - const currentDir = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1) + if (this.config.relativePath && path.indexOf('/') !== 0) { + const currentDir = currentRoute.substring(0, currentRoute.lastIndexOf('/') + 1) return cleanPath(resolvePath(currentDir + path)) } return cleanPath('/' + path) diff --git a/test/unit/base.js b/test/unit/base.js new file mode 100644 index 0000000..036700b --- /dev/null +++ b/test/unit/base.js @@ -0,0 +1,62 @@ +/* eslint-env node, chai, mocha */ +require = require('esm')(module/*, options*/) +const {expect} = require('chai') +const {History} = require('../../src/core/router/history/base') + +class MockHistory extends History { + parse(path) { + return {path} + } +} + +describe('router/history/base', function () { + describe('relativePath true', function () { + var history + + beforeEach(function () { + history = new MockHistory({relativePath: true}) + }) + + it('toURL', function () { + // WHEN + const url = history.toURL('guide.md', {}, '/zh-ch/') + + // THEN + expect(url).equal('/zh-ch/guide') + }) + + it('toURL with double dot', function () { + // WHEN + const url = history.toURL('../README.md', {}, '/zh-ch/') + + // THEN + expect(url).equal('/README') + }) + + it('toURL child path', function () { + // WHEN + const url = history.toURL('config/example.md', {}, '/zh-ch/') + + // THEN + expect(url).equal('/zh-ch/config/example') + }) + + it('toURL absolute path', function () { + // WHEN + const url = history.toURL('/README', {}, '/zh-ch/') + + // THEN + expect(url).equal('/README') + }) + }) + + it('toURL without relative path', function () { + const history = new MockHistory({relativePath: false}) + + // WHEN + const url = history.toURL('README', {}, '/zh-ch/') + + // THEN + expect(url).equal('/README') + }) +}) diff --git a/test/unit/util.js b/test/unit/util.js new file mode 100644 index 0000000..1e65daf --- /dev/null +++ b/test/unit/util.js @@ -0,0 +1,30 @@ +/* eslint-env node, chai, mocha */ +require = require('esm')(module/*, options*/) +const {expect} = require('chai') +const {resolvePath} = require('../../src/core/router/util') + +describe('router/util', function () { + it('resolvePath', async function () { + // WHEN + const result = resolvePath('hello.md') + + // THEN + expect(result).equal('/hello.md') + }) + + it('resolvePath with dot', async function () { + // WHEN + const result = resolvePath('./hello.md') + + // THEN + expect(result).equal('/hello.md') + }) + + it('resolvePath with two dots', async function () { + // WHEN + const result = resolvePath('test/../hello.md') + + // THEN + expect(result).equal('/hello.md') + }) +})