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
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@
package org.testingisdocumenting.znai.extensions.footnote;

public record FootnoteId(String id) {
public int asNumber() {
return Integer.parseInt(id);
}

public boolean isNumber() {
return id != null && id.matches("\\d+");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public MarkupParserResult parse(Path path, String markdown) {
Node node = fullParser.parse(markdown);
MarkdownVisitor visitor = parsePartial(node, path, parserHandler);

if (!visitor.getUnresolvedFootnoteRefs().isEmpty()) {
throw new IllegalArgumentException("undefined footnote reference(s): " +
String.join(", ", visitor.getUnresolvedFootnoteRefs()));
}

if (visitor.hasPluginWarnings()) {
reportWarnings(path, visitor.getParameterWarnings());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,26 @@

import java.nio.file.Path;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MarkdownVisitor extends AbstractVisitor {
private static final Pattern FOOTNOTE_REFERENCE_PATTERN = Pattern.compile("\\[\\^([^]]+)]");

private final ComponentsRegistry componentsRegistry;
private final Path path;
private final ParserHandler parserHandler;
private boolean sectionStarted;

private final Set<PluginParamWarning> parameterWarnings;
private final Set<String> unresolvedFootnoteRefs;

public MarkdownVisitor(ComponentsRegistry componentsRegistry, Path path, ParserHandler parserHandler) {
this.componentsRegistry = componentsRegistry;
this.path = path;
this.parserHandler = parserHandler;
this.parameterWarnings = new LinkedHashSet<>();
this.unresolvedFootnoteRefs = new LinkedHashSet<>();
}

public boolean isSectionStarted() {
Expand All @@ -72,6 +78,10 @@ public Set<PluginParamWarning> getParameterWarnings() {
return parameterWarnings;
}

public Set<String> getUnresolvedFootnoteRefs() {
return unresolvedFootnoteRefs;
}

@Override
public void visit(Paragraph paragraph) {
parserHandler.onParagraphStart();
Expand All @@ -95,7 +105,13 @@ public void visit(StrongEmphasis strongEmphasis) {

@Override
public void visit(Text text) {
parserHandler.onSimpleText(text.getLiteral());
String literal = text.getLiteral();
Matcher matcher = FOOTNOTE_REFERENCE_PATTERN.matcher(literal);
while (matcher.find()) {
unresolvedFootnoteRefs.add(matcher.group(1));
}

parserHandler.onSimpleText(literal);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,7 @@ public void onTable(MarkupTableData tableData) {
public void onFootnoteDefinition(ParsedFootnote footnote) {
parsedFootnotes.put(footnote.id(), footnote);

int indexToUse = footnote.id().isNumber() ?
footnote.id().asNumber():
++footnoteAutoIdx;

int indexToUse = ++footnoteAutoIdx;
footnoteIdxById.put(footnote.id(), indexToUse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,15 +617,15 @@ after footnote
["type": "SoftLineBreak"],
["text": "numeric one ", "type": "SimpleText"],
[
"label": "5",
"label": "3",
"content": [["type": "Paragraph", "content": [["text": "footnote num5", "type": "SimpleText"]]]],
"type": "FootnoteReference"
],
["text": " what do we do with this one", "type": "SimpleText"],
["type": "SoftLineBreak"],
["text": "and what do we do with this ", "type": "SimpleText"],
[
"label": "8",
"label": "4",
"content": [["type": "Paragraph", "content": [["text": "footnote num8", "type": "SimpleText"]]]],
"type": "FootnoteReference"
]
Expand All @@ -637,6 +637,16 @@ after footnote
]
}

@Test
void "undefined footnote reference"() {
code {
parse("text with [^undefined] reference")
} should throwException(~/undefined footnote reference\(s\): undefined/)

parse("text with `[^undefined]` reference")
parse("```[^ref]```")
}

@Test
void "embedded html block"() {
parse("""
Expand Down
3 changes: 3 additions & 0 deletions znai-docs/znai/flow/footnotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ To add a reference to the footnote use `[^my-id]` which will result in [^my-id]
Constructor()
```

Note: numeric footnotes are treated as text footnotes, and they will be assigned the auto incremented
number in order of appearance.

# Preview

Hover mouse over a footnote reference see its content in a tooltip.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add: numeric Footnotes auto increment and ignore the given numeric value
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add: Undefined footnote references fail the build
Loading