Improved video desc

This commit is contained in:
WardPearce
2024-04-03 04:49:36 +13:00
parent 3561a3acda
commit 81ef96c316
3 changed files with 33 additions and 16 deletions
+29 -14
View File
@@ -40,31 +40,46 @@ export interface PhasedDescription {
export function phaseDescription(content: string): PhasedDescription {
const timestamps: { title: string; time: number; timePretty: string; }[] = [];
console.log(content);
const lines = content.split('\n');
const regex = /(\d+:\d+(?::\d+)?)(?:\s(.+))?/;
const filteredLines = lines.filter(line => {
const match = regex.exec(line);
const urlRegex = /<a href="([^"]+)"/;
const timestampRegex = /<a href="([^"]+)" data-onclick="jump_to_time" data-jump-time="(\d+)">(\d+:\d+(?::\d+)?)<\/a>\s*(.+)/;
if (match !== null) {
const timestamp = match[1];
const title = match[2] || '';
timestamps.push({
time: convertToSeconds(timestamp),
title: title,
timePretty: timestamp
});
return false;
let filteredLines: string[] = [];
lines.forEach(
(line) => {
const urlMatch = urlRegex.exec(line);
const timestampMatch = timestampRegex.exec(line);
if (urlMatch !== null && timestampMatch === null) {
// If line contains a URL but not a timestamp, modify the URL
const modifiedLine = line.replace(/<a href="([^"]+)"/, '<a href="$1" target="_blank" rel="noopener noreferrer" class="link"');
filteredLines.push(modifiedLine);
} else if (timestampMatch !== null) {
// If line contains a timestamp, extract details and push into timestamps array
const time = timestampMatch[2];
const timestamp = timestampMatch[3];
const title = timestampMatch[4] || '';
timestamps.push({
time: parseInt(time),
title: title,
timePretty: timestamp
});
} else {
filteredLines.push(line);
}
}
);
return true;
});
const filteredContent = filteredLines.join('\n');
return { description: filteredContent, timestamps: timestamps };
}
function convertToSeconds(time: string): number {
const parts = time.split(':').map(part => parseInt(part));
let seconds = 0;
@@ -217,7 +217,9 @@
<p class="bold">
{numberWithCommas(data.video.viewCount)} views • {data.video.publishedText}
</p>
{@html data.video.descriptionHtml}
<div style="white-space: pre-line; overflow-wrap: break-word;">
{@html data.content.description}
</div>
{#if data.content}
{#if data.content.timestamps.length > 0}
<h6 style="margin-bottom: .3em;">Chapters</h6>
@@ -45,7 +45,7 @@ export async function load({ params, url }) {
returnYTDislikes: returnYTDislikes,
comments: comments,
subscribed: await amSubscribed(video.authorId),
content: phaseDescription(video.description),
content: phaseDescription(video.descriptionHtml),
playlistId: url.searchParams.get('playlist'),
personalPlaylists: personalPlaylists
};