-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow basic markup syntax custom previews (#6232)
Add MarkdownFormatter using https://github.com/vsch/flexmark-java/ to format markdown. To configure Markdown in custom previews add the "Markdown" formatter. Markdown is enabled by default for the comment field as requested in #6194
- Loading branch information
1 parent
50c2b41
commit d4e46b0
Showing
10 changed files
with
194 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/org/jabref/logic/layout/format/MarkdownFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.jabref.logic.layout.format; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.jabref.logic.layout.LayoutFormatter; | ||
|
||
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension; | ||
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension; | ||
import com.vladsch.flexmark.html.HtmlRenderer; | ||
import com.vladsch.flexmark.parser.Parser; | ||
import com.vladsch.flexmark.util.ast.Node; | ||
import com.vladsch.flexmark.util.data.MutableDataSet; | ||
|
||
public class MarkdownFormatter implements LayoutFormatter { | ||
|
||
private final Parser parser; | ||
private final HtmlRenderer renderer; | ||
|
||
public MarkdownFormatter() { | ||
MutableDataSet options = new MutableDataSet(); | ||
options.set(Parser.EXTENSIONS, List.of( | ||
StrikethroughExtension.create(), | ||
TaskListExtension.create() | ||
)); | ||
|
||
parser = Parser.builder(options).build(); | ||
renderer = HtmlRenderer.builder(options).build(); | ||
} | ||
|
||
@Override | ||
public String format(final String fieldText) { | ||
Objects.requireNonNull(fieldText, "Field Text should not be null, when handed to formatter"); | ||
|
||
Node document = parser.parse(fieldText); | ||
String html = renderer.render(document); | ||
|
||
// workaround HTMLChars transforming "\n" into <br> by returning a one liner | ||
return html.replaceAll("\\r\\n|\\r|\\n", " ").trim(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/test/java/org/jabref/logic/layout/format/MarkdownFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.jabref.logic.layout.format; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class MarkdownFormatterTest { | ||
|
||
private MarkdownFormatter markdownFormatter; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
markdownFormatter = new MarkdownFormatter(); | ||
} | ||
|
||
@Test | ||
void formatWhenFormattingPlainTextThenReturnsTextWrappedInParagraph() { | ||
assertEquals("<p>Hello World</p>", markdownFormatter.format("Hello World")); | ||
} | ||
|
||
@Test | ||
void formatWhenFormattingComplexMarkupThenReturnsOnlyOneLine() { | ||
assertFalse(markdownFormatter.format("Markup\n\n* list item one\n* list item 2\n\n rest").contains("\n")); | ||
} | ||
|
||
@Test | ||
void formatWhenFormattingEmptyStringThenReturnsEmptyString() { | ||
assertEquals("", markdownFormatter.format("")); | ||
} | ||
|
||
@Test | ||
void formatWhenFormattingNullThenThrowsException() { | ||
Exception exception = assertThrows(NullPointerException.class, () -> markdownFormatter.format(null)); | ||
assertEquals("Field Text should not be null, when handed to formatter", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void formatWhenMarkupContainingStrikethroughThenContainsMatchingDel() { | ||
// Only test strikethrough extension | ||
assertTrue(markdownFormatter.format("a ~~b~~ b").contains("<del>b</del>")); | ||
} | ||
|
||
@Test | ||
void formatWhenMarkupContainingTaskListThenContainsFormattedTaskList() { | ||
String actual = markdownFormatter.format("Some text\n" + | ||
"* [ ] open task\n" + | ||
"* [x] closed task\n\n" + | ||
"some other text"); | ||
// Only test list items | ||
assertTrue(actual.contains("<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"disabled\" readonly=\"readonly\" /> open task</li>")); | ||
assertTrue(actual.contains("<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"checked\" disabled=\"disabled\" readonly=\"readonly\" /> closed task</li>")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters