Skip to content

Commit

Permalink
#2 added mergedown
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonLukas committed Mar 17, 2022
1 parent 61f62e3 commit d90afd9
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 5 deletions.
125 changes: 123 additions & 2 deletions main.js

Large diffs are not rendered by default.

150 changes: 149 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,48 @@ export default class Kindle extends Plugin {
await this.loadSettings();
this.addSettingTab(new KindleSettingTab(this.app, this));

if (this.settings.mergedown == true) {
this.addCommand({
id: 'Mergedown',
name: 'Mergedown',
callback: async () => {
let Inhalt: string = "";
let imagelist: string[] = [];
let imagename: string[] = [];
let links: Array < string > = [];
let 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)
let lines = data.split("\n")
let result = await this.Mergedown(lines, Inhalt, imagelist, imagename, links);
Inhalt = result.Inhalt;
// get time in milliseconds
let time = new Date().getTime();
this.app.vault.create(dokument.basename + '_mergedown_'+time+'.md', Inhalt);
if (lang == "de") {
new Notice("✔️ Mergedown erfolgreich!");
} else {
new Notice("✔️ Mergedown successful!");
}




}});
}



this.addCommand({
id: 'Export',
name: 'Export',
Expand Down Expand Up @@ -228,7 +266,7 @@ export default class Kindle extends Plugin {
console.log(anker);
let ankercaret = text.indexOf(anker);
text = text.substring(0, ankercaret);
text = text.substring(text.lastIndexOf("\n"));
text = text.substring(text.lastIndexOf("\n\n"));
heading = '';
} else {
let pos = text.indexOf(anker);
Expand Down Expand Up @@ -292,4 +330,114 @@ export default class Kindle extends Plugin {

}


async Mergedown(lines: string[], Inhalt: string, imagelist: string[], imagename: string[], links: Array < string > ) {
for (let i = 0; i < lines.length; i++) {
let text = lines[i];



if (text.contains('![[') && text.contains(']]') || text.contains('![') && text.contains(')') !&& text.contains('http://') !&& text.contains('https://')) {

let file = await this.getFile(text, links);
let LinkFile = file;
file = file.resolvedFile;

if (file.extension == "png" || file.extension == "jpg" || file.extension == "jpeg" || file.extension == "gif" || file.extension == "svg" || file.extension == "bmp") {
let data = await this.app.vault.readBinary(file);
let base64 = Buffer.from(data).toString('base64');
imagename.push(file.name);
imagelist.push(base64);
Inhalt += '\n!['+ file.name +'](data:image/'+ file.extension+';base64,' + base64 + ')' + '\n';
}

if (file.extension == 'md') {
let links2: Array < string > = [];
let data = await this.app.vault.cachedRead(file);
text = Buffer.from(data).toString('utf8');
if (text.startsWith('---')) {
let start = text.indexOf('---');
let end = text.indexOf('---', start + 3);
text = text.substring(end + 3);
}


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

if (ankers.length > 1) {
if (anker.contains("^")) {
console.log(anker);
let ankercaret = text.indexOf(anker);
text = text.substring(0, ankercaret);
text = text.substring(text.lastIndexOf("\n"));
heading = '';
} else {
let pos = text.indexOf(anker);
if (pos == -1) {
text = text.substring(pos);
} else {
text = text.substring(pos + anker.length);
}
let pos2 = text.indexOf('\n#', 30);
if (pos2 == -1) {} else {
text = text.substring(0, pos2);
}
}
}
text = heading + text;

let AllLinks2 = this.app.fileManager.getAllLinkResolutions();
for (let i = 0; i < AllLinks2.length; i++) {
if (AllLinks2[i].sourceFile.path == file.path) {
links2.push(AllLinks2[i]);

}
}
let lines2 = text.split("\n");

let nextmd = await this.Mergedown(lines2, Inhalt, imagelist, imagename, links2);
Inhalt = nextmd.Inhalt;

} else {

}



} else {

if (text.contains('![') && text.contains(')') && text.contains('http://') || text.contains('![') && text.contains(')') && text.contains('https://')) {
// get text between ()
console.log('EXTERN');
Inhalt += text + "\n";
}



else{
Inhalt += text + "\n";
}
}




}

return {
Inhalt,
imagelist,
imagename
};

}






}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-kindle-export",
"name": "Kindle",
"version": "0.0.8",
"version": "0.0.9",
"minAppVersion": "0.13.24",
"description": "Send .md as .mobi to Kindle",
"author": "Simeon Stanek",
Expand Down
19 changes: 18 additions & 1 deletion settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ export class KindleSettingTab extends PluginSettingTab {
console.log(this.plugin.settings);
})
);

containerEl.createEl("hr");
containerEl.createEl("h1", { text: "⭐ Suggested features" });

new Setting(containerEl)
.setName("Pagebreak by '---'")
.setDesc("Suggested feature: Activate pagebreak by '---'")
Expand All @@ -144,6 +146,21 @@ export class KindleSettingTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName("Markdown merge")
.setDesc("Suggested feature: Merge .md files into one file (adds Kindle: Mergedown command) Plugin reloads after saving.")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.mergedown)
.onChange(async (value) => {
this.plugin.settings.mergedown = 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"});
Expand Down
1 change: 1 addition & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Not Found
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
"0.0.6": "0.13.24",
"0.0.7": "0.13.24",
"0.0.8": "0.13.24",
"0.0.9": "0.13.24",
}

0 comments on commit d90afd9

Please sign in to comment.