-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
357 lines (300 loc) · 9.94 KB
/
main.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import { copy, existsSync, walk } from "jsr:@std/fs";
import { parseArgs } from "jsr:@std/cli/parse-args";
// logseq supports "TODO/DOING/DONE" syntax for TODOs, however obsidian only
// supports conventional markdown "[ ]".
//
// Uncovered scenarios
// 1. logseq also supports adding deadlines through specific syntax, i.e. "DEADLINE: <2022-11-08 Tue 15:00>"
export function replaceTODOs(text: string) {
return text.replaceAll("- TODO", "- [ ]")
.replaceAll("- DOING", "- [ ]")
.replaceAll("- DONE", "- [x]");
}
// logseq supports "#", "#[[]]" and "[[]]" as links, however obsidian only supports "[[]]".
// ref: https://forum.obsidian.md/t/a-guide-on-links-vs-tags-in-obsidian/28231
//
// Uncovered scenarios:
// 1. "#k8s/#helm" gets mapped to "[[k8s/#helm]]"
// 2. "(#k8s or #helm)" gets mapped to "(#k8s or [[helm]])"
export function replaceTagsForLinks(text: string) {
let copy = "";
let migrating = false;
for (let i = 0; i < text.length; i++) {
if (text[i] === "#") {
// if the previous char is non-white, then it's probably a URL.
// NOTE: this provides false positives, i.e. phrases with parentheses:
// (#lemon or #lime)
if (i > 0 && text[i - 1] !== " ") {
copy += text[i];
continue;
}
// if the next space is another hash or a space, it's probably markdown heading syntax`.
if (text[i + 1] === "#" || text[i + 1] === " ") {
copy += text[i];
continue;
}
// if it's a logseq query, keep it.
if (text[i + 1] === "+") {
copy += text[i];
continue;
}
// if it's a ref with brackets, just remove the hash.
if (text[i + 1] === "[" && text[i + 2] === "[") {
continue;
}
// remove hash and wrap word in square brackets
if (migrating == false) {
copy += "[[";
migrating = true;
}
} else {
if (migrating) {
if (
// line breaks or spacing characters
text[i] !== " " && text[i] !== "\n" && text[i] !== "\t" &&
// punctuation marks
text[i] !== "," && text[i] !== "." && text[i] !== ";" &&
text[i] !== ":" &&
text[i] !== "?" && text[i] !== "!" &&
text[i] !== "/" && text[i] !== "\\"
) {
copy += text[i];
} else if (
(text[i] === "." || text[i] === "/") && text[i + 1] !== " "
) {
// avoid splitting something like #google.com or #something/other
// NOTE: this provides false positives like #helm/#k8s
copy += text[i];
} else {
copy += "]]";
copy += text[i];
migrating = false;
}
} else {
copy += text[i];
}
}
}
return copy;
}
export function replaceNumberedLists(text: string) {
const lines = text.split("\n");
let counter = 0;
const logseqNumberingAnnotation = "logseq.order-list-type:: number";
// First filter out the metadata lines and keep track of which lines should be numbered
const processedLines = lines
.filter((line) => !line.includes(logseqNumberingAnnotation))
.map((line) => {
const nextLine = lines[lines.indexOf(line) + 1];
const isListItem = line.trim().startsWith("-");
const isNumbered = nextLine?.includes(logseqNumberingAnnotation);
if (isListItem && isNumbered) {
counter++;
return line.replace(/^(\s*)-/, `$1${counter}.`);
} else {
counter = 0;
}
return line;
});
return processedLines.join("\n");
}
export function replacePageProperties(text: string) {
const lines = text.split("\n");
const properties: string[] = [];
const content: string[] = [];
let processingProperties = true;
for (const line of lines) {
if (processingProperties) {
// Skip empty lines at the start
if (line.trim() === "") {
continue;
}
// If we hit a non-property line, we're done with properties
if (!line.includes("::")) {
processingProperties = false;
content.push(line);
continue;
}
// Convert property to YAML format
const [key, value] = line.split("::");
// Convert [[links]] to plain text by removing brackets
const cleanValue = value.trim().replace(/\[\[(.*?)\]\]/g, "$1");
properties.push(`${key.trim()}: ${cleanValue}`);
} else {
content.push(line);
}
}
// Only add frontmatter if we found properties
if (properties.length > 0) {
return `---\n${properties.join("\n")}\n---\n\n${content.join("\n")}`;
}
return text;
}
export function removeCollapsedBlocks(text: string) {
return text
.split("\n")
.filter((x) => !x.includes("collapsed:: true"))
.join("\n");
}
export function removeLogbooks(text: string) {
const lines = text.split("\n");
const result: string[] = [];
let inLogbook = false;
for (const line of lines) {
if (line.trim() === ":LOGBOOK:") {
inLogbook = true;
continue;
}
if (line.trim() === ":END:" && inLogbook) {
inLogbook = false;
continue;
}
if (!inLogbook) {
result.push(line);
}
}
return result.join("\n");
}
async function updateConfigForLogseqStructure(filepath: string) {
const text = await Deno.readTextFile(filepath);
const config = JSON.parse(text);
const updatedConfig = {
...config,
// Instead of changing the directory strucuture, simply modify obsidian config
// to use logseq-native directory strucuture.
// One added benefit of this is that we can circle back to logseq ocasionally
// and it'll read our notes.
newFileLocation: "folder",
newFileFolderPath: "pages",
attachmentFolderPath: "asset",
// We don't want to show logseq backups and .edn configs in obsidian search.
userIgnoreFilters: ["logseq/"],
};
const updatedText = JSON.stringify(updatedConfig, null, 2);
if (text !== updatedText) {
await Deno.writeTextFile(filepath, updatedText);
console.log("updated", filepath);
}
}
export function updateRelativePaths(
content: string,
originalPath: string,
newPath: string,
): string {
// Calculate directory depths for path adjustment
const oldDepth = originalPath.split("/").length - 1;
const newDepth = newPath.split("/").length - 1;
if (oldDepth === newDepth) return content;
const depthDiff = newDepth - oldDepth;
return content.replace(/(?:\.\.?\/(?:[\w\-.]+\/?)*[\w\-.]*)/g, (match) => {
const prefix = "../".repeat(depthDiff);
return `${prefix}${match}`;
});
}
export function organizeFileWithSlashEncoding(filepath: string) {
if (!filepath.includes("%2F")) {
return { newPath: filepath, shouldMove: false };
}
const newPath = filepath.replaceAll("%2F", "/");
const targetDir = newPath.split("/").slice(0, -1).join("/");
return {
newPath,
targetDir,
shouldMove: true,
};
}
async function run() {
const flags = parseArgs(Deno.args, {
string: ["in", "out"],
boolean: ["force"],
default: { in: ".", force: false },
});
const input = flags.in;
const output = flags.out;
if (!output) {
throw "provide --out for now";
}
// validate that input EXISTS
if (!existsSync(input)) {
console.log(`${input} doesn't exist. Provide a valid input path.`);
Deno.exit(1);
}
if (existsSync(output)) {
if (flags.force) {
await Deno.remove(output, { recursive: true });
} else {
console.log(
`${output} already exists. Provide a different path to copy the logseq graph.`,
);
Deno.exit(1);
}
}
await copy(input, output, { preserveTimestamps: true });
console.log(`copied logseq graph to ${output}`);
await Deno.remove(`${output}/logseq`, { recursive: true });
for await (const walkEntry of walk(output)) {
const type = walkEntry.isSymlink
? "symlink"
: walkEntry.isFile
? "file"
: "directory";
// We only care about the markdown bits right now.
if (type !== "file" || !walkEntry.path.includes(".md")) {
continue;
}
const original = await Deno.readTextFile(walkEntry.path);
let withUpdatedRelativePaths = original;
const {
newPath,
shouldMove,
targetDir,
} = organizeFileWithSlashEncoding(walkEntry.path);
if (shouldMove) {
await Deno.mkdir(targetDir!, { recursive: true });
await Deno.rename(walkEntry.path, newPath);
console.log(`moved ${walkEntry.path} to ${newPath}`);
withUpdatedRelativePaths = updateRelativePaths(
original,
walkEntry.path,
newPath,
);
if (withUpdatedRelativePaths !== original) {
console.log(`updated relative paths in ${newPath}`);
}
walkEntry.path = newPath;
}
const withTODOs = replaceTODOs(withUpdatedRelativePaths);
if (withTODOs !== original) {
console.log("updated TODOs for", walkEntry.path);
}
const withLinks = replaceTagsForLinks(withTODOs);
if (withLinks !== withTODOs) {
console.log("updated links for", walkEntry.path);
}
const withNumberedLists = replaceNumberedLists(withLinks);
if (withLinks !== withNumberedLists) {
console.log("updated numbered lists for", walkEntry.path);
}
const withPageProperties = replacePageProperties(withNumberedLists);
if (withPageProperties !== withNumberedLists) {
console.log("updated page properties for", walkEntry.path);
}
const withoutCollapsedBlocks = removeCollapsedBlocks(withPageProperties);
if (withPageProperties !== withoutCollapsedBlocks) {
console.log("removed collapsed blocks for", walkEntry.path);
}
const withoutLogbooks = removeLogbooks(withoutCollapsedBlocks);
if (withoutCollapsedBlocks !== withoutLogbooks) {
console.log("removed logbooks for", walkEntry.path);
}
await Deno.writeTextFile(walkEntry.path, withoutLogbooks);
}
if (!existsSync(".obsidian/app.json")) {
Deno.createSync(".obsidian/app.json");
Deno.writeTextFileSync(".obsidian/app.json", "{}");
}
await updateConfigForLogseqStructure(".obsidian/app.json");
}
if (import.meta.main) {
run();
}