0

Given the following input files in src/, how do I output the files on the dist/ folder?

Project
+-- src
|   a.md
|   b.md
+-- dest
|   a/index.html
|   b/index.html

// Gulpfile.js
const gulp = require('gulp');
const md = require('gulp-markdown');

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

I think this is not a duplicate of Gulp.js - Use path from gulp.src() in gulp.dest(). That question deals with variable directory names and variable filenames. This question deals with variable directory names but with a constant filename.

Community
  • 1
  • 1
jpls93
  • 614
  • 1
  • 7
  • 19

2 Answers2

3

Try using the gulp-rename plugin https://www.npmjs.com/package/gulp-rename

You could use a function for name mapping:

const rename = require('gulp-rename');

...

gulp.task('md', () => {
  return gulp.src('src/**/*.md')
    .pipe(md())
    .pipe(rename((path) => {
      path.dirname += "/" + path.basename;
      path.basename = "index";
    })
    .pipe(gulp.dest('dest'))
})
Ananth Rao
  • 1,232
  • 1
  • 9
  • 19
0

Using gulp-if:

Run

npm install gulp-if --save-dev

Your task

// Gulpfile.js
const gulp = require('gulp');
const md = require('gulp-markdown');
const gulpif = require('gulp-if');

gulp.task('md', () => {
  return gulp.src('src/**/*.md')
    .pipe(md())
    .pipe(gulpif('a.md', gulp.dest('dest/a')))
    .pipe(gulpif('b.md', gulp.dest('dest/b')));
})

Alternative

Using the else part of gulp-if, shorter but less readable

gulp.task('md', () => {
  return gulp.src('src/**/*.md')
    .pipe(md())
    .pipe(gulpif('a.md', gulp.dest('dest/a'), gulpif('b.md', gulp.dest('dest/b'))));
})
Wilmer SH
  • 1,417
  • 12
  • 20
  • I'm sorry. this is wrong. This will output +-- dest | a.html | b.html instead of +-- dest | a/index.html | b/index.html – jpls93 Mar 10 '17 at 15:03
  • @JohnPatrick, I misunderstood the question. I think you can just use gulp-if for conditional destinations. I will change the answer to reflect an example. – Wilmer SH Mar 10 '17 at 15:07