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 f04d9f1
Showing
10 changed files
with
165 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
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
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); | ||
return html.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
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
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 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