diff options
Diffstat (limited to 'src/main/java/eu')
-rw-r--r-- | src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java | 6 | ||||
-rw-r--r-- | src/main/java/eu/mulk/mulkcms2/benki/bookmarks/MarkdownConverter.java | 41 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java index 8985948..6bbd3a6 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java @@ -9,6 +9,7 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.Table; +import javax.persistence.Transient; @Entity @Table(name = "bookmarks", schema = "benki") @@ -30,4 +31,9 @@ public class Bookmark extends Post { joinColumns = @JoinColumn(name = "bookmark")) @Column(name = "tag") public Set<String> tags; + + @Transient + public String getDescriptionHtml() { + return new MarkdownConverter().htmlify(description); + } } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/MarkdownConverter.java b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/MarkdownConverter.java index 0a09b40..1fae62c 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/MarkdownConverter.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/MarkdownConverter.java @@ -1,5 +1,46 @@ package eu.mulk.mulkcms2.benki.bookmarks; +import com.vladsch.flexmark.ext.abbreviation.AbbreviationExtension; +import com.vladsch.flexmark.ext.autolink.AutolinkExtension; +import com.vladsch.flexmark.ext.definition.DefinitionExtension; +import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension; +import com.vladsch.flexmark.ext.tables.TablesExtension; +import com.vladsch.flexmark.ext.typographic.TypographicExtension; +import com.vladsch.flexmark.html.HtmlRenderer; +import com.vladsch.flexmark.parser.Parser; +import com.vladsch.flexmark.util.data.MutableDataSet; +import java.util.Arrays; +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped public class MarkdownConverter { + private final Parser parser; + private final HtmlRenderer renderer; + + MarkdownConverter() { + var options = new MutableDataSet(); + options.set( + Parser.EXTENSIONS, + Arrays.asList( + TablesExtension.create(), + StrikethroughExtension.create(), + AbbreviationExtension.create(), + AutolinkExtension.create(), + DefinitionExtension.create(), + TypographicExtension.create())); + options.set(Parser.FENCED_CODE_BLOCK_PARSER, true); + options.set(HtmlRenderer.SOFT_BREAK, "\n"); + options.set(HtmlRenderer.HARD_BREAK, "<br />\n"); + options.set(TypographicExtension.ENABLE_SMARTS, true); + options.set(TypographicExtension.ENABLE_QUOTES, true); + + this.parser = Parser.builder(options).build(); + this.renderer = HtmlRenderer.builder(options).build(); + } + + public String htmlify(String markdown) { + var parsedDocument = parser.parse(markdown); + return renderer.render(parsedDocument); + } } |