Skip to content

Instantly share code, notes, and snippets.

@khiet
Last active May 11, 2021 21:07
Show Gist options
  • Save khiet/b1f77c2b3a7d770f1df0c49580fac70e to your computer and use it in GitHub Desktop.
Save khiet/b1f77c2b3a7d770f1df0c49580fac70e to your computer and use it in GitHub Desktop.
Format images in table in Github description
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