From 424e16e8d27f8e7459089554f5036a07d7ea252e Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 9 Feb 2020 18:29:56 +0100 Subject: Book Marx: Properly format bookmarks. Change-Id: Idd6f2c4622d4b1a16c74b2d3af02e637625732fc --- .../eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java | 6 ++++ .../benki/bookmarks/MarkdownConverter.java | 41 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) (limited to 'src/main/java/eu') 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 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, "
\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); + } } -- cgit v1.2.3