Skip to content

Commit

Permalink
Merge pull request #4 from lugenx/fix-annotations
Browse files Browse the repository at this point in the history
Fix annotations
  • Loading branch information
lugenx authored May 24, 2023
2 parents afee63b + 779b827 commit cea62b9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
29 changes: 25 additions & 4 deletions convertFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,24 @@ const convertFile = (file) => {
return title ? `**${title.trim()}**` : "";
};

const formatText = (text) => {
return text?.replaceAll(/\n(.+)/g, "\n\t- $1");
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) => {
Expand All @@ -46,15 +62,20 @@ const convertFile = (file) => {
};

const formattedTitle = formatTitle(file.title);
const formattedText = formatText(file.textContent);
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- ${formattedText || formattedList || ""} \n\t- ${formattedAttachments}`;
}\n\t- ${
formattedTextAnnoContent || formattedList || ""
} \n\t- ${formattedAttachments}`;

return { mdFileName, content };
};
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const runSecondQuestion = () => {
try {
const fileContent = fs.readFileSync(`${sourceDirectory}/${file}`);
const jsonData = JSON.parse(fileContent);
if (jsonData.isTrashed) continue;
const { mdFileName, content } = convertFile(jsonData);

const pathToAppend = createOrReturnDirectory(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keep2log",
"version": "1.1.0",
"version": "1.3.1",
"bin": {
"keep2log": "index.js"
},
Expand Down

0 comments on commit cea62b9

Please sign in to comment.