-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
loader.js
181 lines (153 loc) · 5.08 KB
/
loader.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
170
171
172
173
174
175
176
177
178
179
180
181
import { fetchTree } from '@octolinker/helper-github-api';
import { bulkAction } from '@octolinker/helper-octolinker-api';
import uniqWith from 'lodash.uniqwith';
import isEqual from 'lodash.isequal';
function getRepoMetadata(data) {
// Find first internal links which contains the information we need
// TODO handle also githubSearch resolver results. Right now this works,
// because a relative file resolver is used as well in the less and sass plugin
return data.find(({ type }) => type === 'internal-link') || {};
}
function removeDuplicates(payload) {
return uniqWith(payload, (left, right) =>
// The link property is part of the item and prevents us from using a
// simple isEqual operation. By concatenating all other props we can easily
// identify duplications
isEqual(
left.type + left.registry + left.target,
right.type + right.registry + right.target,
),
);
}
function injectLiveDemoUrl(url) {
return url.replace('https://github.com', process.env.OCTOLINKER_LIVE_DEMO);
}
function groupMatchesByType(matches) {
const flattenUrls = [].concat(
...matches.map((match) =>
match.urls.map((url) => ({
...url,
link: match.link,
})),
),
);
const apiItems = removeDuplicates(flattenUrls).filter(({ type }) =>
['registry', 'ping'].includes(type),
);
const internalItems = flattenUrls.filter(({ type }) =>
['internal-link', 'github-search'].includes(type),
);
const trustedItems = flattenUrls.filter(({ type }) =>
['trusted-url'].includes(type),
);
return {
apiItems,
internalItems,
trustedItems,
};
}
function insertLinks({
matches,
githubTree,
octolinkerApiResponse,
user,
repo,
branch,
}) {
matches.forEach(({ urls, link }) => {
if (link.href) {
// Return early if link is already set
return;
}
for (const item of urls) {
if (item.type === 'internal-link') {
if (githubTree.includes(item.path.replace(/%20/g, ' '))) {
link.href = item.url;
if (process.env.OCTOLINKER_LIVE_DEMO) {
link.href = injectLiveDemoUrl(link.href);
}
break;
}
} else if (item.type === 'github-search') {
const allMatches = githubTree.filter((path) =>
path.endsWith(item.target),
);
if (allMatches.length === 1) {
link.href = `https://github.com/${user}/${repo}/blob/${branch}/${allMatches[0]}`;
if (process.env.OCTOLINKER_LIVE_DEMO) {
link.href = injectLiveDemoUrl(link.href);
}
break;
}
// TODO implement https://www.npmjs.com/package/fast-levenshtein
} else if (['registry', 'ping'].includes(item.type)) {
const finalUrl = octolinkerApiResponse.find(
({ type, target }) =>
(type === item.registry || type === item.type) &&
target === item.target,
);
if (finalUrl && finalUrl.result) {
link.href = finalUrl.result;
if (finalUrl.result.startsWith('https://github.com')) {
[, , , user, repo] = finalUrl.result.split('/');
link.dataset.repositoryHovercardsEnabled = true;
link.dataset.hovercardType = 'repository';
link.dataset.hovercardUrl = `/${user}/${repo}/hovercard`;
}
if (process.env.OCTOLINKER_LIVE_DEMO) {
link.href = injectLiveDemoUrl(link.href);
}
break;
}
}
}
});
}
export default async function (matches) {
const { apiItems, internalItems, trustedItems } = groupMatchesByType(matches);
let octolinkerApiResponsePromise = [];
let githubTreePromise = [];
if (apiItems.length) {
octolinkerApiResponsePromise = bulkAction(apiItems);
}
let user;
let repo;
let branch;
if (internalItems.length) {
({ user, repo, branch } = getRepoMetadata(internalItems));
if (user && repo && branch) {
githubTreePromise = fetchTree({ user, repo, branch });
}
}
trustedItems.forEach((item) => {
item.link.href = item.target;
});
const octolinkerApiResponse = await octolinkerApiResponsePromise;
const githubTree = await githubTreePromise;
// TODO explore resolving on the fly so we don't have to wait for both calls to finish
insertLinks({
matches,
octolinkerApiResponse,
githubTree,
user,
repo,
branch,
});
if (process.env.OCTOLINKER_LIVE_DEMO) {
let parentElement = document;
const activeLineNumber = window.location.hash.match(/#LO([0-9]+)/);
if (activeLineNumber && activeLineNumber[1]) {
parentElement = document.getElementById(`LC${activeLineNumber[1]}`);
}
const el = parentElement.querySelector('.octolinker-link[href]');
if (el) {
el.classList.add('octospotlight');
const container = document.createElement('div');
container.innerHTML = `<div class="octospotlight-inner">${el.innerHTML}</div>`;
el.innerHTML = container.innerHTML;
const spot = document.createElement('div');
spot.classList.add('octospotlight-dot');
el.querySelector('.octospotlight-inner').appendChild(spot);
}
}
}