• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

treasonx/grunt-markdown: Markdown gruntjs task with code highlighting

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

treasonx/grunt-markdown

开源软件地址(OpenSource Url):

https://github.com/treasonx/grunt-markdown

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

grunt-markdown

Build Status

This grunt task takes a set of markdown files and converts them to HTML. It supports GFM with code highlighting. The code highlighting is done using highlight.js.

Getting Started

Install this grunt plugin next to your project's grunt.js gruntfile with:

npm install grunt-markdown --save-dev

Then add this line to your gruntfile:

grunt.loadNpmTasks('grunt-markdown');

Documentation

Creating a markdown task is simple. For the basic functionality add the following config in your gruntfile:

grunt.initConfig({
  markdown: {
    all: {
      files: [
        {
          expand: true,
          src: 'docs/src/*.md',
          dest: 'docs/html/',
          ext: '.html'
        }
      ]
    }
  }
});

Here is an example config using all of the options:

grunt.initConfig({
  markdown: {
    all: {
      files: [
        {
          expand: true,
          src: 'docs/src/*.md',
          dest: 'docs/html/',
          ext: '.html'
        }
      ],
      options: {
        template: 'myTemplate.jst',
        preCompile: function(src, context) {},
        postCompile: function(src, context) {},
        templateContext: {},
        contextBinder: false,
        contextBinderMark: '@@@',
        autoTemplate: true,
        autoTemplateFormat: 'jst',
        markdownOptions: {
          gfm: true,
          highlight: 'manual',
          codeLines: {
            before: '<span>',
            after: '</span>'
          }
        }
      }
    }
  }
});

These are the properties that the markdown task accepts:

  • files: This plugin supports use of the files API introduced in Grunt 0.4.0. Files may be specified using any one of the Compact Format, Files Objects Format, or Files Array Format (as in the above example).
  • options: options to be passed to the markdown parser
    • template: If you wish to specify your own html template, use the template option. Include the following line: <%=content%> where you want the compiled markdown inserted in your template
    • markdownOptions: Options passed directly to the markdown parser.
    • preCompile: is run before the markdown is compiled
    • postCompile: is run after the markdown has been compiled
    • templateContext: the default context for template expansion
    • contextBinder: this option is useful when we want to bind some parameters directly from markdown files. All data is stored in templateContext object.
    • contextBinderMark: with this option we can pass any marker between which we can grab your special parameters from markdown templates.
    • autoTemplate: if this option is set to true, script will search for template automatically. Template must be placed in this same catalog where markdown files are.
    • autoTemplateFormat: the template format when autoTemplate is true.

modifying content with preCompile and postCompile

Sometimes there is a need to modify the markdown content prior to compilation. This is most commonly used to augment the template context with meta data before expanding the html template.

preCompile

This function is run prior to the compilation of md to html. It has the following format:

  function(src, context) {
    //do stuff to src and context
    //optionally return the modified src
  }

postCompile

This function is run after the md has been converted to html. It has the following format:

  function(src, context) {
    //do stuff to src and context
    //optionally return the modified src
  }

templateContext

This object is used to expand your html template. Any data added to this object will be available in the template using the template syntax <%=myAttr%>.

This can also be a function which is expected to return a context object.

markdownOptions

Most markdown options are passed as-is to the marked markdown parser. The only option that is processed prior to compiling the markdown is the highlight option. If you specify 'auto' or 'manual' the task will handle highlighting code blocks for you using highlight.js. If you pass a custom function as the highlight option it will be used to highlight the code.

  • auto: Will try to detect the language
  • manual: will pass the language name from markdown to the highlight function
  • codeLines: specify text that should wrap code lines

contextBinder

Below you can see example how to use this option.

  markdown: {
    all: {
      files: [
        {
          expand: true,
          src: 'docs/src/*.md',
          dest: 'docs/html/',
          ext: '.html'
        }
      ],
      options: {
        template: 'myTemplate.jst',
        preCompile: function(src, context) {},
        postCompile: function(src, context) {},
        templateContext: {},
        contextBinder: true,
        contextBinderMark: '@@@',
        markdownOptions: {
          gfm: true,
          highlight: 'manual',
          codeLines: {
            before: '<span>',
            after: '</span>'
          }
        }
      }
    }
  }

Then inside markdown file we have to put: <!-- @@@key:value@@@ --> and it will be equal to:

templateContext: {
  key: 'value'
}

License

Copyright (c) 2012-2013 James Morrin Licensed under the MIT license.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap