Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Adjust GFM strikethrough parsing (fixes commonmark#267)
Browse files Browse the repository at this point in the history
We used to accept 2 or more tildes for strikethrough. But GitHub
actually accepts a single tilde as well, but rejects more than 2 tildes.
This brings our implementation more in line with GitHub's.
  • Loading branch information
robinst committed Oct 19, 2022
1 parent afb31c8 commit 4ad649f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ with the exception that 0.x versions can break between minor versions.

## [Unreleased]
### Fixed
- A single pipe (optional whitespace) now ends a table instead of crashing or
being treated as an empty row, for consistency with GitHub (#255).
- GitHub tables: A single pipe (optional whitespace) now ends a table
instead of crashing or being treated as an empty row, for consistency
with GitHub (#255).
- GitHub strikethrough: A single tilde now also works, and more than two
tildes are not accepted anymore. This brings us in line with what
GitHub actually does, which is a bit underspecified (#267)

## [0.19.0] - 2022-06-02
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,33 @@ public char getClosingCharacter() {

@Override
public int getMinLength() {
return 2;
return 1;
}

@Override
public int process(DelimiterRun openingRun, DelimiterRun closingRun) {
if (openingRun.length() >= 2 && closingRun.length() >= 2) {
// Use exactly two delimiters even if we have more, and don't care about internal openers/closers.
if (openingRun.length() == closingRun.length() && openingRun.length() <= 2) {
// GitHub only accepts either one or two delimiters, but not a mix or more than that.

Text opener = openingRun.getOpener();

// Wrap nodes between delimiters in strikethrough.
Node strikethrough = new Strikethrough();

SourceSpans sourceSpans = new SourceSpans();
sourceSpans.addAllFrom(openingRun.getOpeners(2));
sourceSpans.addAllFrom(openingRun.getOpeners(openingRun.length()));

for (Node node : Nodes.between(opener, closingRun.getCloser())) {
strikethrough.appendChild(node);
sourceSpans.addAll(node.getSourceSpans());
}

sourceSpans.addAllFrom(closingRun.getClosers(2));
sourceSpans.addAllFrom(closingRun.getClosers(closingRun.length()));
strikethrough.setSourceSpans(sourceSpans.getSourceSpans());

opener.insertAfter(strikethrough);

return 2;
return openingRun.length();
} else {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public class StrikethroughTest extends RenderingTestCase {
.extensions(EXTENSIONS).build();

@Test
public void oneTildeIsNotEnough() {
assertRendering("~foo~", "<p>~foo~</p>\n");
public void oneTildeIsEnough() {
assertRendering("~foo~", "<p><del>foo</del></p>\n");
}

@Test
public void twoTildesYay() {
public void twoTildesWorksToo() {
assertRendering("~~foo~~", "<p><del>foo</del></p>\n");
}

Expand All @@ -48,23 +48,22 @@ public void unmatched() {

@Test
public void threeInnerThree() {
assertRendering("a ~~~foo~~~", "<p>a ~<del>foo</del>~</p>\n");
assertRendering("a ~~~foo~~~", "<p>a ~~~foo~~~</p>\n");
}

@Test
public void twoInnerThree() {
assertRendering("~~foo~~~", "<p><del>foo</del>~</p>\n");
assertRendering("~~foo~~~", "<p>~~foo~~~</p>\n");
}

@Test
public void tildesInside() {
assertRendering("~~foo~bar~~", "<p><del>foo~bar</del></p>\n");
assertRendering("~~foo~~bar~~", "<p><del>foo</del>bar~~</p>\n");
assertRendering("~~foo~~~bar~~", "<p><del>foo</del>~bar~~</p>\n");
assertRendering("~~foo~~~~bar~~", "<p><del>foo</del><del>bar</del></p>\n");
assertRendering("~~foo~~~~~bar~~", "<p><del>foo</del>~<del>bar</del></p>\n");
assertRendering("~~foo~~~~~~bar~~", "<p><del>foo</del>~~<del>bar</del></p>\n");
assertRendering("~~foo~~~~~~~bar~~", "<p><del>foo</del>~~~<del>bar</del></p>\n");
assertRendering("~~foo~~~bar~~", "<p><del>foo~~~bar</del></p>\n");
assertRendering("~~foo~~~~bar~~", "<p><del>foo~~~~bar</del></p>\n");
assertRendering("~~foo~~~~~bar~~", "<p><del>foo~~~~~bar</del></p>\n");
assertRendering("~~foo~~~~~~bar~~", "<p><del>foo~~~~~~bar</del></p>\n");
}

@Test
Expand Down

0 comments on commit 4ad649f

Please sign in to comment.