improved paths

This commit is contained in:
Ell 2023-08-18 10:01:18 +02:00
parent 6c781d560c
commit babb0ea22f
4 changed files with 61 additions and 7 deletions

View file

@ -1,7 +1,6 @@
import {arrayBufferToBase64, Notice, Plugin, requestUrl, TFile} from "obsidian"; import {arrayBufferToBase64, Notice, Plugin, requestUrl, TFile} from "obsidian";
import {defaultSettings, JSPSettings, SharedItem} from "./settings"; import {defaultSettings, JSPSettings, SharedItem} from "./settings";
import {JSPSettingsTab} from "./settings-tab"; import {JSPSettingsTab} from "./settings-tab";
import {basename, extname} from "path";
import {JSPView} from "./view"; import {JSPView} from "./view";
export default class JustSharePleasePlugin extends Plugin { export default class JustSharePleasePlugin extends Plugin {
@ -194,7 +193,7 @@ export default class JustSharePleasePlugin extends Plugin {
} }
async deleteFile(item: SharedItem, notice = true): Promise<boolean> { async deleteFile(item: SharedItem, notice = true): Promise<boolean> {
let name = basename(item.path, extname(item.path)); let name = removeExtension(item.path);
try { try {
await requestUrl({ await requestUrl({
url: `${this.settings.url}/share.php?id=${item.id}`, url: `${this.settings.url}/share.php?id=${item.id}`,
@ -221,7 +220,7 @@ export default class JustSharePleasePlugin extends Plugin {
async copyShareLink(item: SharedItem, notice = true): Promise<void> { async copyShareLink(item: SharedItem, notice = true): Promise<void> {
await navigator.clipboard.writeText(`${this.settings.url}#${item.id}`); await navigator.clipboard.writeText(`${this.settings.url}#${item.id}`);
if (notice) if (notice)
new Notice(`Copied link to ${basename(item.path, extname(item.path))} to clipboard`); new Notice(`Copied link to ${removeExtension(item.path)} to clipboard`);
} }
async preProcessMarkdown(file: TFile): Promise<string> { async preProcessMarkdown(file: TFile): Promise<string> {
@ -247,7 +246,7 @@ export default class JustSharePleasePlugin extends Plugin {
let resolved = this.app.metadataCache.getFirstLinkpathDest(url, file.path).path; let resolved = this.app.metadataCache.getFirstLinkpathDest(url, file.path).path;
let attachment = this.app.vault.getAbstractFileByPath(resolved); let attachment = this.app.vault.getAbstractFileByPath(resolved);
let data = arrayBufferToBase64(await this.app.vault.readBinary(attachment as TFile)); let data = arrayBufferToBase64(await this.app.vault.readBinary(attachment as TFile));
let img = `<img src="data:image/${extname(resolved).substring(1)};base64, ${data}" alt="${alt}">`; let img = `<img src="data:image/${resolved.split(".").pop()};base64, ${data}" alt="${alt}">`;
text = text.substring(0, match.index) + img + text.substring(match.index + match[0].length); text = text.substring(0, match.index) + img + text.substring(match.index + match[0].length);
} catch (e) { } catch (e) {
console.log(`Error embedding attachment ${url}: ${e}`); console.log(`Error embedding attachment ${url}: ${e}`);
@ -263,4 +262,11 @@ export default class JustSharePleasePlugin extends Plugin {
leaf.view.refresh(); leaf.view.refresh();
} }
} }
}
export function removeExtension(file: string): string {
let split = file.split(".");
split.pop();
return split.join(".");
} }

View file

@ -1,6 +1,5 @@
import {ButtonComponent, ItemView, TFile, WorkspaceLeaf} from "obsidian"; import {ButtonComponent, ItemView, TFile, WorkspaceLeaf} from "obsidian";
import {basename, extname} from "path"; import JustSharePleasePlugin, {removeExtension} from "./main";
import JustSharePleasePlugin from "./main";
export class JSPView extends ItemView { export class JSPView extends ItemView {
@ -20,7 +19,7 @@ export class JSPView extends ItemView {
for (let shared of this.plugin.settings.shared) { for (let shared of this.plugin.settings.shared) {
let file = this.plugin.app.vault.getAbstractFileByPath(shared.path) as TFile; let file = this.plugin.app.vault.getAbstractFileByPath(shared.path) as TFile;
let div = content.createDiv({cls: "just-share-please-shared-item"}); let div = content.createDiv({cls: "just-share-please-shared-item"});
div.createSpan({cls: "just-share-please-shared-name", text: basename(shared.path, extname(shared.path))}); div.createSpan({cls: "just-share-please-shared-name", text: removeExtension(shared.path)});
new ButtonComponent(div) new ButtonComponent(div)
.setClass("clickable-icon") .setClass("clickable-icon")
.setTooltip("Copy JSP link") .setTooltip("Copy JSP link")

View file

@ -5,6 +5,11 @@
"id": "234df7b7", "id": "234df7b7",
"password": "0ef1504bfe83cd9ca812434caa7aacf4", "password": "0ef1504bfe83cd9ca812434caa7aacf4",
"path": "Cool Test Note.md" "path": "Cool Test Note.md"
},
{
"id": "014b266c",
"password": "1c9459d7cdac804f72272c8f6d4a9abc",
"path": "dir/Cool Test Note.md"
} }
], ],
"stripFrontmatter": true, "stripFrontmatter": true,

View file

@ -0,0 +1,44 @@
---
test: yes
---
this is the SECOND cool test note!!
> How are you?
```js
$.ajax({
method: "get",
url: id ? `share.php?id=${id}` : "index.md",
success: t => {
main.html(DOMPurify.sanitize(md.render(t)));
// scroll to anchor
let element = $(window.location.hash);
if (element.length)
$(window).scrollTop(element.offset().top);
},
error: (r, s, e) => main.html(`<div class="error"><p>Error loading shared note with id <code>${id}</code>: ${e}</p><p><a href="#">Home</a></p></div>`)
});
```
cool!!
blah blah i added and removed this
The following is $x^2 = 7$, but more complicated!
$$
x^2 + \sum_{i = 1}^{10000} x^2 \cdot 0 = 7
$$
## Some images
image!
![this is an image my friends, and this is my alt text](Obsidian_TtC7w4GA86.png)
wikilink image!
![[Pasted image 20230816130420.png]]
nice