nunjucks-markdown

2018-11-10 admin

nunjucks-markdown是什么

什么是nunjucks-markdown,A markdown extension for nunjucks

nunjucks-markdown使用教程帮助文档

nunjucks-markdown Build Status

Greenkeeper badge

A nunjuck extension that adds a markdown tag. This plugin allows you to choose your own markdown renderer.

Install

npm install nunjucks-markdown --save

Usage

Register the extension with nunjucks

var nunjucks = require('nunjucks'),
    markdown = require('nunjucks-markdown'),
    marked = require('marked');

var env = nunjucks.configure('views');

// The second argument can be any function that renders markdown
markdown.register(env, marked);

Add markdown to your templates

{% markdown %}
Hello World
===========
# Do stuff
{% endmarkdown %}

You can also provide the markdown tag with a template to render

{% markdown "post.md" %}

Note: This method doesn’t require a closing tag

As you would expect, you can add tags inside your markdown tag

{% markdown %}
{% include 'post1.md' %}
{% include 'post2.md' %}
{% endmarkdown %}

Using with Gulp

Gulp requires a little more explicit settings than standard npm. See https://mozilla.github.io/nunjucks/api.html#custom-tags

see example-gulpefile.js based on https://gist.github.com/kerryhatcher/1382950af52f3082ecdc668bba5aa11b

var nunjucks = require('nunjucks'),
    markdown = require('nunjucks-markdown'),
    marked = require('marked'),
    gulpnunjucks = require('gulp-nunjucks');

var templates = 'src/templates'; //Set this as the folder that contains your nunjuck files

var env = new nunjucks.Environment(new nunjucks.FileSystemLoader(templates));

// The second argument can be any function that renders markdown
markdown.register(env, marked);

gulp.task('pages', function() {
    // Gets .html files. see file layout at bottom
    return gulp.src([templates + '/*.html', templates + '/**/*.html'])
        // Renders template with nunjucks and marked
        .pipe(gulpnunjucks.compile("", {env: env}))
        // output files in dist folder
        .pipe(gulp.dest(dist))
});

Markdown Options

Nunjucks-markdown doesn’t require you to use any particular markdown renderer. If you were to use marked here’s a good example of how it could be configured.

var marked = require('marked');

marked.setOptions({
  renderer: new marked.Renderer(),
  gfm: true,
  tables: true,
  breaks: false,
  pendantic: false,
  sanitize: true,
  smartLists: true,
  smartypants: false
});

markdown.register(env, marked);

For more information configuration options, checkout marked.

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-45042.html

文章标题:nunjucks-markdown

回到顶部