forked from digisfera/useref
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtransformReferences.js
37 lines (30 loc) · 1.07 KB
/
transformReferences.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
const resources = require('./resources');
const refManager = require('./refManager');
module.exports = (blocks, content, options) => {
let replaced = content;
const refm = Object.create(refManager);
// Determine the linefeed from the content
const linefeed = /\r\n/g.test(content) ? '\r\n' : '\n';
// handle blocks
Object.keys(blocks).forEach(key => {
const block = blocks[key].join(linefeed);
const lines = block.split(linefeed);
const indent = (lines[0].match(/^\s*/) || [])[0];
const ccmatches = block.match(resources.regcc);
const blockContent = lines.slice(1, -1).join(linefeed);
let ref = refm.getRef(block, blockContent, options);
if (ref !== null) {
ref = indent + ref;
// Reserve IE conditional comment if exist
if (ccmatches) {
ref = indent + ccmatches[1] + linefeed + ref + linefeed + indent + ccmatches[2];
}
if (options.noconcat) {
replaced = replaced.replace(block, blockContent);
} else {
replaced = replaced.replace(block, ref);
}
}
});
return replaced;
};