Skip to content

Commit

Permalink
feat(fetch): Add fallback languages configuration.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Romakita committed Mar 1, 2018
1 parent 01bbbaa commit 7768f7f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
22 changes: 22 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,25 @@ window.$docsify = {
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"
]
};
```
49 changes: 37 additions & 12 deletions src/core/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,39 @@ export function fetchMixin (proto) {
proto._fetch = function (cb = noop) {
const { path, query } = this.route
const qs = stringifyQuery(query, ['id'])
const { loadNavbar, loadSidebar, requestHeaders } = this.config

const { loadNavbar, loadSidebar, requestHeaders, fallbackLanguages } = this.config
// Abort last request
last && last.abort && last.abort()

last = get(this.router.getFile(path) + qs, true, requestHeaders)
const file = this.router.getFile(path)
last = get(file + qs, true, requestHeaders)

// Current page is html
this.isHTML = /\.html$/g.test(path)
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 = () => {
if (!loadSidebar) return cb()
Expand All @@ -45,14 +69,15 @@ export function fetchMixin (proto) {
}

// Load main content
last.then(
(text, opt) => {
this._renderMain(text, opt, loadSideAndNav)
},
_ => {
this._renderMain(null, {}, loadSideAndNav)
}
)
last
.then(
(text, opt) => {
this._renderMain(text, opt, loadSideAndNav)
},
_ => {
return getFallBackPage(file) || this._renderMain(null, {}, loadSideAndNav)
}
)

// Load nav
loadNavbar &&
Expand Down

0 comments on commit 7768f7f

Please sign in to comment.