-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathgithub-issue-to-markdown.html
212 lines (175 loc) · 4.37 KB
/
github-issue-to-markdown.html
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
margin-bottom: 24px;
}
.input-group {
margin-bottom: 24px;
}
label {
display: block;
margin-bottom: 8px;
}
input {
width: 100%;
padding: 8px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background: #2ea44f;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #2c974b;
}
button:disabled {
background: #94d3a2;
cursor: not-allowed;
}
.output {
margin-top: 24px;
}
textarea {
width: 100%;
min-height: 300px;
padding: 12px;
font-size: 16px;
font-family: monospace;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
.error {
color: #cf222e;
margin-top: 8px;
}
.copy-button {
margin-top: 12px;
}
</style>
</head>
<body>
<h1>Convert GitHub issue to markdown</h1>
<div class="input-group">
<label for="issue-url">GitHub issue URL</label>
<input
type="text"
id="issue-url"
placeholder="https://github.com/owner/repo/issues/123"
>
</div>
<button id="convert">Convert to markdown</button>
<div class="output">
<textarea id="markdown-output" readonly></textarea>
<button class="copy-button" id="copy">Copy to clipboard</button>
</div>
<p class="error" id="error"></p>
<script type="module">
const urlInput = document.getElementById('issue-url')
const convertButton = document.getElementById('convert')
const markdownOutput = document.getElementById('markdown-output')
const copyButton = document.getElementById('copy')
const errorElement = document.getElementById('error')
function parseGitHubUrl(url) {
try {
const urlObj = new URL(url)
const [, owner, repo, , number] = urlObj.pathname.split('/')
return { owner, repo, number }
} catch (e) {
throw new Error('Invalid GitHub URL')
}
}
function convertToMarkdown(issue, comments) {
let md = `# ${issue.title}\n\n`
md += `*Posted by @${issue.user.login}*\n\n`
md += issue.body + '\n\n'
if (comments.length > 0) {
md += '---\n\n'
comments.forEach(comment => {
md += `### Comment by @${comment.user.login}\n\n`
md += comment.body + '\n\n'
md += '---\n\n'
})
}
return md
}
async function getAllPages(url) {
let allItems = []
let nextUrl = url
while (nextUrl) {
const response = await fetch(nextUrl)
if (!response.ok) {
throw new Error('Failed to fetch data')
}
const items = await response.json()
allItems = allItems.concat(items)
// Check for pagination in Link header
const link = response.headers.get('Link')
nextUrl = null
if (link) {
const nextLink = link.split(',').find(s => s.includes('rel="next"'))
if (nextLink) {
nextUrl = nextLink.split(';')[0].trim().slice(1, -1)
}
}
}
return allItems
}
async function fetchIssueAndComments(owner, repo, number) {
const issueUrl = `https://api.github.com/repos/${owner}/${repo}/issues/${number}`
const commentsUrl = `${issueUrl}/comments`
const [issue, comments] = await Promise.all([
fetch(issueUrl).then(res => {
if (!res.ok) throw new Error('Failed to fetch issue')
return res.json()
}),
getAllPages(commentsUrl)
])
return { issue, comments }
}
convertButton.addEventListener('click', async () => {
errorElement.textContent = ''
markdownOutput.value = ''
convertButton.disabled = true
try {
const { owner, repo, number } = parseGitHubUrl(urlInput.value)
const { issue, comments } = await fetchIssueAndComments(owner, repo, number)
const markdown = convertToMarkdown(issue, comments)
markdownOutput.value = markdown
} catch (error) {
errorElement.textContent = error.message
} finally {
convertButton.disabled = false
}
})
copyButton.addEventListener('click', () => {
markdownOutput.select()
document.execCommand('copy')
const originalText = copyButton.textContent
copyButton.textContent = 'Copied'
copyButton.disabled = true
setTimeout(() => {
copyButton.textContent = originalText
copyButton.disabled = false
}, 2000)
})
</script>
</body>
</html>