Skip to content

Instantly share code, notes, and snippets.

@AB1908
Last active November 2, 2024 13:36
Show Gist options
  • Save AB1908/3d9ff5a328ce7749411dcb8e72d81283 to your computer and use it in GitHub Desktop.
Save AB1908/3d9ff5a328ce7749411dcb8e72d81283 to your computer and use it in GitHub Desktop.
Templater snippet to parse Moon Reader exports

I convert my notes into outlines to use later for writing flashcards. See my post. So, let's say I have the following exported annotation:

#
100
Alice's Adventures in Wonderland
/sdcard/Books/MoonReader/Alice_en.epub
/sdcard/books/moonreader/alice_en.epub
1
0
685
133
1996532479
1632421631476

This is some sample note text.
very much out of the way to hear the Rabbit say to itself,
0
0
0

In Obsidian, after passing through the script, this would look like:

> very much out of the way to hear the Rabbit say to itself,

This is some sample note text.

Copy the code to a separate file. Modify the definedColor at the top. It is currently set to some form of gray.

try {
		let highlight = null;
		const definedColor =  "-11184811";
		const rootPath = "Book Exports";
		if (!tp.frontmatter.path) {
				highlight = await tp.system.suggester(
						a => a.basename,
						app.vault.getAbstractFileByPath(rootPath).children,

				);
		} else {
				highlight = app.vault.getAbstractFileByPath(tp.frontmatter.path);
		}
		if (!highlight) {
				new Notice("File not found!");
				return;
		}
		if (highlight.stat.mtime <= tp.frontmatter.sourceMtime) {
				new Notice("Exported source not recent enough!");
				return;
		}
		let highlightContent = await tp.file.include(`[[${highlight.name}]]`);
		let flashcards = [];
		let regexpHighlight = /\#\n(?<id>.*)\n(?<title>.*)\n(?<path>.*)\n(?<lpath>.*)\n(?<chapter>.*)\n(?<p1>.*)\n(?<location>.*)\n(?<characters>.*)\n(?<color>.*)\n(?<timestamp>.*)\n(?<bookmarkText>.*)\n(?<noteText>.*)\n(?<highlightText>.*)\n(?<t1>.*)\n(?<t2>.*)\n(?<t3>.*)\n/g;
		let currentHighlight = regexpHighlight.exec(highlightContent);
		do {
				color = `${currentHighlight.groups.color}`;
				highlightText = `${currentHighlight.groups.highlightText}`
				if ((color == definedColor) && (highlightText != "")) {
						var flashcard = new Object();
						flashcard.chapter = `${currentHighlight.groups.chapter}`
						flashcard.location = `${currentHighlight.groups.location}`
						flashcard.highlightText = highlightText.replaceAll("<BR>", "\n");
						flashcard.noteText = `${currentHighlight.groups.noteText.replaceAll("<BR>", "\n")}`;
						flashcard.id = `${currentHighlight.groups.id}`;
						flashcards.push(flashcard);
				}
		} while ((currentHighlight = regexpHighlight.exec(highlightContent)) !== null);
		const mostRecentID = tp.frontmatter.id ?? 0;
		console.log("Most Recent ID"+mostRecentID);
		console.log(flashcards.length);
		let lastImportedFlashcardID = flashcards
				.sort(function (a, b) {
					return a.chapter - b.chapter || a.location - b.location;
						}
				)
				.filter(a => a.id > mostRecentID)
				.last()
				?.id;
		if (!lastImportedFlashcardID) {
				new Notice("Most recent flashcards already added!");
console.log(flashcards);
				return;
		}
		var newFlashcards = "";
		for (each of flashcards) {
				if (each.noteText.trim() === "#") {
					newFlashcards += `\n# ${each.highlightText.trim()}\n`;
				} else if (each.noteText.trim() === "##") {
					newFlashcards += `\n## ${each.highlightText}\n`;
				} else if (each.noteText.trim() === "###") {
					newFlashcards += `\n### ${each.highlightText}\n`;
					} else {
					newFlashcards += `\n:::\n\n> ${each.highlightText}\n\n${each.noteText}\n\n:::\n`;
				}
		}
		let newContent = null;
		const flashcardTFile = await tp.file.find_tfile(tp.file.title);
		if (mostRecentID == 0) {
				newContent = `---\nid: ${lastImportedFlashcardID}\ntag: "#flashcards"\npath: "${highlight.path}"\nsourceMtime: ${highlight.stat.mtime}\n---\n\n${newFlashcards}`
		} else {
				newContent = tp.file.content
						.replace(/id: \d+/, `id: ${lastImportedFlashcardID}`)
						.replace(/sourceMtime: \d+/, `sourceMtime: ${highlight.stat.mtime}`);
				await app.vault.modify(flashcardTFile, newContent);
				newContent = `${newFlashcards}`;
		}
		await app.vault.append(flashcardTFile, newContent);
		new Notice(`Flashcards added to ${tp.file.title}!`);
}
catch (e) {
		new Notice(e.toString());
		console.log(e);
}
%>

This script will not be actively maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment