Gulp Front Matter +Markdown through Nunjucks

前端 未结 2 1417
小鲜肉
小鲜肉 2021-01-05 17:02

I\'m working on adding some simple Markdown processing to my Gulp process, but I can\'t quite get the pieces to work together. I seem to be missing the step between getting

2条回答
  •  时光说笑
    2021-01-05 18:00

    You need gulp-wrap and original nunjucks.

    gulp-nunjucks is a tool for compiling the stream of nunjucks templates, but what you need to do is to wrap your contents in a nunjucks template and that is what gulp-wrap is for.

    Try npm install gulp-wrap nunjucks in addition to other settings and then the following should work.

    gulpfile

    var gulp = require('gulp')
    var wrap = require('gulp-wrap')
    var frontMatter = require('gulp-front-matter')
    var marked = require('gulp-marked')
    
    var fs = require('fs')
    
    gulp.task('pages:md', function() {
      gulp.src('./content/**/*.md')
        .pipe(frontMatter())
        .pipe(marked())
        .pipe(wrap(function (data) {
          return fs.readFileSync('path/to/layout/' + data.file.frontMatter.layout).toString()
        }, null, {engine: 'nunjucks'}))
        .pipe(gulp.dest('build/'))
    });
    

    markdown

    ---
    title: Title
    layout: layout.nunjucks
    nav_active: home
    ---
    
    ...markdown content...
    

    layout.nunjucks

    {{ file.frontMatter.title }}

    {{ contents }}

提交回复
热议问题