Skip to content

Commit 24412cd

Browse files
committed
feat(src): add alias feature
1 parent aedf16e commit 24412cd

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<script src="//unpkg.com/docsify" data-ga="UA-XXXXX-Y"></script>
1616
<script src="//unpkg.com/docsify/lib/plugins/ga.js"></script>
1717
```
18+
1819
## 2.1.0
1920
### Features
2021
- Add search plugin

dev.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
</head>
88
<body>
99
<nav>
10-
<a href="#/">En</a>
10+
<a href="#/">en</a>
1111
<a href="#/zh-cn">中文</a>
12+
<a href="#/changelog">Changlog</a>
1213
</nav>
1314
<div id="app"></div>
1415
</body>
1516
<script>
1617
window.$docsify = {
18+
alias: {
19+
'/changelog': 'https://raw.githubusercontent.com/QingWei-Li/docsify/master/CHANGELOG'
20+
},
1721
search: {
1822
maxAge: 0
1923
}

docs/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
<nav>
1313
<a href="#/">En</a>
1414
<a href="#/zh-cn">中文</a>
15+
<a href="#/changelog">Changlog</a>
1516
</nav>
1617
<div id="app"></div>
1718
</body>
19+
<script>
20+
window.$docsify = {
21+
alias: {
22+
'/changelog': 'https://raw.githubusercontent.com/QingWei-Li/docsify/master/CHANGELOG'
23+
}
24+
}
25+
</script>
1826
<script
1927
src="//unpkg.com/docsify/lib/docsify.min.js"
2028
data-max-level="4"

src/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,24 @@ let cacheRoute = null
4747
let cacheXhr = null
4848

4949
const mainRender = function (cb) {
50-
const route = OPTIONS.basePath + utils.getRoute()
50+
let page
51+
let route = utils.getRoute()
5152
if (cacheRoute === route) return cb()
5253

53-
let basePath = cacheRoute = route
54+
let basePath = cacheRoute = OPTIONS.basePath + route
5455

5556
if (!/\//.test(basePath)) {
5657
basePath = ''
5758
} else if (basePath && !/\/$/.test(basePath)) {
5859
basePath = basePath.match(/(\S*\/)[^\/]+$/)[1]
5960
}
6061

61-
let page
62+
// replace route
63+
if (OPTIONS.alias && OPTIONS.alias['/' + route]) {
64+
route = OPTIONS.alias['/' + route]
65+
} else {
66+
route = OPTIONS.basePath + route
67+
}
6268

6369
if (!route) {
6470
page = OPTIONS.homepage || 'README.md'

src/plugins/search.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ const getAllPaths = function () {
4444
/**
4545
* return file path
4646
*/
47-
const genFilePath = function (path) {
48-
const basePath = window.$docsify.basePath
47+
const genFilePath = function (path, basePath = window.$docsify.basePath) {
4948
let filePath = /\/$/.test(path) ? `${path}README.md` : `${path}.md`
5049

5150
filePath = basePath + filePath
@@ -294,15 +293,24 @@ const searchPlugin = function () {
294293
const paths = isAuto ? getAllPaths() : CONFIG.paths
295294
const len = paths.length
296295
const { load, marked, slugify } = window.Docsify.utils
296+
const alias = window.$docsify.alias
297297
const done = () => {
298298
localStorage.setItem('docsify.search.expires', Date.now() + CONFIG.maxAge)
299299
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
300300
}
301301

302302
paths.forEach(path => {
303303
if (INDEXS[path]) return count++
304+
let route
304305

305-
load(genFilePath(path)).then(content => {
306+
// replace route
307+
if (alias && alias[path]) {
308+
route = genFilePath(alias[path] || path, '')
309+
} else {
310+
route = genFilePath(path)
311+
}
312+
313+
load(route).then(content => {
306314
genIndex(path, marked(content))
307315
slugify.clear()
308316
count++

0 commit comments

Comments
 (0)