A user wrote an issue asking if inside the bio (or any arbitrary tag), any tagged accounts can be automatically linked like on Github.

I gave them this function:

  <script>
let bio = "The coolest company to work for is @github!  There is also @aws and @microsoft.";
let found = false;
for (let i = 0; i < bio.length; i++) {
let currentChar = bio[i];

if (currentChar.startsWith("@")) {
found = true;
continue // we don't need to look at other chars until next iterations
}

if (found) {
if (/[^A-Za-z0-9]/.test(currentChar)) { // we found the end of the link (you can add more special chars to this)
let tag = "<a href=https://github.com/" + link.substr(1) + ">" + link + "</a>"
linkedBio += tag + currentChar // we add the special char at the end of the tag so that it actually appears, otherwise it does not
found = false;
} else {
}
} else {
}
}

if (link.length > 0) { // means we need to add a link that is the last possible thing without anything after it
let tag = "<a href=https://github.com/" + link.substr(1) + ">" + link + "</a>"
}

</script>


I was going to use string.split(" "), but then I realized that if I did that I would destroy the specific formatting of the text if I used array.join(" ") after all of the formatting changes.

Ex: Original: "I work for the coolest companies: @aws, @microsoft, and @apple"

Joined: "I work for the coolest companies: @aws , @microsoft , and @apple"

The commas are messed up.

Is there a way to slim it down and/or simplify it? JS isn't my strongest language.

The function you want is replace:
function link_ats(text) {
return text.replace( /@(\w+)/g, '<a href=https://github.com/$1>@$1</a>' )