Only use backslash escapes in autocomplete when needed

Autocompletion for references happens on the frontend. Those references
are turned into actual references on the backend, but only after
Markdown processing has happened. That means that if a reference
contains a character that Markdown might consume, it won't render
correctly. So we need to do some escaping on the frontend.

We have these potential problem characters:
https://docs.gitlab.com/ee/user/markdown.html#emphasis

1. ~ - this is ~~strikethrough~~, but only when doubled.
2. _ - used for _emphasis_, doubled is __bold__.
3. * - also used for *emphasis*, doubled is **bold** also.
4. ` - used for `code spans`, any number works.

We don't need to escape `-` any more. When it comes to being inside a
word:

1. a~~b~~ has strikethrough, so it needs to be escaped everywhere.
2. a_b_ has no emphasis (see [a]) so it only needs to be escaped at the
   start and end of words.
3. a*b* has emphasis, so it needs to be escaped everywhere.
4. a`b` has a code span, so it needs to be escaped everywhere.

Or, in code terms:

1. Always escape ~~, *, and ` when being inserted by autocomplete.
2. Escape _ when it's either at the beginning or the end of a word.

[a]: https://docs.gitlab.com/ee/user/markdown.html#multiple-underscores-in-words
97 jobs for <span class="ref-name">markdown-autocomplete-escaping</span> in 46 minutes and 50 seconds (queued for 32 seconds)
Status Job ID Name Coverage
  Build
manual #198226641
gitlab-org allowed to fail manual
review-docs-deploy-manual
 
  Prepare
passed #198226655
docker gitlab-org
build-qa-image

00:05:01

passed #198226652
gitlab-org
compile-assets

00:09:17

passed #198226656
gitlab-org
retrieve-tests-metadata

00:01:21

passed #198226654
gitlab-org
setup-test-env

00:08:43

 
  Test
passed #198226671
allowed to fail
code_quality

00:09:28

passed #198226752
gitlab-org
danger-review

00:02:59

passed #198226736
gitlab-org
db:check-schema-pg

00:02:54

passed #198226735
gitlab-org
db:migrate:reset-mysql

00:04:38

passed #198226734
gitlab-org
db:migrate:reset-pg

00:04:19

passed #198226740
gitlab-org
db:rollback-mysql

00:04:40

passed #198226739
gitlab-org
db:rollback-pg

00:03:31

passed #198226673
allowed to fail
dependency_scanning

00:05:24

passed #198226659
gitlab-org
docs lint

00:02:43

passed #198226732
gitlab-org
downtime_check

00:02:40

passed #198226733
gitlab-org
ee_compat_check

00:03:39

passed #198226660
gitlab-org docker
gitlab:assets:compile

00:19:43

passed #198226742
gitlab-org
gitlab:setup-mysql

00:07:04

passed #198226741
gitlab-org
gitlab:setup-pg

00:05:53

passed #198226753
gitlab-org
gitlab_git_test

00:00:34

passed #198226664
gitlab-org
jest

00:12:35

passed #198226663
gitlab-org
karma

00:13:48

57.56%
passed #198226738
gitlab-org
migration:path-mysql

00:16:22

passed #198226737
gitlab-org
migration:path-pg

00:13:04

passed #198226754
gitlab-org
no_ee_check

00:00:34

manual #198226670
allowed to fail manual
package-and-qa
passed #198226668
gitlab-org
qa-frontend-node:10

00:12:25

passed #198226667
gitlab-org
qa-frontend-node:8

00:13:44

passed #198226669
gitlab-org allowed to fail
qa-frontend-node:latest

00:11:27

passed #198226665
gitlab-org
qa:internal

00:03:33

passed #198226666
gitlab-org
qa:selectors

00:03:28

passed #198226743
review-build-cng

00:20:53

passed #198226744
gitlab-org allowed to fail
review-deploy

00:28:56

passed #198226748
docker gitlab-org allowed to fail
review-performance

00:31:33

manual #198226746
docker gitlab-org allowed to fail manual
review-qa-all
passed #198226745
docker gitlab-org allowed to fail
review-qa-smoke

00:32:05

manual #198226749
gitlab-org allowed to fail manual
review-stop
passed #198226730
gitlab-org
rspec-fast-spec-helper

00:03:24

passed #198226674
gitlab-org
rspec-pg 1/50

00:24:36

passed #198226683
gitlab-org
rspec-pg 10/50

00:23:34

