Skip to content

toc: Do not remove diacritical marks when slugify_unicode is used #1121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/change_log/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ title: Change Log
Python-Markdown Change Log
=========================

Under development: version 3.3.5 (a bug-fix release).

* Make the `slugify_unicode` function not remove diacritical marks (#1118).

Feb 24, 2021: version 3.3.4 (a bug-fix release).

* Properly parse unclosed tags in code spans (#1066).
Expand Down
11 changes: 7 additions & 4 deletions markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@
import xml.etree.ElementTree as etree


def slugify(value, separator, encoding='ascii'):
def slugify(value, separator, unicode=False):
""" Slugify a string, to make it URL friendly. """
value = unicodedata.normalize('NFKD', value).encode(encoding, 'ignore')
value = re.sub(r'[^\w\s-]', '', value.decode(encoding)).strip().lower()
if not unicode:
# Replace Extended Latin characters with ASCII, i.e. žlutý → zluty
value = unicodedata.normalize('NFKD', value)
value = value.encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value).strip().lower()
return re.sub(r'[{}\s]+'.format(separator), separator, value)


def slugify_unicode(value, separator):
""" Slugify a string, to make it URL friendly while preserving Unicode characters. """
return slugify(value, separator, 'utf-8')
return slugify(value, separator, unicode=True)


IDCOUNT_RE = re.compile(r'^(.*)_([0-9]+)$')
Expand Down
18 changes: 14 additions & 4 deletions tests/test_syntax/extensions/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,9 @@ def testPermalinkWithUnicodeInID(self):
from markdown.extensions.toc import slugify_unicode
self.assertMarkdownRenders(
'# Unicode ヘッダー',
'<h1 id="unicode-ヘッター">' # noqa
'<h1 id="unicode-ヘッダー">' # noqa
'Unicode ヘッダー' # noqa
'<a class="headerlink" href="#unicode-ヘッター" title="Permanent link">&para;</a>' # noqa
'<a class="headerlink" href="#unicode-ヘッダー" title="Permanent link">&para;</a>' # noqa
'</h1>', # noqa
extensions=[TocExtension(permalink=True, slugify=slugify_unicode)]
)
Expand All @@ -545,9 +545,19 @@ def testPermalinkWithUnicodeTitle(self):
from markdown.extensions.toc import slugify_unicode
self.assertMarkdownRenders(
'# Unicode ヘッダー',
'<h1 id="unicode-ヘッター">' # noqa
'<h1 id="unicode-ヘッダー">' # noqa
'Unicode ヘッダー' # noqa
'<a class="headerlink" href="#unicode-ヘッター" title="パーマリンク">&para;</a>' # noqa
'<a class="headerlink" href="#unicode-ヘッダー" title="パーマリンク">&para;</a>' # noqa
'</h1>', # noqa
extensions=[TocExtension(permalink=True, permalink_title="パーマリンク", slugify=slugify_unicode)]
)

def testPermalinkWithExtendedLatinInID(self):
self.assertMarkdownRenders(
'# Théâtre',
'<h1 id="theatre">' # noqa
'Théâtre' # noqa
'<a class="headerlink" href="#theatre" title="Permanent link">&para;</a>' # noqa
'</h1>', # noqa
extensions=[TocExtension(permalink=True)]
)