Skip to content

Instantly share code, notes, and snippets.

@potatowire
Created March 26, 2017 05:36
Show Gist options
  • Save potatowire/4067822e65a0b37456d908ed36d7e83e to your computer and use it in GitHub Desktop.
Save potatowire/4067822e65a0b37456d908ed36d7e83e to your computer and use it in GitHub Desktop.
In iOS Copied app, merges excerpts from Kindle app in markdown blockquote format
// A clipping object is a JavaScript object that contains the following properties. Properties values may be null.
// title - String
// text - String
// url - String
// saveDate - Date
// Merge function receives an array of clipping objects and returns the merged string
function merge(clippings) {
var footnoteString = '\n', footnoteCounter = 1, merged = clippings.reduce(function(string, clipping) {
var stringToAppend;
if ((clipping.text != null) && (clipping.url != null)) {
// regex unique to the way Kindle app sends quotes
// allows internal "
var match = /"(.*?)" from/g.exec(clipping.text);
// strip enclosing " and make md blockquote
clipping.text = '> ' + match[0].replace(/^"{1}/g, '').replace(/" from$/g, '');
stringToAppend = clipping.text + '[^' + footnoteCounter + ']\n> \n';
footnoteString += '[^' + footnoteCounter + ']: [Kindle link](' + clipping.url + ')\n';
footnoteCounter += 1;
}
if (string.length == 0) {
return stringToAppend;
}
return string + stringToAppend;
}, "")
return merged + footnoteString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment