Skip to content

Commit d57e8c9

Browse files
committed
Merge branch 'heading-contains-italics'
* heading-contains-italics: Add message text Ignore underscores in codeblocks Fix typo Fix text numbers Add more complex edge cases for italic in headings Support all test cases in italic_in_heading Treat underscore for italic and underscore as text
2 parents 8e83a14 + 62d0152 commit d57e8c9

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

markdowntoc/id.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def heading_to_id(self, heading):
1818
if self.markdown_preview == 'github':
1919
_h1 = self.postprocess_inject_header_id('<h1>%s</h1>' % heading)
2020
pattern = r'<h1 id="(.*)">.*</h1>'
21-
matchs = re.finditer(pattern, _h1)
22-
for match in matchs:
21+
matches = re.finditer(pattern, _h1)
22+
for match in matches:
2323
return match.groups()[0]
2424
elif self.markdown_preview == 'markdown':
2525
return self.slugify(heading, '-')
@@ -34,6 +34,20 @@ def heading_to_id(self, heading):
3434
return self.do_id_replacements(_id)
3535

3636
def do_id_replacements(self, _str):
37+
38+
# **Ignore the texts in codeblocks
39+
# **[1] stock and take over codeblocks
40+
matches = re.finditer(r'`([^`]*)`', _str)
41+
_str = re.sub(r'`([^`]*)`', '[codeblock]', _str)
42+
43+
# Treat '_' for italic and '_' as text
44+
_str = re.sub(r'_([^_ ]{1}.*[^_ ]{1})_', '\\1', _str)
45+
46+
# **[2] recover codeblocks
47+
for match in matches:
48+
_str = _str.replace('[codeblock]', match.groups()[0], 1)
49+
50+
# User setting replacements
3751
for group in self.id_replacements:
3852
_str = re.sub(group['pattern'], group['replacement'], _str)
3953
return _str

messages/3.0.4.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# MarkdownTOC - 3.0.4
2+
3+
---
4+
5+
**NOTE: 3.x has "breacking chnages"**
6+
7+
If you are using 3.x for the first time and have any problems, please check [Upgrade Guide](https://github.com/naokazuterada/MarkdownTOC/releases/tag/3.0.0) at first.
8+
9+
---
10+
11+
## Changes
12+
13+
- Fix anchor problem for italic texts Ref: [#136](https://github.com/naokazuterada/MarkdownTOC/issues/136)

tests/italic_in_heading.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# coding:utf-8
2+
from base import TestBase
3+
4+
class TestItalic(TestBase):
5+
"""Test for attributes 'bracket'"""
6+
7+
# for debug
8+
# def tearDown(self):
9+
# pass
10+
11+
# TODO: How can we remove "autolink=true" only in these tests below ?
12+
13+
bracket_text = \
14+
"""
15+
16+
<!-- MarkdownTOC autolink=true -->
17+
18+
<!-- /MarkdownTOC -->
19+
20+
# this is _italic_
21+
# _this is italic_
22+
# _this is not italic __
23+
# this _is italic_
24+
# _ this is not italic _
25+
# 2 _ this is not italic _
26+
# _this is not italic with markdown error _
27+
# 2 _this is not italic with markdown error _
28+
# `_should ignore underscores in codeblocks_`
29+
# `_should ignore underscores in codeblocks 2_ `
30+
# this is ` _more complex_ ` exmaple
31+
"""
32+
33+
def test_italic_in_inheading1(self):
34+
toc = self.init_update(self.bracket_text)['toc']
35+
self.assert_In('- [this is _italic_](#this-is-italic)', toc)
36+
37+
def test_italic_in_inheading2(self):
38+
toc = self.init_update(self.bracket_text)['toc']
39+
self.assert_In('- [_this is italic_](#this-is-italic-1)', toc)
40+
41+
def test_italic_in_inheading3(self):
42+
toc = self.init_update(self.bracket_text)['toc']
43+
self.assert_In('- [_this is not italic __](#_this-is-not-italic-__)', toc)
44+
45+
def test_italic_in_inheading4(self):
46+
toc = self.init_update(self.bracket_text)['toc']
47+
self.assert_In('- [this _is italic_](#this-is-italic-2)', toc)
48+
49+
def test_italic_in_inheading5(self):
50+
toc = self.init_update(self.bracket_text)['toc']
51+
self.assert_In('- [_ this is not italic _](#_-this-is-not-italic-_)', toc)
52+
53+
def test_italic_in_inheading6(self):
54+
toc = self.init_update(self.bracket_text)['toc']
55+
self.assert_In('- [2 _ this is not italic _](#2-_-this-is-not-italic-_)', toc)
56+
57+
def test_italic_in_inheading7(self):
58+
toc = self.init_update(self.bracket_text)['toc']
59+
self.assert_In('- [_this is not italic with markdown error _](#_this-is-not-italic-with-markdown-error-_)', toc)
60+
61+
def test_italic_in_inheading8(self):
62+
toc = self.init_update(self.bracket_text)['toc']
63+
self.assert_In('- [2 _this is not italic with markdown error _](#2-_this-is-not-italic-with-markdown-error-_)', toc)
64+
65+
def test_italic_in_inheading9(self):
66+
toc = self.init_update(self.bracket_text)['toc']
67+
self.assert_In('- [`_should ignore underscores in codeblocks_`](#_should-ignore-underscores-in-codeblocks_)', toc)
68+
69+
def test_italic_in_inheading10(self):
70+
toc = self.init_update(self.bracket_text)['toc']
71+
self.assert_In('- [`_should ignore underscores in codeblocks 2_ `](#_should-ignore-underscores-in-codeblocks-2_-)', toc)
72+
73+
def test_italic_in_inheading11(self):
74+
toc = self.init_update(self.bracket_text)['toc']
75+
self.assert_In('- [this is ` _more complex_ ` exmaple](#this-is-_more-complex_-exmaple)', toc)

0 commit comments

Comments
 (0)