问题
I'm hoping to do something like this:
gulp.src('src/*.html')
.pipe(fm ({
property: 'fm'
}))
.pipe(wrap({
src: fm['template']
}))
where template
is a value in the front-matter. I can create a separate function where I pass the value using gulp-tap but I was hoping to keep it
"simple". I don't think gulp-wrap
supports accessing front-matter vars.
回答1:
This seems to be one solution but it starts to fail as I add more complexity, for instance with gulp-swig.
The source file contains:
---
template: full
---
Text content is here.
and the script:
var frontMatter = require('gulp-front-matter'),
gulp = require('gulp'),
tap = require('gulp-tap'),
wrap = require('gulp-wrap')
gulp.task('default', function() {
gulp.src('src/*.html')
.pipe(frontMatter())
.pipe(tap(function(file) {
gulp.src(file.path)
.pipe(frontMatter ({
remove: true
}))
.pipe(wrap({
src: file.frontMatter['template'] + '.tpl' // ==> full.tpl
}))
.pipe(gulp.dest('build/'))
}))
});
回答2:
You need to pass a function as the first argument of gulp-wrap
.
The document of gulp-wrap
says
When a Function then the function will be called and should return the template content. This function get the data object as first parameter.
Something like the following should work.
var gulp = require('gulp')
var fm = require('gulp-front-matter')
var wrap = require('gulp-wrap')
var fs = require('fs')
gulp.src('src/*.html')
.pipe(fm({property: 'fm'}))
.pipe(wrap(function (data) {
return fs.readFileSync('path/to/layout_dir/' + data.file.fm.template + '.tpl').toString()
}))
.pipe(gulp.dest('build/'))
See also: Gulp Front Matter +Markdown through Nunjucks
来源:https://stackoverflow.com/questions/29788001/passing-gulp-front-matter-value-to-gulp-wrap