mirror of https://github.com/thumbsup/thumbsup
parent
f3571718ce
commit
df1c6a7073
@ -1,9 +1,19 @@
|
||||
const path = require('path')
|
||||
const process = require('process')
|
||||
|
||||
exports.fromPath = function (filepath) {
|
||||
// already a URL (typically provided as a CLI argument, e.g. link prefix)
|
||||
if (filepath.match(/^(http|https|file):\/\//)) {
|
||||
return filepath
|
||||
}
|
||||
// absolute paths should start with file://
|
||||
const prefix = path.isAbsolute(filepath) ? 'file://' : ''
|
||||
// convert \ to / but only on Windows
|
||||
if (process.platform === 'win32') {
|
||||
filepath = filepath.replace(/\\/g, '/')
|
||||
}
|
||||
const encoded = encodeURIComponent(filepath)
|
||||
return encoded.replace(/%2F/g, '/')
|
||||
// encode URLs, but decode overly-encoded slashes
|
||||
filepath = encodeURIComponent(filepath).replace(/%2F/g, '/')
|
||||
// prepend the prefix if needed
|
||||
return prefix + filepath
|
||||
}
|
||||
|
Loading…
Reference in New Issue