forked from OctoLinker/OctoLinker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (58 loc) · 1.48 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
import Blob from './blob';
import {
getBlobWrapper,
getPath,
readLines,
getParentSha,
isGist,
} from './helper';
function parseBlob(el) {
const path = getPath(el);
const lines = readLines(el);
if (!lines.length) {
return;
}
// Does not work if left side is empty (eg new files) https://github.com/OctoLinker/OctoLinker/commit/b97dfbfdbf3dee5f4836426e6dac6d6f473461db?diff=split#diff-e56633f72ecc521128b3db6586074d2c
const isDiff =
lines.filter(({ side }) => ['left', 'right'].includes(side)).length > 0;
if (isDiff) {
const diffLineFilter =
(type) =>
({ side, ...rest }) => {
if ([type, 'context'].includes(side)) {
return { ...rest };
}
};
const leftBlob = new Blob({
el,
path,
lines: lines.map(diffLineFilter('left')).filter(Boolean),
branch: getParentSha(),
type: 'diffLeft',
});
const rightBlob = new Blob({
el,
path,
lines: lines.map(diffLineFilter('right')).filter(Boolean),
type: 'diffRight',
});
return [leftBlob, rightBlob];
}
let type = isGist() ? 'gist' : 'full';
if (el.getElementsByTagName('pre').length) {
type = 'snippet';
}
return new Blob({ el, path, lines, type });
}
export default class BlobReader {
hasBlobs() {
return !!getBlobWrapper(document).length;
}
read(rootElement) {
return [].concat(
...getBlobWrapper(rootElement)
.map((el) => parseBlob(el))
.filter(Boolean),
);
}
}