-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: Virtual Routes Support #1799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ba998e8
add first test
illBeRoy 7c92837
new VirtualRoutes mixin that handles routes. fetch tries to use Virtu…
illBeRoy 7a2d0d0
cover all basic use cases
illBeRoy 3f359ed
regex matching in routes
illBeRoy 53c507f
covered all virtual routes tests
illBeRoy 9d816bc
added hack to fix config test on firefox
illBeRoy fb084f8
removed formatting regex matches into string routes
illBeRoy 1866fa2
added support for "next" function
illBeRoy 8bf6890
added docs
illBeRoy a022b3d
navigate now supports both hash and history routerModes
illBeRoy 1447898
waiting for networkidle in navigateToRoute helper
illBeRoy e8a12c0
promiseless implementation
illBeRoy e81429d
remove firefox workaround from catchPluginErrors test, since we no lo…
illBeRoy f0be4ca
updated docs
illBeRoy dbf45d4
updated docs for "alias" as well
illBeRoy dfbf77f
minor rephrasing
illBeRoy a245a2b
removed non-legacy code from exact-match; updated navigateToRoute hel…
illBeRoy 4277cc5
moved endsWith from router utils to general utils; added startsWith u…
illBeRoy 4bd7ac8
updated docs per feedback
illBeRoy ebfc235
moved navigateToRoute helper into the virtual-routes test file
illBeRoy bea7308
moved navigateToRoute to top of file
illBeRoy c748b33
updated docs per pr comments
illBeRoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function startsWith(str, prefix) { | ||
return str.indexOf(prefix) === 0; | ||
} | ||
|
||
export function endsWith(str, suffix) { | ||
return str.indexOf(suffix, str.length - suffix.length) !== -1; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { startsWith, endsWith } from '../util/str'; | ||
|
||
/** | ||
* Adds beginning of input (^) and end of input ($) assertions if needed into a regex string | ||
* @param {string} matcher the string to match | ||
* @returns {string} | ||
*/ | ||
export function makeExactMatcher(matcher) { | ||
const matcherWithBeginningOfInput = startsWith(matcher, '^') | ||
? matcher | ||
: `^${matcher}`; | ||
|
||
const matcherWithBeginningAndEndOfInput = endsWith( | ||
matcherWithBeginningOfInput, | ||
'$' | ||
) | ||
? matcherWithBeginningOfInput | ||
: `${matcherWithBeginningOfInput}$`; | ||
|
||
return matcherWithBeginningAndEndOfInput; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { makeExactMatcher } from './exact-match'; | ||
import { createNextFunction } from './next'; | ||
|
||
/** @typedef {import('../Docsify').Constructor} Constructor */ | ||
|
||
/** @typedef {Record<string, string | VirtualRouteHandler>} VirtualRoutesMap */ | ||
/** @typedef {(route: string, match: RegExpMatchArray | null) => string | void | Promise<string | void> } VirtualRouteHandler */ | ||
|
||
/** | ||
* @template {!Constructor} T | ||
* @param {T} Base - The class to extend | ||
*/ | ||
export function VirtualRoutes(Base) { | ||
return class VirtualRoutes extends Base { | ||
/** | ||
* Gets the Routes object from the configuration | ||
* @returns {VirtualRoutesMap} | ||
*/ | ||
routes() { | ||
return this.config.routes || {}; | ||
} | ||
|
||
/** | ||
* Attempts to match the given path with a virtual route. | ||
* @param {string} path the path of the route to match | ||
* @returns {Promise<string | null>} resolves to string if route was matched, otherwise null | ||
*/ | ||
matchVirtualRoute(path) { | ||
const virtualRoutes = this.routes(); | ||
const virtualRoutePaths = Object.keys(virtualRoutes); | ||
|
||
let done = () => null; | ||
|
||
/** | ||
* This is a tail recursion that iterates over all the available routes. | ||
* It can result in one of two ways: | ||
* 1. Call itself (essentially reviewing the next route) | ||
* 2. Call the "done" callback with the result (either the contents, or "null" if no match was found) | ||
*/ | ||
function asyncMatchNextRoute() { | ||
const virtualRoutePath = virtualRoutePaths.shift(); | ||
if (!virtualRoutePath) { | ||
return done(null); | ||
} | ||
|
||
const matcher = makeExactMatcher(virtualRoutePath); | ||
const matched = path.match(matcher); | ||
|
||
if (!matched) { | ||
return asyncMatchNextRoute(); | ||
} | ||
|
||
const virtualRouteContentOrFn = virtualRoutes[virtualRoutePath]; | ||
|
||
if (typeof virtualRouteContentOrFn === 'string') { | ||
const contents = virtualRouteContentOrFn; | ||
return done(contents); | ||
} | ||
|
||
if (typeof virtualRouteContentOrFn === 'function') { | ||
const fn = virtualRouteContentOrFn; | ||
|
||
const [next, onNext] = createNextFunction(); | ||
onNext(contents => { | ||
if (typeof contents === 'string') { | ||
return done(contents); | ||
} else if (contents === false) { | ||
return done(null); | ||
} else { | ||
return asyncMatchNextRoute(); | ||
} | ||
}); | ||
|
||
if (fn.length <= 2) { | ||
const returnedValue = fn(path, matched); | ||
return next(returnedValue); | ||
} else { | ||
return fn(path, matched, next); | ||
} | ||
} | ||
|
||
return asyncMatchNextRoute(); | ||
} | ||
|
||
return { | ||
then: function (cb) { | ||
done = cb; | ||
asyncMatchNextRoute(); | ||
}, | ||
}; | ||
} | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** @typedef {((value: any) => void) => void} OnNext */ | ||
/** @typedef {(value: any) => void} NextFunction */ | ||
|
||
/** | ||
* Creates a pair of a function and an event emitter. | ||
* When the function is called, the event emitter calls the given callback with the value that was passed to the function. | ||
* @returns {[NextFunction, OnNext]} | ||
*/ | ||
export function createNextFunction() { | ||
let storedCb = () => null; | ||
|
||
function next(value) { | ||
storedCb(value); | ||
} | ||
|
||
function onNext(cb) { | ||
storedCb = cb; | ||
} | ||
|
||
return [next, onNext]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.