Skip to content

Commit ed0d3b5

Browse files
committed
[plugin/takumi] Add emoji support
1 parent 1a3c67d commit ed0d3b5

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

‎plugins/takumi/src/service.ts‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { Context, Disposable } from '@fraqjs/fraq';
2-
import { type ConstructRendererOptions, type Font, Renderer, type RenderOptions } from '@takumi-rs/core';
3-
import type { ReactElementLike } from '@takumi-rs/helpers';
2+
import {
3+
type ConstructRendererOptions,
4+
extractResourceUrls,
5+
type Font,
6+
Renderer,
7+
type RenderOptions,
8+
} from '@takumi-rs/core';
9+
import { fetchResources, type Node, type ReactElementLike } from '@takumi-rs/helpers';
10+
import { type EmojiType, extractEmojis } from '@takumi-rs/helpers/emoji';
411
import { fromHtml } from '@takumi-rs/helpers/html';
512
import { fromJsx } from '@takumi-rs/helpers/jsx';
613
import type { ReactNode } from 'react';
@@ -95,6 +102,24 @@ export class TakumiService implements Disposable {
95102
);
96103
}
97104

105+
async renderJsxWithEmoji(
106+
jsx: ReactNode | ReactElementLike,
107+
renderOptions?: RenderOptions,
108+
signal?: AbortSignal,
109+
emojiType: EmojiType = 'noto',
110+
): Promise<Buffer> {
111+
const { node, stylesheets } = await fromJsx(jsx);
112+
const { node: processedNode, fetchedResources } = await this.processEmoji(node, emojiType);
113+
return this.renderer.render(
114+
processedNode,
115+
withMergedStylesheets(stylesheets, {
116+
...renderOptions,
117+
fetchedResources: [...fetchedResources, ...(renderOptions?.fetchedResources ?? [])],
118+
}),
119+
combineAbortSignals(signal, this.abortController.signal),
120+
);
121+
}
122+
98123
async renderHtml(html: string, renderOptions?: RenderOptions, signal?: AbortSignal): Promise<Buffer> {
99124
const { node, stylesheets } = fromHtml(html);
100125
return this.renderer.render(
@@ -104,6 +129,31 @@ export class TakumiService implements Disposable {
104129
);
105130
}
106131

132+
async renderHtmlWithEmoji(
133+
html: string,
134+
renderOptions?: RenderOptions,
135+
signal?: AbortSignal,
136+
emojiType: EmojiType = 'noto',
137+
): Promise<Buffer> {
138+
const { node, stylesheets } = fromHtml(html);
139+
const { node: processedNode, fetchedResources } = await this.processEmoji(node, emojiType);
140+
return this.renderer.render(
141+
processedNode,
142+
withMergedStylesheets(stylesheets, {
143+
...renderOptions,
144+
fetchedResources: [...fetchedResources, ...(renderOptions?.fetchedResources ?? [])],
145+
}),
146+
combineAbortSignals(signal, this.abortController.signal),
147+
);
148+
}
149+
150+
private async processEmoji(node: Node, emojiType: EmojiType = 'twemoji') {
151+
const processedNode = extractEmojis(node, emojiType);
152+
const resourceUrls = extractResourceUrls(processedNode);
153+
const fetchedResources = await fetchResources(resourceUrls);
154+
return { node: processedNode, fetchedResources };
155+
}
156+
107157
dispose() {
108158
this.abortController.abort();
109159
}

0 commit comments

Comments
 (0)