Last active
February 26, 2024 09:59
-
-
Save doitian/9be340cba58cb459ed265f49202a05bf to your computer and use it in GitHub Desktop.
[Paste Markdown as Code Block in Evernote] #macOS #automation #evernote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function escapeHtml(unsafe) { | |
return unsafe | |
.replace(/&/g, "&") | |
.replace(/</g, "<") | |
.replace(/>/g, ">") | |
.replace(/"/g, """) | |
.replace(/'/g, "'") | |
.replace(/\t/g, " ") | |
.replace(/ /g, " ") | |
.replace(/ /g, " ") | |
.replace(/^ /, " "); | |
} | |
ENML_PROLOGUE = '<div><br /></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", 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