Skip to content

Commit

Permalink
feat: support heading for append and prepend mode
Browse files Browse the repository at this point in the history
close #11
  • Loading branch information
Vinzent03 committed May 13, 2021
1 parent bfc6005 commit 53936cb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ To get the URI in a more convenient way you can use the commands to copy Advance
| append | filepath, data, mode=append | Only appends `data` to `filepath` |
| prepend | filepath, data, mode=prepend | Only prepends `data` to `filepath` |

The `heading` parameter is for `mode=append` and `mode=prepend` supported too.

## Navigation

| / | parameters | explanation |
Expand All @@ -36,6 +38,8 @@ To get the URI in a more convenient way you can use the commands to copy Advance
| append | daily=true, data, mode=append | Only appends `data` to today's daily note. The file will be created, if the file does not already exist |
| prepend | daily=true, data, mode=prepend | Only prepends `data` to today's daily note. The file will be created, if the file does not already exist |

The `heading` parameter is for `mode=append` and `mode=prepend` supported too.

## Execute command

| / | parameters | explanation |
Expand Down
99 changes: 76 additions & 23 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,16 @@ export default class AdvancedURI extends Plugin {

} else if (parameters.mode === "prepend") {
if (file instanceof TFile) {
this.prepend(file, parameters.data);
this.prepend(file, parameters);
} else {
this.prepend(path, parameters.data);
this.prepend(path, parameters);
}

} else if (parameters.mode === "append") {
if (file instanceof TFile) {
this.append(file, parameters.data);
this.append(file, parameters);
} else {
this.append(path, parameters.data);
this.append(path, parameters);
}

} else if (file instanceof TFile) {
Expand Down Expand Up @@ -272,13 +272,13 @@ export default class AdvancedURI extends Plugin {
if (!dailyNote) {
dailyNote = await createDailyNote(moment);
}
this.prepend(dailyNote, parameters.data);
this.prepend(dailyNote, parameters);

} else if (parameters.data && parameters.mode === "append") {
if (!dailyNote) {
dailyNote = await createDailyNote(moment);
}
this.append(dailyNote, parameters.data);
this.append(dailyNote, parameters);

} else if (parameters.data && dailyNote) {
new Notice("File already exists");
Expand All @@ -299,31 +299,63 @@ export default class AdvancedURI extends Plugin {

}

async append(file: TFile | string, data: string) {
async append(file: TFile | string, parameters: Parameters) {
let path: string;
let fileData: string;
if (file instanceof TFile) {
fileData = await this.app.vault.read(file);
path = file.path;
} else {
path = file;
fileData = "";
let dataToWrite: string;
if (parameters.heading) {
if (file instanceof TFile) {
path = file.path;
const line = this.getEndAndBeginningOfHeading(file, parameters.heading)?.lastLine;
if (line === undefined) return;

const data = await this.app.vault.read(file);
const lines = data.split("\n");

lines.splice(line, 0, ...parameters.data.split("\n"));
dataToWrite = lines.join("\n");
}
}
else {
let fileData: string;
if (file instanceof TFile) {
fileData = await this.app.vault.read(file);
path = file.path;
} else {
path = file;
fileData = "";
}
dataToWrite = fileData + "\n" + parameters.data;
}
const dataToWrite = fileData + "\n" + data;
this.writeAndOpenFile(path, dataToWrite);
}

async prepend(file: TFile | string, data: string) {
async prepend(file: TFile | string, parameters: Parameters) {
let path: string;
let fileData: string;
if (file instanceof TFile) {
fileData = await this.app.vault.read(file);
path = file.path;
let dataToWrite: string;
if (parameters.heading) {
if (file instanceof TFile) {
path = file.path;
const line = this.getEndAndBeginningOfHeading(file, parameters.heading)?.firstLine;
if (line === undefined) return;

const data = await this.app.vault.read(file);
const lines = data.split("\n");

lines.splice(line, 0, ...parameters.data.split("\n"));
dataToWrite = lines.join("\n");
}

} else {
path = file;
fileData = "";
let fileData: string;
if (file instanceof TFile) {
fileData = await this.app.vault.read(file);
path = file.path;
} else {
path = file;
fileData = "";
}
dataToWrite = parameters.data + "\n" + fileData;
}
const dataToWrite = data + "\n" + fileData;
this.writeAndOpenFile(path, dataToWrite);
}

Expand All @@ -341,6 +373,27 @@ export default class AdvancedURI extends Plugin {
}
}

getEndAndBeginningOfHeading(file: TFile, heading: string): { "lastLine": number, "firstLine": number; } {
const cache = this.app.metadataCache.getFileCache(file);
const sections = cache.sections;
const foundHeading = cache.headings?.find(e => e.heading === heading);


if (foundHeading) {
const foundSectionIndex = sections.findIndex(section => section.type === "heading" && section.position.start.line === foundHeading.position.start.line);
const restSections = sections.slice(foundSectionIndex + 1);

const nextHeadingIndex = restSections?.findIndex(e => e.type === "heading");

const lastSection = restSections[(nextHeadingIndex !== -1 ? nextHeadingIndex : restSections.length) - 1] ?? sections[foundSectionIndex];
const lastLine = lastSection.position.end.line + 1;

return { "lastLine": lastLine, "firstLine": sections[foundSectionIndex].position.end.line + 1 };
} else {
new Notice("Can't find heading");
}
}

async setCursor(mode: Parameters["mode"]) {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view) {
Expand Down

0 comments on commit 53936cb

Please sign in to comment.