-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters