Skip to content

Commit 469614d

Browse files
footnotes: numeric footnotes auto increment and validation (#1495)
1 parent 7ab7c71 commit 469614d

8 files changed

Lines changed: 40 additions & 14 deletions

File tree

znai-core/src/main/java/org/testingisdocumenting/znai/extensions/footnote/FootnoteId.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,4 @@
1717
package org.testingisdocumenting.znai.extensions.footnote;
1818

1919
public record FootnoteId(String id) {
20-
public int asNumber() {
21-
return Integer.parseInt(id);
22-
}
23-
24-
public boolean isNumber() {
25-
return id != null && id.matches("\\d+");
26-
}
2720
}

znai-core/src/main/java/org/testingisdocumenting/znai/parser/commonmark/MarkdownParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public MarkupParserResult parse(Path path, String markdown) {
6868
Node node = fullParser.parse(markdown);
6969
MarkdownVisitor visitor = parsePartial(node, path, parserHandler);
7070

71+
if (!visitor.getUnresolvedFootnoteRefs().isEmpty()) {
72+
throw new IllegalArgumentException("undefined footnote reference(s): " +
73+
String.join(", ", visitor.getUnresolvedFootnoteRefs()));
74+
}
75+
7176
if (visitor.hasPluginWarnings()) {
7277
reportWarnings(path, visitor.getParameterWarnings());
7378
}

znai-core/src/main/java/org/testingisdocumenting/znai/parser/commonmark/MarkdownVisitor.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,26 @@
4444

4545
import java.nio.file.Path;
4646
import java.util.*;
47+
import java.util.regex.Matcher;
48+
import java.util.regex.Pattern;
4749

4850
public class MarkdownVisitor extends AbstractVisitor {
51+
private static final Pattern FOOTNOTE_REFERENCE_PATTERN = Pattern.compile("\\[\\^([^]]+)]");
52+
4953
private final ComponentsRegistry componentsRegistry;
5054
private final Path path;
5155
private final ParserHandler parserHandler;
5256
private boolean sectionStarted;
5357

5458
private final Set<PluginParamWarning> parameterWarnings;
59+
private final Set<String> unresolvedFootnoteRefs;
5560

5661
public MarkdownVisitor(ComponentsRegistry componentsRegistry, Path path, ParserHandler parserHandler) {
5762
this.componentsRegistry = componentsRegistry;
5863
this.path = path;
5964
this.parserHandler = parserHandler;
6065
this.parameterWarnings = new LinkedHashSet<>();
66+
this.unresolvedFootnoteRefs = new LinkedHashSet<>();
6167
}
6268

6369
public boolean isSectionStarted() {
@@ -72,6 +78,10 @@ public Set<PluginParamWarning> getParameterWarnings() {
7278
return parameterWarnings;
7379
}
7480

81+
public Set<String> getUnresolvedFootnoteRefs() {
82+
return unresolvedFootnoteRefs;
83+
}
84+
7585
@Override
7686
public void visit(Paragraph paragraph) {
7787
parserHandler.onParagraphStart();
@@ -95,7 +105,13 @@ public void visit(StrongEmphasis strongEmphasis) {
95105

96106
@Override
97107
public void visit(Text text) {
98-
parserHandler.onSimpleText(text.getLiteral());
108+
String literal = text.getLiteral();
109+
Matcher matcher = FOOTNOTE_REFERENCE_PATTERN.matcher(literal);
110+
while (matcher.find()) {
111+
unresolvedFootnoteRefs.add(matcher.group(1));
112+
}
113+
114+
parserHandler.onSimpleText(literal);
99115
}
100116

101117
@Override

znai-core/src/main/java/org/testingisdocumenting/znai/parser/docelement/DocElementCreationParserHandler.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,7 @@ public void onTable(MarkupTableData tableData) {
225225
public void onFootnoteDefinition(ParsedFootnote footnote) {
226226
parsedFootnotes.put(footnote.id(), footnote);
227227

228-
int indexToUse = footnote.id().isNumber() ?
229-
footnote.id().asNumber():
230-
++footnoteAutoIdx;
231-
228+
int indexToUse = ++footnoteAutoIdx;
232229
footnoteIdxById.put(footnote.id(), indexToUse);
233230
}
234231

znai-core/src/test/groovy/org/testingisdocumenting/znai/parser/MarkdownParserTest.groovy

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,15 @@ after footnote
617617
["type": "SoftLineBreak"],
618618
["text": "numeric one ", "type": "SimpleText"],
619619
[
620-
"label": "5",
620+
"label": "3",
621621
"content": [["type": "Paragraph", "content": [["text": "footnote num5", "type": "SimpleText"]]]],
622622
"type": "FootnoteReference"
623623
],
624624
["text": " what do we do with this one", "type": "SimpleText"],
625625
["type": "SoftLineBreak"],
626626
["text": "and what do we do with this ", "type": "SimpleText"],
627627
[
628-
"label": "8",
628+
"label": "4",
629629
"content": [["type": "Paragraph", "content": [["text": "footnote num8", "type": "SimpleText"]]]],
630630
"type": "FootnoteReference"
631631
]
@@ -637,6 +637,16 @@ after footnote
637637
]
638638
}
639639

640+
@Test
641+
void "undefined footnote reference"() {
642+
code {
643+
parse("text with [^undefined] reference")
644+
} should throwException(~/undefined footnote reference\(s\): undefined/)
645+
646+
parse("text with `[^undefined]` reference")
647+
parse("```[^ref]```")
648+
}
649+
640650
@Test
641651
void "embedded html block"() {
642652
parse("""

znai-docs/znai/flow/footnotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ To add a reference to the footnote use `[^my-id]` which will result in [^my-id]
1818
Constructor()
1919
```
2020
21+
Note: numeric footnotes are treated as text footnotes, and they will be assigned the auto incremented
22+
number in order of appearance.
23+
2124
# Preview
2225
2326
Hover mouse over a footnote reference see its content in a tooltip.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: numeric Footnotes auto increment and ignore the given numeric value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: Undefined footnote references fail the build

0 commit comments

Comments
 (0)