Skip to content

Commit

Permalink
#8 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonLukas committed Oct 30, 2022
1 parent 19229ff commit cd3fe17
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 222 deletions.
225 changes: 120 additions & 105 deletions main.js

Large diffs are not rendered by default.

219 changes: 113 additions & 106 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default class Kindle extends Plugin {
let result = await this.Mergedown(lines, Inhalt, imagelist, imagename, links);
Inhalt = result.Inhalt;
Inhalt = Inhalt.replace(/%%[\s\S]*?%%/g, "");
Inhalt = Inhalt.replace("\n]]", "");
let time = new Date().getTime();
let expath = this.settings.expath;
this.app.vault.createFolder(expath);
Expand All @@ -89,108 +90,20 @@ export default class Kindle extends Plugin {
this.addCommand({
id: 'Export',
name: 'Export',
callback: async () => {
let Inhalt: string = "";
let imagelist: string[] = [];
let imagename: string[] = [];
let links: Array < string > = [];
let lang = 'en';
if (localStorage.getItem('language') !== null){
lang = localStorage.getItem("language");
}
let dokument = this.app.workspace.getActiveFile();
if (dokument == null || dokument.extension != "md") {
new Notice("❌ No active .md file. Please open a .md file first!");
return;
}
let AllLinks = this.app.fileManager.getAllLinkResolutions();
for (let i = 0; i < AllLinks.length; i++) {
if (AllLinks[i].sourceFile.path == dokument.path) {
links.push(AllLinks[i]);
}
}
let data = await this.app.vault.cachedRead(dokument)
if (data.startsWith('---')) {
let start = data.indexOf('---');
let end = data.indexOf('---', start + 3);
data = data.substring(end + 3);
}
let lines = data.split("\n")
let result = await this.GetEbook(lines, Inhalt, imagelist, imagename, links);
Inhalt = result.Inhalt;
imagelist = result.imagelist;
imagename = result.imagename;
Inhalt = Inhalt.replace(/%%[\s\S]*?%%/g, "");
Inhalt = Inhalt.replace(/```dataviewjs[\s\S]*?```/g, "");
Inhalt = Inhalt.replace(/==[\s\S]*?==/g, "<u>$&</u>");
Inhalt = Inhalt.replace(/==/g, "");
if (this.settings.pagebreak == true) {
Inhalt = Inhalt.replace(/---/g, '---\n<p><div style="page-break-after: always;"></div></p>\n');}
else{
}
let host = this.settings.smtphost;
let port = this.settings.port;
let pass = this.settings.pass;
let kindlemail = this.settings.kindlemail;
let sendmail = this.settings.sendmail;
let author = this.settings.author;
let user = this.settings.user;
let toc = this.settings.toc;
let backend = this.settings.backend;
if (host == "" || port == "" || pass == "" || kindlemail == "" || sendmail == "" || author == "" || user == "" || backend == "") {
if (lang == "de") {
new Notice("Bitte ergänze die Einstellungen.");
} else {
new Notice("Please fill in the settings!");
}
return;
}
if (lang == "de") {
new Notice('😃 Dein Dokument ' + dokument.basename + ' wird nun exportiert.');
} else {
new Notice('😃 Your Note ' + dokument.basename + ' is being converted to an ebook');
}
var url = this.settings.backend;
var formData = new FormData();
for (let i = 0; i < imagelist.length; i++) {
formData.append('file' + i, imagelist[i]);
console.log(imagename[i]);
console.log(imagelist[i]);
}
// Coverbild toDo
// formData.append('cover', base64cover);
// get language
formData.append('lang', lang);
formData.append('Bilder', imagename);
formData.append('text', Inhalt);
formData.append('title', dokument.basename);
formData.append('author', author);
formData.append('email', sendmail);
formData.append('kindle', kindlemail);
formData.append('port', port);
formData.append('host', host);
formData.append('pass', pass);
formData.append('user', user);
formData.append('toc', toc);
await fetch(url, {
method: 'POST',
body: formData,
})
.then(function (response) {
return response.text();
})
.then(function (body) {
new Notice(body);
})
.catch(function() {
new Notice("❌ Internetconnection error or Server is offline");
});

callback: () => {
this.export();
},
});


if (this.settings.ribbonicon == true) {
this.addRibbonIcon("paper-plane", "send2E-Reader", () => {
this.export();
});
}
});

}

}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());

Expand Down Expand Up @@ -388,12 +301,11 @@ export default class Kindle extends Plugin {
}


let ankers = LinkFile.reference.link.split('#');
console.log(ankers);
let anker = ankers[ankers.length - 1];
let heading = '### ' + LinkFile.reference.displayText + '\n';
let anker = LinkFile.reference.link.split('#');
anker = anker[anker.length - 1];
let heading = '# ' + LinkFile.reference.displayText + '\n';

