diff --git a/feishu-pages/src/doc.ts b/feishu-pages/src/doc.ts index 8f49827..d3baa65 100644 --- a/feishu-pages/src/doc.ts +++ b/feishu-pages/src/doc.ts @@ -11,7 +11,7 @@ import { printMemoryUsage, writeTemplfile } from './utils'; * @param document_id doc.obj_token * @returns */ -export const fetchDocBody = async (fileDoc: Doc) => { + export const fetchDocBody = async (fileDoc: Doc): Promise<{ contentFile: string; meta: Record; fileTokens: Record; hasCache: boolean }> => { let document_id = fileDoc.obj_token; const doc = { @@ -21,15 +21,20 @@ export const fetchDocBody = async (fileDoc: Doc) => { blocks: [], }; - const fetchDocBlocks = async (document_id: string) => { + const fetchDocBlocks = async (document_id: string): Promise<{ hasCache: boolean; blocks: any[] }> => { // Check cache in .cache/docs/${document_id}.json + let hasCache = false; let cacheBlocks = path.join(CACHE_DIR, 'blocks', document_id + '.json'); fs.mkdirSync(path.dirname(cacheBlocks), { recursive: true }); if (fs.existsSync(cacheBlocks)) { const doc = JSON.parse(fs.readFileSync(cacheBlocks, 'utf-8')); if (doc?.obj_edit_time === fileDoc.obj_edit_time) { + hasCache = true; console.info('Cache hit doc: ', document_id, '...'); - return doc.blocks; + return { + hasCache, + blocks: doc.blocks, + }; } } @@ -49,10 +54,13 @@ export const fetchDocBody = async (fileDoc: Doc) => { blocks, }) ); - return blocks; + return { hasCache, blocks }; }; - doc.blocks = await fetchDocBlocks(document_id); + + let { blocks, hasCache } = await fetchDocBlocks(document_id); + + doc.blocks = blocks; printMemoryUsage('loaded doc blocks'); const render = new MarkdownRenderer(doc as any); @@ -64,9 +72,10 @@ export const fetchDocBody = async (fileDoc: Doc) => { let tmp_filename = writeTemplfile(content); return { - cotnent_file: tmp_filename, + contentFile: tmp_filename, meta, fileTokens, + hasCache, }; }; diff --git a/feishu-pages/src/index.ts b/feishu-pages/src/index.ts index a94e091..f7d4b6a 100644 --- a/feishu-pages/src/index.ts +++ b/feishu-pages/src/index.ts @@ -66,11 +66,12 @@ import { fetchAllDocs } from './wiki'; const fetchDocBodies = async (docs: FileDoc[]) => { for (let idx = 0; idx < docs.length; idx++) { const doc = docs[idx]; - const { cotnent_file, fileTokens, meta } = await fetchDocBody(doc); + const { contentFile, fileTokens, meta, hasCache } = await fetchDocBody(doc); - doc.cotnent_file = cotnent_file; + doc.contentFile = contentFile; doc.meta = meta; doc.fileTokens = fileTokens; + doc.hasCache = hasCache; await fetchDocBodies(doc.children); } @@ -97,9 +98,9 @@ const fetchDocAndWriteFile = async ( const folder = path.dirname(filename); fs.mkdirSync(folder, { recursive: true }); - let { cotnent_file, fileTokens } = doc; + let { contentFile, fileTokens } = doc; - let content = fs.readFileSync(cotnent_file, 'utf-8'); + let content = fs.readFileSync(contentFile, 'utf-8'); // Replace node_token to slug @@ -118,7 +119,9 @@ const fetchDocAndWriteFile = async ( let out = ''; out += metaInfo + '\n\n'; - content = await downloadFiles(content, fileTokens, folder); + if (!doc.hasCache) { + content = await downloadFiles(content, fileTokens, folder); + } out += content; diff --git a/feishu-pages/src/summary.ts b/feishu-pages/src/summary.ts index d989f56..0c0f22b 100644 --- a/feishu-pages/src/summary.ts +++ b/feishu-pages/src/summary.ts @@ -7,10 +7,11 @@ export interface FileDoc extends Doc { position: number; filename: string; /// The content file path (in Tmp path) - cotnent_file?: string; + contentFile?: string; meta?: Record; fileTokens?: any; children: FileDoc[]; + hasCache?: boolean; } /**