Skip to content

Commit 43abf0d

Browse files
author
Sylvain Brocard
committed
feat: allows relative path, fixed docsifyjs#590
1 parent 6ac7bac commit 43abf0d

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

docs/configuration.md

+40
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,46 @@ window.$docsify = {
150150
};
151151
```
152152

153+
## relativePath
154+
155+
- Type: `Boolean`
156+
- Default: `false`
157+
158+
If **true** links are relative to the current context.
159+
160+
For example, the directory structure is as follows:
161+
162+
```text
163+
.
164+
└── docs
165+
├── README.md
166+
├── guide.md
167+
└── zh-cn
168+
├── README.md
169+
├── guide.md
170+
└── config
171+
└── example.md
172+
```
173+
174+
With relative path **enabled** and current URL `http://domain.com/zh-cn/README`, given links will resolve to:
175+
176+
```text
177+
guide.md => http://domain.com/zh-cn/guide
178+
config/example.md => http://domain.com/zh-cn/config/example
179+
../README.md => http://domain.com/README
180+
/README.md => http://domain.com/README
181+
```
182+
183+
```js
184+
window.$docsify = {
185+
// Relative path enabled
186+
relativePath: true,
187+
188+
// Relative path disabled (default value)
189+
relativePath: false
190+
};
191+
```
192+
153193
## coverpage
154194

155195
- Type: `Boolean|String|String[]|Object`

src/core/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export default function () {
2525
formatUpdated: '',
2626
externalLinkTarget: '_blank',
2727
routerMode: 'hash',
28-
noCompileLinks: []
28+
noCompileLinks: [],
29+
relativePath: false
2930
},
3031
window.$docsify
3132
)

src/core/router/history/base.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
isAbsolutePath,
44
stringifyQuery,
55
cleanPath,
6-
replaceSlug
6+
replaceSlug,
7+
resolvePath
78
} from '../util'
89
import {noop, merge} from '../../util/core'
910

@@ -76,6 +77,10 @@ export class History {
7677
(idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path
7778
}
7879

80+
if (this.config.relativePath && !path.startsWith('/')) {
81+
const currentDir = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1)
82+
return cleanPath(resolvePath(currentDir + path))
83+
}
7984
return cleanPath('/' + path)
8085
}
8186
}

src/core/router/util.js

+14
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ export const cleanPath = cached(path => {
5353
return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/')
5454
})
5555

56+
export const resolvePath = cached(path => {
57+
const segments = path.replace(/^\//, '').split('/')
58+
let resolved = []
59+
for (let i = 0, len = segments.length; i < len; i++) {
60+
const segment = segments[i]
61+
if (segment === '..') {
62+
resolved.pop()
63+
} else if (segment !== '.') {
64+
resolved.push(segment)
65+
}
66+
}
67+
return '/' + resolved.join('/')
68+
})
69+
5670
export function getPath(...args) {
5771
return cleanPath(args.join('/'))
5872
}

0 commit comments

Comments
 (0)