passed #198226684
gitlab-org
rspec-pg 11/50

00:18:24

passed #198226685
gitlab-org
rspec-pg 12/50

00:20:42

passed #198226686
gitlab-org
rspec-pg 13/50

00:19:57

passed #198226687
gitlab-org
rspec-pg 14/50

00:17:18

passed #198226688
gitlab-org
rspec-pg 15/50

00:16:14

passed #198226689
gitlab-org
rspec-pg 16/50

00:15:52

passed #198226690
gitlab-org
rspec-pg 17/50

00:19:30

passed #198226691
gitlab-org
rspec-pg 18/50

00:19:43

passed #198226692
gitlab-org
rspec-pg 19/50

00:17:58

passed #198226675
gitlab-org
rspec-pg 2/50

00:22:27

passed #198226693
gitlab-org
rspec-pg 20/50

00:19:21

passed #198226695
gitlab-org
rspec-pg 21/50

00:19:22

passed #198226696
gitlab-org
rspec-pg 22/50

00:22:01

passed #198226697
gitlab-org
rspec-pg 23/50

00:20:20

passed #198226698
gitlab-org
rspec-pg 24/50

00:19:48

passed #198226699
gitlab-org
rspec-pg 25/50

00:20:02

passed #198226700
gitlab-org
rspec-pg 26/50

00:16:09

passed #198226702
gitlab-org
rspec-pg 27/50

00:22:40

passed #198226703
gitlab-org
rspec-pg 28/50

00:23:34

passed #198226704
gitlab-org
rspec-pg 29/50

00:22:46

passed #198226676
gitlab-org
rspec-pg 3/50

00:17:33

passed #198226705
gitlab-org
rspec-pg 30/50

00:17:46

passed #198226706
gitlab-org
rspec-pg 31/50

00:18:09

passed #198226707
gitlab-org
rspec-pg 32/50

00:21:43

passed #198226708
gitlab-org
rspec-pg 33/50

00:22:37

passed #198226709
gitlab-org
rspec-pg 34/50

00:16:52

passed #198226710
gitlab-org
rspec-pg 35/50

00:18:27

passed #198226712
gitlab-org
rspec-pg 36/50

00:18:03

passed #198226714
gitlab-org
rspec-pg 37/50

00:19:14

passed #198226715
gitlab-org
rspec-pg 38/50

00:16:37

passed #198226716
gitlab-org
rspec-pg 39/50

00:18:26

passed #198239556
gitlab-org
rspec-pg 4/50

00:19:10

passed #198226717
gitlab-org
rspec-pg 40/50

00:23:42

passed #198226718
gitlab-org
rspec-pg 41/50

00:18:30

passed #198226720
gitlab-org
rspec-pg 42/50

00:19:58

passed #198226721
gitlab-org
rspec-pg 43/50

00:20:09

passed #198226722
gitlab-org
rspec-pg 44/50

00:21:37

passed #198226723
gitlab-org
rspec-pg 45/50

00:19:29

passed #198226724
gitlab-org
rspec-pg 46/50

00:18:52

passed #198226726
gitlab-org
rspec-pg 47/50

00:20:10

passed #198226727
gitlab-org
rspec-pg 48/50

00:18:42

passed #198226728
gitlab-org
rspec-pg 49/50

00:14:23

passed #198226678
gitlab-org
rspec-pg 5/50

00:21:23

passed #198226729
gitlab-org
rspec-pg 50/50

00:18:49

passed #198239684
gitlab-org
rspec-pg 6/50

00:24:46

passed #198226680
gitlab-org
rspec-pg 7/50

00:24:10

passed #198226681
gitlab-org
rspec-pg 8/50

00:19:04

passed #198226682
gitlab-org
rspec-pg 9/50

00:17:01

passed #198226672
allowed to fail
sast

00:07:06

passed #198226731
gitlab-org
static-analysis

00:11:58

failed #198226677
gitlab-org
rspec-pg 4/50

00:05:26

failed #198226679
gitlab-org
rspec-pg 6/50

00:05:34

 
  Post Test
passed #198226759
gitlab-org
coverage

00:05:17

95.1%
passed #198226762
gitlab-org allowed to fail
flaky-examples-check

00:02:06

passed #198226756
gitlab-org
jsdoc

00:03:22

passed #198226755
gitlab-org
lint:javascript:report

00:03:14

 
  Post Cleanup
manual #198226764
gitlab-org allowed to fail manual
review-docs-cleanup