Repository: Python-Markdown/markdown (python)

Issue:
Regression in unescaping backslash in TOC title
Now I found another issue similar to #1358 and I checked that its fix doesn't also fix this one.

Regression in 3.4 - c0f6e5a31ea8e7fe98910a0523144c2a96fa9bf1 is the first bad commit

Reproduction code:
```python
import markdown

md = markdown.Markdown(extensions=['toc'])
s = r'''
### hello \[foo]
'''
md.convert(s)
print(md.toc_tokens)
```

Output before vs after:
```python
[{'level': 3, 'id': 'hello-foo', 'name': 'hello [foo]', 'children': []}]
```
```python
[{'level': 3, 'id': 'hello-foo', 'name': 'hello \x0291\x03foo]', 'children': []}]
```


Pull request:
Unescape backslash escaped TOC token names
Fixes #1360.

Patch:
diff --git a/docs/change_log/index.md b/docs/change_log/index.md
index 75242d6..baec79d 100644
--- a/docs/change_log/index.md
+++ b/docs/change_log/index.md
@@ -7,6 +7,7 @@ Python-Markdown Change Log
 
 * Add a special case for initial 's to smarty extension (#1305).
 * Unescape any backslash escaped inline raw HTML (#1358).
+* Unescape backslash escaped TOC token names (#1360).
 
 March 23, 2023: version 3.4.3 (a bug-fix release).
 
diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py
index 9927b99..4070281 100644
--- a/markdown/extensions/toc.py
+++ b/markdown/extensions/toc.py
@@ -289,10 +289,10 @@ class TocTreeprocessor(Treeprocessor):
                     toc_tokens.append({
                         'level': int(el.tag[-1]),
                         'id': el.attrib["id"],
-                        'name': stashedHTML2text(
+                        'name': unescape(stashedHTML2text(
                             code_escape(el.attrib.get('data-toc-label', text)),
                             self.md, strip_entities=False
-                        )
+                        ))
                     })
 
                 # Remove the data-toc-label attribute as it is no longer needed


Test patch:
diff --git a/tests/test_syntax/extensions/test_toc.py b/tests/test_syntax/extensions/test_toc.py
index d879f6e..d64c6d3 100644
--- a/tests/test_syntax/extensions/test_toc.py
+++ b/tests/test_syntax/extensions/test_toc.py
@@ -442,6 +442,23 @@ class TestTOC(TestCase):
         self.assertMarkdownRenders(
             r'# escaped\_character',
             '<h1 id="escaped_character">escaped_character</h1>',
+            expected_attrs={
+                'toc': (
+                    '<div class="toc">\n'
+                      '<ul>\n'                                                           # noqa
+                        '<li><a href="#escaped_character">escaped_character</a></li>\n'  # noqa
+                      '</ul>\n'                                                          # noqa
+                    '</div>\n'                                                           # noqa
+                ),
+                'toc_tokens': [
+                    {
+                        'level': 1,
+                        'id': 'escaped_character',
+                        'name': 'escaped_character',
+                        'children': []
+                    }
+                ]
+            },
             extensions=['toc']
         )