const fs = require('fs'); const path = require('path'); class NotImplementedError extends Error {} const MARKDOWN_EXT = new Set(['.md', '.markdown']); /** * 檔案 → md 轉換(T3 骨架只做 .md passthrough;T4 補 Markitdown 邏輯)。 * srcPath: 來源檔案絕對路徑(客戶知識資料夾內) * 回傳:{ relOutputPath, content }(相對 target repo 子目錄的輸出路徑 + md 內容) */ function transformFile(srcPath, watchDir) { const ext = path.extname(srcPath).toLowerCase(); const relSrc = path.relative(watchDir, srcPath); if (MARKDOWN_EXT.has(ext)) { const raw = fs.readFileSync(srcPath, 'utf8'); const content = stampSourcePath(raw, relSrc); const relOutputPath = relSrc; return { relOutputPath, content }; } // 非 md(docx/pptx/pdf...)— T4 待補 Markitdown 轉檔,這裡先誠實丟未實作,不假裝轉好。 throw new NotImplementedError( `${relSrc}:非 .md 格式轉檔待 T4(Markitdown adapter)補上,本骨架先跳過`, ); } /** md frontmatter 補 source_path(溯源用,design.md §4:「md frontmatter 記原檔路徑」)。 */ function stampSourcePath(raw, relSrc) { const stamp = `source_path: "${relSrc}"`; if (raw.startsWith('---\n')) { const end = raw.indexOf('\n---', 4); if (end !== -1) { const fm = raw.slice(4, end); if (fm.includes('source_path:')) return raw; // 已有就不重複塞 return `---\n${stamp}\n${fm}\n---${raw.slice(end + 4)}`; } } return `---\n${stamp}\n---\n\n${raw}`; } module.exports = { transformFile, NotImplementedError };