You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
thumbsup/src/model/url.js

20 lines
677 B
JavaScript

const path = require('node:path')
const process = require('node: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, '/')
}
// encode URLs, but decode overly-encoded slashes
filepath = encodeURIComponent(filepath).replace(/%2F/g, '/')
// prepend the prefix if needed
return prefix + filepath
}