forked from popeindustries/inline-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (47 loc) · 1.23 KB
/
index.js
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
'use strict';
const { isFilepath } = require('./lib/utils');
const context = require('./lib/context');
const path = require('path');
const parse = require('./lib/parse');
const run = require('./lib/run');
/**
* Inline sources found in 'htmlpath'
* @param {String} htmlpath
* @param {Object} options
* - {String} attribute
* - {Boolean} compress
* - {Object} fs
* - {Array} handlers
* - {Array} ignore
* - {Boolean} pretty
* - {String} rootpath
* - {Boolean} swallowErrors
* - {Boolean} svgAsImage
* @returns {Promise<String>}
*/
exports.inlineSource = function inlineSource(htmlpath, options = {}) {
return new Promise(async (resolve, reject) => {
const ctx = context.create(options);
// Load html content
if (isFilepath(htmlpath)) {
ctx.htmlpath = path.resolve(htmlpath);
try {
ctx.html = ctx.fs.readFileSync(ctx.htmlpath, 'utf8');
} catch (err) {
return reject(err);
}
// Passed file content instead of path
} else {
ctx.html = htmlpath;
}
try {
await parse(ctx);
if (ctx.sources.length > 0) {
await run(ctx, ctx.sources, ctx.swallowErrors);
}
} catch (err) {
return reject(err);
}
resolve(ctx.html);
});
};