Last active
May 11, 2021 21:07
-
-
Save khiet/b1f77c2b3a7d770f1df0c49580fac70e to your computer and use it in GitHub Desktop.
Format images in table in Github description
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const textarea = document.querySelector('textarea[name="pull_request[body]"]'); | |
const text = textarea.value; | |
const lines = text.split("\n") | |
const images = lines.filter(line => { | |
return line.includes("https://user-images"); | |
}); | |
let output = ''; | |
let imgs = images.map(image => { | |
const match = image.match(/\((.*?)\)/); | |
if (match) { | |
return `<img src=${match[1]} width=${1200/images.length} />`; | |
} | |
}); | |
// remove undefined | |
imgs = imgs.filter(e => e); | |
// construct table | |
output += `|`; | |
imgs.forEach(img => { | |
output += ` |`; | |
}); | |
output += `\n|`; | |
imgs.forEach(img => { | |
output += ` -------- |`; | |
}); | |
output += `\n|`; | |
imgs.forEach(img => { | |
output += ` ${img} |`; | |
}); | |
const non_images = lines.filter(line => { | |
return !line.includes("https://user-images"); | |
}); | |
const insertIndex = lines.findIndex(line => { | |
return line.includes("https://user-images"); | |
}); | |
non_images.splice(insertIndex, 0, output); | |
result = non_images.join('\n'); | |
textarea.value = result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment