Skip to content

Commit

Permalink
fix: regex breaking on some usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash Gray committed Jul 22, 2023
1 parent 17ad3f5 commit c525971
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions shield-generator/pages/api/shield/[user].js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export default async function handler(req, res) {
return userInfo;
}

// stolen from https://stackoverflow.com/a/6969486
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

// get the shield from shields.io and returns it
async function makeShield() {
let { t, p } = await getUserInfo();
Expand Down Expand Up @@ -81,15 +86,16 @@ export default async function handler(req, res) {
const svgShield = await rawShield.text();

// fix the username and invite url being capitalized
let svgShieldFix = svgShield.replace(new RegExp(t.toUpperCase(), "g"), t);
let svgShieldFix = svgShield.replace(new RegExp(escapeRegExp(t.toUpperCase()), "g"), t);

svgShieldFix = svgShieldFix.replace(
new RegExp("HTTPS://DISCORD.GG/ZKSPFFWQDG", "g"),
"https://discord.gg/zkspfFwqDg"
);

// make the username bold
svgShieldFix = svgShieldFix.replace(
new RegExp(`fill="#fff">${t}</text>`, "g"),
new RegExp(`fill="#fff">${escapeRegExp(t)}</text>`, "g"),
`fill="#fff" font-weight="bold">${t}</text>`
);

Expand Down

1 comment on commit c525971

@vercel
Copy link

@vercel vercel bot commented on c525971 Jul 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.