Skip to content

Commit

Permalink
Improve formatting of issue/comment bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
djahandarie committed Mar 5, 2023
1 parent 7f11173 commit d69fcea
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,14 @@ function createGithubIssue(repo, oldIssue, closeOriginal) {
preventReferences: false,
},
(item) => {
const newIssueBody = `From ${urlObj.currentRepo} created by [${oldIssue.user.login}](${oldIssue.user.html_url}): [${urlObj.organization}/${urlObj.currentRepo}#${urlObj.issueNumber}](https://github.com/${urlObj.organization}/${urlObj.currentRepo}/issues/${urlObj.issueNumber}) \n\n${oldIssue.body}`;
const quotedOldBody = addBlockQuote(oldIssue.body)
const createdAtDate = oldIssue.created_at.split('T')[0];
const newBody = `**[<img src="https://avatars.githubusercontent.com/u/${oldIssue.user.id}?s=17&v=4" width="17" height="17" style="vertical-align: middle;"> @${oldIssue.user.login}](${oldIssue.user.html_url})** opened issue [${urlObj.organization}/${urlObj.currentRepo}#${urlObj.issueNumber}](${oldIssue.html_url}) on ${createdAtDate}: \n\n${quotedOldBody}`

// build new issue
const newIssue = {
title: oldIssue.title,
body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody,
body: item.preventReferences ? preventReferences(newBody) : newBody,
labels: oldIssue.labels,
}

Expand All @@ -348,11 +350,6 @@ function createGithubIssue(repo, oldIssue, closeOriginal) {
})
}

function preventReferences(text) {
// replace "github.com" links with "www.github.com" links, which do not cause references on the original issue due to the "www" (see https://github.com/orgs/community/discussions/23123#discussioncomment-3239240)
return text.replace(/https:\/\/github.com\//gi, "https://www.github.com/")
}

function cloneOldIssueComments(newIssue, repo, url) {
return ajaxRequest('GET', '', url).then((comments) => {
chrome.storage.sync.get(
Expand All @@ -371,8 +368,11 @@ function cloneOldIssueComments(newIssue, repo, url) {

comments.data.reduce(
(p, comment) => p.then(_ => {
const quotedOldBody = addBlockQuote(comment.body)
const createdAtDate = comment.created_at.split('T')[0];
const newBody = `**[<img src="https://avatars.githubusercontent.com/u/${comment.user.id}?s=17&v=4" width="17" height="17" style="vertical-align: middle;"> @${comment.user.login}](${comment.user.html_url})** commented [on ${createdAtDate}](${comment.html_url}): \n\n${quotedOldBody}`
const c = {
body: item.preventReferences ? preventReferences(comment.body) : comment.body,
body: item.preventReferences ? preventReferences(newBody) : newBody,
}
return ajaxRequest('POST', c, `https://api.github.com/repos/${repo}/issues/${newIssue}/comments`)
}),
Expand Down Expand Up @@ -588,3 +588,12 @@ function openModal() {
$('#kaminoModal').addClass('in')
$('#kaminoModal').css('display', 'block')
}

function addBlockQuote(text) {
return text.replace(/^/gm, "> ")
}

function preventReferences(text) {
// replace "github.com" links with "www.github.com" links, which do not cause references on the original issue due to the "www" (see https://github.com/orgs/community/discussions/23123#discussioncomment-3239240)
return text.replace(/https:\/\/github.com\//gi, "https://www.github.com/")
}

0 comments on commit d69fcea

Please sign in to comment.