* 检查文件是否应该被排除 * @param file 要检查的文件 * @param patterns 排除模式列表 * @returns 如果文件应该被排除则返回 true
(file: TFile, patternsStr: string)
| 8 | * @returns 如果文件应该被排除则返回 true |
| 9 | */ |
| 10 | static shouldExclude(file: TFile, patternsStr: string): boolean { |
| 11 | if (!patternsStr || patternsStr.trim().length === 0) { |
| 12 | return false; |
| 13 | } |
| 14 | |
| 15 | // 将逗号分隔的字符串分割成数组 |
| 16 | const patterns = patternsStr |
| 17 | .split(',') |
| 18 | .map(pattern => pattern.trim()) |
| 19 | .filter(pattern => pattern.length > 0); |
| 20 | |
| 21 | const filePath = file.path; |
| 22 | const fileName = file.basename; |
| 23 | |
| 24 | return patterns.some(pattern => { |
| 25 | // 移除前后空格 |
| 26 | pattern = pattern.trim(); |
| 27 | |
| 28 | // 如果是空字符串,跳过 |
| 29 | if (!pattern) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | // 处理笔记链接格式 [[note]] |
| 34 | if (pattern.startsWith('[[') && pattern.endsWith(']]')) { |
| 35 | const noteName = pattern.slice(2, -2); |
| 36 | return fileName === noteName; |
| 37 | } |
| 38 | |
| 39 | // 处理文件扩展名格式 *.extension |
| 40 | if (pattern.startsWith('*.')) { |
| 41 | const extension = pattern.slice(2); |
| 42 | return file.extension === extension || filePath.endsWith(extension); |
| 43 | } |
| 44 | |
| 45 | // 处理文件夹路径 |
| 46 | // 确保路径格式一致(移除开头的 '/') |
| 47 | const normalizedPattern = pattern.replace(/^\/+/, ''); |
| 48 | const normalizedPath = filePath.replace(/^\/+/, ''); |
| 49 | |
| 50 | // 检查文件是否在指定文件夹中 |
| 51 | return normalizedPath.startsWith(normalizedPattern + '/') || |
| 52 | normalizedPath === normalizedPattern; |
| 53 | }); |
| 54 | } |
| 55 | } |
no test coverage detected