Skip to content

Commit 0a8deed

Browse files
committed
feat: Qualify URLs then see if the origins match.
This takes a string input, and converts it into a fully-qualified URL. For example, `./foo` becomes `http://example.com/current/path/foo`. Then we can compare it to other fully-qualified `URL` instances.
1 parent 1d46637 commit 0a8deed

File tree

1 file changed

+12
-23
lines changed
  • packages/docsify-server-renderer

1 file changed

+12
-23
lines changed

packages/docsify-server-renderer/index.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,19 @@ function cwd(...args) {
1414
return resolve(process.cwd(), ...args);
1515
}
1616

17+
// Borrowed from https://j11y.io/snippets/getting-a-fully-qualified-url.
18+
function qualifyURL(url){
19+
var img = document.createElement('img');
20+
img.src = url; // set string url
21+
url = img.src; // get qualified url
22+
img.src = ''; // prevent the server request
23+
return url;
24+
}
25+
1726
function isExternal(url) {
18-
let match = url.match(
19-
/^([^:\/?#]+:)?(?:\/\/([^\/?#]*))?([^?#]+)?(\?[^#]*)?(#.*)?/
20-
);
21-
if (
22-
typeof match[1] === 'string' &&
23-
match[1].length > 0 &&
24-
match[1].toLowerCase() !== location.protocol
25-
) {
26-
return true;
27-
}
28-
if (
29-
typeof match[2] === 'string' &&
30-
match[2].length > 0 &&
31-
match[2].replace(
32-
new RegExp(
33-
':(' + { 'http:': 80, 'https:': 443 }[location.protocol] + ')?$'
34-
),
35-
''
36-
) !== location.host
37-
) {
38-
return true;
39-
}
40-
return false;
27+
url = qualifyURL(url)
28+
url = new URL(url)
29+
return url.origin !== location.origin
4130
}
4231

4332
function mainTpl(config) {

0 commit comments

Comments
 (0)