-
Notifications
You must be signed in to change notification settings - Fork 103
/
index.ts
64 lines (58 loc) · 1.84 KB
/
index.ts
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
#!/usr/bin/env node
import * as fs from "fs";
import * as graph from "pagerank.js";
import * as path from "path";
import createLinkMap from "./lib/createLinkMap";
import readAllNotes from "./lib/readAllNotes";
import updateBacklinks from "./lib/updateBacklinks";
(async () => {
const baseNotePath = process.argv[2];
if (!baseNotePath || baseNotePath === "--help") {
console.log("Usage: note-link-janitor [NOTE_DIRECTORY]");
return;
}
const notes = await readAllNotes(baseNotePath);
const linkMap = createLinkMap(Object.values(notes));
// Sort by PageRank
for (const note of linkMap.keys()) {
const entry = linkMap.get(note)!;
for (const linkingNote of entry.keys()) {
graph.link(linkingNote, note, 1.0);
}
}
const noteRankings: { [key: string]: number } = {};
graph.rank(0.85, 0.000001, function(node, rank) {
noteRankings[node] = rank;
});
await Promise.all(
Object.keys(notes).map(async notePath => {
const backlinks = linkMap.get(notes[notePath].title);
const newContents = updateBacklinks(
notes[notePath].parseTree,
notes[notePath].noteContents,
backlinks
? [...backlinks.keys()]
.map(sourceTitle => ({
sourceTitle,
context: backlinks.get(sourceTitle)!
}))
.sort(
(
{ sourceTitle: sourceTitleA },
{ sourceTitle: sourceTitleB }
) =>
(noteRankings[sourceTitleB] || 0) -
(noteRankings[sourceTitleA] || 0)
)
: []
);
if (newContents !== notes[notePath].noteContents) {
await fs.promises.writeFile(
path.join(baseNotePath, path.basename(notePath)),
newContents,
{ encoding: "utf-8" }
);
}
})
);
})();