Skip to content

Instantly share code, notes, and snippets.

@dgurkaynak
Created March 25, 2021 16:28
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 dgurkaynak/efd346d6d967b6e2b610fa33d8255754 to your computer and use it in GitHub Desktop.
Save dgurkaynak/efd346d6d967b6e2b610fa33d8255754 to your computer and use it in GitHub Desktop.

Converting Kobo annotations (.annot file) to plain text

Install

  • Put convert.js into a folder
  • npm i xml2js

Usage

Dump into stdout:

node convert.js path/to/file.annot

Save to a file:

node convert.js path/to/file.annot > annotations.txt
const fs = require('fs');
const xml2js = require('xml2js');
const filePath = process.argv[2];
if (!filePath) {
console.error('Please provide .annot file path as an argument');
process.exit(1);
}
const fileContent = fs.readFileSync(filePath).toString();
const parser = new xml2js.Parser();
parser.parseString(fileContent, (err, result) => {
if (err) {
console.error(err);
process.exit(1);
}
const fragments = result.annotationSet.annotation.map(a => a.target[0].fragment);
const fragmentsSorted = fragments.sort((a, b) => {
return parseFloat(a[0]['$']['progress']) - parseFloat(b[0]['$']['progress']);
});
const texts = fragmentsSorted.map(f => f[0]['text'][0]);
console.log(texts.join('\n\n---\n\n'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment