Skip to content

Commit

Permalink
fix: fix cross-origin links in history router mode (#1967)
Browse files Browse the repository at this point in the history
  • Loading branch information
trusktr authored and sy-records committed Jun 24, 2023
1 parent 6a7d15b commit 2312fee
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 51 deletions.
30 changes: 9 additions & 21 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,6 @@ window.$docsify = {
};
```

## crossOriginLinks

- Type: `Array`

When `routerMode: 'history'`, you may face cross-origin issues. See [#1379](https://github.com/docsifyjs/docsify/issues/1379).
In Markdown content, there is a simple way to solve it: see extends Markdown syntax `Cross-Origin link` in [helpers](helpers.md).

```js
window.$docsify = {
crossOriginLinks: ['https://example.com/cross-origin-link'],
};
```

## el

- Type: `String`
Expand Down Expand Up @@ -688,6 +675,7 @@ window.$docsify = {
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
Expand All @@ -701,23 +689,23 @@ window.$docsify = {
'/foo': '# Custom Markdown',

// RegEx match w/ synchronous function
'/bar/(.*)': function(route, matched) {
'/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/)
'/baz/(.*)': function (route, matched, next) {
// Requires `fetch` polyfill for legacy browsers (https://github.github.io/fetch/)
fetch('/api/users?id=12345')
.then(function(response) {
.then(function (response) {
next('# Custom Markdown');
})
.catch(function(err) {
.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:
Expand Down
8 changes: 0 additions & 8 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set th
[link](/demo ':disabled')
```

## Cross-Origin link

Only when you set both the `routerMode: 'history'` and `externalLinkTarget: '_self'`, you need to add this configuration for those Cross-Origin links.

```md
[example.com](https://example.com/ ':crossorgin')
```

## GitHub Task Lists

```md
Expand Down
1 change: 0 additions & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default function (vm) {
catchPluginErrors: true,
cornerExternalLinkTarget: '_blank',
coverpage: '',
crossOriginLinks: [],
el: '#app',
executeScript: null,
ext: '.md',
Expand Down
11 changes: 0 additions & 11 deletions src/core/render/compiler/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ export const linkCompiler = ({
);
}

// 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)';
Expand Down
4 changes: 2 additions & 2 deletions src/core/router/history/hash.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noop } from '../../util/core';
import { isExternal, noop } from '../../util/core';
import { on } from '../../util/dom';
import { endsWith } from '../../util/str';
import { parseQuery, cleanPath, replaceSlug } from '../util';
Expand Down Expand Up @@ -48,7 +48,7 @@ export class HashHistory extends History {
on('click', e => {
const el = e.target.tagName === 'A' ? e.target : e.target.parentNode;

if (el && el.tagName === 'A' && !/_blank/.test(el.target)) {
if (el && el.tagName === 'A' && !isExternal(el.href)) {
navigating = true;
}
});
Expand Down
11 changes: 3 additions & 8 deletions src/core/router/history/html5.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noop } from '../../util/core';
import { isExternal, noop } from '../../util/core';
import { on } from '../../util/dom';
import { parseQuery, getPath } from '../util';
import { History } from './base';
Expand All @@ -24,15 +24,10 @@ export class HTML5History extends History {
on('click', e => {
const el = e.target.tagName === 'A' ? e.target : e.target.parentNode;

if (el && el.tagName === 'A' && !/_blank/.test(el.target)) {
if (el && el.tagName === 'A' && !isExternal(el.href)) {
e.preventDefault();
const url = el.href;
// solve history.pushState cross-origin issue
if (this.config.crossOriginLinks.indexOf(url) !== -1) {
window.open(url, '_self');
} else {
window.history.pushState({ key: url }, '', url);
}
window.history.pushState({ key: url }, '', url);
cb({ event: e, source: 'navigate' });
}
});
Expand Down

0 comments on commit 2312fee

Please sign in to comment.