From 7f5d3acbcd6e8558f00697fbf3bf70a895e32ee7 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Sat, 28 Sep 2019 20:16:38 +0800 Subject: [PATCH] fix: better absolute url handling --- packages/saber-utils/index.d.ts | 6 +++--- packages/saber-utils/package.json | 3 ++- packages/saber-utils/src/isAbsoluteUrl.js | 1 + packages/saber/lib/utils/assetsAttribute.js | 8 +++++--- packages/saber/vue-renderer/lib/template-plugins/link.js | 4 ++-- yarn.lock | 5 +++++ 6 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 packages/saber-utils/src/isAbsoluteUrl.js diff --git a/packages/saber-utils/index.d.ts b/packages/saber-utils/index.d.ts index 40d35c03c..07699bdc8 100644 --- a/packages/saber-utils/index.d.ts +++ b/packages/saber-utils/index.d.ts @@ -2,9 +2,9 @@ declare module 'saber-utils' { import fs from 'fs-extra' import glob from 'fast-glob' - type Slash = (input: string) => string + const slash: (input: string) => string - const slash: Slash + const isAbsoluteUrl: (input: string) => boolean - export { fs, glob, slash } + export { fs, glob, slash, isAbsoluteUrl } } diff --git a/packages/saber-utils/package.json b/packages/saber-utils/package.json index aec1c0622..b442047b4 100644 --- a/packages/saber-utils/package.json +++ b/packages/saber-utils/package.json @@ -15,7 +15,8 @@ "devDependencies": { "@zeit/ncc": "0.20.5", "fast-glob": "2.2.6", - "fs-extra": "8.1.0" + "fs-extra": "8.1.0", + "is-absolute-url": "^3.0.2" }, "license": "MIT" } diff --git a/packages/saber-utils/src/isAbsoluteUrl.js b/packages/saber-utils/src/isAbsoluteUrl.js new file mode 100644 index 000000000..c0282592e --- /dev/null +++ b/packages/saber-utils/src/isAbsoluteUrl.js @@ -0,0 +1 @@ +module.exports = require('is-absolute-url') diff --git a/packages/saber/lib/utils/assetsAttribute.js b/packages/saber/lib/utils/assetsAttribute.js index 060c25bb9..30a071b0b 100644 --- a/packages/saber/lib/utils/assetsAttribute.js +++ b/packages/saber/lib/utils/assetsAttribute.js @@ -1,13 +1,15 @@ // @ts-check const { join } = require('path') -const { slash } = require('saber-utils') +const { slash, isAbsoluteUrl } = require('saber-utils') /** - * Check if it's external resource + * It's considered external resource + * When it's an absolute url or starting with `/` + * `/path` is used to reference files in static folder * @param {string} str */ -const isExternal = str => /^https?:\/\//.test(str) || /^\//.test(str) +const isExternal = str => isAbsoluteUrl(str) || /^\//.test(str) const MARK = '@@!!SABER_ASSET_MARK_e5968b9a!!@@' diff --git a/packages/saber/vue-renderer/lib/template-plugins/link.js b/packages/saber/vue-renderer/lib/template-plugins/link.js index 185cae2b1..b3d9e1cc3 100644 --- a/packages/saber/vue-renderer/lib/template-plugins/link.js +++ b/packages/saber/vue-renderer/lib/template-plugins/link.js @@ -1,4 +1,4 @@ -const isExernal = input => /^[^:]+:/i.test(input) +const { isAbsoluteUrl } = require('saber-utils') module.exports = ({ openLinkInNewTab = true } = {}) => tree => { tree.walk(node => { @@ -10,7 +10,7 @@ module.exports = ({ openLinkInNewTab = true } = {}) => tree => { } if (node.tag === 'a' && node.attrs.href) { - if (isExernal(node.attrs.href)) { + if (isAbsoluteUrl(node.attrs.href)) { // Add attributes for external link if (/^https?:\/\//.test(node.attrs.href)) { node.attrs = Object.assign( diff --git a/yarn.lock b/yarn.lock index 815f151ee..5e7e5a7dd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6727,6 +6727,11 @@ is-absolute-url@^2.0.0: resolved "https://registry.npmjs.org/is-absolute-url/-/is-absolute-url-2.1.0.tgz#50530dfb84fcc9aa7dbe7852e83a37b93b9f2aa6" integrity sha1-UFMN+4T8yap9vnhS6Do3uTufKqY= +is-absolute-url@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/is-absolute-url/-/is-absolute-url-3.0.2.tgz#554f2933e7385cc46e94351977ca2081170a206e" + integrity sha512-+5g/wLlcm1AcxSP7014m6GvbPHswDx980vD/3bZaap8aGV9Yfs7Q6y6tfaupgZ5O74Byzc8dGrSCJ+bFXx0KdA== + is-accessor-descriptor@^0.1.6: version "0.1.6" resolved "https://registry.npmjs.org/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz#a9e12cb3ae8d876727eeef3843f8a0897b5c98d6"