From 7736bf89dc94524d0cce6dc0b42a6ef430ecab2e Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 20 Jul 2024 01:39:10 +0800 Subject: [PATCH] feat: add list content token --- src/server/model/notification/token/index.ts | 5 +++++ src/server/model/notification/token/tokenizer/base.ts | 11 ++++++++++- src/server/model/notification/token/tokenizer/html.ts | 5 +++++ .../model/notification/token/tokenizer/markdown.ts | 9 +++++++++ src/server/model/notification/token/type.ts | 8 +++++++- 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/server/model/notification/token/index.ts b/src/server/model/notification/token/index.ts index 3dc46ce..ad892fe 100644 --- a/src/server/model/notification/token/index.ts +++ b/src/server/model/notification/token/index.ts @@ -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(); diff --git a/src/server/model/notification/token/tokenizer/base.ts b/src/server/model/notification/token/tokenizer/base.ts index 7ec183c..da8168f 100644 --- a/src/server/model/notification/token/tokenizer/base.ts +++ b/src/server/model/notification/token/tokenizer/base.ts @@ -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('') diff --git a/src/server/model/notification/token/tokenizer/html.ts b/src/server/model/notification/token/tokenizer/html.ts index b12656b..8b27b55 100644 --- a/src/server/model/notification/token/tokenizer/html.ts +++ b/src/server/model/notification/token/tokenizer/html.ts @@ -1,5 +1,6 @@ import { ImageContentToken, + ListContentToken, NewlineContentToken, ParagraphContentToken, TitleContentToken, @@ -37,4 +38,8 @@ export class HTMLContentTokenizer extends BaseContentTokenizer { parseUrl(token: UrlContentToken): string { return `${token.title ?? token.url}`; } + + parseList(token: ListContentToken) { + return ``; + } } diff --git a/src/server/model/notification/token/tokenizer/markdown.ts b/src/server/model/notification/token/tokenizer/markdown.ts index 68d9f79..0a141bd 100644 --- a/src/server/model/notification/token/tokenizer/markdown.ts +++ b/src/server/model/notification/token/tokenizer/markdown.ts @@ -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' + ); + } } diff --git a/src/server/model/notification/token/type.ts b/src/server/model/notification/token/type.ts index 243bf78..8f6d74e 100644 --- a/src/server/model/notification/token/type.ts +++ b/src/server/model/notification/token/type.ts @@ -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;