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
|
(() => {
const contentLength = Infinity; // Set an appropriate length limit for the content 2048
// Fetch required libraries
const fetchLibraries = async () => {
const [Turndown, Readability] = await Promise.all([
import('https://unpkg.com/[email protected]?module'),
import('https://unpkg.com/@tehshrike/[email protected]'),
]);
return { Turndown: Turndown.default, Readability: Readability.default };
};
// Get HTML content of the selected text
const 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;
};
// Generate a safe file name
const 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;
};
// Get meta content by attribute and value
const getMetaContent = (attr, value) => {
var element = document.querySelector(`meta[${attr}='${value}']`);
return element ? element.getAttribute("content").trim() : "";
};
// Format author information
const formatAuthor = (byline) => {
var author = byline || getMetaContent("name", "author") || getMetaContent("property", "author") || getMetaContent("property", "og:site_name");
return author ? `"[[${author}]]"` : "";
};
// Format the date and time string as YYYY-MM-DDTHH:mm:ss
const formatDateString = (date) => {
const options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short' };
return date.toISOString('en-US', options).replace(/,/g, ''); // Remove comma in the time zone
};
// Markdownify the captured content
const markdownifyContent = (content, Turndown) => {
return new Turndown({
headingStyle: 'atx',
hr: '~~~',
bulletListMarker: '-',
codeBlockStyle: 'fenced',
emDelimiter: '*',
}).turndown(content);
};
// Generate markdown content
const generateMarkdown = ({ title, byline, content, tags, url, formattedDate, author, Turndown }) => {
const markdownBody = markdownifyContent(content, Turndown);
return `---\n`
+ `type: linklog\n`
+ `title: ${title}\n`
+ `source: ${url}\n`
+ `date: ${formattedDate}\n`
+ `tags: [${tags}]\n`
+ `---\n\n`
+ `${markdownBody}`;
};
// Generate Obsidian link
const generateObsidianLink = ({ folder, fileName, content }) => {
return `obsidian://new?`
+ `file=${encodeURIComponent(folder + fileName)}`
+ `&content=${encodeURIComponent(content)}`;
};
// Main function
fetchLibraries().then(async ({ Turndown, Readability }) => {
const vault = ''; // Optional vault name
const folder = 'content/linklog/'; // Optional folder name
let tags = 'Reading'; // Optional tags
const selection = getSelectionHtml();
const { title, byline, content } = new Readability(document.cloneNode(true)).parse();
const fileName = getFileName(title);
const today = new Date();
const formattedDate = formatDateString(today);
const author = formatAuthor(byline);
const fileContent = generateMarkdown({ title, byline, content, tags, url: document.URL, formattedDate, author, Turndown });
const linkref = `\n\n-\n\source: [${title}](${document.URL})\n`;
const obsidianLink = generateObsidianLink({ folder, fileName, content: `${fileContent}${linkref}` });
document.location.href = obsidianLink;
});
})();
|