Skip to content
Snippets Groups Projects
Commit edfc3d02 authored by Robin Stocker's avatar Robin Stocker
Browse files

Some more comments

parent 37e507ab
Branches
Tags
No related merge requests found
...@@ -51,26 +51,26 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen ...@@ -51,26 +51,26 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen
@Override @Override
public Set<Class<? extends Node>> getNodeTypes() { public Set<Class<? extends Node>> getNodeTypes() {
return new HashSet<>(Arrays.asList( return new HashSet<>(Arrays.asList(
Document.class,
Heading.class,
Paragraph.class,
BlockQuote.class, BlockQuote.class,
BulletList.class, BulletList.class,
Code.class,
Document.class,
Emphasis.class,
FencedCodeBlock.class, FencedCodeBlock.class,
HardLineBreak.class,
Heading.class,
HtmlBlock.class, HtmlBlock.class,
ThematicBreak.class, HtmlInline.class,
Image.class,
IndentedCodeBlock.class, IndentedCodeBlock.class,
Link.class, Link.class,
ListItem.class, ListItem.class,
OrderedList.class, OrderedList.class,
Image.class, Paragraph.class,
Emphasis.class, SoftLineBreak.class,
StrongEmphasis.class, StrongEmphasis.class,
Text.class, Text.class,
Code.class, ThematicBreak.class
HtmlInline.class,
SoftLineBreak.class,
HardLineBreak.class
)); ));
} }
...@@ -325,6 +325,14 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen ...@@ -325,6 +325,14 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen
@Override @Override
public void visit(Text text) { public void visit(Text text) {
// Text is tricky. In Markdown special characters (`-`, `#` etc.) can be escaped (`\-`, `\#` etc.) so that
// they're parsed as plain text. Currently, whether a character was escaped or not is not recorded in the Node,
// so here we don't know. If we just wrote out those characters unescaped, the resulting Markdown would change
// meaning (turn into a list item, heading, etc.).
// You might say "Why not store that in the Node when parsing", but that wouldn't work for the use case where
// nodes are constructed directly instead of via parsing. This renderer needs to work for that too.
// So currently, when in doubt, we escape. For special characters only occurring at the beginning of a line,
// we only escape them then (we wouldn't want to escape every `.` for example).
String literal = text.getLiteral(); String literal = text.getLiteral();
if (writer.isAtLineStart() && !literal.isEmpty()) { if (writer.isAtLineStart() && !literal.isEmpty()) {
char c = literal.charAt(0); char c = literal.charAt(0);
...@@ -382,6 +390,7 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen ...@@ -382,6 +390,7 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen
CharMatcher escape = text.getParent() instanceof Heading ? textEscapeInHeading : textEscape; CharMatcher escape = text.getParent() instanceof Heading ? textEscapeInHeading : textEscape;
if (literal.endsWith("!") && text.getNext() instanceof Link) { if (literal.endsWith("!") && text.getNext() instanceof Link) {
// If we wrote the `!` unescaped, it would turn the link into an image instead.
writer.writeEscaped(literal.substring(0, literal.length() - 1), escape); writer.writeEscaped(literal.substring(0, literal.length() - 1), escape);
writer.write("\\!"); writer.write("\\!");
} else { } else {
...@@ -494,6 +503,9 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen ...@@ -494,6 +503,9 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen
} }
} }
/**
* Visits nodes to check if there are any soft or hard line breaks.
*/
private static class LineBreakVisitor extends AbstractVisitor { private static class LineBreakVisitor extends AbstractVisitor {
private boolean lineBreak = false; private boolean lineBreak = false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment