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

grunt-writer

v0.1.2

Published

Convert markdowns to a static site

Downloads

10

Readme

grunt-writer

Convert markdowns and rendering using handlebars, with some custom recipes

The plugin doesn't play alone. If you are going to make a static site from markdown documents, try to use the awesome yeoman generator: generator-pencil that have full power you need when dealing with a website.

Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-writer --save-dev

One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-writer');

The "writer" task

In your project's Gruntfile, add a section named writer to the data object passed into grunt.initConfig().

grunt.initConfig({
  writer: {
    your_target: {
      options: {
        // Target-specific options go here.
      },
      files: {
          // Specify the files you want to convert
      }
    }
  },
})

Example Config:

grunt.initConfig({
  writer: {
    all: {
      files: [{
        expand: true,
        src: "**/*.md",
        dest: "dist",
        ext: ".html",
        cwd: "src/contents"
      }],
      options: {
        markdownOptions: {
          gfm: true,
          sanitize: false,
          breaks: true,
          smartypants: true,
          highlight: "auto",
          langPrefix: "hjs-"
        }
      }
    }
  },
})

Options

All options are optional

writerOptions

Handlebar rendering options.

  • writerOptions.datasource root for JSON data files. context.json will loaded into template context automatically.
  • writerOptions.viewsource view template root
  • writerOptions.partials root for view partials
  • writerOptions.helpers root for view helpers

markdownOptions

Options passed directly to marked

context

A hash that stores local variables available in templates

documentOptions

Options used to configure Document class. These options will affect all document class.

documentTypes

Register custom docuemnt types

Convert flow

  1. Scan all markdowns
  • Grab meta data embbed in markdown
  • Build the documents tree, so the builder have general view of the markdown collection
  1. Run the converter
  • Strip the meta section outside markdown documents
  • Convert markdown to html
  • Save the converted result to template context
  1. Render the template
  • Using Document class accroding to metadata field type (default to Document)
  • Find the handlebar template according to metadata field template (defaults to article)
  • Write out

Write a markdown

Markdown can have its own metadata

---
date: Tue May 13 2014 11:28:29 GMT+0800 (CST)
author: Robin
template: article
title: A Brand New Page
slug: a-brand-new-page
---
Awesome content

Hypen-sperated contents are formated in YAML. It won't exist in the final HTML docuemnts but avaiable in template rendering context as metadata object.

Special meta filed:

  • title: Page title
  • slug: page slug that used as part of url
  • date: creation time
  • template: template to render
  • type: document class
  • docOptions: option hash passed to document constructor

Document Class

Register a custom document class

In options of Gruntfile.js:

{
  "writer": {
    documentTypes: {
      "typeA": DocumentA,
      "typeB": DocumentB
    }
  }
}

DocumentA and DocumentB should be subclass of Docuemnt.

Sublcass Notes

write is the only method that matters.

Document itself that has many attributes that are helpful.

var Document = require("./document").init(grunt),
    assert = require("assert");
return Document.extend({
  write: function() {
    //TODO
  }
});

Attributes:

  • templateContext
    • tree
    • docs
      • metadata
      • src
      • ref
    • filepath
    • reference
    • metadata
    • other data loaded in context.json
  • file
    • dest
    • src
    • filename

tree and docs

tree is a document hierarchy tree:

{
  "@": doc1,
  folder1: {
    "@": doc2,
    "article1.md": doc3
  },
  folder2: {
    "@": doc3
  }
}

In the example above,

  • doc1, doc2, doc3 are isntances of Document class.
  • @ is alias to index.md

docs is a simple array that contains the Document instances that sorted by creation date of markdown underneath.

Release History

  • 2014-5-13 0.1.0 First opensourced version
  • 2014-5-13 0.1.1 Small fix to adapt to generator-pencil