Skip to content

Commit

Permalink
Fixed:
Browse files Browse the repository at this point in the history
- Folder error of restoring files into the folder which is not exists yet has been fixed.

And tidy up the code.
  • Loading branch information
vrtmrz committed Mar 16, 2022
1 parent a40b278 commit 97e2465
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
52 changes: 35 additions & 17 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ function base64ToArrayBuffer(base64: string): ArrayBuffer {
}
}

async function ensureDirectory(app: App, fullpath: string) {
const pathElements = fullpath.split("/");
pathElements.pop();
let c = "";
for (const v of pathElements) {
c += v;
try {
await app.vault.createFolder(c);
} catch (ex) {
// basically skip exceptions.
if (ex.message && ex.message == "Folder already exists.") {
// especialy this message is.
} else {
new Notice("Folder Create Error");
console.log(ex);
}
}
c += "/";
}
}

export default class ScrewDriverPlugin extends Plugin {
async onload() {
await this.loadSettings();
Expand All @@ -112,6 +133,7 @@ export default class ScrewDriverPlugin extends Plugin {
editor.setValue(`---
# --- Select a directory to dump. ---
${targets}
# --- Prefixes to ignore. ---
ignores:
- /node_modules
Expand All @@ -131,17 +153,14 @@ filters:
name: "Dump files",
editorCallback: async (editor: Editor, view: MarkdownView) => {
const data = view.data;
const stx = data.indexOf("\n---");
if (!data.startsWith("---") || stx === -1) {
const bodyStartIndex = data.indexOf("\n---");
if (!data.startsWith("---") || bodyStartIndex === -1) {
new Notice("Frontmatter was not found.");
}
//
const yaml = data.substring(3, stx);
console.dir(yaml);
const yaml = data.substring(3, bodyStartIndex);
const yamlData = parseYaml(yaml);
let newData = "---" + yaml + "\n---\n\n";
// const fileData = data.substring(stx+3);
console.dir(yamlData);
const target = yamlData.target ?? "";
const ignoresSrc = yamlData.ignores;
const ignores: string[] = Array.isArray(ignoresSrc)
Expand Down Expand Up @@ -196,18 +215,15 @@ filters:
editorCallback: async (editor: Editor, view: MarkdownView) => {
const data = view.data;
if (data.startsWith("---")) {
const stx = data.indexOf("\n---");
const bodyStartIndex = data.indexOf("\n---");

if (stx !== -1) {
const ww = data
.substring(stx)
if (bodyStartIndex !== -1) {
const preBlocks = data
.substring(bodyStartIndex)
.matchAll(/^```([\s\S]*?)\n([\s\S]*?)^```/gm);
for (const www of ww) {
console.dir(www);
const [, filename, data] = www;
for (const preBlock of preBlocks) {
const [, filename, data] = preBlock;
let saveData = data;

console.log(filename);
try {
if (isPlainText(filename)) {
saveData = saveData.replace(/\\`/g, "`");
Expand All @@ -216,13 +232,15 @@ filters:
0,
saveData.lastIndexOf("\n")
);
await ensureDirectory(this.app, filename);
await this.app.vault.adapter.write(
filename,
saveData
);
} else {
const saveDataArrayBuffer =
await base64ToArrayBuffer(saveData);
base64ToArrayBuffer(saveData);
await ensureDirectory(this.app, filename);
await this.app.vault.adapter.writeBinary(
filename,
saveDataArrayBuffer
Expand All @@ -233,7 +251,7 @@ filters:
);
} catch (ex) {
new Notice(`Failed to write ${filename}`);
console.dir(ex);
console.log(ex);
}
}
return;
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-screwdriver",
"name": "Screwdriver",
"version": "0.0.1",
"version": "0.0.2",
"minAppVersion": "0.12.0",
"description": "Utility for picking and putting hidden files",
"author": "vorotamoroz",
Expand Down

0 comments on commit 97e2465

Please sign in to comment.