From 38a89a1d6e224add06fc675947e640eb1c8c7f6f Mon Sep 17 00:00:00 2001 From: RaBBBBBIT <89313637+RaBBBBBIT@users.noreply.github.com> Date: Thu, 24 Oct 2024 21:52:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AF=B9=E7=94=BB=E6=9D=BF?= =?UTF-8?q?=E7=9A=84=E8=AF=BB=E5=8F=96=20(#17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用https://open.feishu.cn/open-apis/board/v1/whiteboards/:whiteboard_id/download_as_image接口获取画板缩略图并以图片形式渲染 --------- Co-authored-by: Jason Lee --- feishu-docx/src/markdown_renderer.ts | 34 ++++++++++++++++++++++++++++ feishu-docx/src/renderer.ts | 2 +- feishu-docx/src/types.ts | 7 +++++- feishu-pages/src/feishu.ts | 7 ++++-- feishu-pages/src/index.ts | 4 +++- yarn.lock | 33 +++++++++++++++------------ 6 files changed, 68 insertions(+), 19 deletions(-) diff --git a/feishu-docx/src/markdown_renderer.ts b/feishu-docx/src/markdown_renderer.ts index f1bc4e6..ea69595 100644 --- a/feishu-docx/src/markdown_renderer.ts +++ b/feishu-docx/src/markdown_renderer.ts @@ -143,6 +143,9 @@ export class MarkdownRenderer extends Renderer { case BlockType.Iframe: buf.write(this.parseIframe(block)); break; + case BlockType.Board: + buf.write(this.parseBoard(block.board)); + break; case BlockType.SyncedBlock: buf.write(this.parseSyncedBlock(block)); break; @@ -774,6 +777,37 @@ export class MarkdownRenderer extends Renderer { return buf; } + parseBoard(board: ImageBlock): Buffer | string { + const buf = new Buffer(); + + const align = getAlignStyle(board.align); + let alignAttr = ''; + if (align != 'left') { + alignAttr = ` align="${align}"`; + } + + const el = createElement('img'); + el.setAttribute('src', board.token); + if (board.width) { + el.setAttribute('src-width', board.width.toString()); + } + // Only give height when width is not given + if (board.height) { + el.setAttribute('src-height', board.height.toString()); + } + if (align && align != 'left') { + el.setAttribute('align', align); + } + + buf.write(el.outerHTML); + buf.write('\n'); + + this.addFileToken('board', board.token); + console.info("New board added: " + board.token) + + return buf; + } + parseSyncedBlock(block: Block): Buffer { const buf = new Buffer(); diff --git a/feishu-docx/src/renderer.ts b/feishu-docx/src/renderer.ts index 98d714d..f3acf11 100644 --- a/feishu-docx/src/renderer.ts +++ b/feishu-docx/src/renderer.ts @@ -59,7 +59,7 @@ export class Renderer { * @param type * @param token */ - addFileToken(type: 'file' | 'image', token: string) { + addFileToken(type: 'file' | 'image' | 'board', token: string) { this.fileTokens[token] = { token, type, diff --git a/feishu-docx/src/types.ts b/feishu-docx/src/types.ts index ad130fe..f9b1345 100644 --- a/feishu-docx/src/types.ts +++ b/feishu-docx/src/types.ts @@ -66,6 +66,10 @@ export enum BlockType { TableCell = 32, View = 33, QuoteContainer = 34, + /** + * 画板 + */ + Board = 43, /** * 同步块 */ @@ -462,6 +466,7 @@ export interface Block { image: ImageBlock; table: TableBlock; table_cell: TextBlock; + board: ImageBlock; } export function getAlignStyle(align: StyleAlign) { @@ -479,5 +484,5 @@ export function getAlignStyle(align: StyleAlign) { export interface FileToken { token: string; - type: 'file' | 'image'; + type: 'file' | 'image' | 'board'; } diff --git a/feishu-pages/src/feishu.ts b/feishu-pages/src/feishu.ts index d00c7d4..2215174 100644 --- a/feishu-pages/src/feishu.ts +++ b/feishu-pages/src/feishu.ts @@ -242,7 +242,7 @@ const isValidCacheExist = (cacheFilePath: string) => { * @param localPath * @returns */ -export const feishuDownload = async (fileToken: string, localPath: string) => { +export const feishuDownload = async (fileToken: string, localPath: string, type: string) => { const cacheFilePath = path.join(CACHE_DIR, fileToken); const cacheFileMetaPath = path.join(CACHE_DIR, `${fileToken}.headers.json`); fs.mkdirSync(CACHE_DIR, { recursive: true }); @@ -259,9 +259,12 @@ export const feishuDownload = async (fileToken: string, localPath: string) => { console.info(" -> Cache hit:", fileToken); } else { console.info("Downloading file", fileToken, "..."); + console.info("File type is", type, "..."); res = await axios .get( - `${feishuConfig.endpoint}/open-apis/drive/v1/medias/${fileToken}/download`, + type === "board" ? + `${feishuConfig.endpoint}/open-apis/board/v1/whiteboards/${fileToken}/download_as_image` : + `${feishuConfig.endpoint}/open-apis/drive/v1/medias/${fileToken}/download`, { responseType: "stream", headers: { diff --git a/feishu-pages/src/index.ts b/feishu-pages/src/index.ts index cf0defc..c4ecdce 100644 --- a/feishu-pages/src/index.ts +++ b/feishu-pages/src/index.ts @@ -139,6 +139,7 @@ const fetchDocAndWriteFile = async ( * * @param content * @param fileTokens + * @param boardTokens * @param docFolder * @returns */ @@ -155,7 +156,8 @@ const downloadFiles = async ( for (const fileToken in fileTokens) { const filePath = await feishuDownload( fileToken, - path.join(path.join(DOCS_DIR, 'assets'), fileToken) + path.join(path.join(DOCS_DIR, 'assets'), fileToken), + fileTokens[fileToken].type ); if (!filePath) { continue; diff --git a/yarn.lock b/yarn.lock index 063e0c9..49c3140 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2012,20 +2012,6 @@ fb-watchman@^2.0.0: dependencies: bser "2.1.1" -feishu-docx@0.6.3: - version "0.6.3" - resolved "https://registry.yarnpkg.com/feishu-docx/-/feishu-docx-0.6.3.tgz#9c9a2f0d49324a262053f02278d7dc0e2973e4bc" - integrity sha512-+zibgYVY7chFu/jHwKiv0y/BZvLk6AqIe6iRm7gYswJDB2JIbjb5tFBMXcv/vYAUpH80gNDmR2hcInq4R24mNA== - dependencies: - "@types/node" "^20.5.7" - dotenv "^16.3.1" - feishu-docx "^0.1.3" - js-yaml "^4.1.0" - jsdom "^22.1.0" - marked "^9.1.2" - marked-xhtml "^1.0.7" - typescript "^5.2.2" - feishu-docx@^0.1.3: version "0.1.4" resolved "https://registry.yarnpkg.com/feishu-docx/-/feishu-docx-0.1.4.tgz#53e506ec7d8acc8641522b55c9e8625d2fff0648" @@ -2569,6 +2555,16 @@ jest-diff@^29.6.4: jest-get-type "^29.6.3" pretty-format "^29.6.3" +jest-diff@^29.7.0: + version "29.7.0" + resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-29.7.0.tgz#017934a66ebb7ecf6f205e84699be10afd70458a" + integrity sha512-LMIgiIrhigmPrs03JHpxUh2yISK3vLFPkAodPeo0+BuF7wA2FoQbkEg1u8gBYBThncu7e1oEDUfIXVuTqLRUjw== + dependencies: + chalk "^4.0.0" + diff-sequences "^29.6.3" + jest-get-type "^29.6.3" + pretty-format "^29.7.0" + jest-docblock@^29.6.3: version "29.6.3" resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-29.6.3.tgz#293dca5188846c9f7c0c2b1bb33e5b11f21645f2" @@ -3447,6 +3443,15 @@ pretty-format@^29.6.3: ansi-styles "^5.0.0" react-is "^18.0.0" +pretty-format@^29.7.0: + version "29.7.0" + resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-29.7.0.tgz#ca42c758310f365bfa71a0bda0a807160b776812" + integrity sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ== + dependencies: + "@jest/schemas" "^29.6.3" + ansi-styles "^5.0.0" + react-is "^18.0.0" + prompts@^2.0.1: version "2.4.2" resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"