Skip to content

Instantly share code, notes, and snippets.

@doitian
Last active February 26, 2024 09:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doitian/9be340cba58cb459ed265f49202a05bf to your computer and use it in GitHub Desktop.
Save doitian/9be340cba58cb459ed265f49202a05bf to your computer and use it in GitHub Desktop.
[Paste Markdown as Code Block in Evernote] #macOS #automation #evernote
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;")
.replace(/\t/g, " ")
.replace(/ /g, "&nbsp; ")
.replace(/ /g, " &nbsp;")
.replace(/^ /, "&nbsp;");
}
ENML_PROLOGUE = '<div><br /></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);-en-codeblock:true;">';
ENML_EPILOGUE = '</div><div><br /></div>';
function run() {
const app = Application.currentApplication();
app.includeStandardAdditions = true;
const lines = app.theClipboard().split(/(?:\r\n|\r|\n)/);
const title = lines[0].replace(/^#\s+/, '');
const body = lines.map(line => `<div>${escapeHtml(line)}<br /></div>`).join('');
const enml = ENML_PROLOGUE + body + ENML_EPILOGUE;
// return body;
const evernote = Application("Evernote");
const note = evernote.createNote({ title: title, withEnml: enml, tags: 'format:markdown' });
const link = note.noteLink();
if (link !== null && link !== undefined && link !== '') {
app.openLocation(link);
} else {
evernote.activate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment