-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroam.ts
62 lines (58 loc) · 1.88 KB
/
roam.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
const readline = require("readline");
const fs = require("fs");
const path = require("path");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Where's your drafts export? ", (fileName: string) => {
fs.readFile(fileName, "utf8", (err: any, data: string) => {
const draftsExportData: draftsExportType = JSON.parse(data);
const dirName = path.dirname(fileName);
const baseName = path.basename(fileName);
const roamFileName = path.join(dirName, `${baseName}-roamImport.json`);
const roamData = transformDraftsData(draftsExportData);
fs.writeFile(roamFileName, JSON.stringify(roamData), (err: any) =>
err ? console.log("fuck", err) : console.log("done")
);
rl.close();
});
});
const transformDraftsData = (data: draftsExportType): roamImportType[] =>
data.map((draftsData) => {
const content = draftsData.content.split("\n");
const tags = draftsData.tags;
const tagsString = `${tags
.map((tag) => `#[[${tag}]]`)
.join(" ")} #[[Drafts]]`;
const metaMetaData = {
"create-email": "[email protected]",
"create-time": new Date(draftsData.created_at).getTime(),
"edit-time": new Date(draftsData.modified_at).getTime(),
"edit-email": "[email protected]",
};
const title =
content[0].length <= 50
? content[0]
: `Drafts: Brain Fart in ${tags[0] ?? ""} -- uuid: ${draftsData.uuid}`;
const metaData = {
...metaMetaData,
title,
uuid: draftsData.uuid,
};
const metaChildrenData = [
{
string: "Author:: [[Akash]]",
...metaMetaData,
},
{
string: `Tags:: ${tagsString}`,
...metaMetaData,
},
];
const children = content.map((text) => ({
string: text,
...metaMetaData,
}));
return { ...metaData, children: [...metaChildrenData, ...children] };
});