You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/configuration.md
+86Lines changed: 86 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,7 @@ The config can also be defined as a function, in which case the first argument i
35
35
- Type: `Object`
36
36
37
37
Set the route alias. You can freely manage routing rules. Supports RegExp.
38
+
Do note that order matters! If a route can be matched by multiple aliases, the one you declared first takes precedence.
38
39
39
40
```js
40
41
window.$docsify= {
@@ -680,6 +681,91 @@ window.$docsify = {
680
681
};
681
682
```
682
683
684
+
## routes
685
+
686
+
- Type: `Object`
687
+
688
+
Define "virtual" routes that can provide content dynamically. A route is a map between the expected path, to either a string or a function. If the mapped value is a string, it is treated as markdown and parsed accordingly. If it is a function, it is expected to return markdown content.
689
+
690
+
A route function receives up to three parameters:
691
+
1.`route` - the path of the route that was requested (e.g. `/bar/baz`)
692
+
2.`matched` - the [`RegExpMatchArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) that was matched by the route (e.g. for `/bar/(.+)`, you get `['/bar/baz', 'baz']`)
693
+
3.`next` - this is a callback that you may call when your route function is async
694
+
695
+
Do note that order matters! Routes are matched the same order you declare them in, which means that in cases where you have overlapping routes, you might want to list the more specific ones first.
696
+
697
+
```js
698
+
window.$docsify= {
699
+
routes: {
700
+
// Basic match w/ return string
701
+
'/foo':'# Custom Markdown',
702
+
703
+
// RegEx match w/ synchronous function
704
+
'/bar/(.*)':function(route, matched) {
705
+
return'# Custom Markdown';
706
+
},
707
+
708
+
// RegEx match w/ asynchronous function
709
+
'/baz/(.*)':function(route, matched, next) {
710
+
// Requires `fetch` polyfill for legacy browsers (https://github.github.io/fetch/)
711
+
fetch('/api/users?id=12345')
712
+
.then(function(response) {
713
+
next('# Custom Markdown');
714
+
})
715
+
.catch(function(err) {
716
+
// Handle error...
717
+
});
718
+
}
719
+
}
720
+
}
721
+
```
722
+
723
+
Other than strings, route functions can return a falsy value (`null` \ `undefined`) to indicate that they ignore the current request:
724
+
725
+
```js
726
+
window.$docsify= {
727
+
routes: {
728
+
// accepts everything other than dogs (synchronous)
729
+
'/pets/(.+)':function(route, matched) {
730
+
if (matched[0] ==='dogs') {
731
+
returnnull;
732
+
} else {
733
+
return'I like all pets but dogs';
734
+
}
735
+
}
736
+
737
+
// accepts everything other than cats (asynchronous)
738
+
'/pets/(.*)':function(route, matched, next) {
739
+
if (matched[0] ==='cats') {
740
+
next();
741
+
} else {
742
+
// Async task(s)...
743
+
next('I like all pets but cats');
744
+
}
745
+
}
746
+
}
747
+
}
748
+
```
749
+
750
+
Finally, if you have a specific path that has a real markdown file (and therefore should not be matched by your route), you can opt it out by returning an explicit `false` value:
751
+
752
+
```js
753
+
window.$docsify= {
754
+
routes: {
755
+
// if you look up /pets/cats, docsify will skip all routes and look for "pets/cats.md"
756
+
'/pets/cats':function(route, matched) {
757
+
returnfalse;
758
+
}
759
+
760
+
// but any other pet should generate dynamic content right here
0 commit comments