Skip to content

Commit

Permalink
Revert "Virtual Routes Support (#1799)"
Browse files Browse the repository at this point in the history
This reverts commit 5bf58d6.
  • Loading branch information
Koooooo-7 authored Oct 26, 2022
1 parent 300893b commit 60cd96f
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 570 deletions.
9 changes: 1 addition & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ module.exports = {
'no-shadow': [
'error',
{
allow: [
'Events',
'Fetch',
'Lifecycle',
'Render',
'Router',
'VirtualRoutes',
],
allow: ['Events', 'Fetch', 'Lifecycle', 'Render', 'Router'],
},
],
'no-unused-vars': ['error', { args: 'none' }],
Expand Down
86 changes: 0 additions & 86 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ The config can also be defined as a function, in which case the first argument i
- Type: `Object`

Set the route alias. You can freely manage routing rules. Supports RegExp.
Do note that order matters! If a route can be matched by multiple aliases, the one you declared first takes precedence.

```js
window.$docsify = {
Expand Down Expand Up @@ -681,91 +680,6 @@ window.$docsify = {
};
```

## routes

- Type: `Object`

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.

A route function receives up to three parameters:
1. `route` - the path of the route that was requested (e.g. `/bar/baz`)
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']`)
3. `next` - this is a callback that you may call when your route function is async

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.

```js
window.$docsify = {
routes: {
// Basic match w/ return string
'/foo': '# Custom Markdown',

// RegEx match w/ synchronous function
'/bar/(.*)': function(route, matched) {
return '# Custom Markdown';
},

// RegEx match w/ asynchronous function
'/baz/(.*)': function(route, matched, next) {
// Requires `fetch` polyfill for legacy browsers (https://github.github.io/fetch/)
fetch('/api/users?id=12345')
.then(function(response) {
next('# Custom Markdown');
})
.catch(function(err) {
// Handle error...
});
}
}
}
```

Other than strings, route functions can return a falsy value (`null` \ `undefined`) to indicate that they ignore the current request:

```js
window.$docsify = {
routes: {
// accepts everything other than dogs (synchronous)
'/pets/(.+)': function(route, matched) {
if (matched[0] === 'dogs') {
return null;
} else {
return 'I like all pets but dogs';
}
}

// accepts everything other than cats (asynchronous)
'/pets/(.*)': function(route, matched, next) {
if (matched[0] === 'cats') {
next();
} else {
// Async task(s)...
next('I like all pets but cats');
}
}
}
}
```

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:

```js
window.$docsify = {
routes: {
// if you look up /pets/cats, docsify will skip all routes and look for "pets/cats.md"
'/pets/cats': function(route, matched) {
return false;
}

// but any other pet should generate dynamic content right here
'/pets/(.+)': function(route, matched) {
const pet = matched[0];
return `your pet is ${pet} (but not a cat)`;
}
}
}
```

## subMaxLevel

- Type: `Number`
Expand Down
6 changes: 1 addition & 5 deletions src/core/Docsify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Router } from './router/index.js';
import { Render } from './render/index.js';
import { Fetch } from './fetch/index.js';
import { Events } from './event/index.js';
import { VirtualRoutes } from './virtual-routes/index.js';
import initGlobalAPI from './global-api.js';

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

// eslint-disable-next-line new-cap
export class Docsify extends Fetch(
// eslint-disable-next-line new-cap
Events(Render(VirtualRoutes(Router(Lifecycle(Object)))))
) {
export class Docsify extends Fetch(Events(Render(Router(Lifecycle(Object))))) {
constructor() {
super();

Expand Down
1 change: 0 additions & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export default function (vm) {
notFoundPage: true,
relativePath: false,
repo: '',
routes: {},
routerMode: 'hash',
subMaxLevel: 0,
themeColor: '',
Expand Down
47 changes: 14 additions & 33 deletions src/core/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,44 +96,25 @@ export function Fetch(Base) {
// Abort last request

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

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

// create a handler that should be called if content was fetched successfully
const contentFetched = (text, opt) => {
this._renderMain(
text,
opt,
this._loadSideAndNav(path, qs, loadSidebar, cb)
);
};

// and a handler that is called if content failed to fetch
const contentFailedToFetch = _ => {
this._fetchFallbackPage(path, qs, cb) || this._fetch404(file, qs, cb);
};

// attempt to fetch content from a virtual route, and fallback to fetching the actual file
if (!this.isRemoteUrl) {
this.matchVirtualRoute(path).then(contents => {
if (typeof contents === 'string') {
contentFetched(contents);
} else {
request(file + qs, true, requestHeaders).then(
contentFetched,
contentFailedToFetch
);
}
});
} else {
// if the requested url is not local, just fetch the file
request(file + qs, true, requestHeaders).then(
contentFetched,
contentFailedToFetch
);
}
// Load main content
req.then(
(text, opt) =>
this._renderMain(
text,
opt,
this._loadSideAndNav(path, qs, loadSidebar, cb)
),
_ => {
this._fetchFallbackPage(path, qs, cb) ||
this._fetch404(file, qs, cb);
}
);

// Load nav
loadNavbar &&
Expand Down
3 changes: 1 addition & 2 deletions src/core/router/history/hash.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { noop } from '../../util/core';
import { on } from '../../util/dom';
import { endsWith } from '../../util/str';
import { parseQuery, cleanPath, replaceSlug } from '../util';
import { parseQuery, cleanPath, replaceSlug, endsWith } from '../util';
import { History } from './base';

function replaceHash(path) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/router/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ export function getPath(...args) {
export const replaceSlug = cached(path => {
return path.replace('#', '?id=');
});

export function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
7 changes: 0 additions & 7 deletions src/core/util/str.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/core/virtual-routes/exact-match.js

This file was deleted.

93 changes: 0 additions & 93 deletions src/core/virtual-routes/index.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/core/virtual-routes/next.js

This file was deleted.

Loading

0 comments on commit 60cd96f

Please sign in to comment.