sparkartgroup / gulp-markdown-to-json

Parse Markdown and YAML ā†’ compile Markdown to HTML ā†’ wrap it all up in JSON
MIT License
75 stars 14 forks source link

Possible to generate 2 different outputs? #19

Closed kyooriouskoala closed 7 years ago

kyooriouskoala commented 7 years ago

Hi! I'm generating html file with PUG via markdown json:

---
template: index.pug
title: Europa
---
This is a test. 

marked.setOptions({
  pedantic: true,
  smartypants: true
});

gulp.task('docs', function() {
  return gulp.src('./src/docs/pages/*.md')
    .pipe($.plumber()) // this just ensures that errors are logged
    .pipe($.markdownToJson(marked))
    .pipe($.wrap(function(data) {
      // read correct pug template from disk
      var template = 'src/docs/templates/' + data.contents.template;
      return fs.readFileSync(template).toString();
    }, {}, { engine: 'pug' }))
    .pipe($.rename({extname:'.html'}))
    .pipe(gulp.dest('./dist/docs'))
});

Then what I'd like to do next is to generate a consolidated JSON output with 'body' key removed for navigation structure purpose.

gulp.task('nav-structure', function() {
  return gulp.src('./src/patterns/**/*.md')
    .pipe($.plumber()) 
    .pipe($.util.buffer())
    .pipe($.markdownToJson(marked, 'patterns.json', (data, file) => {
      delete data.body;
      data.path = file.path;
      return data;
    }))
    .pipe(gulp.dest('./src/docs/json'));
});

The consolidated JSON output is what I expected but it strips off the 'body' from the generated HTML pages as well.

pushred commented 7 years ago

Hmm to clarify: are you saying that the use of the second consolidation task is impacting the result of the first, when both are run concurrently?

If so try transforming/returning a clone of data like this:

markdownToJson(marked, 'patterns.json', (data, file) => {
  var navItem = Object.assign({}, data);
  delete navItem.body;
  navItem.path = file.path;
  return navItem;
})

My example is probably bad encouraging mutating the Vinyl object, was not expecting the possibility of an instance to be shared across separate tasks..

kyooriouskoala commented 7 years ago

Hmm to clarify: are you saying that the use of the second consolidation task is impacting the result of the first, when both are run concurrently?

Yup!

I attempted to "clone" the data but it wasn't working; must have done it wrong. Tried your solution and unfortunately it doesn't seem to be working either = /

I'm open to try new solutions if you have any other. In the mean time, I'll just use an extra gulp plugin for the navigation structure.

Thank you!

levsthings commented 7 years ago

You can try run-sequence for running the tasks sequentially.

pushred commented 7 years ago

@kyooriouskoala think I found the culprit, thanks for reporting this! Please confirm my fix addresses your issue:

npm install sparkartgroup/gulp-markdown-to-json#fix-cached-plugin-config
kyooriouskoala commented 7 years ago

@pushred : Yay! It works! šŸ‘

pushred commented 7 years ago

Thanks for testing! Released as 1.0.2