forked from OctoLinker/OctoLinker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
216 lines (177 loc) · 4.87 KB
/
helper.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function $(selector, rootElement = document) {
return rootElement.querySelector(selector);
}
function $$(selector, rootElement = document) {
return [...rootElement.querySelectorAll(selector)];
}
function getBlobCodeInner(el) {
if ($$('.react-blob-row').length) {
return $$('.react-blob-row', el);
}
return $$('.blob-code-inner', el);
}
function getBlobWrapper(rootElement) {
return $$(
`
[data-selector="repos-split-pane-content"] table,
.blob-wrapper,
.js-blob-wrapper,
[class*="highlight-source-"]
`,
rootElement,
);
}
function mergeRepoAndFilePath(repoPath, filePath) {
const repoUrl = repoPath
.trim()
.split('#')[0]
.replace(/pull\/[0-9]+\/files/, 'blob')
.split('..')[0];
return `${repoUrl}/${filePath.trim()}`;
}
function isGist() {
return !!$('#gist-pjax-container');
}
function getParentSha() {
// Pull request diff view
const input = $('[name="comparison_start_oid"]');
if (input && input.value) {
return input.value;
}
// Pull request diff for unauthenticated users
const url = $('.js-load-contents');
if (url && url.dataset.contentsUrl) {
return url.dataset.contentsUrl.match(/base_sha=([0-9a-z]+)/)[1];
}
// Commit diff view
const el = $('.sha-block .sha[data-hotkey]');
return el ? el.textContent : null;
}
function getPath(el) {
// New code view experience
if ($('react-app')?.attributes['initial-path']?.value) {
return $('react-app').attributes['initial-path'].value;
}
// When current page is a diff view get path from "View" button
let rootSelector =
el.parentElement.parentElement.querySelectorAll('.file-actions a');
// When current diff blob is loaded on-demand get path from "View" button
if (!rootSelector.length) {
rootSelector =
el.parentElement.parentElement.parentElement.querySelectorAll(
'.file-actions a',
);
}
let ret = [...rootSelector]
.find((element) => element.textContent.trim() === 'View file')
?.getAttribute('href');
if (!ret) {
ret = $('.js-permalink-shortcut')?.getAttribute('href');
}
// When current page is a gist, get path from blob name
if (isGist()) {
ret = $('.gist-blob-name', el.parentElement)?.textContent.trim();
if (ret && !ret.startsWith('/')) {
return `/${ret}`;
}
}
// when page has pull request comment(s)
const fileHeader = $('summary', el.parentElement.parentElement);
if (!ret && fileHeader) {
const filePath = $('a', fileHeader)?.textContent;
const repoPath = $('a', fileHeader)?.getAttribute('href');
ret = mergeRepoAndFilePath(repoPath, filePath);
}
return ret ? ret.trim() : undefined;
}
function getLineNumber(el) {
// blob view
if (el.id) {
const result = /^LC?([0-9]+)$/.exec(el.id);
if (result && result[1]) {
return parseInt(result[1], 10);
}
}
// split diff view
let lineNumber = el
.closest('td')
?.previousElementSibling?.getAttribute('data-line-number');
// unified diff view
if (!lineNumber) {
lineNumber = el
.closest('tr')
?.querySelector('td')
?.getAttribute('data-line-number');
}
lineNumber = Number.parseInt(lineNumber, 10);
return Number.isNaN(lineNumber) ? null : lineNumber;
}
function diffMetaInformation(el) {
// New code view experience
if (el.classList.contains('react-blob-row')) {
return {};
}
const td = el.closest('td');
// Blob view
if (td.id.startsWith('LC') || isGist()) {
return {};
}
let side;
// split diff
if (td.cellIndex === 1) {
side = 'left';
} else if (td.cellIndex === 3) {
side = 'right';
}
// unified diff
if (!side && td.cellIndex === 2) {
if (td.classList.contains('blob-code-addition')) {
side = 'right';
} else if (td.classList.contains('blob-code-deletion')) {
side = 'left';
} else if (td.classList.contains('blob-code-context')) {
side = 'context';
}
}
// Diff view
return {
side,
};
}
function readLine(el) {
const lineNumber = getLineNumber(el);
if (!lineNumber) {
return null;
}
// Ignore suggested code changes
if (el.closest('.js-suggested-changes-blob')) {
return null;
}
// Each array element represents a single line.
// Therefore we can get ride of the newline here.
const ret = {
value: el.textContent.replace(/\n/, ''),
lineNumber,
...diffMetaInformation(el),
};
return ret;
}
function readLines(el) {
if (
el.classList.contains('highlight') &&
el.firstElementChild &&
el.firstElementChild.nodeName === 'PRE'
) {
const issueCode = el.getElementsByTagName('pre');
if (issueCode.length) {
return issueCode[0].textContent.split(/\n/).map((line, index) => ({
value: line,
lineNumber: index + 1,
}));
}
}
return getBlobCodeInner(el)
.map(readLine)
.filter((line) => !!line);
}
export { getPath, getBlobWrapper, readLines, getParentSha, isGist };