Skip to content

Instantly share code, notes, and snippets.

@EdwardPrentice
Created August 8, 2024 08:08
Show Gist options
  • Save EdwardPrentice/2df79a94ed73ecf9394123319eea0170 to your computer and use it in GitHub Desktop.
Save EdwardPrentice/2df79a94ed73ecf9394123319eea0170 to your computer and use it in GitHub Desktop.
Mattia Righetti Code Refactored
// content-extractor.ts
import $ from "jquery";
const extractContent = (selector: string, subSelector: string) => {
return $(selector)
.first()
.find(subSelector)
.map((_, el) => $(el).text().trim())
.get()
.filter((text) => text.length > 0)
.join("\n\n");
};
function isNotion(): boolean {
return $("main.notion-page").length > 0;
}
function extractNotionContent() {
$("main.notion-page").find(".notion-collection-page-properties").remove();
return extractContent("main.notion-page", "p, h1, h2, h3, h4, h5, h6, div, li");
}
function isSubstack(): boolean {
return $(".available-content").length > 0;
}
function extractSubstackContent() {
return extractContent(".available-content", "p, h1, h2, h3, h4, h5, h6");
}
function isBeehiv(): boolean {
return $("#content-blocks").length > 0;
}
function extractBeehivContent() {
return extractContent("#content-blocks", "p, h1, h2, h3, h4, h5, h6");
}
function isArticle(): boolean {
return $("article").length > 0;
}
function extractArticle() {
$("article").find("style, script, div").remove();
$("article").find(".ml-form-embedContainer").remove();
return extractContent("article", "p, h1, h2, h3, h4, h5, h6, li, figure");
}
function getContent() {
if (isNotion()) {
return extractNotionContent();
}
if (isSubstack()) {
return extractSubstackContent();
}
if (isBeehiv()) {
return extractBeehivContent();
}
if (isArticle()) {
return extractArticle();
}
return $("main").text();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment