path-prefix
v1.0.0
Published
Create a relative path to a directory
Downloads
20
Readme
path-prefix
Create a relative path to a directory
Returns a string you can append to the beginning of a path to make it relative to another path. See Example below for a use case.
Installation
npm install path-prefix
Usage
const pathPrefix = require('path-prefix');
const fileA = '/path/to/file.html';
const fileB = '/path/to/subdirectory/file.html';
const root = '/path/to/file';
// No prefix is needed if the file is right inside the root
pathPrefix(fileA, root); // => ''
// If the file is in a different directory, a relative path prefix is made
pathPrefix(fileB, root); // => '../'
Example
Use this if you're generating HTML and need all of your <link>
, <img>
, and <script>
tags to be relative paths.
const pathPrefix = require('path-prefix');
const path = '/src/about/index.html';
const root = '/src';
const link = `<link rel="stylesheet" href="${pathPrefix(path, root)}styles/style.css">`;
// => '<link rel="stylesheet" href="../styles/style.css">'
API
pathPrefix(file, root)
Create a string that can be prepended to a file path that needs to resolve to a root directory.
- file (String): Path to the file.
- root (String): Path to the root directory.
Returns an empty string if the file is at the root, or a series of ../
characters if the file is in a subdirectory relative to the root.
Local Development
git clone https://github.com/gakimball/path-prefix
cd path-prefix
npm install
npm test
License
MIT © Geoff Kimball