|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Converts a GitHub issue JSON file (with comments) to a readable Markdown format. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * bun run github-issue-to-markdown.ts <input-json-file> [output-md-file] |
| 7 | + * |
| 8 | + * If output file is not specified, it will use the input filename with .md extension |
| 9 | + */ |
| 10 | + |
| 11 | +import { readFileSync, writeFileSync } from 'fs' |
| 12 | +import { resolve, dirname, basename, extname } from 'path' |
| 13 | + |
| 14 | +interface GitHubUser { |
| 15 | + login: string |
| 16 | + id: number |
| 17 | +} |
| 18 | + |
| 19 | +interface GitHubComment { |
| 20 | + id: number |
| 21 | + user: GitHubUser |
| 22 | + created_at: string |
| 23 | + updated_at: string |
| 24 | + body: string |
| 25 | + author_association: string |
| 26 | +} |
| 27 | + |
| 28 | +interface GitHubIssue { |
| 29 | + number: number |
| 30 | + title: string |
| 31 | + user: GitHubUser |
| 32 | + created_at: string |
| 33 | + updated_at: string |
| 34 | + body: string |
| 35 | + author_association: string |
| 36 | + comments: number |
| 37 | + comments_data?: GitHubComment[] |
| 38 | +} |
| 39 | + |
| 40 | +function formatDate(dateString: string): string { |
| 41 | + const date = new Date(dateString) |
| 42 | + return date.toISOString().split('T')[0] |
| 43 | +} |
| 44 | + |
| 45 | +function convertIssueToMarkdown(issue: GitHubIssue): string { |
| 46 | + let markdown = '' |
| 47 | + |
| 48 | + // Issue header |
| 49 | + markdown += `# Issue #${issue.number}: ${issue.title}\n\n` |
| 50 | + |
| 51 | + // Issue metadata |
| 52 | + markdown += `**Author:** ${issue.user.login}\n` |
| 53 | + markdown += `**Date:** ${formatDate(issue.created_at)}\n` |
| 54 | + markdown += `**Comments:** ${issue.comments}\n\n` |
| 55 | + |
| 56 | + // Issue body |
| 57 | + markdown += `## Issue Description\n\n` |
| 58 | + markdown += `${issue.body}\n\n` |
| 59 | + |
| 60 | + // Comments section |
| 61 | + if (issue.comments_data && issue.comments_data.length > 0) { |
| 62 | + markdown += `## Comments (${issue.comments_data.length})\n\n` |
| 63 | + |
| 64 | + for (const comment of issue.comments_data) { |
| 65 | + markdown += `---\n\n` |
| 66 | + markdown += `### Comment by ${comment.user.login} on ${formatDate(comment.created_at)}\n\n` |
| 67 | + |
| 68 | + if (comment.author_association && comment.author_association !== 'NONE') { |
| 69 | + markdown += `_${comment.author_association}_\n\n` |
| 70 | + } |
| 71 | + |
| 72 | + markdown += `${comment.body}\n\n` |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return markdown |
| 77 | +} |
| 78 | + |
| 79 | +function main() { |
| 80 | + const args = process.argv.slice(2) |
| 81 | + |
| 82 | + if (args.length === 0) { |
| 83 | + console.error('Error: No input file specified') |
| 84 | + console.error( |
| 85 | + 'Usage: bun run github-issue-to-markdown.ts <input-json-file> [output-md-file]', |
| 86 | + ) |
| 87 | + process.exit(1) |
| 88 | + } |
| 89 | + |
| 90 | + const inputFile = resolve(args[0]) |
| 91 | + let outputFile: string |
| 92 | + |
| 93 | + if (args.length > 1) { |
| 94 | + outputFile = resolve(args[1]) |
| 95 | + } else { |
| 96 | + // Generate output filename from input filename |
| 97 | + const dir = dirname(inputFile) |
| 98 | + const base = basename(inputFile, extname(inputFile)) |
| 99 | + outputFile = resolve(dir, `${base}.md`) |
| 100 | + } |
| 101 | + |
| 102 | + try { |
| 103 | + // Read and parse JSON file |
| 104 | + console.log(`Reading ${inputFile}...`) |
| 105 | + const jsonContent = readFileSync(inputFile, 'utf-8') |
| 106 | + const issue: GitHubIssue = JSON.parse(jsonContent) |
| 107 | + |
| 108 | + // Convert to Markdown |
| 109 | + console.log('Converting to Markdown...') |
| 110 | + const markdown = convertIssueToMarkdown(issue) |
| 111 | + |
| 112 | + // Write output file |
| 113 | + console.log(`Writing to ${outputFile}...`) |
| 114 | + writeFileSync(outputFile, markdown, 'utf-8') |
| 115 | + |
| 116 | + console.log('✓ Conversion complete!') |
| 117 | + console.log(` Input: ${inputFile}`) |
| 118 | + console.log(` Output: ${outputFile}`) |
| 119 | + console.log(` Comments: ${issue.comments_data?.length || 0}`) |
| 120 | + } catch (error) { |
| 121 | + if (error instanceof Error) { |
| 122 | + console.error(`Error: ${error.message}`) |
| 123 | + } else { |
| 124 | + console.error('An unknown error occurred') |
| 125 | + } |
| 126 | + process.exit(1) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +main() |
0 commit comments