My gulp file produces sets of html files using Nunjucks. I want to use those files to output some html to text files. I found an NPM that turns html file to text but I"m not 100% sure how or if I can use this inside a gulp task. The module is html-to-text and I'm trying to use it like this:
var html2text = require('html-to-text');
gulp.task('html2text',function(){
return gulp.src('build/*.html')
.pipe(html2text.fromFile())
.pipe(gulp.dest('build/txt'))
});
Obviously it's complaining about not getting a file to translate to txt. I feel like this should be doable but I'm not super well versed with gulpfiles yet.
var gulp = require('gulp');
var glob = require('glob-fs')({ gitignore: true });
var html2text = require('html-to-text');
var fs = require('fs');
var path = require('path');
gulp.task('default', function () {
glob.readdirStream('build/*.html')
.on('data', function (file) {
console.log(file.path);
html2text.fromFile(file.path, (err, text) => {
if (err) return console.error(err);
// strip off extension and parent directories
var filename = path.basename(file.path, '.html');
// I assume you want a.html text to be written to 'build/txt/a.txt'
fs.writeFileSync(path.join('build/txt', filename + '.txt'), text, 'utf8');
});
});
});
You don't have to use gulp.src and gulp.dest and work with streams in a gulp task if you don't want to. The above task globs your files one-by-one and operates on them individually, sending each file name to the html2text function.
See the glob-fs package.