* 导出选中的多个高亮为 Markdown 文件 * @param highlights 要导出的高亮数组 * @returns 返回创建的新文件 * @throws 如果没有高亮或创建文件失败
(highlights: HighlightInfo[])
| 47 | * @throws 如果没有高亮或创建文件失败 |
| 48 | */ |
| 49 | async exportHighlightsAsMarkdown(highlights: HighlightInfo[]): Promise<TFile> { |
| 50 | if (!highlights || highlights.length === 0) { |
| 51 | throw new Error(t("No highlights to export.")); |
| 52 | } |
| 53 | |
| 54 | const settings = this.getPluginSettings(); |
| 55 | |
| 56 | // 按文件分组高亮 |
| 57 | const highlightsByFile: Record<string, HighlightInfo[]> = {}; |
| 58 | |
| 59 | for (const highlight of highlights) { |
| 60 | if (!highlight.filePath) continue; |
| 61 | |
| 62 | if (!highlightsByFile[highlight.filePath]) { |
| 63 | highlightsByFile[highlight.filePath] = []; |
| 64 | } |
| 65 | |
| 66 | highlightsByFile[highlight.filePath].push(highlight); |
| 67 | } |
| 68 | |
| 69 | // 生成内容 |
| 70 | const contentParts: string[] = []; |
| 71 | |
| 72 | // 为每个文件生成内容 |
| 73 | for (const filePath in highlightsByFile) { |
| 74 | const file = this.app.vault.getAbstractFileByPath(filePath); |
| 75 | if (!(file instanceof TFile)) continue; |
| 76 | |
| 77 | contentParts.push(`## ${file.basename}`); |
| 78 | contentParts.push(""); |
| 79 | |
| 80 | // 使用模板或默认方式生成内容 |
| 81 | const fileHighlights = highlightsByFile[filePath]; |
| 82 | const customTemplate = settings?.export?.exportTemplate; |
| 83 | |
| 84 | // 如果有自定义模板且不为空,使用模板解析方式 |
| 85 | if (customTemplate && customTemplate.trim() !== '') { |
| 86 | const templateContent = await this.contentRenderer.generateContentFromTemplate(file, fileHighlights, customTemplate); |
| 87 | if (templateContent.trim() !== '') { |
| 88 | contentParts.push(templateContent); |
| 89 | } |
| 90 | } else { |
| 91 | // 如果没有自定义模板或模板为空,使用原来的方式 |
| 92 | const defaultContent = await this.contentRenderer.generateDefaultContent(file, fileHighlights); |
| 93 | contentParts.push(defaultContent); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | const content = contentParts.join("\n"); |
| 98 | |
| 99 | // 获取导出路径并创建文件 |
| 100 | const exportPath = settings?.export?.exportPath || ''; |
| 101 | const fileName = `Selected Highlights ${window.moment().format("YYYYMMDDHHmmss")}`; |
| 102 | |
| 103 | return await this.createExportFile(fileName, content, exportPath); |
| 104 | } |
| 105 | |
| 106 | /** |
no test coverage detected