Skip to content

Commit 9f6d930

Browse files
committed
feat(fetch): Add fallback languages configuration.
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.
1 parent 01bbbaa commit 9f6d930

File tree

2 files changed

+67
-20
lines changed

2 files changed

+67
-20
lines changed

docs/configuration.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,25 @@ window.$docsify = {
413413
ext: '.md'
414414
};
415415
```
416+
417+
## fallbackLanguages
418+
419+
* type: `Array<string>`
420+
421+
List of languages that will fallback to the default language when a page is request and didn't exists for the given local.
422+
423+
Example:
424+
425+
- try to fetch the page of `/de/overview`. If this page exists, it'll be displayed
426+
- then try to fetch the default page `/overview` (depending on the default language). If this page exists, it'll be displayed
427+
- then display 404 page.
428+
429+
430+
```js
431+
window.$docsify = {
432+
fallbackLanguages: [
433+
"fr",
434+
"de"
435+
]
436+
};
437+
```

src/core/fetch/index.js

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,39 @@ export function fetchMixin (proto) {
2222
proto._fetch = function (cb = noop) {
2323
const { path, query } = this.route
2424
const qs = stringifyQuery(query, ['id'])
25-
const { loadNavbar, loadSidebar, requestHeaders } = this.config
26-
25+
const { loadNavbar, loadSidebar, requestHeaders, fallbackLanguages } = this.config
2726
// Abort last request
2827
last && last.abort && last.abort()
2928

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

3232
// Current page is html
33-
this.isHTML = /\.html$/g.test(path)
33+
this.isHTML = /\.html$/g.test(file)
34+
35+
const getFallBackPage = (file) => {
36+
if (!fallbackLanguages) {
37+
return false
38+
}
39+
40+
const local = file.split('/')[1]
41+
42+
if (fallbackLanguages.indexOf(local) === -1) {
43+
return false
44+
}
45+
46+
file = file.replace(new RegExp(`^/${local}`), '')
47+
48+
return get(file + qs, true, requestHeaders)
49+
.then(
50+
(text, opt) => {
51+
this._renderMain(text, opt, loadSideAndNav)
52+
},
53+
_ => {
54+
return this._renderMain(null, {}, loadSideAndNav)
55+
}
56+
)
57+
}
3458

3559
const loadSideAndNav = () => {
3660
if (!loadSidebar) return cb()
@@ -45,25 +69,26 @@ export function fetchMixin (proto) {
4569
}
4670

4771
// Load main content
48-
last.then(
49-
(text, opt) => {
50-
this._renderMain(text, opt, loadSideAndNav)
51-
},
52-
_ => {
53-
this._renderMain(null, {}, loadSideAndNav)
54-
}
55-
)
72+
last
73+
.then(
74+
(text, opt) => {
75+
this._renderMain(text, opt, loadSideAndNav)
76+
},
77+
_ => {
78+
return getFallBackPage(file) || this._renderMain(null, {}, loadSideAndNav)
79+
}
80+
)
5681

5782
// Load nav
5883
loadNavbar &&
59-
loadNested(
60-
path,
61-
qs,
62-
loadNavbar,
63-
text => this._renderNav(text),
64-
this,
65-
true
66-
)
84+
loadNested(
85+
path,
86+
qs,
87+
loadNavbar,
88+
text => this._renderNav(text),
89+
this,
90+
true
91+
)
6792
}
6893

6994
proto._fetchCover = function () {

0 commit comments

Comments
 (0)