grunt-md-jst

0.1.0 • Public • Published

grunt-md-jst

Grunt plugin to compile Markdown files into JST modules.

Getting Started

This plugin requires Grunt ~0.4.5

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-md-jst --save-dev

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

grunt.loadNpmTasks('grunt-md-jst');

The "md_jst" task

Overview

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

grunt.initConfig({
  md_jst: {
    target: {
        options: {
            // Options here
        },
        files: {
          'dist/help.js': ['src/markdown/help.md']
        },
    }
 
  },
});

Options

options.separator

Type: String Default: linefeed + linefeed

Concatenated files will be joined on this string.

options.namespace

Type: String Default value: 'MD'

The namespace in which the precompiled templates will be assigned. Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping. When false with amd option set true, templates will be returned directly from the AMD wrapper.

options.processName

Type: function Default: null

This option accepts a function which takes one argument (the template filepath) and returns a string which will be used as the key for the precompiled template object. The example below stores all templates on the default JST namespace in capital letters.

options: {
  processName: function(filepath) {
    return filepath.toUpperCase();
  }
}

options.templateSettings

Type: Object Default: null

The settings passed to underscore when compiling templates.

jst: {
  compile: {
    options: {
      templateSettings: {
        interpolate : /\{\{(.+?)\}\}/g
      }
    },
    files: {
      "path/to/compiled/templates.js": ["path/to/source/**/*.html"]
    }
  }
}

options.amd

Type: boolean Default: false

Wraps the output file with an AMD define function and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly.

define(function() {
    //...//
    return this['[template namespace]'];
});

Example:

options: {
  amd: true
}

options.processContent

Type: function

This option accepts a function which takes one argument (the file content) and returns a string which will be used as template string. The example below strips whitespace characters from the beginning and the end of each line.

options: {
  processContent: function(src) {
    return src.replace(/(^\s+|\s+$)/gm, '');
  }
}

options.wrapper

Type: Object or String

A string or object representing the path(s) of the wrapper file. A wrapper is a jst file that can contain meta data from your markdown file. For more information on this option, see grunt-md, which this plugin is based on.

options.mmOptions

Type: Object

Hash of options to pass to the marky-mark compilation process.

Usage Examples

Default Options

This example will load a single markdown file into a compiled jst to be loaded by the browser

md_jst: {
  compile: {
    files: {
      "path/to/compiled/templates.js": ["path/to/source/one.md"]
    }
  }
}

Multiple

This example compiles multiple files into a single file. Note that the normal file composition rules for grunt apply.

md_jst: {
  compile: {
    files: {
      "path/to/compiled/templates.js": ["path/to/source/one.md", "path/to/source/another.md"]
    }
  }
}

Template Settings

This example overrides the interpolate setting of lodash's template function.

md_jst: {
  compile: {
    options: {
      templateSettings: {
        interpolate : /\{\{(.+?)\}\}/g
      }
    },
    files: {
      "path/to/compiled/templates.js": ["path/to/source/**/*.html"]
    }
  }
}

Wrapper

This example uses a wrapper to put around the generated html

jst: {
  compile: {
    options: {
      templateSettings: {
        interpolate : /\{\{(.+?)\}\}/g
      }
    },
    files: {
      "path/to/compiled/templates.js": ["path/to/source/**/*.html"]
    }
  }
}

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

v0.1.0 Initial release

Readme

Keywords

Package Sidebar

Install

npm i grunt-md-jst

Weekly Downloads

1

Version

0.1.0

License

none

Last publish

Collaborators

  • jbaldwin