-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (130 loc) · 2.78 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const path = require('path');
const unified = require('unified');
const parse = require('rehype-parse');
module.exports = partials;
function partials(options = {}) {
let left = 0;
let tree;
let file;
let next;
const defaultOptions = {
max: 1,
cwd: '',
handle: null,
messages: true,
noresolve: false
};
const data = this.data('settings') || {};
const settings = Object.assign(
defaultOptions,
data,
options,
{fragment: true}
);
const processor = unified().use(parse, settings);
return transformer;
function transformer(_tree, _file, _next) {
tree = _tree;
file = _file;
next = _next;
/* istanbul ignore next */
try {
const {handle} = settings;
settings.handle = handle || require('fs').readFile;
} catch (error) {
return next(error);
}
return each(tree);
}
function each(node, parent, dirname = '', list = []) {
left += 1;
return handle(node, parent, dirname, list, () => {
if (!node.children) {
return done();
}
for (const child of node.children) {
each(child, node, dirname, [...list]);
}
return done();
});
}
function done() {
left -= 1;
if (left < 1) {
return next(null, tree, file);
}
}
function handle(node, parent, dirname, list, callback) {
if (node.type !== 'comment') {
return callback();
}
const current = handlePath(node, dirname);
if (!current) {
return callback();
}
list.push(current)
if (occurs(current, list) > settings.max) {
return callback()
}
return settings.handle(current, (error, data) => {
if (error) {
return handleError(error, node, callback);
}
const subtree = handlePartial(current, data);
const index = parent.children.indexOf(node);
parent.children.splice(
...[index, 1].concat(subtree.children)
);
each(subtree, node, path.dirname(current), list);
return callback();
});
}
function handleError(error, node, callback) {
if (settings.fatal) {
return next(error);
}
if (settings.messages) {
file.message(
error.message,
node.position,
'partials:' + error.code
);
}
return callback();
}
function handlePartial(path, data) {
const subfile = {
path,
messages: [],
contents: String(data).trim()
};
const subtree = processor()
.parse(subfile);
if (settings.messages) {
file.messages = file.messages.concat(subfile.messages);
}
return subtree;
}
function handlePath(node, dirname) {
const match = node.value.match(
/^\s*href=['"](.*)['"]\s*$/
);
if (!match) {
return;
}
let current;
if (settings.noresolve) {
current = match[1];
} else {
current = path.resolve(
settings.cwd,
dirname,
match[1]
);
}
return current;
}
function occurs(path, pathsList) {
return pathsList.filter(item => item === path).length;
}
}