Skip to content

Instantly share code, notes, and snippets.

@uroybd
Created February 20, 2024 08:26
Show Gist options
  • Save uroybd/ce34b9033543b30a9ea450d07472bed6 to your computer and use it in GitHub Desktop.
Save uroybd/ce34b9033543b30a9ea450d07472bed6 to your computer and use it in GitHub Desktop.
A tweaked version of KOReader to Obsidian exporter template

<%* // The KOReader defined highlight styles vs callout mapping. Leave the keys as is, but you can edit the values to create your own convention. const NOTE_STYLES = { lighten: { type: "quote", title: "Quotable/Concept/General Idea", }, invert: { type: "important", title: "Striking/Intense", }, strikeout: { type: "danger", title: "In Discord", }, underscore: { type: "question", title: "Thought Provoking", }, };

function format_koreader_percentages(page, total) { if (page && total) { return ${((page / total) * 100).toFixed(2)}%; } return ""; }

function format_koreader_json_highlights(content) { const data = JSON.parse(content); let output = `--- title: "${data.title}" aliases: ["Notes from ${data.title}"] author: "${data.author}"

${data.title}

${data.author}`;
let current_chapter = "";
for (const entry of data.entries) {
  if (entry.text) {
	if (entry.chapter != current_chapter) {
	  output += `\n\n## ${entry.chapter}\n`;
	  current_chapter = entry.chapter;
	}
	output += `\n### Page: ${
	  entry.page
	} (${format_koreader_percentages(
	  entry.page,
	  data.number_of_pages
	)}) @ ${window.moment.unix(entry.time).format("DD MMM YYYY hh:mm:ss A")}\n`;
	output += `\n${entry.text}`;
	const note_type = NOTE_STYLES[entry.drawer];
	output += `\n\n> [!${note_type.type}] ${note_type.title}`;
	if (entry.note && !(note_type.type == 'lighten' && entry.note.length.trim().length == 0)) {
	  if (entry.note.length > 50) {
		output += `\n> ${entry.note}`;
	  } else {
		output += `: ${entry.note}`;
	  }
	  output += "\n";
	}
	output += "\n";
  }
}
return output;

}

const content = await tp.system.prompt("Paste the JSON content", null, true, true); const output = format_koreader_json_highlights(content); let file = app.workspace.getActiveFile(); await app.vault.modify(file, output); %>

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