jpls93
jpls93

Reputation: 614

Gulp - gulp-markdown-to-json type error

Given the examples from the READMEs of marked and markdown-it:

From markdown-it's README.md

var md = require('markdown-it')();
var result = md.render('# markdown-it rulezz!');

From marked's README.md

var marked = require('marked');
console.log(marked('I am using __markdown__.'));

I tried to create Gulp tasks to transform my .md files to .json. My preferred markdown parser is markdown-it but I'm having an error with the following configuration:

// Gulpfile.js
const gulp = require('gulp');
const md2json = require('gulp-markdown-to-json');
const md = require('marked');
const mdb = require('markdown-it')();

console.log(md('*markdown*')); // => Works
console.log(mdb.render('*markdown*')); // => Works

gulp.task('md', () => {
  return gulp.src('src/**/*.md')
    .pipe(md2json(md))
    .pipe(gulp.dest('dst'))
})
// => Success

gulp.task('it', () => {
  return gulp.src('src/**/*.md')
    .pipe(md2json(mdb.render))
    .pipe(gulp.dest('dst'))
})
// => Error
// UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError in plugin 'gulp-markdown-to-json'
// Message:
//     Cannot read property 'renderer' of undefined

Am I doing something wrong?

Upvotes: 0

Views: 111

Answers (1)

jpls93
jpls93

Reputation: 614

I think you've lost this context here:

.pipe(md2json(mdb.render))

Try

.pipe(md2json(mdb.render.bind(mdb))

-puzrin

Upvotes: 1

Related Questions