valeriangalliat / markdown-it-anchor

A markdown-it plugin that adds an `id` attribute to headings and optionally permalinks.
The Unlicense
292 stars 72 forks source link

Some apps like Whatsapp and Telegram treat some characters at the end of a link as punctuation #81

Closed TimDaub closed 3 years ago

TimDaub commented 3 years ago

I started using this library on my blog for headlines and my first headline was /blog#the-algorithm%3B-it's-working!. But when I shared the link with my users on Telegram and Whatsapp, they didn't end up at the headline's ID.

I first thought it had to do with the package's encoding of my "slugification". However, it turns out that Whatsapp and Telegram simply don't count certain letters as part of a link when at the end. Here's an example:

signal-2021-02-24-142016

I know that I can simply overwrite the slugification. But would it make sense to make that a default behavior? I was expecting it to generate safe links.

TimDaub commented 3 years ago

How I ended up fixing it for now:

slugify: s => {
  s = s.replace(/[^a-zA-Z0-9]+/g, "");
  return encodeURIComponent(s);
}

(I ended up replacing all special characters with the empty string. The resulting URI is now: /blog#TheAlgorithmItsWorking

valeriangalliat commented 3 years ago

Hey!

I understand the concern. The idea behind the default behavior was to provide a safe (per spec) URL without being opinionated on how the slug should look like, and without requiring an external module either.

I personally use the slugify function from the string module on my own blog (see User-Friendly URLs which does something very similar to what you do and works for me, but would also strip the whole title when written in non-latin languages (and also requires an external library) which is why it's not the default.

Hope that makes sense =)

TimDaub commented 3 years ago

OK, all good. Thanks!