|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import * as vscode from 'vscode'; |
| 4 | +import { Trail } from '../models/breadcrumb'; |
| 5 | +import { formatRangeLabel } from './format'; |
| 6 | +import { fullPathToRelative } from '../utils'; |
| 7 | + |
| 8 | +const escapeCodeBlock = (value: string) => value.replace(/```/g, '\`\`\`'); |
| 9 | + |
| 10 | +const headline = (level: number, text: string) => `${'#'.repeat(level)} ${text}`; |
| 11 | + |
| 12 | +const formatDate = (value: string | undefined) => |
| 13 | + value ? new Date(value).toLocaleString() : undefined; |
| 14 | + |
| 15 | +const buildSummary = (trail: Trail) => { |
| 16 | + const files = new Set(trail.crumbs.map((crumb) => fullPathToRelative(crumb.uri.fsPath))); |
| 17 | + const first = formatDate(trail.crumbs[0]?.createdAt); |
| 18 | + const last = formatDate(trail.crumbs[trail.crumbs.length - 1]?.createdAt); |
| 19 | + |
| 20 | + const lines: string[] = []; |
| 21 | + lines.push(headline(2, 'Summary')); |
| 22 | + lines.push(''); |
| 23 | + lines.push(`- **Total crumbs:** ${trail.crumbs.length}`); |
| 24 | + lines.push(`- **Files touched:** ${files.size}`); |
| 25 | + lines.push(`- **Generated:** ${formatDate(new Date().toISOString())}`); |
| 26 | + if (first && last) { |
| 27 | + lines.push(`- **Investigation window:** ${first} – ${last}`); |
| 28 | + } |
| 29 | + lines.push(''); |
| 30 | + return lines.join('\n'); |
| 31 | +}; |
| 32 | + |
| 33 | +const buildCrumbSection = (trail: Trail) => { |
| 34 | + const lines: string[] = []; |
| 35 | + lines.push(headline(2, 'Trail')); |
| 36 | + lines.push(''); |
| 37 | + |
| 38 | + trail.crumbs.forEach((crumb, index) => { |
| 39 | + const filePath = fullPathToRelative(crumb.uri.fsPath); |
| 40 | + const rangeLabel = formatRangeLabel(crumb.range); |
| 41 | + const createdAt = formatDate(crumb.createdAt) ?? 'n/a'; |
| 42 | + lines.push(headline(3, `${index + 1}. ${filePath}:${rangeLabel}`)); |
| 43 | + lines.push(''); |
| 44 | + lines.push(`- **Captured:** ${createdAt}`); |
| 45 | + if (crumb.note) { |
| 46 | + lines.push(`- **Note:** ${crumb.note}`); |
| 47 | + } |
| 48 | + lines.push(''); |
| 49 | + lines.push('```'); |
| 50 | + lines.push(escapeCodeBlock(crumb.snippet)); |
| 51 | + lines.push('```'); |
| 52 | + lines.push(''); |
| 53 | + }); |
| 54 | + |
| 55 | + return lines.join('\n'); |
| 56 | +}; |
| 57 | + |
| 58 | +const generateTrailMarkdown = (trail: Trail) => { |
| 59 | + const lines: string[] = []; |
| 60 | + lines.push(headline(1, `Breadcrumb Trail – ${trail.name}`)); |
| 61 | + lines.push(''); |
| 62 | + if (trail.description) { |
| 63 | + lines.push(trail.description); |
| 64 | + lines.push(''); |
| 65 | + } |
| 66 | + lines.push(buildSummary(trail)); |
| 67 | + lines.push(buildCrumbSection(trail)); |
| 68 | + return lines.join('\n'); |
| 69 | +}; |
| 70 | + |
| 71 | +export const exportTrailToMarkdown = async (trail: Trail, uri?: vscode.Uri) => { |
| 72 | + if (!trail.crumbs.length) { |
| 73 | + vscode.window.showInformationMessage('[Breadcrumbs] Cannot export an empty trail.'); |
| 74 | + return undefined; |
| 75 | + } |
| 76 | + |
| 77 | + const markdown = generateTrailMarkdown(trail); |
| 78 | + const buffer = Buffer.from(markdown, 'utf8'); |
| 79 | + |
| 80 | + if (!uri) { |
| 81 | + const fileNameSafe = trail.name.replace(/[^a-z0-9\-_]+/gi, '-').replace(/-+/g, '-'); |
| 82 | + const defaultUri = vscode.workspace.workspaceFolders?.length |
| 83 | + ? vscode.Uri.joinPath( |
| 84 | + vscode.workspace.workspaceFolders[0].uri, |
| 85 | + `${fileNameSafe || 'breadcrumb-trail'}.md`, |
| 86 | + ) |
| 87 | + : undefined; |
| 88 | + |
| 89 | + uri = await vscode.window.showSaveDialog({ |
| 90 | + filters: { Markdown: ['md', 'markdown'] }, |
| 91 | + defaultUri, |
| 92 | + saveLabel: 'Export Breadcrumb Trail', |
| 93 | + }); |
| 94 | + if (!uri) { |
| 95 | + return undefined; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + await vscode.workspace.fs.writeFile(uri, buffer); |
| 100 | + |
| 101 | + const selection = await vscode.window.showInformationMessage( |
| 102 | + `Breadcrumb trail exported to ${uri.fsPath}.`, |
| 103 | + 'Open export', |
| 104 | + ); |
| 105 | + |
| 106 | + if (selection === 'Open export') { |
| 107 | + const document = await vscode.workspace.openTextDocument(uri); |
| 108 | + await vscode.window.showTextDocument(document, { preview: false }); |
| 109 | + } |
| 110 | + |
| 111 | + return uri; |
| 112 | +}; |
0 commit comments