Skip to content

Commit

Permalink
fix: better absolute url handling
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Sep 28, 2019
1 parent b56296a commit 7f5d3ac
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/saber-utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
3 changes: 2 additions & 1 deletion packages/saber-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
1 change: 1 addition & 0 deletions packages/saber-utils/src/isAbsoluteUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('is-absolute-url')
8 changes: 5 additions & 3 deletions packages/saber/lib/utils/assetsAttribute.js
Original file line number Diff line number Diff line change
@@ -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!!@@'

Expand Down
4 changes: 2 additions & 2 deletions packages/saber/vue-renderer/lib/template-plugins/link.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const isExernal = input => /^[^:]+:/i.test(input)
const { isAbsoluteUrl } = require('saber-utils')

module.exports = ({ openLinkInNewTab = true } = {}) => tree => {
tree.walk(node => {
Expand All @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 7f5d3ac

Please sign in to comment.