mirror of
https://gitee.com/docsifyjs/docsify.git
synced 2024-11-30 11:08:58 +08:00
feat(src): add alias feature
This commit is contained in:
parent
aedf16e433
commit
24412cdaf0
@ -15,6 +15,7 @@
|
||||
<script src="//unpkg.com/docsify" data-ga="UA-XXXXX-Y"></script>
|
||||
<script src="//unpkg.com/docsify/lib/plugins/ga.js"></script>
|
||||
```
|
||||
|
||||
## 2.1.0
|
||||
### Features
|
||||
- Add search plugin
|
||||
|
6
dev.html
6
dev.html
@ -7,13 +7,17 @@
|
||||
</head>
|
||||
<body>
|
||||
<nav>
|
||||
<a href="#/">En</a>
|
||||
<a href="#/">en</a>
|
||||
<a href="#/zh-cn">中文</a>
|
||||
<a href="#/changelog">Changlog</a>
|
||||
</nav>
|
||||
<div id="app"></div>
|
||||
</body>
|
||||
<script>
|
||||
window.$docsify = {
|
||||
alias: {
|
||||
'/changelog': 'https://raw.githubusercontent.com/QingWei-Li/docsify/master/CHANGELOG'
|
||||
},
|
||||
search: {
|
||||
maxAge: 0
|
||||
}
|
||||
|
@ -12,9 +12,17 @@
|
||||
<nav>
|
||||
<a href="#/">En</a>
|
||||
<a href="#/zh-cn">中文</a>
|
||||
<a href="#/changelog">Changlog</a>
|
||||
</nav>
|
||||
<div id="app"></div>
|
||||
</body>
|
||||
<script>
|
||||
window.$docsify = {
|
||||
alias: {
|
||||
'/changelog': 'https://raw.githubusercontent.com/QingWei-Li/docsify/master/CHANGELOG'
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script
|
||||
src="//unpkg.com/docsify/lib/docsify.min.js"
|
||||
data-max-level="4"
|
||||
|
12
src/index.js
12
src/index.js
@ -47,10 +47,11 @@ let cacheRoute = null
|
||||
let cacheXhr = null
|
||||
|
||||
const mainRender = function (cb) {
|
||||
const route = OPTIONS.basePath + utils.getRoute()
|
||||
let page
|
||||
let route = utils.getRoute()
|
||||
if (cacheRoute === route) return cb()
|
||||
|
||||
let basePath = cacheRoute = route
|
||||
let basePath = cacheRoute = OPTIONS.basePath + route
|
||||
|
||||
if (!/\//.test(basePath)) {
|
||||
basePath = ''
|
||||
@ -58,7 +59,12 @@ const mainRender = function (cb) {
|
||||
basePath = basePath.match(/(\S*\/)[^\/]+$/)[1]
|
||||
}
|
||||
|
||||
let page
|
||||
// replace route
|
||||
if (OPTIONS.alias && OPTIONS.alias['/' + route]) {
|
||||
route = OPTIONS.alias['/' + route]
|
||||
} else {
|
||||
route = OPTIONS.basePath + route
|
||||
}
|
||||
|
||||
if (!route) {
|
||||
page = OPTIONS.homepage || 'README.md'
|
||||
|
@ -44,8 +44,7 @@ const getAllPaths = function () {
|
||||
/**
|
||||
* return file path
|
||||
*/
|
||||
const genFilePath = function (path) {
|
||||
const basePath = window.$docsify.basePath
|
||||
const genFilePath = function (path, basePath = window.$docsify.basePath) {
|
||||
let filePath = /\/$/.test(path) ? `${path}README.md` : `${path}.md`
|
||||
|
||||
filePath = basePath + filePath
|
||||
@ -294,6 +293,7 @@ const searchPlugin = function () {
|
||||
const paths = isAuto ? getAllPaths() : CONFIG.paths
|
||||
const len = paths.length
|
||||
const { load, marked, slugify } = window.Docsify.utils
|
||||
const alias = window.$docsify.alias
|
||||
const done = () => {
|
||||
localStorage.setItem('docsify.search.expires', Date.now() + CONFIG.maxAge)
|
||||
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
|
||||
@ -301,8 +301,16 @@ const searchPlugin = function () {
|
||||
|
||||
paths.forEach(path => {
|
||||
if (INDEXS[path]) return count++
|
||||
let route
|
||||
|
||||
load(genFilePath(path)).then(content => {
|
||||
// replace route
|
||||
if (alias && alias[path]) {
|
||||
route = genFilePath(alias[path] || path, '')
|
||||
} else {
|
||||
route = genFilePath(path)
|
||||
}
|
||||
|
||||
load(route).then(content => {
|
||||
genIndex(path, marked(content))
|
||||
slugify.clear()
|
||||
count++
|
||||
|
Loading…
Reference in New Issue
Block a user