diff --git a/mkdocs/compat.py b/mkdocs/compat.py
index 49bd396a..518a4937 100644
--- a/mkdocs/compat.py
+++ b/mkdocs/compat.py
@@ -13,6 +13,7 @@ if PY2:
     httpserver = httpserver
     import SocketServer
     socketserver = SocketServer
+    from HTMLParser import HTMLParser
 
     import itertools
     zip = itertools.izip
@@ -30,6 +31,7 @@ else:  # PY3
     httpserver = httpserver
     import socketserver
     socketserver = socketserver
+    from html.parser import HTMLParser
 
     zip = zip
 
diff --git a/mkdocs/toc.py b/mkdocs/toc.py
index 410aff5a..89627381 100644
--- a/mkdocs/toc.py
+++ b/mkdocs/toc.py
@@ -14,9 +14,7 @@ The steps we take to generate a table of contents are:
 * Parse table of contents HTML into the underlying data structure.
 """
 
-import re
-
-TOC_LINK_REGEX = re.compile('<a href=["]([^"]*)["]>([^<]*)</a>')
+from mkdocs.compat import HTMLParser
 
 
 class TableOfContents(object):
@@ -52,6 +50,32 @@ class AnchorLink(object):
         return ret
 
 
+class TOCParser(HTMLParser):
+
+    def __init__(self):
+        HTMLParser.__init__(self)
+        self.links = []
+
+        self.in_anchor = True
+        self.attrs = None
+        self.title = ''
+
+    def handle_starttag(self, tag, attrs):
+
+        if tag == 'a':
+            self.in_anchor = True
+            self.attrs = dict(attrs)
+
+    def handle_endtag(self, tag):
+        if tag == 'a':
+            self.in_anchor = False
+
+    def handle_data(self, data):
+
+        if self.in_anchor:
+            self.title += data
+
+
 def _parse_html_table_of_contents(html):
     """
     Given a table of contents string that has been automatically generated by
@@ -63,9 +87,11 @@ def _parse_html_table_of_contents(html):
     parents = []
     ret = []
     for line in lines:
-        match = TOC_LINK_REGEX.search(line)
-        if match:
-            href, title = match.groups()
+        parser = TOCParser()
+        parser.feed(line)
+        if parser.title:
+            href = parser.attrs['href']
+            title = parser.title
             nav = AnchorLink(title, href)
             # Add the item to its parent if required.  If it is a topmost
             # item then instead append it to our return value.

Not all headers are automatically linked
I have an API reference site for a project that's hosted on ReadTheDocs using mkdocs as the documentation engine. Headers that contain things like `<code>` blocks aren't linked, while all others seem to be.

I can reproduce this locally with a plain mkdocs install using the RTD theme.

Here's an example:
http://carbon.lpghatguy.com/en/latest/Classes/Collections.Tuple/

All three of the methods in that page should be automatically linked in the sidebar navigation, but only the one without any fancy decoration is. All of them have been given valid HTML ids, so they're possible to link, they just aren't.

The markdown for that page, which works around a couple RTD bugs and doesn't look that great, is here:
https://raw.githubusercontent.com/lua-carbon/carbon/master/docs/Classes/Collections.Tuple.md
diff --git a/mkdocs/tests/toc_tests.py b/mkdocs/tests/toc_tests.py
index 03ab9cd0..b0bdea11 100644
--- a/mkdocs/tests/toc_tests.py
+++ b/mkdocs/tests/toc_tests.py
@@ -29,6 +29,20 @@ class TableOfContentsTests(unittest.TestCase):
         toc = self.markdown_to_toc(md)
         self.assertEqual(str(toc).strip(), expected)
 
+    def test_indented_toc_html(self):
+        md = dedent("""
+        # Heading 1
+        ## <code>Heading</code> 2
+        ## Heading 3
+        """)
+        expected = dedent("""
+        Heading 1 - #heading-1
+            Heading 2 - #heading-2
+            Heading 3 - #heading-3
+        """)
+        toc = self.markdown_to_toc(md)
+        self.assertEqual(str(toc).strip(), expected)
+
     def test_flat_toc(self):
         md = dedent("""
         # Heading 1

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov
3.7
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi @ file:///croot/certifi_1671487769961/work/certifi
coverage==7.2.7
exceptiongroup==1.2.2
ghp-import==2.1.0
importlib-metadata==6.7.0
iniconfig==2.0.0
Jinja2==3.1.6
Markdown==2.4.1
MarkupSafe==2.1.5
-e git+https://github.com/mkdocs/mkdocs.git@74e60382b84b3af9969b30cc2cd9a98894d113f5#egg=mkdocs
packaging==24.0
pluggy==1.2.0
pytest==7.4.4
pytest-cov==4.1.0
python-dateutil==2.9.0.post0
PyYAML==6.0.1
six==1.17.0
tomli==2.0.1
typing_extensions==4.7.1
watchdog==3.0.0
zipp==3.15.0

name: mkdocs
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - coverage==7.2.7
      - exceptiongroup==1.2.2
      - ghp-import==2.1.0
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - jinja2==3.1.6
      - markdown==2.4.1
      - markupsafe==2.1.5
      - packaging==24.0
      - pluggy==1.2.0
      - pytest==7.4.4
      - pytest-cov==4.1.0
      - python-dateutil==2.9.0.post0
      - pyyaml==6.0.1
      - six==1.17.0
      - tomli==2.0.1
      - typing-extensions==4.7.1
      - watchdog==3.0.0
      - zipp==3.15.0
prefix: /opt/conda/envs/mkdocs

mkdocs/tests/toc_tests.py::TableOfContentsTests::test_indented_toc_html
mkdocs/tests/toc_tests.py::TableOfContentsTests::test_flat_h2_toc mkdocs/tests/toc_tests.py::TableOfContentsTests::test_flat_toc mkdocs/tests/toc_tests.py::TableOfContentsTests::test_indented_toc mkdocs/tests/toc_tests.py::TableOfContentsTests::test_mixed_toc
mkdocs/compat.py mkdocs/toc.py
mkdocs/compat.py mkdocs/toc.py