added frontmatter stripping

This commit is contained in:
Ell 2023-08-16 12:47:47 +02:00
parent 36f68d47bc
commit c9a1dae9a2
6 changed files with 22 additions and 963 deletions

955
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -14,7 +14,6 @@
"devDependencies": {
"@types/node": "^16.11.6",
"builtin-modules": "^3.2.0",
"electron": "^13.6.2",
"esbuild": "0.14.0",
"esbuild-plugin-copy": "^1.3.0",
"obsidian": "latest",

View file

@ -7,7 +7,6 @@ export default class JustSharePleasePlugin extends Plugin {
// TODO panel that displays all shares, including ones for removed files, and allows unsharing or updating them
// TODO add a setting for auto-refreshing uploads when saving
// TODO strip frontmatter before uploading? maybe optionally
settings: JSPSettings;
async onload(): Promise<void> {
@ -113,7 +112,7 @@ export default class JustSharePleasePlugin extends Plugin {
let response = await requestUrl({
url: `${this.settings.url}/share.php`,
method: "POST",
body: JSON.stringify({content: await this.app.vault.cachedRead(file)})
body: JSON.stringify({content: this.preProcessMarkdown(await this.app.vault.cachedRead(file))})
});
let shared = response.json as SharedItem;
shared.path = file.path;
@ -140,7 +139,7 @@ export default class JustSharePleasePlugin extends Plugin {
url: `${this.settings.url}/share.php?id=${item.id}`,
method: "PATCH",
headers: {"Password": item.password},
body: JSON.stringify({content: await this.app.vault.cachedRead(file)})
body: JSON.stringify({content: this.preProcessMarkdown(await this.app.vault.cachedRead(file))})
});
new Notice(`Successfully updated ${file.basename} on JSP`);
return true;
@ -184,4 +183,12 @@ export default class JustSharePleasePlugin extends Plugin {
if (notice)
new Notice(`Copied link to ${basename(item.path, extname(item.path))} to clipboard`);
}
preProcessMarkdown(text: string): string {
// strip frontmatter
if (this.settings.stripFrontmatter)
text = text.replace(/^---\s*\n.*?\n---\s*\n(.*)$/s, "$1");
return text;
}
}

View file

@ -1,12 +1,14 @@
export const defaultSettings: JSPSettings = {
url: "http://localhost:8080",
shared: []
shared: [],
stripFrontmatter: true
};
export interface JSPSettings {
url: string;
shared: SharedItem[];
stripFrontmatter: boolean;
}

View file

@ -7,9 +7,10 @@
"path": "Second note.md"
},
{
"id": "281d96f9",
"password": "2eb4979a9e853df98f5fcdc2cdc4b770",
"id": "fa0cf7f7",
"password": "b82b04752742c61bd602b52f535cb484",
"path": "Cool Test Note.md"
}
]
],
"stripFrontmatter": true
}

View file

@ -1,3 +1,8 @@
---
test: yes
---
# Cool Test Note
This is a cool test note, my friends!