From 883a023624cab85408b700eef186e236d28153d9 Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Thu, 15 Dec 2022 02:48:35 +0100 Subject: [PATCH] feat(TextRun): add support for formatting (#254) --- src/parser/classes/misc/TextRun.ts | 6 ++++++ test/constants.ts | 4 ++++ test/main.test.ts | 14 ++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/parser/classes/misc/TextRun.ts b/src/parser/classes/misc/TextRun.ts index 230019af..4edb46b8 100644 --- a/src/parser/classes/misc/TextRun.ts +++ b/src/parser/classes/misc/TextRun.ts @@ -3,9 +3,15 @@ import NavigationEndpoint from '../NavigationEndpoint'; class TextRun { text: string; endpoint: NavigationEndpoint | undefined; + bold: boolean; + italics: boolean; + strikethrough: boolean; constructor(data: any) { 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; } } diff --git a/test/constants.ts b/test/constants.ts index 543938bf..9b15794f 100644 --- a/test/constants.ts +++ b/test/constants.ts @@ -10,6 +10,10 @@ export const VIDEOS = [ { ID: 'I1qsF0WQy8c', QUERY: 'mkbhd', + }, + { + ID: 'OqiXFXlYFi8', + QUERY: 'formatted comment text' } ]; diff --git a/test/main.test.ts b/test/main.test.ts index f6094935..f7667f38 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -2,6 +2,7 @@ import fs from 'fs'; import Innertube from '..'; import { CHANNELS, VIDEOS } from './constants'; import { streamToIterable } from '../src/utils/Utils'; +import TextRun from '../src/parser/classes/misc/TextRun'; describe('YouTube.js Tests', () => { let yt: Innertube; @@ -68,6 +69,19 @@ describe('YouTube.js Tests', () => { threads = await yt.getComments(VIDEOS[1].ID); expect(threads.contents.length).toBeGreaterThan(0); }); + + it('should parse formatted comments', async () => { + const threads = await yt.getComments(VIDEOS[3].ID); + const authorComment = threads.contents.find(t => t.comment?.author_is_channel_owner) + expect(authorComment).not.toBeUndefined(); + + expect(authorComment!.comment?.content.runs?.length).toBeGreaterThan(0) + const runs = authorComment!.comment!.content.runs! as TextRun[] + + expect(runs[0].bold).toBeTruthy() + expect(runs[2].italics).toBeTruthy() + expect(runs[4].strikethrough).toBeTruthy() + }) it('should retrieve next batch of comments', async () => { const next = await threads.getContinuation();