feat: add list content token

This commit is contained in:
moonrailgun 2024-07-20 01:39:10 +08:00
parent 03904d26e0
commit 7736bf89dc
5 changed files with 36 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import { TelegramContentTokenizer } from './tokenizer/telegram';
import {
ContentToken,
ImageContentToken,
ListContentToken,
NewlineContentToken,
ParagraphContentToken,
TextContentToken,
@ -40,6 +41,10 @@ export const token = {
url,
title,
}),
list: (items: ContentToken[]): ListContentToken => ({
type: 'list',
items,
}),
};
export const baseContentTokenizer = new BaseContentTokenizer();

View File

@ -1,6 +1,7 @@
import {
ContentToken,
ImageContentToken,
ListContentToken,
NewlineContentToken,
ParagraphContentToken,
TextContentToken,
@ -33,7 +34,11 @@ export class BaseContentTokenizer {
return token.url;
}
parse(tokens: ContentToken[]) {
parseList(token: ListContentToken) {
return token.items.map((item) => this.parse([item])).join('\n');
}
parse(tokens: ContentToken[]): string {
return tokens
.map((token) => {
if (token.type === 'text') {
@ -56,6 +61,10 @@ export class BaseContentTokenizer {
return this.parseNewline(token);
}
if (token.type === 'list') {
return this.parseList(token);
}
return '';
})
.join('')

View File

@ -1,5 +1,6 @@
import {
ImageContentToken,
ListContentToken,
NewlineContentToken,
ParagraphContentToken,
TitleContentToken,
@ -37,4 +38,8 @@ export class HTMLContentTokenizer extends BaseContentTokenizer {
parseUrl(token: UrlContentToken): string {
return `<a href="${token.url}">${token.title ?? token.url}</a>`;
}
parseList(token: ListContentToken) {
return `<ul>${token.items.map((item) => `<li>${this.parse([item])}</li>`).join('')}</ul>`;
}
}

View File

@ -1,5 +1,6 @@
import {
ImageContentToken,
ListContentToken,
ParagraphContentToken,
TitleContentToken,
UrlContentToken,
@ -32,4 +33,12 @@ export class MarkdownContentTokenizer extends BaseContentTokenizer {
parseUrl(token: UrlContentToken): string {
return `[${token.title ?? ''}](${token.url})`;
}
parseList(token: ListContentToken) {
return (
'\n' +
token.items.map((item) => `- ${this.parse([item])}`).join('\n') +
'\n'
);
}
}

View File

@ -29,10 +29,16 @@ export type UrlContentToken = {
title?: string;
};
export type ListContentToken = {
type: 'list';
items: ContentToken[];
};
export type ContentToken =
| TextContentToken
| ImageContentToken
| TitleContentToken
| ParagraphContentToken
| NewlineContentToken
| UrlContentToken;
| UrlContentToken
| ListContentToken;