torontojs / slack-overflow

Respond to a Slack slash command with Stackoverflow answers.
1 stars 2 forks source link

handle encoding / decoding #5

Open alex-wilmer opened 6 years ago

alex-wilmer commented 6 years ago

questions with special characters need to be encoded:

image

responses with special characters need to be decoded:

image

owenip commented 6 years ago

May I work on this issue? Thinking of using regex to decode the special characters.

mihok commented 6 years ago

Yas! Go for it @owenip

owenip commented 6 years ago

Hey @mihok , @alex-wilmer , I am wondering how many special characters should be supported. Please see HTML named character References.

Current Approaches for most common special characters

function unescapeHtml(text) {
  return text
    .replace(/&/g, '&')
    .replace(/&lt;/g, "<")
    .replace(/&gt;/g, ">")
    .replace(/&quot;/g, "\"")
    .replace(/&#039;/g, "'");
}
alex-wilmer commented 6 years ago

Isn't encodeURI and decodeURI sufficient for this?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI

owenip commented 6 years ago

Not Escaped:
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

encodeURI doesn't handle those characters.

Also stackOverflow api return named characters which could not be converted by decodeURI