Skip to content

Commit

Permalink
feat: allow appending / prepending at line
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-elias-alvarez committed Apr 11, 2024
1 parent 54cf458 commit a564d0b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
41 changes: 22 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,24 @@ export default class AdvancedURI extends Plugin {
dataToWrite = lines.join("\n");
}
} else {
let fileData: string;
if (file instanceof TFile) {
fileData = await this.app.vault.read(file);
path = file.path;
const fileData = await this.app.vault.read(file);
if (parameters.line) {
let line = Math.max(Number(parameters.line) - 1, 0);
const lines = fileData.split("\n");
if (lines[line]?.trim() !== "") {
line += 1;
}
lines.splice(line, 0, parameters.data);
dataToWrite = lines.join("\n");
} else {
dataToWrite = fileData + "\n" + parameters.data;
}
} else {
path = file;
fileData = "";
dataToWrite = parameters.data;
}
dataToWrite = fileData + "\n" + parameters.data;
}
return this.writeAndOpenFile(path, dataToWrite, parameters);
}
Expand Down Expand Up @@ -392,24 +401,18 @@ export default class AdvancedURI extends Plugin {
}
} else {
if (file instanceof TFile) {
path = file.path;
const fileData = await this.app.vault.read(file);
const cache = this.app.metadataCache.getFileCache(file);

let line = 0;
if (cache.frontmatterPosition) {
const line = cache.frontmatterPosition.end.line;
const first = fileData
.split("\n")
.slice(0, line + 1)
.join("\n");
const last = fileData
.split("\n")
.slice(line + 1)
.join("\n");
dataToWrite = first + "\n" + parameters.data + "\n" + last;
} else {
dataToWrite = parameters.data + "\n" + fileData;
line += cache.frontmatterPosition.end.line + 1;
} else if (parameters.line) {
line += Math.max(Number(parameters.line) - 1, 0);
}
path = file.path;
const lines = fileData.split("\n");
lines.splice(line, 0, parameters.data);
dataToWrite = lines.join("\n");
} else {
path = file;
dataToWrite = parameters.data;
Expand Down Expand Up @@ -577,7 +580,7 @@ export default class AdvancedURI extends Plugin {
}

async setCursorInLine(parameters: Parameters) {
const rawLine = parameters.line;
const rawLine = Number(parameters.line);
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const viewState = view.leaf.getViewState();
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export interface Parameters {
"x-error"?: string;
saveworkspace?: "true";
updateplugins?: "true";
line?: number;
line?: string;
/**
* @deprecated Use "openMode" instead
*/
Expand Down

0 comments on commit a564d0b

Please sign in to comment.