Skip to content

Commit 2312fee

Browse files
trusktrsy-records
authored andcommittedJun 24, 2023
fix: fix cross-origin links in history router mode (#1967)
1 parent 6a7d15b commit 2312fee

File tree

6 files changed

+14
-51
lines changed

6 files changed

+14
-51
lines changed
 

‎docs/configuration.md

+9-21
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,6 @@ window.$docsify = {
140140
};
141141
```
142142

143-
## crossOriginLinks
144-
145-
- Type: `Array`
146-
147-
When `routerMode: 'history'`, you may face cross-origin issues. See [#1379](https://github.com/docsifyjs/docsify/issues/1379).
148-
In Markdown content, there is a simple way to solve it: see extends Markdown syntax `Cross-Origin link` in [helpers](helpers.md).
149-
150-
```js
151-
window.$docsify = {
152-
crossOriginLinks: ['https://example.com/cross-origin-link'],
153-
};
154-
```
155-
156143
## el
157144

158145
- Type: `String`
@@ -688,6 +675,7 @@ window.$docsify = {
688675
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.
689676

690677
A route function receives up to three parameters:
678+
691679
1. `route` - the path of the route that was requested (e.g. `/bar/baz`)
692680
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']`)
693681
3. `next` - this is a callback that you may call when your route function is async
@@ -701,23 +689,23 @@ window.$docsify = {
701689
'/foo': '# Custom Markdown',
702690

703691
// RegEx match w/ synchronous function
704-
'/bar/(.*)': function(route, matched) {
692+
'/bar/(.*)': function (route, matched) {
705693
return '# Custom Markdown';
706694
},
707695

708696
// RegEx match w/ asynchronous function
709-
'/baz/(.*)': function(route, matched, next) {
710-
// Requires `fetch` polyfill for legacy browsers (https://github.github.io/fetch/)
697+
'/baz/(.*)': function (route, matched, next) {
698+
// Requires `fetch` polyfill for legacy browsers (https://github.github.io/fetch/)
711699
fetch('/api/users?id=12345')
712-
.then(function(response) {
700+
.then(function (response) {
713701
next('# Custom Markdown');
714702
})
715-
.catch(function(err) {
703+
.catch(function (err) {
716704
// Handle error...
717705
});
718-
}
719-
}
720-
}
706+
},
707+
},
708+
};
721709
```
722710

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

‎docs/helpers.md

-8
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set th
6565
[link](/demo ':disabled')
6666
```
6767

68-
## Cross-Origin link
69-
70-
Only when you set both the `routerMode: 'history'` and `externalLinkTarget: '_self'`, you need to add this configuration for those Cross-Origin links.
71-
72-
```md
73-
[example.com](https://example.com/ ':crossorgin')
74-
```
75-
7668
## GitHub Task Lists
7769

7870
```md

‎src/core/config.js

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default function (vm) {
1212
catchPluginErrors: true,
1313
cornerExternalLinkTarget: '_blank',
1414
coverpage: '',
15-
crossOriginLinks: [],
1615
el: '#app',
1716
executeScript: null,
1817
ext: '.md',

‎src/core/render/compiler/link.js

-11
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ export const linkCompiler = ({
4343
);
4444
}
4545

46-
// special case to check crossorigin urls
47-
if (
48-
config.crossorgin &&
49-
linkTarget === '_self' &&
50-
compilerClass.config.routerMode === 'history'
51-
) {
52-
if (compilerClass.config.crossOriginLinks.indexOf(href) === -1) {
53-
compilerClass.config.crossOriginLinks.push(href);
54-
}
55-
}
56-
5746
if (config.disabled) {
5847
attrs.push('disabled');
5948
href = 'javascript:void(0)';

‎src/core/router/history/hash.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noop } from '../../util/core';
1+
import { isExternal, noop } from '../../util/core';
22
import { on } from '../../util/dom';
33
import { endsWith } from '../../util/str';
44
import { parseQuery, cleanPath, replaceSlug } from '../util';
@@ -48,7 +48,7 @@ export class HashHistory extends History {
4848
on('click', e => {
4949
const el = e.target.tagName === 'A' ? e.target : e.target.parentNode;
5050

51-
if (el && el.tagName === 'A' && !/_blank/.test(el.target)) {
51+
if (el && el.tagName === 'A' && !isExternal(el.href)) {
5252
navigating = true;
5353
}
5454
});

‎src/core/router/history/html5.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noop } from '../../util/core';
1+
import { isExternal, noop } from '../../util/core';
22
import { on } from '../../util/dom';
33
import { parseQuery, getPath } from '../util';
44
import { History } from './base';
@@ -24,15 +24,10 @@ export class HTML5History extends History {
2424
on('click', e => {
2525
const el = e.target.tagName === 'A' ? e.target : e.target.parentNode;
2626

27-
if (el && el.tagName === 'A' && !/_blank/.test(el.target)) {
27+
if (el && el.tagName === 'A' && !isExternal(el.href)) {
2828
e.preventDefault();
2929
const url = el.href;
30-
// solve history.pushState cross-origin issue
31-
if (this.config.crossOriginLinks.indexOf(url) !== -1) {
32-
window.open(url, '_self');
33-
} else {
34-
window.history.pushState({ key: url }, '', url);
35-
}
30+
window.history.pushState({ key: url }, '', url);
3631
cb({ event: e, source: 'navigate' });
3732
}
3833
});

0 commit comments

Comments
 (0)