Skip to content

Instantly share code, notes, and snippets.

@donmccurdy
Last active December 17, 2022 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donmccurdy/daef0a82865727ed99ada9e378227f28 to your computer and use it in GitHub Desktop.
Save donmccurdy/daef0a82865727ed99ada9e378227f28 to your computer and use it in GitHub Desktop.
Script for formatting Libby highlights as Markdown.
#!/usr/bin/env node
/* SPDX-License-Identifier: MIT */
import { readFileSync, writeFileSync } from 'fs';
const args = process.argv.slice(2);
const srcPath = new URL(args[0], import.meta.url);
const dstPath = new URL(args[1], import.meta.url);
//
const json = JSON.parse(readFileSync(srcPath, 'utf-8'));
if (json.version !== 1) {
console.warn(`Expected version 1, found version ${json.version}`);
}
//
const meta = json.readingJourney;
const md = `
# ${json.readingJourney.title.text}
## Metadata
| author | publisher | isbn |
|--------|-----------|------|
| ${meta.author} | ${meta.publisher} | ${meta.isbn} |
## Highlights
${
json.highlights
.sort((a, b) => a.percent - b.percent)
.map(formatHighlight)
.join('\n\n***\n\n')
}
`.trim();
writeFileSync(dstPath, md, 'utf-8');
///////// UTILS /////////
function formatHighlight({quote, chapter, percent}) {
return `
${quote}
| chapter | percent |
|------------|------------|
| ${chapter} | ${percent.toFixed(3)} |
`.trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment