-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnotes.ts
More file actions
74 lines (66 loc) · 2.06 KB
/
notes.ts
File metadata and controls
74 lines (66 loc) · 2.06 KB
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
import type { INoteV3, INotesEnvelopeV3, Link } from "@fluffylabs/links-metadata";
import type { Report } from "./report";
export function generateNotes(report: Report, label?: string): INotesEnvelopeV3 {
const perLink = new Map<string, { file: string; link: Link }[]>();
const commonPathComponents: Map<string, number>[] = [];
for (const [file, links] of report.detected) {
const parts = file.split("/");
for (let i = 0; i < parts.length; i++) {
commonPathComponents[i] = commonPathComponents[i] || new Map();
commonPathComponents[i].set(parts[i], (commonPathComponents[i].get(parts[i]) || 0) + 1);
}
for (const link of links) {
// consolidate urls to the newest version
const url = link.updated || link.url;
const list = perLink.get(url) ?? [];
list.push({
file,
link,
});
perLink.set(url, list);
}
}
const noOfFiles = report.detected.size;
// avoid stripping commonPath if we only have one file.
const commonPath =
noOfFiles < 2
? ""
: commonPathComponents
.map((x) => {
for (const [k, count] of x.entries()) {
if (count === noOfFiles) {
return k;
}
return null;
}
})
.filter((x) => x)
.join("/");
const notes = [];
for (const [_, linkData] of perLink) {
const note = linkToNote(linkData, commonPath);
if (label) {
note.labels.push(label);
}
notes.push(note);
}
return {
version: 3,
notes,
};
}
function linkToNote(linkData: { file: string; link: Link }[], path: string): INoteV3 {
const { link } = linkData[0];
// if we knew the github repository address this could actually be links to browse the files.
const content = linkData.map((x) => `${x.file.replace(path, "")}:${x.link.lineNumber}`).join("\n");
return {
noteVersion: 3,
content,
date: Date.now(),
author: "",
version: link.version,
labels: [],
selectionStart: link.selectionStart,
selectionEnd: link.selectionEnd,
};
}