refactor(electron): server side plugin (#3360)

This commit is contained in:
Alex Yang
2023-07-25 14:32:34 -07:00
committed by GitHub
parent 521e505a01
commit 10f879f29a
28 changed files with 79 additions and 840 deletions

View File

@@ -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"
}
}

View 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'
);
};
};