Skip to content

Commit

Permalink
Better structure and parsing for summary object
Browse files Browse the repository at this point in the history
  • Loading branch information
FlaminSarge committed Nov 2, 2024
1 parent 008ee63 commit 724fb29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/components/ChatSummary.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
{/if}
</Icon>
</span>
{#each summary.header as run}
{#each summary.item.header as run}
{#if run.type === 'text'}
<span class="align-middle">{run.text}</span>
{/if}
Expand All @@ -61,10 +61,10 @@
</div>
{#if !shorten && !dismissed}
<div class="mt-1 whitespace-pre-line" transition:slide|local={{ duration: 300 }}>
<MessageRun runs={summary.subheader} deleted forceDark forceTLColor={Theme.DARK}/>
<MessageRun runs={summary.item.subheader} deleted forceDark forceTLColor={Theme.DARK}/>
</div>
<div class="mt-1 whitespace-pre-line" transition:slide|local={{ duration: 300 }}>
<MessageRun runs={summary.summary} forceDark forceTLColor={Theme.DARK}/>
<MessageRun runs={summary.item.message} forceDark forceTLColor={Theme.DARK}/>
</div>
{/if}
</div>
Expand Down
25 changes: 22 additions & 3 deletions src/ts/chat-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,33 @@ const parseMessageRuns = (runs?: Ytc.MessageRun[]): Ytc.ParsedRun[] => {
return parsedRuns;
};

const splitRunsByNewline = (runs: Ytc.ParsedRun[], maxSplit: number = -1): Ytc.ParsedRun[][] => {
// takes an array of runs, finds newline-only runs, and splits the array by them, up to maxSplit times
let currSplit = 0;
const output: Ytc.ParsedRun[][] = [];
output.push(runs.reduce((acc: Ytc.ParsedRun[], run: Ytc.ParsedRun) => {
if (run.type === 'text' && run.text === '\n' && (maxSplit == -1 || currSplit < maxSplit)) {
currSplit++;
output.push(acc);
return [];
}
acc.push(run);
return acc;
}, []));
return output;
}

const parseChatSummary = (renderer: Ytc.AddChatItem, isEphemeral: boolean | undefined, bannerTimeoutMs: number | undefined): Ytc.ParsedSummary | undefined => {
const baseRenderer = renderer.liveChatBannerChatSummaryRenderer!;
const runs = parseMessageRuns(renderer.liveChatBannerChatSummaryRenderer?.chatSummary.runs);
const splitRuns = splitRunsByNewline(runs, 2);
const item: Ytc.ParsedSummary = {
type: 'summary',
header: [runs[0]],
subheader: [runs[2]],
summary: runs.slice(4),
item: {
header: splitRuns[0],
subheader: splitRuns[1],
message: splitRuns[2],
},
icon: baseRenderer.icon && {
iconType: baseRenderer.icon?.iconType.toLowerCase(),
},
Expand Down
8 changes: 5 additions & 3 deletions src/ts/typings/ytc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,11 @@ declare namespace Ytc {

interface ParsedSummary {
type: 'summary';
header: ParsedRun[];
subheader: ParsedRun[];
summary: ParsedRun[];
item: {
header: ParsedRun[];
subheader: ParsedRun[];
message: ParsedRun[];
};
id: string;
icon?: {
/** Unlocalized string */
Expand Down

0 comments on commit 724fb29

Please sign in to comment.