|
56 | 56 | * footnotes: Support footnotes as in use on daringfireball.net and |
57 | 57 | implemented in other Markdown processors (tho not in Markdown.pl v1.0.1). |
58 | 58 | * header-ids: Adds "id" attributes to headers. The id value is a slug of |
59 | | - the header text. |
| 59 | + the header text. With `{"strip-syntax": True}`, the slug is made from the |
| 60 | + rendered header, so link URLs, image paths, inline HTML tags and emphasis |
| 61 | + markers are left out of it. |
60 | 62 | * highlightjs-lang: Allows specifying the language which used for syntax |
61 | 63 | highlighting when using fenced-code-blocks and highlightjs. |
62 | 64 | * html-classes: Takes a dict mapping html tag names (lowercase) to a |
@@ -1667,6 +1669,8 @@ def _do_links(self, text: str) -> str: |
1667 | 1669 | text = link_processor.run(text) |
1668 | 1670 | return text |
1669 | 1671 |
|
| 1672 | + _html_tag_re = re.compile(r'<[^>]*>') |
| 1673 | + |
1670 | 1674 | def header_id_from_text(self, |
1671 | 1675 | text: str, |
1672 | 1676 | prefix: str, |
@@ -1751,16 +1755,26 @@ def _h_sub(self, match: re.Match[str]) -> str: |
1751 | 1755 | if demote_headers: |
1752 | 1756 | n = min(n + demote_headers, 6) |
1753 | 1757 | header_id_attr = "" |
| 1758 | + html = self._run_span_gamut(header_group) |
1754 | 1759 | if "header-ids" in self.extras: |
1755 | 1760 | # in safe mode, inline HTML is hashed by now. Unhash it so the |
1756 | 1761 | # sanitized HTML doesn't get swapped back into the id attribute |
1757 | | - header_text = self._unhash_html_spans(header_group) |
| 1762 | + if self.extras["header-ids"].get('strip-syntax'): |
| 1763 | + # build the id from the rendered header so that markdown syntax |
| 1764 | + # (link URLs, image paths, emphasis markers) doesn't end up in it. |
| 1765 | + header_text = self._html_tag_re.sub('', self._unhash_html_spans(html)) |
| 1766 | + if "footnotes" in self.extras: |
| 1767 | + # footnote refs are numbered later on. Drop the placeholder |
| 1768 | + # so it doesn't end up in the id |
| 1769 | + marker = self._footnote_marker + '-' |
| 1770 | + header_text = header_text.replace(marker, '') |
| 1771 | + else: |
| 1772 | + header_text = self._unhash_html_spans(header_group) |
1758 | 1773 | header_id = self.header_id_from_text( |
1759 | 1774 | self._unescape_special_chars(header_text), |
1760 | 1775 | self.extras["header-ids"].get('prefix'), n) |
1761 | 1776 | if header_id: |
1762 | 1777 | header_id_attr = ' id="%s"' % header_id |
1763 | | - html = self._run_span_gamut(header_group) |
1764 | 1778 | if "toc" in self.extras and header_id: |
1765 | 1779 | self._toc_add_entry(n, header_id, html) |
1766 | 1780 | return "<h%d%s>%s</h%d>\n\n" % (n, header_id_attr, html, n) |
|
0 commit comments