-
Notifications
You must be signed in to change notification settings - Fork 2
/
convertFile.js
83 lines (69 loc) · 2.41 KB
/
convertFile.js
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
const convertFile = (file) => {
const date = new Date(file.createdTimestampUsec / 1000);
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
let hours = date.getHours();
let minutes = date.getMinutes();
if (minutes === 60) {
hours += 1;
minutes = 0;
}
hours = hours.toString().padStart(2, "0");
minutes = minutes.toString().padStart(2, "0");
const mdFileName = `${year}_${month}_${day}.md`;
const formatTitle = (title) => {
return title ? `**${title.trim()}**` : "";
};
const formatTextAnnoContent = (text, annotations) => {
let formattedText = text?.replaceAll(/\n(.+)/g, "\n\t- $1");
if (!annotations) return formattedText;
let formattedAnnotationsStr = "";
for (let annotation of annotations) {
let formattedAnnotation = `\n[${annotation.title}](${annotation.url})`;
if (text && text.includes(annotation.url)) {
formattedText = formattedText.replace(
annotation.url,
formattedAnnotation
);
} else {
formattedAnnotationsStr += formattedAnnotation;
}
}
return `${formattedText} ${formattedAnnotationsStr}`;
};
const formatList = (list) => {
const mappedList = list?.map((item) =>
item.isChecked ? `DONE ${item.text}` : `TODO ${item.text}`
);
return mappedList?.join("\n\t- ");
};
const formatAttachments = (attachments) => {
if (!attachments) return "";
let formatted = "";
for (let attachment of attachments) {
formatted += `\n![attc.](../assets/${attachment.filePath})`;
}
return formatted;
};
const formatLabels = (labels) => {
const mappedLabel = labels?.map((label) => `#[[${label.name}]]`);
return mappedLabel?.join(" ");
};
const formattedTitle = formatTitle(file.title);
const formattedTextAnnoContent = formatTextAnnoContent(
file.textContent,
file.annotations
);
const formattedList = formatList(file.listContent);
const formattedAttachments = formatAttachments(file.attachments);
const formattedLabels = formatLabels(file.labels);
const timestamp = `${hours}:${minutes}`;
const content = `\n- ${formattedTitle} (${timestamp}) ${
formattedLabels || ""
}\n\t- ${
formattedTextAnnoContent || formattedList || ""
} \n\t- ${formattedAttachments}`;
return { mdFileName, content };
};
module.exports = convertFile;