feat(VideoSecondaryInfo): add support for attributed descriptions (#325)

This commit is contained in:
LuanRT
2023-02-26 16:47:47 -03:00
committed by GitHub
parent a0e6cef00f
commit f933cb45bc
3 changed files with 95 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
import NavigationEndpoint from '../NavigationEndpoint.js';
import { escape, Run } from './Text.js';
import type { RawNode } from '../../index.js';
class TextRun implements Run {
text: string;
@@ -7,13 +8,15 @@ class TextRun implements Run {
bold: boolean;
italics: boolean;
strikethrough: boolean;
attachment;
constructor(data: any) {
constructor(data: RawNode) {
this.text = data.text;
this.bold = Boolean(data.bold);
this.italics = Boolean(data.italics);
this.strikethrough = Boolean(data.strikethrough);
this.endpoint = data.navigationEndpoint ? new NavigationEndpoint(data.navigationEndpoint) : undefined;
this.attachment = data.attachment;
}
toString() {
@@ -22,16 +25,30 @@ class TextRun implements Run {
toHTML(): string {
const tags: string[] = [];
if (this.bold) tags.push('b');
if (this.italics) tags.push('i');
if (this.strikethrough) tags.push('s');
const escaped_text = escape(this.text);
const styled_text = tags.map((tag) => `<${tag}>`).join('') + escaped_text + tags.map((tag) => `</${tag}>`).join('');
const wrapped_text = `<span style="white-space: pre-wrap;">${styled_text}</span>`;
if (this.attachment) {
if (this.attachment.element.type.imageType.image.sources.length) {
const { url } = this.attachment.element.type.imageType.image.sources[0];
if (this.endpoint) {
const nav_url = this.endpoint.toURL();
if (nav_url) return `<a href="${nav_url}" class="yt-ch-link" display: block; width: fit-content; font-size: small;><img src="${url}" style="vertical-align: middle; height: ${this.attachment.element.properties.layoutProperties.height.value}px; width: ${this.attachment.element.properties.layoutProperties.width.value}px;">${wrapped_text}</a>`;
}
}
}
if (this.endpoint) {
const url = this.endpoint.toURL();
if (url) return `<a href="${url}">${wrapped_text}</a>`;
}
return wrapped_text;
}
}