Skip to content

Commit 60cd96f

Browse files
authored
Revert "Virtual Routes Support (#1799)"
This reverts commit 5bf58d6.
1 parent 300893b commit 60cd96f

File tree

12 files changed

+21
-570
lines changed

12 files changed

+21
-570
lines changed

.eslintrc.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,7 @@ module.exports = {
4141
'no-shadow': [
4242
'error',
4343
{
44-
allow: [
45-
'Events',
46-
'Fetch',
47-
'Lifecycle',
48-
'Render',
49-
'Router',
50-
'VirtualRoutes',
51-
],
44+
allow: ['Events', 'Fetch', 'Lifecycle', 'Render', 'Router'],
5245
},
5346
],
5447
'no-unused-vars': ['error', { args: 'none' }],

docs/configuration.md

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ The config can also be defined as a function, in which case the first argument i
3535
- Type: `Object`
3636

3737
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.
3938

4039
```js
4140
window.$docsify = {
@@ -681,91 +680,6 @@ window.$docsify = {
681680
};
682681
```
683682

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-
return null;
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-
return false;
758-
}
759-
760-
// but any other pet should generate dynamic content right here
761-
'/pets/(.+)': function(route, matched) {
762-
const pet = matched[0];
763-
return `your pet is ${pet} (but not a cat)`;
764-
}
765-
}
766-
}
767-
```
768-
769683
## subMaxLevel
770684

771685
- Type: `Number`

src/core/Docsify.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Router } from './router/index.js';
22
import { Render } from './render/index.js';
33
import { Fetch } from './fetch/index.js';
44
import { Events } from './event/index.js';
5-
import { VirtualRoutes } from './virtual-routes/index.js';
65
import initGlobalAPI from './global-api.js';
76

87
import config from './config.js';
@@ -12,10 +11,7 @@ import { Lifecycle } from './init/lifecycle';
1211
/** @typedef {new (...args: any[]) => any} Constructor */
1312

1413
// eslint-disable-next-line new-cap
15-
export class Docsify extends Fetch(
16-
// eslint-disable-next-line new-cap
17-
Events(Render(VirtualRoutes(Router(Lifecycle(Object)))))
18-
) {
14+
export class Docsify extends Fetch(Events(Render(Router(Lifecycle(Object))))) {
1915
constructor() {
2016
super();
2117

src/core/config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export default function (vm) {
3333
notFoundPage: true,
3434
relativePath: false,
3535
repo: '',
36-
routes: {},
3736
routerMode: 'hash',
3837
subMaxLevel: 0,
3938
themeColor: '',

src/core/fetch/index.js

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,44 +96,25 @@ export function Fetch(Base) {
9696
// Abort last request
9797

9898
const file = this.router.getFile(path);
99+
const req = request(file + qs, true, requestHeaders);
99100

100101
this.isRemoteUrl = isExternal(file);
101102
// Current page is html
102103
this.isHTML = /\.html$/g.test(file);
103104

104-
// create a handler that should be called if content was fetched successfully
105-
const contentFetched = (text, opt) => {
106-
this._renderMain(
107-
text,
108-
opt,
109-
this._loadSideAndNav(path, qs, loadSidebar, cb)
110-
);
111-
};
112-
113-
// and a handler that is called if content failed to fetch
114-
const contentFailedToFetch = _ => {
115-
this._fetchFallbackPage(path, qs, cb) || this._fetch404(file, qs, cb);
116-
};
117-
118-
// attempt to fetch content from a virtual route, and fallback to fetching the actual file
119-
if (!this.isRemoteUrl) {
120-
this.matchVirtualRoute(path).then(contents => {
121-
if (typeof contents === 'string') {
122-
contentFetched(contents);
123-
} else {
124-
request(file + qs, true, requestHeaders).then(
125-
contentFetched,
126-
contentFailedToFetch
127-
);
128-
}
129-
});
130-
} else {
131-
// if the requested url is not local, just fetch the file
132-
request(file + qs, true, requestHeaders).then(
133-
contentFetched,
134-
contentFailedToFetch
135-
);
136-
}
105+
// Load main content
106+
req.then(
107+
(text, opt) =>
108+
this._renderMain(
109+
text,
110+
opt,
111+
this._loadSideAndNav(path, qs, loadSidebar, cb)
112+
),
113+
_ => {
114+
this._fetchFallbackPage(path, qs, cb) ||
115+
this._fetch404(file, qs, cb);
116+
}
117+
);
137118

138119
// Load nav
139120
loadNavbar &&

src/core/router/history/hash.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { noop } from '../../util/core';
22
import { on } from '../../util/dom';
3-
import { endsWith } from '../../util/str';
4-
import { parseQuery, cleanPath, replaceSlug } from '../util';
3+
import { parseQuery, cleanPath, replaceSlug, endsWith } from '../util';
54
import { History } from './base';
65

76
function replaceHash(path) {

src/core/router/util.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,7 @@ export function getPath(...args) {
113113
export const replaceSlug = cached(path => {
114114
return path.replace('#', '?id=');
115115
});
116+
117+
export function endsWith(str, suffix) {
118+
return str.indexOf(suffix, str.length - suffix.length) !== -1;
119+
}

src/core/util/str.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/core/virtual-routes/exact-match.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/core/virtual-routes/index.js

Lines changed: 0 additions & 93 deletions
This file was deleted.

src/core/virtual-routes/next.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)