relative-path-to-relative-url

Convert relative file system paths to relative URLs.

Usage no npm install needed!

<script type="module">
  import relativePathToRelativeUrl from 'https://cdn.skypack.dev/relative-path-to-relative-url';
</script>

README

relative-path-to-relative-url

Convert relative file system paths to relative URLs.

Examples

// Unix
relativePathToRelativeUrl('foo/bar')
relativePathToRelativeUrl('/foo/bar')
relativePathToRelativeUrl('./foo/bar')
// -> './foo/bar'

// Windows
relativePathToRelativeUrl('foo\\bar')
relativePathToRelativeUrl('\\foo\\bar')
relativePathToRelativeUrl('.\\foo\\bar')
// -> './foo/bar'

relativePathToRelativeUrl(path)

path is a string containing a relative file path.

Returns a string representing a relative URL.

The URL always starts with ./ to make it relative to the current directory.

Respects the path separator of the host environment, typically / or \.

For example ./foo.js or foo.js or /foo will all be returned as ./foo.js on macOS and Linux, while backslashes will not be converted. Conversely, on Windows .\foo.js will be returned as ./foo.js, while forward slashes will be left unchanged.

See Also