implemented reader mode

This commit is contained in:
Fabian Freund
2024-07-12 10:59:18 +02:00
parent 1704a4fa89
commit 3087bdbd8c
22 changed files with 6935 additions and 20 deletions
+39
View File
@@ -0,0 +1,39 @@
import { Readability, isProbablyReaderable } from '@mozilla/readability';
import DOMPurify from 'dompurify';
function isReaderable() {
return isProbablyReaderable(document);
}
function parseReaderable(doc) {
const reader = new Readability(doc);
const article = reader.parse();
return article;
}
function applyReaderable() {
const clonedDoc = document.cloneNode(true);
const article = parseReaderable(clonedDoc);
const markup = DOMPurify.sanitize(article.content);
// console.log('article title: ', parsed.title);
// console.log('HTML string of processed article content: ', parsed.content);
// console.log('text content of the article, with all the HTML tags removed: ', parsed.textContent);
// console.log('length of an article, in characters: ', parsed.length);
// console.log('article description, or short excerpt from the content: ', parsed.excerpt);
// console.log('author metadata: ', parsed.byline);
// console.log('content direction: ', parsed.dir);
// console.log('name of the site: ', parsed.siteName);
// console.log('content language: ', parsed.lang);
// console.log('published time: ', parsed.publishedTime);
if (markup) {
document.body.innerHTML = '<div style="margin: 4pt;">' + markup + '</div>';
}
}
// Attach functions to the window object
window.isReaderable = isReaderable;
window.applyReaderable = applyReaderable;
export { isReaderable, applyReaderable };