-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
149 lines (119 loc) · 4.06 KB
/
Copy pathindex.js
File metadata and controls
149 lines (119 loc) · 4.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
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
const Crawler = require("./Crawler.js");
const low = require('lowdb')
const FileAsync = require('lowdb/adapters/FileAsync')
function getUrls(text) {
const results = text.match(/(https?:\/\/[^\s|\]|)]+)/g)
return results;
}
function getUrlBacklinks(text) {
const results = text.match(/\[\[(.+)(?:\|)(https?:\/\/[^\s|\]|)]+)(?:]+)/g)
return results;
}
const BASE_FOLDER = "Amorphous";
class AmorphousPlugin {
constructor() {
this.id = 'AmorphousPlugin'
this.name = 'AmorphousPlugin'
this.description = 'Plugin that will index all of your links.'
this.defaultOn = true
}
async init(app, instance) {
this.app = app
this.instance = instance
this.instance.registerStatusBarItem()
}
async initServices() {
const adapter = new FileAsync(`${app.vault.adapter.basePath}/${BASE_FOLDER}/db.json`)
const db = await low(adapter)
this.db = db;
await db.defaults({ urls: [] }).write()
const urls = db.get("urls").value();
const skipUrls = urls.map(entry => entry.url)
this.crawler = new Crawler({baseUrl: `${app.vault.adapter.basePath}/${BASE_FOLDER}`, skipUrls})
this.crawler.on("url-complete", ({id, url, file, html, title}) => {
const vault = this.app.vault
this.showStatus(`${title} is complete`)
const path = `${BASE_FOLDER}/${title.replace('/', ' ')}.md`
this.db.get("urls").push({id, url, title, path}).write()
vault.adapter.write(path, `
[${title}](${url})
<div hidden data-id="${id}"></div>
<div hidden>${html.replace(/\s\s+/g, ' ')}</div>
`)
console.log("@url complete", {id, url, file, html, title})
})
this.crawler.on("finished", this.replaceLinks.bind(this))
}
replaceLinks() {
console.log("@finished crawling; replacing links")
const urlCollection = this.db.get("urls");
this.mdFiles.forEach(async(file, index) => {
if (!file.path.startsWith("Amorphous.beta/")) {
return;
}
const contents = await this.app.vault.cachedRead(file)
let links = getUrls(contents);
const backLinks = getUrlBacklinks(contents)
let updated = contents;
(links || []).forEach(link => {
updated = updated.replace(link, (link, offset, string) => {
const before = string[offset - 1];
if (before === "|") {
return link
}
const urlInfo = urlCollection.find({url: link}).value();
if (!urlInfo) {
return link
}
return `[[${urlInfo.path}|${urlInfo.url}]]`
})
});
(backLinks || []).forEach(link => {
updated = updated.replace(link, (match) => {
const originalMatch = match;
const linkAndLabel = match.replace('[[', '').replace(']]', '');
const [path, url] = linkAndLabel.split("|")
const urlInfo = urlCollection.find({url}).value();
if (!urlInfo) {
return originalMatch;
}
return `[[${urlInfo.path}|${urlInfo.url}]]`
})
})
this.app.vault.adapter.write(file.path, updated)
})
}
async onEnable({ workspace }, instance) {
this.showStatus("initialized")
const vault = this.app.vault;
try {
await vault.createFolder(`${BASE_FOLDER}`)
await vault.createFolder(`${BASE_FOLDER}/_images`)
}catch(err) {
//ignore
console.log(err)
}
await this.initServices()
this.mdFiles.forEach(this.queueFile, this)
this.replaceLinks()
}
async queueFile(file) {
const vault = this.app.vault;
const contents = await vault.cachedRead(file)
this.crawler.processFile(contents, file);
console.log("@file queued", {file})
}
showStatus(msg) {
const { statusBarEl } = this.instance
if (!statusBarEl) return
var node = document.createElement("span");
node.textContent = msg
statusBarEl.innerHTML = ""
statusBarEl.appendChild(node)
}
get mdFiles() {
const vault = this.app.vault;
return vault.getMarkdownFiles().filter(file => !file.path.startsWith(`${BASE_FOLDER}/`))
}
}
module.exports = () => new AmorphousPlugin()