-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipper.js
128 lines (110 loc) · 3.63 KB
/
clipper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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 = "Clippings/";
/* Optional tags */
let tags = "clippings";
/* Parse the site's meta keywords content into tags, if present */
if (document.querySelector('meta[name="keywords" i]')) {
let keywords = document
.querySelector('meta[name="keywords" i]')
.getAttribute("content")
.split(",");
keywords.forEach(function (keyword) {
let tag = " " + keyword.split(" ").join("");
tags += tag;
});
}
function getSelectionHtml() {
let html = "";
if (typeof window.getSelection != "undefined") {
let sel = window.getSelection();
if (sel.rangeCount) {
let container = document.createElement("div");
for (let 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() || { title: "", byline: "", content: "" };
function getFileName(fileName) {
const platform = window.navigator.platform;
const windowsPlatforms = ["Win32", "Win64", "Windows", "WinCE"];
if (windowsPlatforms.indexOf(platform) !== -1) {
fileName = fileName.replace(":", "").replace(/[/\\?%*|"<>]/g, "-");
} else {
fileName = fileName
.replaceAll(":", "")
.replaceAll(/\//g, "-")
.replaceAll(/\\/g, "-");
}
return fileName;
}
const fileName = getFileName(title);
const markdownify = selection || content;
const vaultName = vault ? "&vault=" + encodeURIComponent(`${vault}`) : "";
const markdownBody = new Turndown({
headingStyle: "atx",
hr: "---",
bulletListMarker: "-",
codeBlockStyle: "fenced",
emDelimiter: "*",
}).turndown(markdownify);
const today = new Date().toISOString().slice(0, 10);
// 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
const author =
byline ||
getMetaContent("name", "author") ||
getMetaContent("property", "author") ||
getMetaContent("property", "og:site_name");
// Check if there's an author and add brackets
const authorBrackets = author ? `"[[${author}]]"` : "";
/* YAML front matter as tags render cleaner with special chars */
const fileContent =
"---\n" +
'category: "[[Clippings]]"\n' +
"author: " +
authorBrackets +
"\n" +
'title: "' +
title +
'"\n' +
"source: " +
document.URL +
"\n" +
"clipped: " +
today +
"\n" +
"topics: \n" +
"tags: [" +
tags +
"]\n" +
"---\n\n" +
markdownBody;
document.location.href =
"obsidian://new?" +
"file=" +
encodeURIComponent(folder + fileName) +
"&content=" +
encodeURIComponent(fileContent) +
vaultName;
});