mirror of
https://github.com/docsifyjs/docsify.git
synced 2025-12-08 19:55:52 +00:00
fix: cross-origin url cannot be redirected when "externalLinkTarget" is set to "_self" and "routerMode" is set to "history". (#1062)
* [fix #1046] fix cross-origin url cannot be redirected when "externalLinkTarget" is set to "_self" and "routerMode" is set to "history". * [code format] code format. * update docs * docs refine. * fix(core): cross-orgin link work incorrect (#1046) Fix cross-origin url cannot be redirected when "externalLinkTarget" is set to "_self" and "routerMode" is set to "history". Add new configuration for those cases and completed docs. Fixes #1046 PR Close #1062
This commit is contained in:
parent
6e554f8ebd
commit
fd2cec6bd6
@ -65,6 +65,14 @@ You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set ti
|
||||
[link](/demo ':disabled')
|
||||
```
|
||||
|
||||
## Cross-Origin link
|
||||
|
||||
Only when you both set the `routerMode: 'history'` and `externalLinkTarget: '_self'`, you need add this configuration for those Cross-Origin links.
|
||||
|
||||
```md
|
||||
[example.com](https://example.com/ ':crossorgin')
|
||||
```
|
||||
|
||||
## Github Task Lists
|
||||
|
||||
```md
|
||||
|
||||
@ -32,6 +32,7 @@ export default function() {
|
||||
externalLinkRel: 'noopener',
|
||||
routerMode: 'hash',
|
||||
noCompileLinks: [],
|
||||
crossOriginLinks: [],
|
||||
relativePath: false,
|
||||
topMargin: 0,
|
||||
},
|
||||
|
||||
@ -30,6 +30,17 @@ export const linkCompiler = ({ renderer, router, linkTarget, compilerClass }) =>
|
||||
attrs.push(`target="${config.target}"`);
|
||||
}
|
||||
|
||||
// special case to check crossorigin urls
|
||||
if (
|
||||
config.crossorgin &&
|
||||
linkTarget === '_self' &&
|
||||
compilerClass.config.routerMode === 'history'
|
||||
) {
|
||||
if (compilerClass.config.crossOriginLinks.indexOf(href) === -1) {
|
||||
compilerClass.config.crossOriginLinks.push(href);
|
||||
}
|
||||
}
|
||||
|
||||
if (config.disabled) {
|
||||
attrs.push('disabled');
|
||||
href = 'javascript:void(0)';
|
||||
|
||||
@ -27,7 +27,12 @@ export class HTML5History extends History {
|
||||
if (el.tagName === 'A' && !/_blank/.test(el.target)) {
|
||||
e.preventDefault();
|
||||
const url = el.href;
|
||||
window.history.pushState({ key: url }, '', url);
|
||||
// solve history.pushState cross-origin issue
|
||||
if (this.config.crossOriginLinks.indexOf(url) !== -1) {
|
||||
window.open(url, '_self');
|
||||
} else {
|
||||
window.history.pushState({ key: url }, '', url);
|
||||
}
|
||||
cb({ event: e, source: 'navigate' });
|
||||
}
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user