1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| 'use strict';
const fs = require('fs'); const path = require('path'); const chokidar = require('chokidar');
hexo.extend.filter.register('after_generate', function() { console.log('开始处理 YAML 文件复制...'); const sourceDir = hexo.source_dir; const publicDir = hexo.public_dir; function findAndCopyYamlFiles(dir, basePath = '') { const files = fs.readdirSync(dir); files.forEach(file => { const fullPath = path.join(dir, file); const relativePath = path.join(basePath, file); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { findAndCopyYamlFiles(fullPath, relativePath); } else if (file.match(/\.(yml|yaml)$/i)) { const destDir = path.join(publicDir, basePath); const destPath = path.join(destDir, file); if (!fs.existsSync(destDir)) { fs.mkdirSync(destDir, { recursive: true }); } try { fs.copyFileSync(fullPath, destPath); console.log(`已复制: ${relativePath} -> ${path.relative(publicDir, destPath)}`); } catch (err) { console.error(`复制失败: ${relativePath}`, err.message); } } }); } findAndCopyYamlFiles(sourceDir); console.log('YAML 文件复制完成'); });
|