Skip to content

Commit

Permalink
make h3 h4
Browse files Browse the repository at this point in the history
  • Loading branch information
dtinth committed Feb 4, 2025
1 parent 0074792 commit 6299e2a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 45 deletions.
50 changes: 50 additions & 0 deletions getEventProcessedContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { load } from "cheerio";
import { ofetch } from "ofetch";

export async function getEventProcessedContent(slug: string) {
const baseUrl = `https://creatorsgarten.org/event/${slug}`;
const url = `https://creatorsgarten.org/event/${slug}?embed=true`;
const html = await ofetch(url);
const processedContent = processContent(html, baseUrl);
return processedContent;
}

function processContent(html: string, baseUrl: string) {
const $ = load(html);
const $content = $(".prose");
let eventId: number | undefined;

{
const eventpopLink = $content
.find('a img[alt="Tickets available at eventpop.me"]')
.closest("a")
.attr("href");
const match = eventpopLink?.match(/e\/(\d+)/);
if (match) {
eventId = +match[1];
}
}

$content
.find('a img[alt="Tickets available at eventpop.me"]')
.closest("p")
.remove();
$content.find("nav").remove();

$content.find("*").removeAttr("id");
$content.find("h2").addClass("mt-5 h3 text-heading");
$content.find("h3").addClass("mt-4 h4");
$content.find("a > span.icon.icon-link").closest("a").remove();
$content.find("a").each(function () {
const $a = $(this);
if ($a.attr("href")) {
$a.attr("href", new URL($a.attr("href")!, baseUrl).toString()).attr(
"target",
"_blank"
);
}
});

const content = $content.html();
return { description: content, eventId };
}
33 changes: 32 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
console.log("Hello via Bun!");
import { getEventProcessedContent } from "./getEventProcessedContent";

export default {
async fetch(req: Request) {
const pathname = new URL(req.url).pathname;
if (pathname === "/") {
return new Response("Go to /slug to preview the event");
}
const match = pathname.match(/^\/([\w]+)$/);
if (!match) {
return new Response("Invalid slug");
}
const processedContent = await getEventProcessedContent(match[1]);
const html = `<!DOCTYPE html>
<html>
<head>
<title>${match[1]}</title>
<link rel="stylesheet" href="https://p-a.popcdn.net/assets/application-178074c211b0f24a979e85edccd296f93d9996bde9593a75a346ec087c120402.css">
</head>
<body class="iframe-event-main-content">
<div class="container">
${processedContent.description}
</div>
</body>
</html>`;
return new Response(html, {
headers: {
"Content-Type": "text/html; charset=utf-8",
},
});
},
};
46 changes: 2 additions & 44 deletions pop.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { load } from "cheerio";
import { SignJWT } from "jose";
import { parseArgs } from "node:util";
import { ofetch } from "ofetch";
import { getEventProcessedContent } from "./getEventProcessedContent";

async function main() {
const args = parseArgs({
Expand All @@ -17,10 +17,7 @@ async function main() {
console.error("No slug provided");
return;
}
const url = `https://creatorsgarten.org/event/${slug}?embed=true`;
const baseUrl = `https://creatorsgarten.org/event/${slug}`;
const html = await ofetch(url);
const processedContent = processContent(html, baseUrl);
const processedContent = await getEventProcessedContent(slug);
const { eventId, description } = processedContent;
if (args.values.test) {
console.warn("Test mode, skipping update");
Expand Down Expand Up @@ -50,43 +47,4 @@ async function main() {
console.log("Update result:", result);
}

function processContent(html: string, baseUrl: string) {
const $ = load(html);
const $content = $(".prose");
let eventId: number | undefined;

{
const eventpopLink = $content
.find('a img[alt="Tickets available at eventpop.me"]')
.closest("a")
.attr("href");
const match = eventpopLink?.match(/e\/(\d+)/);
if (match) {
eventId = +match[1];
}
}

$content
.find('a img[alt="Tickets available at eventpop.me"]')
.closest("p")
.remove();
$content.find("nav").remove();

$content.find("*").removeAttr("id");
$content.find("h2").addClass("mt-5 h3 text-heading");
$content.find("a > span.icon.icon-link").closest("a").remove();
$content.find("a").each(function () {
const $a = $(this);
if ($a.attr("href")) {
$a.attr("href", new URL($a.attr("href")!, baseUrl).toString()).attr(
"target",
"_blank"
);
}
});

const content = $content.html();
return { description: content, eventId };
}

await main();

0 comments on commit 6299e2a

Please sign in to comment.