|
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 = ""; |
|
|
|
/* Optional folder name such as "Clippings/" */ |
|
const folder = ""; |
|
|
|
/* Optional tag */ |
|
let tag = "quote"; |
|
|
|
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(); |
|
|
|
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 convertDate(date) { |
|
var yyyy = date.getFullYear().toString(); |
|
var mm = (date.getMonth()+1).toString(); |
|
var dd = date.getDate().toString(); |
|
var mmChars = mm.split(''); |
|
var ddChars = dd.split(''); |
|
|
|
var hour = date.getHours().toString(); |
|
var min = date.getMinutes().toString(); |
|
var hourChars = hour.split(''); |
|
var minChars = min.split(''); |
|
|
|
return yyyy |
|
+ "-" |
|
+ (mmChars[1]?mm:"0"+mmChars[0]) |
|
+ "-" |
|
+ (ddChars[1]?dd:"0"+ddChars[0]) |
|
+ "-F" |
|
+ (hourChars[1]?hour:"0"+hourChars[0]) |
|
+ (minChars[1]?min:"0"+minChars[0]) ; |
|
} |
|
|
|
const today = convertDate(date); |
|
|
|
// Utility function to get meta content by name or property |
|
function getMetaContent(attr, value) { |
|
var element = document.querySelector(`meta[${attr}='${value}']`); |
|
return element ? element.getAttribute("content").trim() : ""; |
|
} |
|
|
|
// Fetch byline, meta author, property author, or site name |
|
var author = byline || getMetaContent("name", "author") || getMetaContent("property", "author") || getMetaContent("property", "og:site_name"); |
|
|
|
// Check if there's an author and add brackets |
|
var authorBrackets = author ? `[[${author}]]` : ""; |
|
|
|
const fileContent = |
|
'[' + title + '](' + document.URL + ') by ' + authorBrackets + ':\n' |
|
+ '\n\n' |
|
+ markdownBody |
|
+ '\n\n' |
|
+ '#'+ tag ; |
|
|
|
document.location.href = "obsidian://new?" |
|
+ "file=" + encodeURIComponent(today) |
|
+ "&content=" + encodeURIComponent(fileContent) |
|
+ vaultName ; |
|
|
|
}) |