-
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
1 parent
5c959cc
commit c1ebaa2
Showing
9 changed files
with
245 additions
and
83 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
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
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,22 @@ | ||
import { Button } from "@mantine/core"; | ||
import { IconExternalLink } from "@tabler/icons-react"; | ||
|
||
export const ExternalLink = ({ | ||
link, | ||
text, | ||
}: { | ||
link: string; | ||
text: string; | ||
}) => ( | ||
<Button | ||
variant="light" | ||
color="violet" | ||
size="compact-sm" | ||
leftSection={<IconExternalLink />} | ||
component="a" | ||
href={link} | ||
target="_blank" | ||
> | ||
{text} | ||
</Button> | ||
); |
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
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
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
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,5 +1,86 @@ | ||
import { secondsToTimestamp, timestampToSeconds, TimestampRegex } from "./timestamp"; | ||
|
||
export type TextPart = { | ||
type: "text"; | ||
data: string; | ||
bold: boolean; | ||
italic: boolean; | ||
} | { | ||
type: "newline"; | ||
} | { | ||
type: "timestamp"; | ||
time: number; | ||
} | { | ||
type: "link", | ||
href: string; | ||
} | ||
|
||
export const cleanDescription = (text = "") => { | ||
let parser = new DOMParser(); | ||
let doc = parser.parseFromString(text.replaceAll("<br>", "\n"), "text/html"); | ||
return doc.documentElement.textContent; | ||
|
||
let body = doc.children[0].children[1]; | ||
let nodes = [...body.childNodes]; | ||
|
||
let parts: TextPart[] = []; | ||
|
||
for(let node of nodes) { | ||
if(node.nodeType == Node.TEXT_NODE) { | ||
let first = true; | ||
for (let line of node.nodeValue.split("\n")) { | ||
if(!first) { | ||
parts.push({ type: "newline" }); | ||
} else { | ||
first = false; | ||
} | ||
|
||
if(!line) continue; | ||
|
||
parts.push({ | ||
type: "text", | ||
data: line, | ||
bold: false, | ||
italic: false, | ||
}); | ||
} | ||
} else if(node.nodeType == Node.ELEMENT_NODE) { | ||
if(node.nodeName == "A") { | ||
let el = node as HTMLAnchorElement; | ||
let isTimestamp = TimestampRegex.test(el.textContent); | ||
if(isTimestamp) { | ||
parts.push({ | ||
type: "timestamp", | ||
time: timestampToSeconds(el.textContent), | ||
}) | ||
} else { | ||
parts.push({ | ||
type: "link", | ||
href: el.href, | ||
}) | ||
} | ||
} else if(node.nodeName == "B") { | ||
parts.push({ | ||
type: "text", | ||
data: node.nodeValue, | ||
bold: true, | ||
italic: false, | ||
}); | ||
} else if(node.nodeName == "BR") { | ||
parts.push({ type: "newline" }); | ||
} else { | ||
console.log("Unknown tag", node); | ||
} | ||
} | ||
} | ||
|
||
return parts; | ||
}; | ||
|
||
export const textPartsToString = (parts: TextPart[]) => { | ||
return parts.map(part => { | ||
if(part.type == "text") return part.data; | ||
if(part.type == "timestamp") return secondsToTimestamp(part.time); | ||
if(part.type == "link") return part.href; | ||
if(part.type == "newline") return "\n"; | ||
}).join(""); | ||
} |
Oops, something went wrong.