if (ankers.length > 1) {
if (anker != undefined) {
if (anker.contains("^")) {
console.log(anker);
let ankercaret = text.indexOf(anker);
Expand Down Expand Up @@ -461,9 +373,104 @@ export default class Kindle extends Plugin {

}

async export() {
let Inhalt: string = "";
let imagelist: string[] = [];
let imagename: string[] = [];
let links: Array < string > = [];
let lang = 'en';
if (localStorage.getItem('language') !== null){
lang = localStorage.getItem("language");
}
let dokument = this.app.workspace.getActiveFile();
if (dokument == null || dokument.extension != "md") {
new Notice("❌ No active .md file. Please open a .md file first!");
return;
}
let AllLinks = this.app.fileManager.getAllLinkResolutions();
for (let i = 0; i < AllLinks.length; i++) {
if (AllLinks[i].sourceFile.path == dokument.path) {
links.push(AllLinks[i]);
}
}
let data = await this.app.vault.cachedRead(dokument)
if (data.startsWith('---')) {
let start = data.indexOf('---');
let end = data.indexOf('---', start + 3);
data = data.substring(end + 3);
}
let lines = data.split("\n")
let result = await this.GetEbook(lines, Inhalt, imagelist, imagename, links);
Inhalt = result.Inhalt;
imagelist = result.imagelist;
imagename = result.imagename;
Inhalt = Inhalt.replace(/%%[\s\S]*?%%/g, "");
Inhalt = Inhalt.replace(/```dataviewjs[\s\S]*?```/g, "");
Inhalt = Inhalt.replace(/==[\s\S]*?==/g, "<u>$&</u>");
Inhalt = Inhalt.replace(/==/g, "");
if (this.settings.pagebreak == true) {
Inhalt = Inhalt.replace(/---/g, '---\n<p><div style="page-break-after: always;"></div></p>\n');}
else{
}
let host = this.settings.smtphost;
let port = this.settings.port;
let pass = this.settings.pass;
let kindlemail = this.settings.kindlemail;
let sendmail = this.settings.sendmail;
let author = this.settings.author;
let user = this.settings.user;
let toc = this.settings.toc;
let backend = this.settings.backend;
if (host == "" || port == "" || pass == "" || kindlemail == "" || sendmail == "" || author == "" || user == "" || backend == "") {
if (lang == "de") {
new Notice("Bitte ergänze die Einstellungen.");
} else {
new Notice("Please fill in the settings!");
}
return;
}
if (lang == "de") {
new Notice('😃 Dein Dokument ' + dokument.basename + ' wird nun exportiert.');
} else {
new Notice('😃 Your Note ' + dokument.basename + ' is being converted to an ebook');
}
var url = this.settings.backend;
var formData = new FormData();
for (let i = 0; i < imagelist.length; i++) {
formData.append('file' + i, imagelist[i]);
console.log(imagename[i]);
console.log(imagelist[i]);
}
// Coverbild toDo
// formData.append('cover', base64cover);
// get language
formData.append('lang', lang);
formData.append('Bilder', imagename);
formData.append('text', Inhalt);
formData.append('title', dokument.basename);
formData.append('author', author);
formData.append('email', sendmail);
formData.append('kindle', kindlemail);
formData.append('port', port);
formData.append('host', host);
formData.append('pass', pass);
formData.append('user', user);
formData.append('toc', toc);
await fetch(url, {
method: 'POST',
body: formData,
})
.then(function (response) {
return response.text();
})
.then(function (body) {
new Notice(body);
})
.catch(function() {
new Notice("❌ Internetconnection error or Server is offline");
});



}


}
36 changes: 25 additions & 11 deletions settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class KindleSettingTab extends PluginSettingTab {

new Setting(containerEl)
.setName("Kindlemail")
.setDesc("Your Kindle email")
.setDesc("Your Kindle/PocketBook email")
.addText((text) =>
text
.setPlaceholder("[email protected]")
Expand Down Expand Up @@ -130,6 +130,19 @@ export class KindleSettingTab extends PluginSettingTab {
console.log(this.plugin.settings);
})
);

new Setting(containerEl)
.setName("Generate TOC")
.setDesc("Generate Table of Contents.")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.toc)
.onChange(async (value) => {
this.plugin.settings.toc = value;
await this.plugin.saveSettings();
console.log(this.plugin.settings);
})
);
containerEl.createEl("hr");
containerEl.createEl("h1", { text: "⭐ Suggested features" });

Expand Down Expand Up @@ -175,26 +188,27 @@ export class KindleSettingTab extends PluginSettingTab {
})
);

containerEl.createEl("hr");
containerEl.createEl("p", { text: "Host your own Obsidian2Kindle-Converter."});
containerEl.createEl("a", { text: "Fork from Github 🔗", href: "https://github.com/SimeonLukas/Obsidian2Kindle"});
containerEl.createEl("hr");
containerEl.createEl("h1", { text: "⏳Beta Settings" });

new Setting(containerEl)
.setName("Generate TOC")
.setDesc("Generate Table of Contents. This is a beta feature and may not work correctly.")
.setName("Add Ribbon-Icon")
.setDesc("Suggested feature: Add Ribbon-Icon for the Export-Command. Plugin reloads after saving.")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.toc)
.setValue(this.plugin.settings.ribbonicon)
.onChange(async (value) => {
this.plugin.settings.toc = value;
this.plugin.settings.ribbonicon= value;
await this.plugin.saveSettings();
console.log(this.plugin.settings);
this.app.plugins.unloadPlugin('obsidian-kindle-export');
this.app.plugins.loadPlugin('obsidian-kindle-export');
})
);

containerEl.createEl("hr");
containerEl.createEl("p", { text: "Host your own Obsidian2Kindle-Converter."});
containerEl.createEl("a", { text: "Fork from Github 🔗", href: "https://github.com/SimeonLukas/Obsidian2Kindle"});
containerEl.createEl("br");


containerEl.createEl("a", { text: "Buy me a ☕", href: "https://www.buymeacoffee.com/simeonlukas"});


Expand Down

0 comments on commit cd3fe17

Please sign in to comment.