forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
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
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 JabRef#6194
- Loading branch information
1 parent
64e35c1
commit 3cb1048
Showing
10 changed files
with
179 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
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
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() | ||
)); | ||
options.set(HtmlRenderer.NO_P_TAGS_USE_BR, true); | ||
|
||
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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
package org.jabref.logic.layout.format; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
class MarkdownFormatterTest { | ||
|
||
private MarkdownFormatter markdownFormatter; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
markdownFormatter = new MarkdownFormatter(); | ||
} | ||
|
||
@Test | ||
void formatWhenFormattingPlainTextThenReturnsTextWrappedInParagraph() { | ||
assertThat(markdownFormatter.format("Hello World")).isEqualTo("<p>Hello World</p>"); | ||
} | ||
|
||
@Test | ||
void formatWhenFormattingComplexMarkupThenReturnsOnlyOneLine() { | ||
String source = "Markup\n\n* list item one\n* list item 2\n\n rest"; | ||
assertThat(markdownFormatter.format(source)) | ||
.contains("Markup<br />") | ||
.contains("<li>list item one</li>") | ||
.contains("<li>list item 2</li>") | ||
.contains("> rest") | ||
.doesNotContain("\n"); | ||
} | ||
|
||
@Test | ||
void formatWhenFormattingEmptyStringThenReturnsEmptyString() { | ||
assertThat(markdownFormatter.format("")).isEqualTo(""); | ||
} | ||
|
||
@Test | ||
void formatWhenFormattingNullThenThrowsException() { | ||
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> markdownFormatter.format(null)) | ||
.withMessageContaining("Field Text should not be null, when handed to formatter") | ||
.withNoCause(); | ||
} | ||
|
||
@Test | ||
void formatWhenMarkupContainingStrikethroughThenContainsMatchingDel() { | ||
assertThat(markdownFormatter.format("a ~~b~~ b")).contains("<del>b</del>"); | ||
} | ||
|
||
@Test | ||
void formatWhenMarkupContainingTaskListThenContainsFormattedTaskList() { | ||
assertThat(markdownFormatter.format("Some text\n" + | ||
"* [ ] open task\n" + | ||
"* [x] closed task\n\n" + | ||
"some other text")) | ||
.contains("<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"disabled\" readonly=\"readonly\" /> open task</li>") | ||
.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