Skip to content
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
7 changes: 7 additions & 0 deletions docs/change_log/release-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ The following new features have been included in the 3.3 release:
maintain the current behavior in the rebuilt Markdown in HTML extension. A few random
edge-case bugs (see the included tests) were resolved in the process (#803).

* An alternate function `markdown.extensions.headerid.slugify_unicode` has been included
with the [Table of Contents](../extensions/toc.md) extension which supports Unicode
characters in table of contents slugs. The old `markdown.extensions.headerid.slugify`
method which removes non-ASCII characters remains the default. Import and pass
`markdown.extensions.headerid.slugify_unicode` to the `slugify` configuration option
to use the new behavior.

## Bug fixes

The following bug fixes are included in the 3.3 release:
Expand Down
3 changes: 3 additions & 0 deletions docs/extensions/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ The following options are provided to configure the output:

The callable must return a string appropriate for use in HTML `id` attributes.

An alternate version of the default callable supporting Unicode strings is also
provided as `markdown.extensions.headerid.slugify_unicode`.

* **`separator`**:
Word separator. Character which replaces white space in id. Defaults to "`-`".

Expand Down
13 changes: 9 additions & 4 deletions markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
import xml.etree.ElementTree as etree


def slugify(value, separator):
def slugify(value, separator, encoding='ascii'):
""" Slugify a string, to make it URL friendly. """
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
value = re.sub(r'[^\w\s-]', '', value.decode('ascii')).strip().lower()
return re.sub(r'[%s\s]+' % separator, separator, value)
value = unicodedata.normalize('NFKD', value).encode(encoding, 'ignore')
value = re.sub(r'[^\w\s-]', '', value.decode(encoding)).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')


IDCOUNT_RE = re.compile(r'^(.*)_([0-9]+)$')
Expand Down
22 changes: 22 additions & 0 deletions tests/test_syntax/extensions/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,25 @@ def testPermalinkWithEmptyTitle(self):
'</h1>', # noqa
extensions=[TocExtension(permalink=True, permalink_title="")]
)

def testPermalinkWithUnicodeInID(self):
from markdown.extensions.toc import slugify_unicode
self.assertMarkdownRenders(
'# Unicode ヘッダー',
'<h1 id="unicode-ヘッター">' # noqa
'Unicode ヘッダー' # noqa
'<a class="headerlink" href="#unicode-ヘッター" title="Permanent link">&para;</a>' # noqa
'</h1>', # noqa
extensions=[TocExtension(permalink=True, slugify=slugify_unicode)]
)

def testPermalinkWithUnicodeTitle(self):
from markdown.extensions.toc import slugify_unicode
self.assertMarkdownRenders(
'# Unicode ヘッダー',
'<h1 id="unicode-ヘッター">' # noqa
'Unicode ヘッダー' # noqa
'<a class="headerlink" href="#unicode-ヘッター" title="パーマリンク">&para;</a>' # noqa
'</h1>', # noqa
extensions=[TocExtension(permalink=True, permalink_title="パーマリンク", slugify=slugify_unicode)]
)