Skip to content

Commit

Permalink
feat(src): add alias feature
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 11, 2017
1 parent aedf16e commit 24412cd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,24 @@ 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 = ''
} else if (basePath && !/\/$/.test(basePath)) {
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'
Expand Down
14 changes: 11 additions & 3 deletions src/plugins/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -294,15 +293,24 @@ 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))
}

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++
Expand Down

0 comments on commit 24412cd

Please sign in to comment.