Skip to content

Commit

Permalink
[CHG] Reworks appending below headline to work
Browse files Browse the repository at this point in the history
     with entirely empty sections (#73)
  • Loading branch information
czottmann committed Aug 6, 2023
1 parent 6062a44 commit 827867e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
7 changes: 1 addition & 6 deletions src/routes/daily-note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,7 @@ async function handleAppend(
// forgetting a parameter or something in the future.
async function appendAsRequested(filepath: string) {
if (belowHeadline) {
return await appendNoteBelowHeadline(
filepath,
belowHeadline,
content,
ensureNewline,
);
return await appendNoteBelowHeadline(filepath, belowHeadline, content);
}

return await appendNote(filepath, content, ensureNewline);
Expand Down
7 changes: 1 addition & 6 deletions src/routes/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,7 @@ async function handleAppend(
// forgetting a parameter or something in the future.
async function appendAsRequested() {
if (belowHeadline) {
return await appendNoteBelowHeadline(
file,
belowHeadline,
content,
ensureNewline,
);
return await appendNoteBelowHeadline(file, belowHeadline, content);
}

return await appendNote(file, content, ensureNewline);
Expand Down
46 changes: 29 additions & 17 deletions src/utils/file-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ export async function searchAndReplaceInNote(
replacement: string,
): Promise<StringResultObject> {
const res = await getNoteContent(filepath);

if (!res.isSuccess) {
return res;
}
Expand All @@ -230,7 +229,6 @@ export async function appendNote(
shouldEnsureNewline: boolean = false,
): Promise<StringResultObject> {
const res = await getNoteContent(filepath);

if (!res.isSuccess) {
return res;
}
Expand All @@ -250,23 +248,38 @@ export async function appendNoteBelowHeadline(
filepath: string,
belowHeadline: string,
textToAppend: string,
shouldEnsureNewline: boolean = false,
): Promise<StringResultObject> {
const escapedHeadline = belowHeadline
.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&")
.replace(/-/g, "\\x2d")
.replace(/\s+/g, "\\s+");
const searchTerm = new RegExp(
`([\\n^]${escapedHeadline})(\\s*\\n.*?)(\\n#+ |$)`,
"s",
);
const res = await getNoteContent(filepath);
if (!res.isSuccess) {
return res;
}

const escapedReplacement =
(shouldEnsureNewline ? endStringWithNewline(textToAppend) : textToAppend)
.replace(new RegExp(/\$/, "g"), "\\$");
const replacement = `$1$2${escapedReplacement}$3`;
// Split into sections by headline, find the section below the specified
// headline, and append the text to that section
const newContent = res.result
.split(/(?=^#+ )/m)
.map((section) => {
if (!section.startsWith(belowHeadline)) {
return section;
}

// If the section doesn't end with a newline, add one before appending.
// This case might occur if the last headline in the note is the one to
// work with, and the file doesn't end with a newline.
if (!section.includes("\n")) {
section += "\n";
}

return endStringWithNewline(section + textToAppend);
})
.join("");

return await searchAndReplaceInNote(filepath, searchTerm, replacement);
const resFile = await createOrOverwriteNote(filepath, newContent);
if (resFile.isSuccess) {
return success(STRINGS.append_done);
}

return resFile;
}

export async function prependNote(
Expand All @@ -276,7 +289,6 @@ export async function prependNote(
shouldIgnoreFrontMatter: boolean = false,
): Promise<StringResultObject> {
const res = await getNoteContent(filepath);

if (!res.isSuccess) {
return res;
}
Expand Down

0 comments on commit 827867e

Please sign in to comment.