From 292e788a45fb748a32e5bfcb9a92f10063dd9921 Mon Sep 17 00:00:00 2001 From: Joe Pea Date: Sat, 21 Jan 2023 22:55:26 -0800 Subject: [PATCH] ref #1803: the crossOriginLinks option is no longer needed, remove misspelled "crossorgin" link helper, and instead detect cross origin links and don't handle them as internal --- docs/configuration.md | 30 +++++++++--------------------- docs/helpers.md | 8 -------- src/core/config.js | 1 - src/core/render/compiler/link.js | 11 ----------- src/core/router/history/hash.js | 4 ++-- src/core/router/history/html5.js | 11 +++-------- 6 files changed, 14 insertions(+), 51 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 3fdf2a61c..61c3a9b3e 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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` @@ -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 @@ -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: diff --git a/docs/helpers.md b/docs/helpers.md index 6bd8a069d..a6fc95ea5 100644 --- a/docs/helpers.md +++ b/docs/helpers.md @@ -65,14 +65,6 @@ You will get `link`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 diff --git a/src/core/config.js b/src/core/config.js index 5dc9a3850..9a8bdb2f8 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -12,7 +12,6 @@ export default function (vm) { catchPluginErrors: true, cornerExternalLinkTarget: '_blank', coverpage: '', - crossOriginLinks: [], el: '#app', executeScript: null, ext: '.md', diff --git a/src/core/render/compiler/link.js b/src/core/render/compiler/link.js index fd8b81138..613101000 100644 --- a/src/core/render/compiler/link.js +++ b/src/core/render/compiler/link.js @@ -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)'; diff --git a/src/core/router/history/hash.js b/src/core/router/history/hash.js index abb060f2f..05435d2c9 100644 --- a/src/core/router/history/hash.js +++ b/src/core/router/history/hash.js @@ -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'; @@ -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; } }); diff --git a/src/core/router/history/html5.js b/src/core/router/history/html5.js index eeadb1af1..1304d0628 100644 --- a/src/core/router/history/html5.js +++ b/src/core/router/history/html5.js @@ -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'; @@ -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' }); } });