Skip to content

Commit

Permalink
Merge pull request #2 from SaalikLok/request-bug-fixes
Browse files Browse the repository at this point in the history
Launch Bug Fixes
  • Loading branch information
SaalikLok committed Mar 21, 2024
2 parents 191952d + 982a93f commit a27e8a5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 67 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ After installing the plugin, you'll need to connect your Lavadocs account:
With the Lavadocs plugin enabled, sharing your documents is straightforward:

1. Open the document you wish to share in Obsidian.
2. Click on the newly added ⛰️ mountain icon in your sidebar, or run “Push to Lavadocs” from your command palette.
3. Your document will be pushed to Lavadocs, and you should be able to access and share it.
2. Click on the newly added ⛰️ mountain icon in your sidebar.
3. Your document will be pushed to Lavadocs, and you should be able to access and share it!

## Lavadocs Pricing

Expand Down
98 changes: 34 additions & 64 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,17 @@ export default class LavadocsPlugin extends Plugin {

async onload() {
await this.loadSettings();
const title = await this.getActiveFileName();
const content = await this.getActiveFileContent();
const slug = await this.sluggifiedFileName();

const ribbonIconEl = this.addRibbonIcon('mountain', 'Push to Lavadocs', (evt: MouseEvent) => {

const ribbonIconEl = this.addRibbonIcon('mountain', 'Push to Lavadocs', async (evt: MouseEvent) => {
const { title, content, slug } = await this.getActiveFileDetails();

if (!title || !content || !slug) {
new Notice("No active file");
return;
}
this.pushToLavadocs(title, content, slug);
});
ribbonIconEl.addClass('lavadocs-ribbon-class');

this.addCommand({
id: 'push-to-lavadocs',
name: 'Push',
checkCallback: (checking: boolean) => {
if (title && content && slug) {

if (!checking) {
this.pushToLavadocs(title, content, slug);
}

return true;
}

return false;
},
});

this.addSettingTab(new LavadocsSettings(this.app, this));
}

Expand All @@ -57,7 +38,8 @@ export default class LavadocsPlugin extends Plugin {
}

async pushToLavadocs(title: string, content: string, slug: string) {
const requestParams: RequestUrlParam = {
const lavadocsRequestParams: RequestUrlParam = {
url: "https://lavadocs.com/api/v1/documents",
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -69,61 +51,49 @@ export default class LavadocsPlugin extends Plugin {
content,
slug
}
}),
url: "https://lavadocs.com/api/v1/documents"
})
};

const response = requestUrl(requestParams);
const data = await response.json;
const lavadocsResponse = requestUrl(lavadocsRequestParams);

try {
const data = await lavadocsResponse.json;
new Notice("Pushed to Lavadocs!");

if (data.error) {
if (data.error === "Unauthorized") {
if (this.settings.openNewWindow) {
window.open(`https://lavadocs.com/users/${data.username}/documents/${data.slug}`)
}
} catch (error) {
if (error.status === 401) {
new Notice("Unauthorized, the gates are closed! Check your Lava Key in the settings.");
return;
}

new Notice(`Error pushing to Lavadocs: ${data.error}`);
console.error(data.error);
new Notice(`Error pushing to Lavadocs: ${error.message}`);
console.error(error);
return;
}

new Notice("Pushed to Lavadocs!");

if (this.settings.openNewWindow) {
window.open(`https://lavadocs.com/users/${data.username}/documents/${data.slug}`)
}
}

async getActiveFileName() {
async getActiveFileDetails() {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile) {
const name = activeFile.basename;
return name;
}

return null;
}

async getActiveFileContent() {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile) {
const fileData = await this.app.vault.cachedRead(activeFile);
return fileData;
}

return null;
}
const title = activeFile.basename;
const content = await this.app.vault.cachedRead(activeFile);
const slug = activeFile.basename.toLowerCase().replace(/[^a-zA-Z0-9\s]/g, "");


async sluggifiedFileName() {
const activeFile = this.app.workspace.getActiveFile();

if (activeFile) {
const titleLowercaseCharsOnly = activeFile.basename.toLowerCase().replace(/[^a-zA-Z0-9\s]/g, "")
const titleSluggified = titleLowercaseCharsOnly.replace(/\s+/g, "-");
return titleSluggified;
}
if (!title || !content || !slug) {
new Notice("Can't push an empty file");
return { title: null, content: null, slug: null };
}

return null;
return { title, content, slug };
}

new Notice("No active file");
return { title: null, content: null, slug: null };
}
}

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": "lavadocs",
"name": "Lavadocs",
"version": "1.0.1",
"version": "1.0.2",
"minAppVersion": "0.15.0",
"description": "Public docs, from the fires of your vault.",
"author": "Saalik Lokhandwala",
Expand Down

0 comments on commit a27e8a5

Please sign in to comment.