返回 presentation-ai
thumbnail.ts
根目录 / src / lib / presentation / thumbnail.ts
1 import { type PlateSlide } from "@/components/notebook/presentation/utils/parser";
2
3 function isRecord(value: unknown): value is Record<string, unknown> {
4 return typeof value === "object" && value !== null;
5 }
6
7 function getValidUrl(value: unknown): string | null {
8 if (typeof value !== "string") {
9 return null;
10 }
11
12 const trimmedValue = value.trim();
13
14 return trimmedValue.length > 0 ? trimmedValue : null;
15 }
16
17 function getRootImageThumbnailUrl(slide: PlateSlide): string | null {
18 if (slide.rootImage?.embedType && slide.rootImage.embedType !== "image") {
19 return null;
20 }
21
22 return getValidUrl(slide.rootImage?.url);
23 }
24
25 function findFirstInlineImageUrl(nodes: unknown[]): string | null {
26 for (const node of nodes) {
27 if (!isRecord(node)) {
28 continue;
29 }
30
31 if (node.type === "img") {
32 const imageUrl = getValidUrl(node.url);
33
34 if (imageUrl) {
35 return imageUrl;
36 }
37 }
38
39 const children = node.children;
40
41 if (Array.isArray(children)) {
42 const childImageUrl = findFirstInlineImageUrl(children);
43
44 if (childImageUrl) {
45 return childImageUrl;
46 }
47 }
48 }
49
50 return null;
51 }
52
53 export function getPresentationThumbnailUrl(
54 slides: readonly PlateSlide[],
55 ): string | null {
56 for (const slide of slides) {
57 const rootImageUrl = getRootImageThumbnailUrl(slide);
58
59 if (rootImageUrl) {
60 return rootImageUrl;
61 }
62 }
63
64 for (const slide of slides) {
65 const inlineImageUrl = findFirstInlineImageUrl(slide.content);
66
67 if (inlineImageUrl) {
68 return inlineImageUrl;
69 }
70 }
71
72 return null;
73 }
74
75 export function getPresentationSlidesFromContent(
76 content: unknown,
77 ): PlateSlide[] {
78 if (!isRecord(content)) {
79 return [];
80 }
81
82 const slides = content.slides;
83
84 if (!Array.isArray(slides)) {
85 return [];
86 }
87
88 return slides.filter(
89 (slide): slide is PlateSlide =>
90 isRecord(slide) &&
91 typeof slide.id === "string" &&
92 Array.isArray(slide.content),
93 );
94 }
95
95 lines TYPESCRIPT