Skip to content

Commit 2b4f9f1

Browse files
committed
Merge branch 'features/discrete-headings'
* features/discrete-headings: (27 commits) Rewrite according to suggestions Remove requirements.txt as it's not related to the feature itself Cleanup: Removing extra white space Delete wrong line Fix sentence Add message for 4.1.0 Update TOC Add 'Excluded heading' into README Fix unit test Fix unit tests pip install black pip install pycodestyle Fix Python version Suppress appveyor error? rename discrete to exclude, fix unittests unit tests fixed quote alignment quotes rename: discrete → exclude Cleanup ...
2 parents b20063f + 47ee29c commit 2b4f9f1

File tree

7 files changed

+100
-7
lines changed

7 files changed

+100
-7
lines changed

.python-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.7.1

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Sublime Text 3 plugin for generating a Table of Contents (TOC) in a Markdown doc
2828
- [Supported file extensions](#supported-file-extensions)
2929
- [Customizing generation of TOC using attributes](#customizing-generation-of-toc-using-attributes)
3030
- [Auto anchoring when heading has anchor defined](#auto-anchoring-when-heading-has-anchor-defined)
31-
- [Auto linking for _clickable_ TOC](#auto-linking-for-clickable-toc)
31+
- [Auto linking for _clickable_ TOC](#auto-linking-for-_clickable_-toc)
3232
- [Lowercasing in ids](#lowercasing-in-ids)
3333
- [Preserve case](#preserve-case)
3434
- [Lowercase all characters](#lowercase-all-characters)
@@ -41,10 +41,12 @@ Sublime Text 3 plugin for generating a Table of Contents (TOC) in a Markdown doc
4141
- [Customizable list bullets in TOC](#customizable-list-bullets-in-toc)
4242
- [Specify custom indentation prefix](#specify-custom-indentation-prefix)
4343
- [Preserve images in headings](#preserve-images-in-headings)
44+
- [Excluded headings](#excluded-headings)
4445
- [Usage](#usage)
4546
- [Tips](#tips)
4647
- [How to remove anchors added by MarkdownTOC](#how-to-remove-anchors-added-by-markdowntoc)
4748
- [Addressing issues with Github Pages](#addressing-issues-with-github-pages)
49+
- [Using MarkdownTOC with Markdownlint](#using-markdowntoc-with-markdownlint)
4850
- [Limitations](#limitations)
4951
- [Headings in lists are not included in the auto-generated table of contents](#headings-in-lists-are-not-included-in-the-auto-generated-table-of-contents)
5052
- [Attributes](#attributes)
@@ -98,6 +100,7 @@ The **MarkdownTOC** plugin is rich on features and customization, useful for bot
98100
- [Customizable list bullets in TOC](#customizable-list-bullets-in-toc)
99101
- [Specify custom indentation prefix](#specify-custom-indentation-prefix)
100102
- [Preserve images in headings](#preserve-images-in-headings)
103+
- [Excluded headings](#excluded-heading)
101104

102105
### Insertion of TOC based on headings in Markdown document
103106

@@ -722,6 +725,15 @@ Please note that the default for the [attribute](#attributes) is: `false`.
722725

723726
You can change your default setting in your [configuration](#configuration) with the key `defaults.remove_image`.
724727

728+
### Excluded headings
729+
730+
You can exclude certain headings in the TOC by adding a special comment to the line above the line with the heading, as shown below.
731+
732+
```markdown
733+
<!-- MarkdownTOC:excluded -->
734+
## This heading will be excluded
735+
```
736+
725737
## Usage
726738

727739
1. Open your [Markdown] file

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test_script:
1616
# run tests with test coverage report
1717
- ps: .\appveyor.ps1 "run_tests" -coverage -verbose
1818

19-
after_test:
19+
on_finish:
2020
- "SET PYTHON=C:\\Python33"
2121
- "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
2222
- pip install codecov

markdowntoc/markdowntoc_insert.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
PT_ANCHOR = re.compile(r'<a\s+id="[^"]+"\s*>\s*</a>')
2828

2929

30+
31+
# <!-- MarkdownTOC:excluded -->
32+
PT_EXCLUDE = re.compile(
33+
r'^<!--.*(MarkdownTOC:excluded).*-->', re.IGNORECASE
34+
)
35+
36+
3037
class MarkdowntocInsert(sublime_plugin.TextCommand, Base):
3138

3239
def run(self, edit):
@@ -138,6 +145,7 @@ def replace_brackets(m):
138145
return _open + m.group(1) + _close
139146
else:
140147
return m.group(0)
148+
141149
return re.sub(_pattern, replace_brackets, _text)
142150

143151
_text = do_escape(_text, re.compile(
@@ -165,10 +173,15 @@ def get_toc(self, attrs, begin, edit):
165173
for heading in headings:
166174
if begin < heading.end():
167175
lines = self.view.lines(heading)
176+
previous_line = self.view.substr(
177+
self.view.line(lines[0].a - 1))
178+
if PT_EXCLUDE.match(previous_line):
179+
continue
180+
168181
if len(lines) == 1:
169182
# handle hash headings, ### chapter 1
170-
r = sublime.Region(
171-
heading.end() - 1, self.view.line(heading).end())
183+
r = sublime.Region(heading.end() - 1,
184+
self.view.line(heading).end())
172185
text = self.view.substr(r).strip().rstrip('#')
173186
indent = heading.size() - 1
174187
items.append([indent, text, heading.begin()])
@@ -180,8 +193,8 @@ def get_toc(self, attrs, begin, edit):
180193
# ----
181194
text = self.view.substr(lines[0])
182195
if text.strip():
183-
indent = 1 if (
184-
self.view.substr(lines[1])[0] == '=') else 2
196+
heading_type = self.view.substr(lines[1])[0]
197+
indent = 1 if heading_type == '=' else 2
185198
items.append([indent, text, heading.begin()])
186199

187200
if len(items) < 1:

messages/4.1.0.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# MarkdownTOC - 4.1.0
2+
3+
## Changes
4+
5+
- Add Excluded heading feature Ref: [#140](https://github.com/naokazuterada/MarkdownTOC/issues/140), [#149](https://github.com/naokazuterada/MarkdownTOC/pull/149)
6+
- Update "Coding Style" in CONTRIBUTING.md

tests/exclude_heading.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# coding:utf-8
2+
from base import TestBase
3+
4+
class TestExcludeHeading(TestBase):
5+
'''Ignore exclude heading'''
6+
7+
# for debug
8+
# def tearDown(self):
9+
# pass
10+
11+
text_1 = '''
12+
13+
<!-- MarkdownTOC -->
14+
15+
<!-- /MarkdownTOC -->
16+
17+
# heading 1
18+
19+
<!-- MarkdownTOC:excluded -->
20+
# heading 2
21+
22+
# heading 3
23+
24+
'''
25+
26+
def test_exclude_heading(self):
27+
'''Default is no limit'''
28+
toc = self.init_update(self.text_1)['toc']
29+
self.assert_In('- heading 1', toc)
30+
self.assert_NotIn('- heading 2', toc)
31+
self.assert_In('- heading 3', toc)
32+
33+
text_2 = '''
34+
35+
<!-- MarkdownTOC -->
36+
37+
<!-- /MarkdownTOC -->
38+
39+
# level 1
40+
<!-- MarkdownTOC:excluded -->
41+
## level 2
42+
### level 3
43+
<!-- MarkdownTOC:excluded -->
44+
#### level 4
45+
<!-- MarkdownTOC:excluded -->
46+
##### level 5
47+
###### level 6
48+
49+
'''
50+
51+
def test_exclude_heading_level(self):
52+
'''Existence and level is correct'''
53+
toc = self.init_update(self.text_2)['toc']
54+
# existence
55+
self.assert_NotIn('- level 2', toc)
56+
self.assert_NotIn('- level 4', toc)
57+
self.assert_NotIn('- level 5', toc)
58+
# level
59+
self.assert_In('''- level 1
60+
- level 3
61+
- level 6''', toc)

tests/levels.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,4 @@ def test_levels_specific_levels(self):
125125
self.assert_NotIn('- heading 1', toc)
126126
self.assert_In('- heading 2', toc)
127127
self.assert_NotIn('- heading 5', toc)
128-
self.assert_In('- heading 6', toc)
128+
self.assert_In('- heading 6', toc)

0 commit comments

Comments
 (0)