diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2020-02-09 18:29:56 +0100 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2020-02-09 18:29:56 +0100 |
commit | 424e16e8d27f8e7459089554f5036a07d7ea252e (patch) | |
tree | e145b4f178cee555af15e8beb9178b72a289bd3b /src/main/java/eu | |
parent | 51abccb96af42153c1e01a0914d27a6f93638ed5 (diff) |
Book Marx: Properly format bookmarks.
Change-Id: Idd6f2c4622d4b1a16c74b2d3af02e637625732fc
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); + } } |