Skip to content

Commit

Permalink
新增对画板的读取 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaBBBBBIT and huacnlee authored Oct 24, 2024
1 parent 92c113b commit 38a89a1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 19 deletions.
34 changes: 34 additions & 0 deletions feishu-docx/src/markdown_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion feishu-docx/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion feishu-docx/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export enum BlockType {
TableCell = 32,
View = 33,
QuoteContainer = 34,
/**
* 画板
*/
Board = 43,
/**
* 同步块
*/
Expand Down Expand Up @@ -462,6 +466,7 @@ export interface Block {
image: ImageBlock;
table: TableBlock;
table_cell: TextBlock;
board: ImageBlock;
}

export function getAlignStyle(align: StyleAlign) {
Expand All @@ -479,5 +484,5 @@ export function getAlignStyle(align: StyleAlign) {

export interface FileToken {
token: string;
type: 'file' | 'image';
type: 'file' | 'image' | 'board';
}
7 changes: 5 additions & 2 deletions feishu-pages/src/feishu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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: {
Expand Down
4 changes: 3 additions & 1 deletion feishu-pages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const fetchDocAndWriteFile = async (
*
* @param content
* @param fileTokens
* @param boardTokens
* @param docFolder
* @returns
*/
Expand All @@ -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;
Expand Down
33 changes: 19 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2012,20 +2012,6 @@ fb-watchman@^2.0.0:
dependencies:
bser "2.1.1"

[email protected]:
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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 38a89a1

Please sign in to comment.