feat(fetch): Add fallback languages configuration. (#402)
fallbackLanguages give the possibility to configure a list of languages which must used the default language when a page is missing in the requested language.
This commit is contained in:
parent
278a75ebd5
commit
ecc0e04c70
2 changed files with 56 additions and 11 deletions
|
|
@ -413,3 +413,25 @@ window.$docsify = {
|
||||||
ext: '.md'
|
ext: '.md'
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## fallbackLanguages
|
||||||
|
|
||||||
|
* type: `Array<string>`
|
||||||
|
|
||||||
|
List of languages that will fallback to the default language when a page is request and didn't exists for the given local.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
- try to fetch the page of `/de/overview`. If this page exists, it'll be displayed
|
||||||
|
- then try to fetch the default page `/overview` (depending on the default language). If this page exists, it'll be displayed
|
||||||
|
- then display 404 page.
|
||||||
|
|
||||||
|
|
||||||
|
```js
|
||||||
|
window.$docsify = {
|
||||||
|
fallbackLanguages: [
|
||||||
|
"fr",
|
||||||
|
"de"
|
||||||
|
]
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -22,18 +22,40 @@ export function fetchMixin (proto) {
|
||||||
proto._fetch = function (cb = noop) {
|
proto._fetch = function (cb = noop) {
|
||||||
const { path, query } = this.route
|
const { path, query } = this.route
|
||||||
const qs = stringifyQuery(query, ['id'])
|
const qs = stringifyQuery(query, ['id'])
|
||||||
const { loadNavbar, loadSidebar, requestHeaders } = this.config
|
const { loadNavbar, loadSidebar, requestHeaders, fallbackLanguages } = this.config
|
||||||
|
|
||||||
// Abort last request
|
// Abort last request
|
||||||
last && last.abort && last.abort()
|
last && last.abort && last.abort()
|
||||||
|
|
||||||
const file = this.router.getFile(path)
|
const file = this.router.getFile(path)
|
||||||
|
|
||||||
last = get(file + qs, true, requestHeaders)
|
last = get(file + qs, true, requestHeaders)
|
||||||
|
|
||||||
// Current page is html
|
// Current page is html
|
||||||
this.isHTML = /\.html$/g.test(file)
|
this.isHTML = /\.html$/g.test(file)
|
||||||
|
|
||||||
|
const getFallBackPage = (file) => {
|
||||||
|
if (!fallbackLanguages) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const local = file.split('/')[1]
|
||||||
|
|
||||||
|
if (fallbackLanguages.indexOf(local) === -1) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
file = file.replace(new RegExp(`^/${local}`), '')
|
||||||
|
|
||||||
|
return get(file + qs, true, requestHeaders)
|
||||||
|
.then(
|
||||||
|
(text, opt) => {
|
||||||
|
this._renderMain(text, opt, loadSideAndNav)
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
return this._renderMain(null, {}, loadSideAndNav)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const loadSideAndNav = () => {
|
const loadSideAndNav = () => {
|
||||||
if (!loadSidebar) return cb()
|
if (!loadSidebar) return cb()
|
||||||
|
|
||||||
|
|
@ -47,12 +69,13 @@ export function fetchMixin (proto) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load main content
|
// Load main content
|
||||||
last.then(
|
last
|
||||||
|
.then(
|
||||||
(text, opt) => {
|
(text, opt) => {
|
||||||
this._renderMain(text, opt, loadSideAndNav)
|
this._renderMain(text, opt, loadSideAndNav)
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
this._renderMain(null, {}, loadSideAndNav)
|
return getFallBackPage(file) || this._renderMain(null, {}, loadSideAndNav)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue