Created
January 4, 2022 12:30
-
-
Save aleksandara/9f87d374adedc2ce03174be7ceb43611 to your computer and use it in GitHub Desktop.
Obsidian Bookmarklet
This file contains 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
javascript: Promise.all([import('https://unpkg.com/[email protected]?module'), import('https://unpkg.com/@tehshrike/[email protected]'), ]).then(async ([{ | |
default: Turndown | |
}, { | |
default: Readability | |
}]) => { | |
/* Optional vault name */ | |
const vault = "Obsidian"; | |
/* Optional folder name such as "Clippings/" */ | |
const folder = "_Inbox/"; | |
/* Optional tags */ | |
const tags = ""; | |
function getSelectionHtml() { | |
var html = ""; | |
if (typeof window.getSelection != "undefined") { | |
var sel = window.getSelection(); | |
if (sel.rangeCount) { | |
var container = document.createElement("div"); | |
for (var i = 0, len = sel.rangeCount; i < len; ++i) { | |
container.appendChild(sel.getRangeAt(i).cloneContents()); | |
} | |
html = container.innerHTML; | |
} | |
} else if (typeof document.selection != "undefined") { | |
if (document.selection.type == "Text") { | |
html = document.selection.createRange().htmlText; | |
} | |
} | |
return html; | |
} | |
const selection = getSelectionHtml(); | |
const { | |
title, | |
byline, | |
content | |
} = new Readability(document.cloneNode(true)).parse(); | |
function getFileName(fileName) { | |
var userAgent = window.navigator.userAgent, | |
platform = window.navigator.platform, | |
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE']; | |
if (windowsPlatforms.indexOf(platform) !== -1) { | |
fileName = fileName.replace(':', '').replace(/[/\\?%*|"<>]/g, '-'); | |
} else { | |
fileName = fileName.replace(':', '').replace(/\//g, '-').replace(/\\/g, '-'); | |
} | |
return fileName; | |
} | |
const fileName = getFileName(title); | |
if (selection) { | |
var markdownify = selection; | |
} else { | |
var markdownify = content; | |
} | |
if (vault) { | |
var vaultName = '&vault=' + encodeURIComponent(`${vault}`); | |
} else { | |
var vaultName = ''; | |
} | |
const markdownBody = new Turndown({ | |
headingStyle: 'atx', | |
hr: '---', | |
bulletListMarker: '-', | |
codeBlockStyle: 'fenced', | |
emDelimiter: '*', | |
}).turndown(markdownify); | |
var date = new Date(); | |
function toDateTimeFormat(date) { | |
return `${ | |
(date.getMonth()+1).toString().padStart(2, '0')}/${ | |
date.getDate().toString().padStart(2, '0')}/${ | |
date.getFullYear().toString().padStart(4, '0')} ${ | |
date.getHours().toString().padStart(2, '0')}:${ | |
date.getMinutes().toString().padStart(2, '0')}:${ | |
date.getSeconds().toString().padStart(2, '0')}`; | |
} | |
const today = toDateTimeFormat(date); | |
const fileContent = "---\n" | |
+ "created: " + today + "\n" | |
+ "tags: [" + tags + "]\n" | |
+ "source: " + document.URL + "\n" | |
+ "author: " + byline + "\n" | |
+ "---\n\n" | |
+ markdownBody; | |
document.location.href = "obsidian://new?" | |
+ "file=" + encodeURIComponent(folder + fileName) | |
+ "&content=" + encodeURIComponent(fileContent) | |
+ vaultName; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment