mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-11 20:08:37 +00:00
refactor(electron): server side plugin (#3360)
This commit is contained in:
@@ -4,13 +4,15 @@
|
||||
"affinePlugin": {
|
||||
"release": true,
|
||||
"entry": {
|
||||
"core": "./src/index.ts"
|
||||
"core": "./src/index.ts",
|
||||
"server": "./src/server.ts"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@affine/component": "workspace:*",
|
||||
"@blocksuite/icons": "^2.1.27",
|
||||
"@toeverything/plugin-infra": "workspace:*",
|
||||
"foxact": "^0.2.17"
|
||||
"foxact": "^0.2.17",
|
||||
"link-preview-js": "^3.0.4"
|
||||
}
|
||||
}
|
||||
|
||||
65
plugins/bookmark/src/server.ts
Normal file
65
plugins/bookmark/src/server.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { ServerContext } from '@toeverything/plugin-infra/server';
|
||||
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 const entry = (context: ServerContext) => {
|
||||
context.registerCommand(
|
||||
'com.blocksuite.bookmark-block.get-bookmark-data-by-link',
|
||||
async (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],
|
||||
};
|
||||
}
|
||||
);
|
||||
return () => {
|
||||
context.unregisterCommand(
|
||||
'com.blocksuite.bookmark-block.get-bookmark-data-by-link'
|
||||
);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user