Code Monkey home page Code Monkey logo

metalsmith-rootpath's Introduction

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;
  });
  

๐Ÿ˜Ž Now all the files and templates in your Metalsmith build have a rootPath variable assigned to them! I am using the handlebars template language in the examples below, but you can use your template language of choice, or access the rootPath value from the file's metadata 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 value 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

The 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

metalsmith-rootpath's People

Contributors

jameslnewell avatar radiovisual avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.