mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
feat: replace electron to puppeteer (#2700)
Co-authored-by: himself65 <himself65@outlook.com>
(cherry picked from commit fda89b05e7)
This commit is contained in:
@@ -1,11 +1,56 @@
|
||||
import { getMetaData } from './server/get-meta-data';
|
||||
import { getLinkPreview } from 'link-preview-js';
|
||||
|
||||
type MetaData = {
|
||||
title?: string;
|
||||
description?: string;
|
||||
icon?: string;
|
||||
image?: string;
|
||||
[x: string]: string | string[] | undefined;
|
||||
};
|
||||
|
||||
export interface PreviewType {
|
||||
url: string;
|
||||
title: string;
|
||||
siteName: string | undefined;
|
||||
description: string | undefined;
|
||||
mediaType: string;
|
||||
contentType: string | undefined;
|
||||
images: string[];
|
||||
videos: {
|
||||
url: string | undefined;
|
||||
secureUrl: string | null | undefined;
|
||||
type: string | null | undefined;
|
||||
width: string | undefined;
|
||||
height: string | undefined;
|
||||
}[];
|
||||
favicons: string[];
|
||||
}
|
||||
|
||||
export default {
|
||||
getBookmarkDataByLink: async (_: unknown, url: string) => {
|
||||
return getMetaData(url, {
|
||||
shouldReGetHTML: metaData => {
|
||||
return !metaData.title && !metaData.description;
|
||||
getBookmarkDataByLink: async (_: unknown, url: string): Promise<MetaData> => {
|
||||
const previewData = (await getLinkPreview(url, {
|
||||
timeout: 6000,
|
||||
headers: {
|
||||
'user-agent': 'googlebot',
|
||||
},
|
||||
});
|
||||
followRedirects: 'follow',
|
||||
}).catch(() => {
|
||||
return {
|
||||
title: '',
|
||||
siteName: '',
|
||||
description: '',
|
||||
images: [],
|
||||
videos: [],
|
||||
contentType: `text/html`,
|
||||
favicons: [],
|
||||
};
|
||||
})) as PreviewType;
|
||||
|
||||
return {
|
||||
title: previewData.title,
|
||||
description: previewData.description,
|
||||
icon: previewData.favicons[0],
|
||||
image: previewData.images[0],
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user