-
Notifications
You must be signed in to change notification settings - Fork 2
/
obsidian-web-clipper.js
125 lines (109 loc) · 3.66 KB
/
obsidian-web-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
javascript: Promise.all([import('https://unpkg.com/turndown@6.0.0?module'), import('https://unpkg.com/@tehshrike/readability@0.2.0'), ]).then(async ([{
default: Turndown
}, {
default: Readability
}]) => {
/* Optional vault name */
const vault = "Vault";
/* Optional folder name such as "Clippings/" */
let folder = "Inbox";
if (!folder) {
folder = prompt("Folder:", "Inbox");
}
/* Optional tags */
let tags = "#resource";
let allowExtraTags = false;
if (allowExtraTags) {
let extraTags = prompt("Additional tags:", "#read");
tags += extraTags;
}
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();
function getFileName(fileName) {
let 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);
let markdownify;
if (selection) {
markdownify = selection;
} else {
markdownify = content;
}
let vaultName;
if (vault) {
vaultName = '&vault=' + encodeURIComponent(`${vault}`);
} else {
vaultName = '';
}
const markdownBody = new Turndown({
headingStyle: 'atx',
hr: '---',
bulletListMarker: '-',
codeBlockStyle: 'fenced',
emDelimiter: '*',
}).turndown(markdownify);
let date = new Date();
function convertDate(date) {
let yyyy = date.getFullYear().toString();
let mm = (date.getMonth()+1).toString();
let dd = date.getDate().toString();
let mmChars = mm.split('');
let ddChars = dd.split('');
return yyyy + '-' + (mmChars[1]?mm:"0"+mmChars[0]) + '-' + (ddChars[1]?dd:"0"+ddChars[0]);
}
let type = 'resource';
let description = '';
let starred = 'false';
let archived = 'false';
const today = convertDate(date);
const fileContent = '---\n'
+ 'type: ' + type + '\n'
+ 'description: ' + description + '\n'
+ 'starred: ' + starred + '\n'
+ 'archived: ' + archived + '\n'
+ 'captured: ' + today + '\n'
+ '---\n\n'
+ 'Area::\n'
+ 'Projects::\n'
+ 'Author:: ' + byline + '\n'
+ 'Publisher::\n'
+ 'Source:: ' + document.URL + '\n'
+ 'Files::\n'
+ 'Tags:: ' + tags + '\n'
+ '\n---\n'
+ markdownBody;
document.location.href = "obsidian://new?"
+ "name=" + encodeURIComponent(folder + '/' + fileName)
+ "&content=" + encodeURIComponent(fileContent)
+ vaultName;
})