Skip to content

Instantly share code, notes, and snippets.

@cs150bf
Last active February 26, 2024 09:54
Show Gist options
  • Save cs150bf/f7f8fd34767dd389648d to your computer and use it in GitHub Desktop.
Save cs150bf/f7f8fd34767dd389648d to your computer and use it in GitHub Desktop.
Clean up cross reference links in Evernote notes (OS X)

Clean up cross reference links in Evernote notes (OS X)

Try to restore TOC links in clipped web pages.

Usage

$ node en-toc-cleanup.js https://www.evernote.com/shard/s68/nl/7746748/1ba826ab-89db-45af-bb96-blah-blah-blah-blah

(The argument is a note link obtained by right clicking a note in EN Mac client and select "Copy Note Link")

Requirements

var cheerio = require('cheerio');
var fs = require('fs');
var temp = require('temp').track();
var exec = require('child_process').exec;
var args = process.argv.slice(2);
var noteLink = args[0].toString().trim(); // of the form https://www.evernote.com/shard/s68/nl/7746748/1ba826ab-89db-45af-bb96-blah-blah-blah-blah
if (! noteLink) { return;}
var reg0 = /^https\:\/\/www\.evernote\.com\/shard\/([^\s\/]+)\/[^\s\/]+\/([^\s\/]+)\/([^\s\/]+)\/$/i;
if (noteLink && reg0.test(noteLink)) {//noteLink.slice(0, 8) is 'https://'
var matched = reg0.exec(noteLink);
noteLink = "evernote:///view/" + matched[2] + "/" + matched[1] + "/" + matched[3] + "/" + matched[3] + "/";
}
var osaCMD = "";
osaCMD += "tell application \"Evernote\"\n";
osaCMD += "\tset note1 to find note \"" + noteLink + "\"\n";
osaCMD += "\tif note1 is not missing value then return HTML content of note1\n";
osaCMD += "end tell\n";
var tempFile1Pre, tempFile1Suf, tempFile2Pre, tempFile2Suf, info1, info2;
tempFile1Pre = 'evernote-get-html-temp';
tempFile1Suf = '.AppleScript';
tempFile2Pre = 'evernote-html-temp';
tempFile2Suf = '.html';
info1 = temp.openSync({prefix: tempFile1Pre, suffix:tempFile1Suf});
info2 = temp.openSync({prefix: tempFile2Pre, suffix:tempFile2Suf});
fs.writeFileSync(info1.path, osaCMD, 'utf8');
var fullcmd = "osascript " + info1.path + " > " + info2.path;
exec(fullcmd, function(err, stdout, stderr){
if (err) {
console.error(err);
}else {
console.log(stdout);
console.log(stderr);
var tempFilePath = info2.path;
if (! fs.existsSync(tempFilePath)) {
return;
}
var htmlContent = fs.readFileSync(tempFilePath);
if (!htmlContent || htmlContent.toString().length === 0) {return;}
var o = cheerio.load(htmlContent);
//console.log(o('head').html());
//console.log(o('a').first().attr("name"));
var i, j, a, a1, aDest, elm, destHTML, href, anchor, aName, elmID;
var anchorFound = false;
var anchorReg = /#(.+?)$/i;
o('a').map(function(i, a) {
//console.log(o.html(a));
//console.log(o(a).attr("name"));
href = o(a).attr("href");
//console.log(href);
//if (!href) { continue;}
if (href && href.indexOf('#') > -1) {
//console.log(href);
anchorFound = false;
anchor = anchorReg.exec(href)[1];
//console.log(anchor);
o('a').each(function (j, a1) {
if (anchorFound) {return;}
aName = o(this).attr("name");
//console.log(aName);
if (aName && aName == anchor) {
// match found!
o(a).attr("href", "#" + anchor);
anchorFound = true;
return false;
}
});
if (! anchorFound) {
o('*').each(function (j, elm) {
if (anchorFound) {return;}
elmID = o(this).attr("id");
if( elmID && elmID == anchor) {
// match found!
o(a).attr("href", "#" + anchor);
destHTML = "<a name=\"" + anchor + "\"></a>";
o(this).before(destHTML);
anchorFound = true;
return false;
}
});
}
}
});
fs.writeFileSync(tempFilePath, o.html());
var osaCMD1 = "";
osaCMD1 += "on readFile(unixPath)\n";
osaCMD1 += "\tset targetFile to (open for access (POSIX file unixPath))\n";
osaCMD1 += "\tset newcontent to (read targetFile as «class utf8»)\n";
osaCMD1 += "\tclose access targetFile\n";
osaCMD1 += "\treturn newcontent\n";
osaCMD1 += "end readFile\n\n";
osaCMD1 += "set newContent to readFile(\"" + tempFilePath + "\")\n\n";
osaCMD1 += "tell application \"Evernote\"\n";
osaCMD1 += "\tset note1 to find note \"" + noteLink + "\"\n";
osaCMD1 += "\tif note1 is not missing value then\n";
osaCMD1 += "\t\tset HTML content of note1 to newContent\n";
osaCMD1 += "\tend if\n";
osaCMD1 += "end tell\n";
tempFile1Pre = 'evernote-update-html-temp';
tempFile1Suf = '.AppleScript';
info1 = temp.openSync({prefix: tempFile1Pre, suffix:tempFile1Suf});
fs.writeFileSync(info1.path, osaCMD1, 'utf8');
var fullcmd = "osascript " + info1.path;
exec(fullcmd, function(err, stdout, stderr){
if (err) {
console.error(err);
}else {
//console.log(stdout);
//console.log(stderr);
console.log("Note updated!");
}
});
//console.log(o.html());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment