-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetEventProcessedContent.ts
50 lines (44 loc) · 1.39 KB
/
getEventProcessedContent.ts
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
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 };
}