Skip to content

Commit a3778aa

Browse files
committed
fix toc levels parsing as per documentation
1 parent e19204a commit a3778aa

File tree

7 files changed

+383
-89
lines changed

7 files changed

+383
-89
lines changed

VERSION-TODO.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Release 0.60.0](#release-0600)
88
- [API Refactoring](#api-refactoring)
99
- [Next 0.61.xx](#next-061xx)
10+
- [0.61.28](#06128)
1011
- [0.61.26](#06126)
1112
- [0.61.24](#06124)
1213
- [0.61.22](#06122)
@@ -225,6 +226,30 @@ Please give feedback on the upcoming changes if you have concerns about breaking
225226
* [ ] Fix: Html converter to not add spaces between end of inline marker and next punctuation:
226227
`.,:;`
227228

229+
## 0.61.28
230+
231+
* Fix: Toc and SimToc `levels` parsing to properly handle levels as per documentation.
232+
* single numeric >2 is 2-level, ie. `levels=4` should be interpreted as `levels=2-4` but was
233+
`levels=4,4`.
234+
* `levels=x-` to be same as `levels=x-6`, was handled as `levels=x,x`
235+
236+
`levels=levelList` where level list is a comma separated list of levels or ranges. Default is
237+
to include heading levels 2 and 3. If a range is specified using `-` as separator between
238+
numeric values then the included range is from first numeric to last numeric inclusive. If
239+
first numeric is missing then 1 will be used in its place. If last numeric is missing then 6
240+
will be used in its place.
241+
242+
Examples:
243+
* `levels=1` include level 1 only, same as `levels=1,1`
244+
* `levels=2` include level 2 only, same as `levels=2,2`
245+
* `levels=3` include levels 2 and 3
246+
* `levels=4` include levels 2,3 and 4
247+
* `levels=2-4` include levels 2,3 and 4. same as `levels=4`
248+
* `levels=2-4,5` include levels 2,3,4 and 5
249+
* `levels=1,3` include levels 1 and 3
250+
* `levels=-3` include levels 1 to 3
251+
* `levels=3-` include levels 3 to 6
252+
228253
## 0.61.26
229254

230255
* Fix: `SequenceBuilder.toString()` not to add space between sequence parts.

flexmark-ext-toc/src/main/java/com/vladsch/flexmark/ext/toc/internal/TocLevelsOptionParser.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class TocLevelsOptionParser implements OptionParser<TocOptions> {
3737
TOC_LEVELS_MAP.put(1 << 5, "5,5");
3838
TOC_LEVELS_MAP.put(1 << 6, "6,6");
3939
}
40+
4041
public TocLevelsOptionParser(String optionName) {
4142
this.myOptionName = optionName;
4243
}
@@ -46,7 +47,6 @@ public String getOptionName() {
4647
return myOptionName;
4748
}
4849

49-
@SuppressWarnings("unchecked")
5050
@Override
5151
public Pair<TocOptions, List<ParsedOption<TocOptions>>> parseOption(BasedSequence optionText, TocOptions options, MessageProvider provider) {
5252
// may have levels
@@ -71,20 +71,27 @@ public Pair<TocOptions, List<ParsedOption<TocOptions>>> parseOption(BasedSequenc
7171
};
7272

7373
for (BasedSequence option : levelsOptionValue) {
74-
BasedSequence[] optionRange = option.split("-", 2, BasedSequence.SPLIT_TRIM_PARTS);
74+
BasedSequence[] optionRange = option.split("-", 2, BasedSequence.SPLIT_TRIM_PARTS | BasedSequence.SPLIT_INCLUDE_DELIM_PARTS);
7575

7676
Integer rangeStart;
7777
Integer rangeEnd;
7878
parserParams.skip = false;
7979

80-
if (optionRange.length == 2) {
80+
if (optionRange.length >= 2) {
8181
rangeStart = convertWithMessage.apply(optionRange[0]);
82-
rangeEnd = convertWithMessage.apply(optionRange[1]);
82+
rangeEnd = optionRange.length >= 3 ? convertWithMessage.apply(optionRange[2]) : null;
8383
if (rangeStart == null) rangeStart = 1;
8484
if (rangeEnd == null) rangeEnd = 6;
8585
} else {
86-
rangeStart = convertWithMessage.apply(optionRange[0]);
87-
rangeEnd = rangeStart;
86+
// NOTE: 1 means heading level 1 only, 2 means 2 only, rest mean 2-x
87+
Integer optionValue = convertWithMessage.apply(optionRange[0]);
88+
if (optionValue != null && optionValue <= 2) {
89+
rangeStart = optionValue;
90+
rangeEnd = rangeStart;
91+
} else {
92+
rangeStart = optionValue == null ? null : 2;
93+
rangeEnd = optionValue;
94+
}
8895
}
8996

9097
if (!parserParams.skip) {

flexmark-ext-toc/src/main/javadoc/overview.html

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,64 @@
44
</head>
55
<body>
66
<p><strong>flexmark-java table of contents extension</strong></p>
7-
<p>flexmark-java extension for converting <code>[TOC level=</code> <em><strong>#</strong></em> <code>]</code> to an HTML rendered table of contents</p>
8-
<p>where # is a digit specifying the highest level heading to use in the table of contents</p>
7+
<p>Converts <code>[TOC style]</code> text to TocBlock nodes and</p>
8+
<p>Or Sim TOC tag, which has the following format: <code>[TOC style]: # &quot;Title&quot;</code> and includes all
9+
following lines until a blank line. <!--SimTocContent--></p>
10+
<p>Lines after the TOC tag are added to the <code>SimTocContent</code> child node of the SimToc block.</p>
11+
<p>The intention for this tag is to have the <code>SimTocContent</code> updated to reflect the content of the
12+
document.</p>
13+
<ol>
14+
<li>
15+
<p><code>style</code> consists of space separated list of options:</p>
16+
<ul>
17+
<li>
18+
<p><code>levels=levelList</code> where level list is a comma separated list of levels or ranges. Default
19+
is to include heading levels 2 and 3. Examples:</p>
20+
<ul>
21+
<li><code>levels=4</code> include levels 2,3 and 4</li>
22+
<li><code>levels=2-4</code> include levels 2,3 and 4. same as <code>levels=4</code></li>
23+
<li><code>levels=2-4,5</code> include levels 2,3,4 and 5</li>
24+
<li><code>levels=1,3</code> include levels 1 and 3</li>
25+
<li><code>levels=-3</code> include levels 1 to 3</li>
26+
<li><code>levels=3-</code> include levels 3 to 6</li>
27+
</ul>
28+
</li>
29+
<li>
30+
<p><code>html</code> generate HTML version of the TOC</p>
31+
</li>
32+
<li>
33+
<p><code>markdown</code> generate Markdown version of the TOC</p>
34+
</li>
35+
<li>
36+
<p><code>text</code> to only include the text of the heading</p>
37+
</li>
38+
<li>
39+
<p><code>formatted</code> to include text and inline formatting</p>
40+
</li>
41+
<li>
42+
<p><code>hierarchy</code> to render as hierarchical list in order of appearance in the document</p>
43+
</li>
44+
<li>
45+
<p><code>flat</code> to render as a flat list in order of appearance in the document</p>
46+
</li>
47+
<li>
48+
<p><code>reversed</code> to render as a flat list in order of appearance in the document</p>
49+
</li>
50+
<li>
51+
<p><code>sort-up</code> to render as a flat list sorted alphabetically by heading text only, no inlines</p>
52+
</li>
53+
<li>
54+
<p><code>sort-down</code> to render as a flat list sorted reversed alphabetically by heading text only,
55+
no inlines</p>
56+
</li>
57+
<li>
58+
<p><code>bullet</code> to use a bullet list for the TOC items</p>
59+
</li>
60+
<li>
61+
<p><code>numbered</code> to use a numbered list for TOC items</p>
62+
</li>
63+
</ul>
64+
</li>
65+
</ol>
966
</body>
1067
</html>

flexmark-ext-toc/src/main/javadoc/overview.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
**flexmark-java table of contents extension**
22

3-
flexmark-java extension for converting `[TOC level=` ***#*** `]` to an HTML rendered table of contents
3+
Converts `[TOC style]` text to TocBlock nodes and
4+
5+
Or Sim TOC tag, which has the following format: `[TOC style]: # "Title"` and includes all
6+
following lines until a blank line. <!--SimTocContent-->
7+
8+
9+
Lines after the TOC tag are added to the `SimTocContent` child node of the SimToc block.
10+
11+
The intention for this tag is to have the `SimTocContent` updated to reflect the content of the
12+
document.
13+
14+
15+
16+
1. `style` consists of space separated list of options:
17+
18+
* `levels=levelList` where level list is a comma separated list of levels or ranges. Default
19+
is to include heading levels 2 and 3. Examples:
20+
* `levels=4` include levels 2,3 and 4
21+
* `levels=2-4` include levels 2,3 and 4. same as `levels=4`
22+
* `levels=2-4,5` include levels 2,3,4 and 5
23+
* `levels=1,3` include levels 1 and 3
24+
* `levels=-3` include levels 1 to 3
25+
* `levels=3-` include levels 3 to 6
26+
27+
* `html` generate HTML version of the TOC
28+
29+
* `markdown` generate Markdown version of the TOC
30+
31+
* `text` to only include the text of the heading
32+
33+
* `formatted` to include text and inline formatting
34+
35+
* `hierarchy` to render as hierarchical list in order of appearance in the document
36+
37+
* `flat` to render as a flat list in order of appearance in the document
38+
39+
* `reversed` to render as a flat list in order of appearance in the document
40+
41+
* `sort-up` to render as a flat list sorted alphabetically by heading text only, no inlines
42+
43+
* `sort-down` to render as a flat list sorted reversed alphabetically by heading text only,
44+
no inlines
45+
46+
* `bullet` to use a bullet list for the TOC items
47+
48+
* `numbered` to use a numbered list for TOC items
449

5-
where # is a digit specifying the highest level heading to use in the table of contents

flexmark-ext-toc/src/test/resources/ext_simtoc_ast_spec.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ document.
2121
1. `style` consists of space separated list of options:
2222

2323
* `levels=levelList` where level list is a comma separated list of levels or ranges. Default
24-
is to include heading levels 2 and 3. Examples:
24+
is to include heading levels 2 and 3. If a range is specified using `-` as separator
25+
between numeric values then the included range is from first numeric to last numeric
26+
inclusive. If first numeric is missing then 1 will be used in its place. If last numeric is
27+
missing then 6 will be used in its place.
28+
29+
Examples:
30+
* `levels=1` include level 1 only, same as `levels=1,1`
31+
* `levels=2` include level 2 only, same as `levels=2,2`
32+
* `levels=3` include levels 2 and 3
2533
* `levels=4` include levels 2,3 and 4
2634
* `levels=2-4` include levels 2,3 and 4. same as `levels=4`
2735
* `levels=2-4,5` include levels 2,3,4 and 5
2836
* `levels=1,3` include levels 1 and 3
37+
* `levels=-3` include levels 1 to 3
38+
* `levels=3-` include levels 3 to 6
2939

3040
* `html` generate HTML version of the TOC
3141

@@ -1198,13 +1208,21 @@ options, title with escaped chars
11981208
<div>
11991209
<h1>title&quot;s</h1>
12001210
<ul>
1201-
<li><a href="#header-3">Header 3</a></li>
1211+
<li><a href="#header-2">Header 2</a>
1212+
<ul>
1213+
<li><a href="#header-3">Header 3</a></li>
1214+
</ul>
1215+
</li>
12021216
</ul>
12031217
</div>
12041218
<div>
12051219
<h1>title's</h1>
12061220
<ul>
1207-
<li><a href="#header-3">Header 3</a></li>
1221+
<li><a href="#header-2">Header 2</a>
1222+
<ul>
1223+
<li><a href="#header-3">Header 3</a></li>
1224+
</ul>
1225+
</li>
12081226
</ul>
12091227
</div>
12101228
.
@@ -2009,7 +2027,11 @@ Document[0, 58]
20092027
<div md-pos="26-51">
20102028
<h1>titles</h1>
20112029
<ul>
2012-
<li><a href="#header-3">Header 3</a></li>
2030+
<li><a href="#header-2">Header 2</a>
2031+
<ul>
2032+
<li><a href="#header-3">Header 3</a></li>
2033+
</ul>
2034+
</li>
20132035
</ul>
20142036
</div>
20152037
.

flexmark-ext-toc/src/test/resources/ext_toc_ast_spec.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ Converts `[TOC style]` text to TocBlock nodes.
1515
1. `style` consists of space separated list of options:
1616

1717
* `levels=levelList` where level list is a comma separated list of levels or ranges. Default
18-
is to include heading levels 2 and 3. Examples:
18+
is to include heading levels 2 and 3. If a range is specified using `-` as separator
19+
between numeric values then the included range is from first numeric to last numeric
20+
inclusive. If first numeric is missing then 1 will be used in its place. If last numeric is
21+
missing then 6 will be used in its place.
22+
23+
Examples:
24+
* `levels=1` include level 1 only, same as `levels=1,1`
25+
* `levels=2` include level 2 only, same as `levels=2,2`
26+
* `levels=3` include levels 2 and 3
1927
* `levels=4` include levels 2,3 and 4
2028
* `levels=2-4` include levels 2,3 and 4. same as `levels=4`
2129
* `levels=2-4,5` include levels 2,3,4 and 5
2230
* `levels=1,3` include levels 1 and 3
31+
* `levels=-3` include levels 1 to 3
32+
* `levels=3-` include levels 3 to 6
2333

2434
* `html` generate HTML version of the TOC
2535

@@ -1250,7 +1260,11 @@ Document[0, 129]
12501260
<h3 id="header-3" md-pos="16-24">Header 3</h3>
12511261
<div md-pos="26-40">
12521262
<ul>
1253-
<li><a href="#header-3">Header 3</a></li>
1263+
<li><a href="#header-2">Header 2</a>
1264+
<ul>
1265+
<li><a href="#header-3">Header 3</a></li>
1266+
</ul>
1267+
</li>
12541268
</ul>
12551269
</div>
12561270
.
@@ -1303,16 +1317,26 @@ Document[0, 40]
13031317
<h4 id="test-1211">test 1.2.1.1</h4>
13041318
<h4 id="test-1212">test 1.2.1.2</h4>
13051319
<ul>
1320+
<li><a href="#test-001">test 0.0.1</a></li>
1321+
<li><a href="#test-002">test 0.0.2</a></li>
13061322
<li><a href="#test-11">test 1.1</a>
13071323
<ul>
1308-
<li><a href="#test-1111">test 1.1.1.1</a></li>
1309-
<li><a href="#test-1112">test 1.1.1.2</a></li>
1324+
<li><a href="#test-111">test 1.1.1</a>
1325+
<ul>
1326+
<li><a href="#test-1111">test 1.1.1.1</a></li>
1327+
<li><a href="#test-1112">test 1.1.1.2</a></li>
1328+
</ul>
1329+
</li>
13101330
</ul>
13111331
</li>
13121332
<li><a href="#test-12">test 1.2</a>
13131333
<ul>
1314-
<li><a href="#test-1211">test 1.2.1.1</a></li>
1315-
<li><a href="#test-1212">test 1.2.1.2</a></li>
1334+
<li><a href="#test-121">test 1.2.1</a>
1335+
<ul>
1336+
<li><a href="#test-1211">test 1.2.1.1</a></li>
1337+
<li><a href="#test-1212">test 1.2.1.2</a></li>
1338+
</ul>
1339+
</li>
13161340
</ul>
13171341
</li>
13181342
</ul>

0 commit comments

Comments
 (0)