@@ -71,24 +71,27 @@ export async function fetchWikiMarkdown(client, wikiNodeToken) {
7171
7272 if ( node . obj_type === 'docx' ) {
7373 const operation = `Export Wiki document ${ wikiNodeToken } `
74- const response = await callFeishu ( operation , ( ) =>
75- client . request ( {
76- method : 'POST' ,
77- url : `/open-apis/docs_ai/v1/documents/${ encodeURIComponent ( node . obj_token ) } /fetch` ,
78- data : {
79- format : 'markdown' ,
80- export_option : {
81- export_block_id : false ,
82- export_style_attrs : false ,
83- export_cite_extra_data : false
84- } ,
85- extra_param : JSON . stringify ( {
86- enable_user_cite_reference_map : true ,
87- return_html5_block_data : true
88- } )
89- }
90- } )
91- )
74+ const [ response , blocks ] = await Promise . all ( [
75+ callFeishu ( operation , ( ) =>
76+ client . request ( {
77+ method : 'POST' ,
78+ url : `/open-apis/docs_ai/v1/documents/${ encodeURIComponent ( node . obj_token ) } /fetch` ,
79+ data : {
80+ format : 'markdown' ,
81+ export_option : {
82+ export_block_id : false ,
83+ export_style_attrs : false ,
84+ export_cite_extra_data : false
85+ } ,
86+ extra_param : JSON . stringify ( {
87+ enable_user_cite_reference_map : true ,
88+ return_html5_block_data : true
89+ } )
90+ }
91+ } )
92+ ) ,
93+ listDocumentBlocks ( client , node . obj_token )
94+ ] )
9295 const data = assertSuccess ( response , operation )
9396 const document = data ?. document
9497
@@ -100,6 +103,14 @@ export async function fetchWikiMarkdown(client, wikiNodeToken) {
100103 markdown : document . content ,
101104 revisionId : document . revision_id ?? null ,
102105 referenceMap : document . reference_map ?? { } ,
106+ imageMetadata : blocks
107+ . filter ( ( { image } ) => image ?. token )
108+ . map ( ( { image } ) => ( {
109+ token : image . token ,
110+ caption : image . caption ?. content ?. trim ( ) || undefined ,
111+ width : positiveInteger ( image . width ) ,
112+ height : positiveInteger ( image . height )
113+ } ) ) ,
103114 node
104115 }
105116 }
@@ -125,6 +136,34 @@ export async function fetchWikiMarkdown(client, wikiNodeToken) {
125136 )
126137}
127138
139+ export async function listDocumentBlocks ( client , documentId ) {
140+ const blocks = [ ]
141+ let pageToken
142+
143+ do {
144+ const operation = `List blocks in document ${ documentId } `
145+ const response = await callFeishu ( operation , ( ) =>
146+ client . docx . v1 . documentBlock . list ( {
147+ path : { document_id : documentId } ,
148+ params : {
149+ page_size : 500 ,
150+ document_revision_id : - 1 ,
151+ ...( pageToken ? { page_token : pageToken } : { } )
152+ }
153+ } )
154+ )
155+ const data = assertSuccess ( response , operation )
156+ blocks . push ( ...( data ?. items ?? [ ] ) )
157+
158+ if ( data ?. has_more && ! data . page_token ) {
159+ throw new Error ( `${ operation } returned no next page token` )
160+ }
161+ pageToken = data ?. has_more ? data . page_token : undefined
162+ } while ( pageToken )
163+
164+ return blocks
165+ }
166+
128167export async function listWikiNodes ( client , { spaceId, parentNodeToken } ) {
129168 const nodes = [ ]
130169 let pageToken
@@ -149,6 +188,10 @@ export async function listWikiNodes(client, { spaceId, parentNodeToken }) {
149188 return nodes
150189}
151190
191+ function positiveInteger ( value ) {
192+ return Number . isInteger ( value ) && value > 0 ? value : undefined
193+ }
194+
152195export async function downloadDocumentMedia ( client , fileToken ) {
153196 const response = await callFeishu ( `Download document media ${ fileToken } ` , ( ) =>
154197 client . drive . v1 . media . download ( {
0 commit comments