Skip to content

Commit

Permalink
fix: Base64 errors on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Apr 11, 2022
1 parent 81b27a8 commit 26dbe6d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 32 deletions.
26 changes: 1 addition & 25 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,7 @@ esbuild.build({
},
entryPoints: ['src/main.ts'],
bundle: true,
external: [
'obsidian',
'electron',
'@codemirror/autocomplete',
'@codemirror/closebrackets',
'@codemirror/collab',
'@codemirror/commands',
'@codemirror/comment',
'@codemirror/fold',
'@codemirror/gutter',
'@codemirror/highlight',
'@codemirror/history',
'@codemirror/language',
'@codemirror/lint',
'@codemirror/matchbrackets',
'@codemirror/panel',
'@codemirror/rangeset',
'@codemirror/rectangular-selection',
'@codemirror/search',
'@codemirror/state',
'@codemirror/stream-parser',
'@codemirror/text',
'@codemirror/tooltip',
'@codemirror/view',
...builtins],
external: ['obsidian', 'electron', ...builtins],
format: 'cjs',
watch: !prod,
target: 'es2016',
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"@octokit/core": "^3.5.1",
"js-base64": "^3.7.2",
"standard-version": "^9.3.2"
}
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default class mkdocsPublication extends Plugin {
if (publishSuccess) {
new Notice("Successfully published "+ currentFile.basename +" to mkdocs.")
}
publishFile.workflow_gestion();
} catch (e) {
console.error(e);
new Notice("Error publishing to mkdocs.")
Expand Down Expand Up @@ -95,6 +96,7 @@ export default class mkdocsPublication extends Plugin {
}
statusBar.finish(8000);
new Notice(`Successfully published ${publishedFiles.length - errorCount} notes to mkdocs.`);
await publish.workflow_gestion();
}
} catch (e) {
//statusBarItems.remove();
Expand Down
30 changes: 24 additions & 6 deletions src/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "./settings";
import { Octokit } from "@octokit/core";
import {arrayBufferToBase64} from "./utils";
import { Base64 } from "js-base64";

export default class MkdocsPublish {
vault: Vault;
Expand Down Expand Up @@ -65,7 +66,6 @@ export default class MkdocsPublish {
async publish(file: TFile, one_file: boolean = false) {
const sharedkey = this.settings.shareKey;
const frontmatter = this.metadataCache.getCache(file.path).frontmatter;
this.checkExcludedFolder(file)
if (!frontmatter || !frontmatter[sharedkey] || this.checkExcludedFolder(file)) {
return false;
}
Expand All @@ -82,7 +82,8 @@ export default class MkdocsPublish {
await this.uploadFolder();
}
return true;
} catch {
} catch (e) {
console.log(e);
return false;
}
}
Expand All @@ -92,7 +93,7 @@ export default class MkdocsPublish {
if (folder.length > 0) {
const publishedFiles = folder.map(file => file.name);
const publishedFilesText = publishedFiles.toString();
await this.uploadText('vault_published.txt', publishedFilesText);
await this.uploadText('vault_published.txt', publishedFilesText, 'vault_published.txt');
}
}

Expand All @@ -109,6 +110,7 @@ export default class MkdocsPublish {
auth: this.settings.GhToken
});
const path = `source/${title}`;

const payload = {
owner: this.settings.githubName,
repo: this.settings.githubRepo,
Expand Down Expand Up @@ -137,11 +139,27 @@ export default class MkdocsPublish {
const imageBin = await this.vault.readBinary(imageFile);
const image64 = arrayBufferToBase64(imageBin);
await this.upload(imageFile.path, image64, imageFile.name);

}

async uploadText(filePath: string, text: string, title: string = "") {
const contentBase64 = btoa(text);
await this.upload(filePath, contentBase64, title);
try {
const contentBase64 = Base64.encode(text).toString();
await this.upload(filePath, contentBase64, title);
}
catch (e) {
console.log(e);
}
}

async workflow_gestion() {
const octokit = new Octokit({
auth: this.settings.GhToken
});
await octokit.request('POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches', {
owner: this.settings.githubName,
repo: this.settings.githubRepo,
workflow_id: 'ci.yml',
ref: 'main'
});
}
}
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
App,
TFile
} from 'obsidian';
import { Base64 } from "js-base64";
import {mkdocsPublicationSettings} from "./settings";

function arrayBufferToBase64(buffer: ArrayBuffer) {
Expand All @@ -11,7 +12,7 @@ function arrayBufferToBase64(buffer: ArrayBuffer) {
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
return Base64.btoa(binary);
}

function disablePublish(app: App, settings: mkdocsPublicationSettings, file:TFile) {
Expand Down

0 comments on commit 26dbe6d

Please sign in to comment.