首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于带Gulp的Handlebar模板编译分词和标记

基于带Gulp的Handlebar模板编译分词和标记
EN

Stack Overflow用户
提问于 2018-08-22 03:14:38
回答 1查看 974关注 0票数 6

我在这里有一个名为tpage.hbs的Handlebar模板

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Title</title>
  {{> head}}
</head>
<body>
  {{> home-header}}
  {{{ mdcontents }}}
</body>
</html>

headhome-header是偏导数。

我有一个Markdown文件的文件夹,我想基于这个模板制作超文本标记语言页面,在模板中mdcontents所在的位置添加.md文件。

我有以下Gulpfile:

代码语言:javascript
复制
var gulp = require('gulp');

var handlebars = require('gulp-compile-handlebars');
var HB = require('Handlebars'); // I know I don't need two Handlebars imports, I'm just trying different things at this point
var uglify = require('gulp-uglify');
var markdown = require('gulp-markdown');
var tap = require('gulp-tap');

// ...

gulp.task('markhb', function() {
  return gulp.src('./app/templates/tpage.hbs')
    .pipe(handlebars({}, {
      ignorePartials: false,
      batch: ['./app/templates/partials'] /* my partials directory */
    }))
    .pipe(tap(function(file) {
      var template = HB.compile(file.contents.toString());
      console.log(file.contents.toString());

      return gulp.src('./app/content/mdpages/**.md')
        .pipe(markdown())
        .pipe(tap(function(file) {
          var data = {
            mdcontents: file.contents.toString()
          };

          var html = template(data);

          file.contents = new Buffer(html, 'utf-8'); // Buffer() is deprecated, I'll fix that later
        }))
        .pipe(rename({
          extname: '.html'
        }))
        .pipe(gulp.dest('build/pages'));
    }));
});

所以,我的问题是,如果我保留第一个pipe调用,它会很好地加载分数,但它会忽略并完全删除mdcontents。但是,如果我删除第一个pipe调用,它会很好地呈现标记,但会去掉部分。我也不喜欢我现在使用的两个Handlebar库,但在这一点上,我只想要一个工作模板,然后再进行清理。我需要添加、删除或更改什么才能同时呈现部分参数和标记?

EN

回答 1

Stack Overflow用户

发布于 2018-08-22 07:27:54

使用Handlebar markdown中间件,就像这样的https://github.com/helpers/helper-markdown

然后,我们的想法是使用带helper-markdown的Handlebar来包含该文件作为过滤器,而不是让gulp为您进行处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51955282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档