@@ -19,44 +19,35 @@ function loadNested (path, qs, file, next, vm, first) {
19
19
20
20
export function fetchMixin ( proto ) {
21
21
let last
22
- proto . _fetch = function ( cb = noop ) {
23
- const { path, query } = this . route
24
- const qs = stringifyQuery ( query , [ 'id' ] )
25
- const { loadNavbar, loadSidebar, requestHeaders, fallbackLanguages } = this . config
26
- // Abort last request
27
- last && last . abort && last . abort ( )
28
-
29
- const file = this . router . getFile ( path )
30
- last = get ( file + qs , true , requestHeaders )
31
-
32
- // Current page is html
33
- this . isHTML = / \. h t m l $ / g. test ( file )
34
-
35
- const getFallBackPage = ( file ) => {
36
- if ( ! fallbackLanguages ) {
37
- return false
38
- }
39
22
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 } ` ) , '' )
23
+ const abort = ( ) => last && last . abort && last . abort ( )
24
+ const request = ( url , hasbar , requestHeaders ) => {
25
+ abort ( )
26
+ last = get ( url , true , requestHeaders )
27
+ return last
28
+ }
47
29
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
- )
30
+ const get404Path = ( path , config ) => {
31
+ const { notFoundPage, ext } = config
32
+ const defaultPath = '_404' + ( ext || '.md' )
33
+
34
+ switch ( typeof notFoundPage ) {
35
+ case 'boolean' :
36
+ return defaultPath
37
+ case 'string' :
38
+ return notFoundPage
39
+ case 'object' :
40
+ const key = Object
41
+ . keys ( notFoundPage )
42
+ . sort ( ( a , b ) => b . length - a . length )
43
+ . find ( ( key ) => path . match ( new RegExp ( '^' + key ) ) )
44
+
45
+ return key && notFoundPage [ key ] || defaultPath
57
46
}
47
+ }
58
48
59
- const loadSideAndNav = ( ) => {
49
+ proto . _loadSideAndNav = function ( path , qs , loadSidebar , cb ) {
50
+ return ( ) => {
60
51
if ( ! loadSidebar ) return cb ( )
61
52
62
53
const fn = result => {
@@ -67,28 +58,39 @@ export function fetchMixin (proto) {
67
58
// Load sidebar
68
59
loadNested ( path , qs , loadSidebar , fn , this , true )
69
60
}
61
+ }
62
+
63
+ proto . _fetch = function ( cb = noop ) {
64
+ const { path, query } = this . route
65
+ const qs = stringifyQuery ( query , [ 'id' ] )
66
+ const { loadNavbar, requestHeaders, loadSidebar } = this . config
67
+ // Abort last request
68
+
69
+ const file = this . router . getFile ( path )
70
+ const req = request ( file + qs , true , requestHeaders )
71
+
72
+ // Current page is html
73
+ this . isHTML = / \. h t m l $ / g. test ( file )
70
74
71
75
// Load main content
72
- last
76
+ req
73
77
. then (
74
- ( text , opt ) => {
75
- this . _renderMain ( text , opt , loadSideAndNav )
76
- } ,
78
+ ( text , opt ) => this . _renderMain ( text , opt , this . _loadSideAndNav ( path , qs , loadSidebar , cb ) ) ,
77
79
_ => {
78
- return getFallBackPage ( file ) || this . _renderMain ( null , { } , loadSideAndNav )
80
+ this . _fetchFallbackPage ( file , qs , cb ) || this . _fetch404 ( file , qs , cb )
79
81
}
80
82
)
81
83
82
84
// Load nav
83
85
loadNavbar &&
84
- loadNested (
85
- path ,
86
- qs ,
87
- loadNavbar ,
88
- text => this . _renderNav ( text ) ,
89
- this ,
90
- true
91
- )
86
+ loadNested (
87
+ path ,
88
+ qs ,
89
+ loadNavbar ,
90
+ text => this . _renderNav ( text ) ,
91
+ this ,
92
+ true
93
+ )
92
94
}
93
95
94
96
proto . _fetchCover = function ( ) {
@@ -141,6 +143,54 @@ export function fetchMixin (proto) {
141
143
} )
142
144
}
143
145
}
146
+
147
+ proto . _fetchFallbackPage = function ( path , qs , cb = noop ) {
148
+ const { requestHeaders, fallbackLanguages, loadSidebar } = this . config
149
+
150
+ if ( ! fallbackLanguages ) {
151
+ return false
152
+ }
153
+
154
+ const local = path . split ( '/' ) [ 1 ]
155
+
156
+ if ( fallbackLanguages . indexOf ( local ) === - 1 ) {
157
+ return false
158
+ }
159
+ const newPath = path . replace ( new RegExp ( `^/${ local } ` ) , '' )
160
+ const req = request ( newPath + qs , true , requestHeaders )
161
+
162
+ req . then (
163
+ ( text , opt ) => this . _renderMain ( text , opt , this . _loadSideAndNav ( path , qs , loadSidebar , cb ) ) ,
164
+ ( ) => this . _fetch404 ( path , qs , cb )
165
+ )
166
+
167
+ return true
168
+ }
169
+ /**
170
+ * Load the 404 page
171
+ * @param path
172
+ * @param qs
173
+ * @param cb
174
+ * @returns {* }
175
+ * @private
176
+ */
177
+ proto . _fetch404 = function ( path , qs , cb = noop ) {
178
+ const { loadSidebar, requestHeaders, notFoundPage } = this . config
179
+
180
+ const fnLoadSideAndNav = this . _loadSideAndNav ( path , qs , loadSidebar , cb )
181
+
182
+ if ( notFoundPage ) {
183
+ request ( get404Path ( path , this . config ) , true , requestHeaders )
184
+ . then (
185
+ ( text , opt ) => this . _renderMain ( text , opt , fnLoadSideAndNav ) ,
186
+ ( ) => this . _renderMain ( null , { } , fnLoadSideAndNav )
187
+ )
188
+ return true
189
+ }
190
+
191
+ this . _renderMain ( null , { } , fnLoadSideAndNav )
192
+ return false
193
+ }
144
194
}
145
195
146
196
export function initFetch ( vm ) {
0 commit comments