npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

metalsmith-rootpath

v1.0.4

Published

Easily find the relative path to the root directory in your Metalsmith templates

Downloads

489

Readme

metalsmith-rootpath

Easily find the relative path to the root directory in your Metalsmith templates.

Makes relative-to-root links a breeze!

Build Status

Note: metalsmith-rootpath must run after any plugins that move files around, or create new files in your metalsmith build chain (like metalsmith-permalinks, for example). This is because metalsmith-rootpath needs to know what your final output directory will look like before it can assign a correct rootPath value.

Install

$ npm install --save metalsmith-rootpath

Usage

var rootPath = require('metalsmith-rootpath');

Metalsmith(__dirname)
  .use(rootPath())
  .build(function(err) {
    if (err) throw err;
  });
  

:sunglasses: Now all the files and templates in your Metalsmith build have a rootPath variable assigned to them! I am using the handlebars template in the examples below, but you can use your template language of choice, or access the rootPath value from the file's metadata in anyway you like.

Examples

Let's assume you have a directory structure like this:

.
├── index.html
│
├─┬ dir1/
│ └── index.html
│  
├─┬ dir2/ 
│ └─┬ foo/
│   └── index.html
│
└─┬ dir3/ 
  └─┬ foo/
    └─┬ bar/
      └─┬ baz/
        └── index.html

The rootPath values in each index.html file would be:

| File | rootPath | | :----------------------------------|:------------------| | index.html | "" | | dir1/index.html | "../" | | dir2/foo/index.html | "../../" |
| dir3/foo/bar/baz/index.html | "../../../../" |

Relative Links

Use the rootPath variable anywhere you want to grab static files relative to your directory. rootPath will find the root folder no matter how many levels deep your templates are.

For example, if the following line of markup were placed in dir3/foo/bar/baz/index.html:

<link src="{{rootPath}}css/main.css" type="text/css" />

It would result in the following output:

<link src="../../../../css/main.css" type="text/css" />

Relative Navigation

This rootPath variable is useful when building a relative navigation structure, for example, in your Handlebars template partials/navigation.hbs

<ul>
    <li><a href="{{rootPath}}about/">About Us</a></li>
    <li><a href="{{rootPath}}awesome-page/">Awesome Page</a></li>
    <li><a href="{{rootPath}}contact/">Contact Us</a></li>
</ul>

License

MIT @ Michael Wuergler