-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
94 lines (91 loc) · 3.35 KB
/
utils.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
function groupBy(coll, context) {
return coll.reduce(function (memo, item) {
var value = context(item);
if (memo[value] === undefined) {
memo[value] = [];
}
memo[value].push(item);
return memo;
}, {});
}
function getRealOffset(content, offset) {
var regexps = [[/(<[^>]*>)/g, 0], [/(&\w+;)/g, 1]];
var positions = regexps.reduce(function (memo, item) {
var regexp = item[0];
var regexpLength = item[1];
var matches = content.match(regexp);
var matchPos = 0;
for (var matchIndex in (matches || [])) {
if (matches.hasOwnProperty(matchIndex)) {
var match = matches[matchIndex];
var matchOffset = content.substr(matchPos).search(regexp);
memo.push([matchPos + matchOffset, matchPos + matchOffset + match.length, regexpLength]);
matchPos += matchOffset + match.length;
}
}
return memo;
}, []).sort(function (a, b) { return a[0] - b[0]; });
var realOffset = offset;
while (positions.length > 0 && realOffset > positions[0][0]) {
var position = positions.shift();
var length = position[1] - position[0] - position[2];
realOffset += length;
}
return realOffset;
}
function groupEntitiesByLinesAndTypes(allEntities) {
var result = {};
for (var type in allEntities) {
var entities = allEntities[type];
for (var i in entities) {
var entity = JSON.parse(JSON.stringify(entities[i]));
entity.type = type;
var line = parseInt(entity.line, 10);
result[line] = result[line] || [];
result[line].push(entity);
}
}
return result;
}
function applyEntities(github, ref, content, entities, hrefCallback) {
if (content.indexOf("crosshub-link") === -1) {
var newLineContent = "";
var lastStop = 0;
for (var index in entities) {
var entity = entities[index];
var realOffset = getRealOffset(content, entity.offset);
if (realOffset >= lastStop) {
newLineContent += content.substr(lastStop, realOffset - lastStop);
if (entity.type == "references") {
var href = hrefCallback(entity);
var isInternal = href.match(/^#/) || href.match(new RegExp(location.pathname));
var cssClass = "crosshub-link" + (!isInternal ? ' crosshub-link__external' : '');
newLineContent += "<a href='" + href + "' class='" + cssClass + "'>";
} else if (entity.type == "declarations") {
var references = JSON.stringify(
entity.references.map(function (ref) {
return {name: ref.remotePath, remotePath: hrefCallback(ref)};
})
);
newLineContent += "<span class='crosshub-declaration' data-references='" + references + "' data-ref='" + ref + "'>";
}
var end = entity.offset + entity.length;
var realEnd = getRealOffset(content, end);
newLineContent += content.substr(realOffset, realEnd - realOffset);
if (entity.type == "references") {
newLineContent += "</a>";
} else if (entity.type == "declarations") {
newLineContent += "</span>";
}
lastStop = realEnd;
}
}
var lastEntity = entities[entities.length - 1];
var lastEnd = lastEntity.offset + lastEntity.length;
var lastRealEnd = getRealOffset(content, lastEnd);
newLineContent += content.substr(lastRealEnd);
return newLineContent;
} else {
return content;
}
}