44 lines
893 B
JavaScript
44 lines
893 B
JavaScript
module.exports = function parse(text, matches) {
|
|
var result = [];
|
|
|
|
if (matches.length === 0) {
|
|
result.push({
|
|
text: text,
|
|
highlight: false
|
|
});
|
|
} else {
|
|
if (matches[0][0] > 0) {
|
|
result.push({
|
|
text: text.slice(0, matches[0][0]),
|
|
highlight: false
|
|
});
|
|
}
|
|
}
|
|
|
|
matches.forEach(function(match, i) {
|
|
var startIndex = match[0];
|
|
var endIndex = match[1];
|
|
|
|
result.push({
|
|
text: text.slice(startIndex, endIndex),
|
|
highlight: true
|
|
});
|
|
|
|
if (i === matches.length - 1) {
|
|
if (endIndex < text.length) {
|
|
result.push({
|
|
text: text.slice(endIndex, text.length),
|
|
highlight: false
|
|
});
|
|
}
|
|
} else if (endIndex < matches[i + 1][0]) {
|
|
result.push({
|
|
text: text.slice(endIndex, matches[i + 1][0]),
|
|
highlight: false
|
|
});
|
|
}
|
|
});
|
|
|
|
return result;
|
|
};
|