-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (54 loc) · 2.75 KB
/
index.js
File metadata and controls
88 lines (54 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const _ = require('lodash');
const circularJSON = require('circular-json');
const htmlToText = require('html-to-text');
module.exports = function( options ) {
let outputPath = options.outputPath || null, // Specify a path (string) to put the json files eg. /api
createIndexes = options.createIndexes || false, // Specify whether or not to create indexes of the accumulated files
indexPaths = options.indexPaths || false, // Specify paths to source folders with content to index
onlyOutputIndex = options.onlyOutputIndex || false, // Specify if whether or not to only output the index file for each indexPath
stripHTML = options.stripHTML || false, // Specify whether or not to strip html tags from contents
stripHTMLOptions = options.stripHTMLOptions || {
tables: true,
baseElement: 'body',
ignoreImage: true,
ignoreHref: true
} // Specify options for html-to-text
return function( files, metalsmith, done ) {
_(files).forEach( function( file, key ) {
let data = _.omit(file, ['mode', 'stat', '_vinyl', 'stats']);
data.contents = file.contents.toString();
if (stripHTML)
data.contents = stripHTMLContents(data.contents, stripHTMLOptions);
data.contents = new Buffer( circularJSON.stringify( data ), 'utf8' );
let filepath = key.replace('.html', '.json');
files[filepath] = data;
delete files[key];
});
if( createIndexes ) {
if( ! indexPaths ) {
throw new Error('createIndexes is true, but no indexPaths are specified.');
}
_(indexPaths).forEach( function( value ) {
let output = {},
regex = new RegExp( value ),
filepath = value + '.json';
output.contents = [];
_(files).forEach(function( file, key ) {
if( regex.test( key ) ) {
output.contents.push( file.contents );
if( onlyOutputIndex ) {
delete files[key];
}
};
});
output.contents = new Buffer('[' + output.contents.toString() + ']', 'utf8');
files[filepath] = output;
});
}
done();
}
}
function stripHTMLContents(contents, stripHTMLOptions){
return htmlToText.fromString(contents, stripHTMLOptions);
}