feat(Parser): just-in-time YTNode generation (#310)

* refactor: merge NavigatableText into Text

* fix(Text): data might not be object

* refactor: remove GetParserByName from map

* feat(Parser): just-in-time YTNode generation

* refactor: cleanup YTNodeGenerator

* fix: YTNode map imports

* feat(YTNodeGenerator): primative types

Add support for inferring primatives types

* fix(YTNodeGenerator): NavigationEndpoint detection

* fix(YTNodeGenerator): fix generated typescript

Correct types and linting for generated typescript class

* chore: update parsers after merge

* feat: add support for object type inference

* fix: object type def

* docs: basic YTNodeGenerator explanation

* docs: tsdoc for YTNodeGenerator

* docs: update parser updating guide

* fix: apply suggested changes

* docs: accessing generated nodes
This commit is contained in:
Daniel Wykerd
2023-03-15 08:39:36 +02:00
committed by GitHub
parent ffd7d79308
commit 2cee59024c
20 changed files with 1201 additions and 1169 deletions

View File

@@ -1,5 +1,6 @@
import TextRun from './TextRun.js';
import EmojiRun from './EmojiRun.js';
import NavigationEndpoint from '../NavigationEndpoint.js';
import type { RawNode } from '../../index.js';
export interface Run {
@@ -18,8 +19,9 @@ export function escape(text: string) {
}
class Text {
text: string;
text?: string;
runs;
endpoint?: NavigationEndpoint;
constructor(data: RawNode) {
if (data?.hasOwnProperty('runs') && Array.isArray(data.runs)) {
@@ -29,16 +31,28 @@ class Text {
);
this.text = this.runs.map((run) => run.text).join('');
} else {
this.text = data?.simpleText || 'N/A';
this.text = data?.simpleText;
}
if (typeof data === 'object' && data !== null && Reflect.has(data, 'navigationEndpoint')) {
this.endpoint = new NavigationEndpoint(data.navigationEndpoint);
}
if (typeof data === 'object' && data !== null && Reflect.has(data, 'titleNavigationEndpoint')) {
this.endpoint = new NavigationEndpoint(data.titleNavigationEndpoint);
}
if (!this.endpoint)
this.endpoint = (this.runs?.[0] as TextRun)?.endpoint;
}
toHTML() {
return this.runs ? this.runs.map((run) => run.toHTML()).join('') : this.text;
}
isEmpty() {
return this.text === undefined;
}
toString() {
return this.text;
return this.text || 'N/A';
}
}