我用的是gulp-markdown-to-json和gulp-jade
我的目标是从标价文件中获取数据,如下所示:
---
template: index.jade
title: Europa
---
This is a test. 获取template: index.jade文件,并将其与其他变量一起传递给jade编译器。
到目前为止我有这样的想法:
gulp.task('docs', function() {
return gulp
.src('./src/docs/pages/*.md')
.pipe(md({
pedantic: true,
smartypants: true
}))
.pipe(jade({
jade: jade,
pretty: true
}))
.pipe(gulp.dest('./dist/docs'));
});我错过了一个步骤,在json从标价读取,玉石模板文件名输入到gulp.src之前,玉石编译器运行。
发布于 2016-04-08 15:04:02
gulp-jade是您的用例错误的gulp插件。
gulp-jade:
gulp.src('*.jade') .pipe(.).pipe({title:‘Some’,text:'Some‘})gulp-wrap:
gulp.src('*.md') .pipe(.).pipe({src:‘path/my/template.jj’})您的情况比较困难,因为您希望为每个.jade文件提供不同的.md模板。幸运的是,gulp-wrap接受了一个函数,它可以为流中的每个文件返回一个不同的模板:
var gulp = require('gulp');
var md = require('gulp-markdown-to-json');
var jade = require('jade');
var wrap = require('gulp-wrap');
var plumber = require('gulp-plumber');
var rename = require('gulp-rename');
var fs = require('fs');
gulp.task('docs', function() {
return gulp.src('./src/docs/pages/*.md')
.pipe(plumber()) // this just ensures that errors are logged
.pipe(md({ pedantic: true, smartypants: true }))
.pipe(wrap(function(data) {
// read correct jade template from disk
var template = 'src/docs/templates/' + data.contents.template;
return fs.readFileSync(template).toString();
}, {}, { engine: 'jade' }))
.pipe(rename({extname:'.html'}))
.pipe(gulp.dest('./dist/docs'));
});src/docs/pages/test.md
---
template: index.jade
title: Europa
---
This is a test. src/docs/templates/index.jade
doctype html
html(lang="en")
head
title=contents.title
body
h1=contents.title
div !{contents.body}dist/docs/test.html
<!DOCTYPE html><html lang="en"><head><title>Europa</title></head><body><h1>Europa</h1><div><p>This is a test. </p></div></body></html>发布于 2016-04-09 09:32:10
您不需要使用gulp-markdownto-json。如果有很多更好的解决方案。例如:
如何在我的个人博客中使用article-data。
https://stackoverflow.com/questions/36500547
复制相似问题