Embedding markdown in play templates Subscribe Pub Share

Did you ever feel like forgetting all about html markup and using markdown instead, inside a play template?

@md {
  It is really simple to **include markdown text** in a *play template*, using the [common-mark](https://github.com/atlassian/commonmark-java) parser:
- it's simple
- it's fast
}

It's pretty simple, here's a one-file solution, using the awesome common-mark parser. You'll need first of all a play template called md.scala.html :

@(f: =>Html)

@Html(
 org.commonmark.renderer.html.HtmlRenderer.builder().build().render(
    org.commonmark.parser.Parser.builder().build().parse(f.toString))
  )

And then, in your build.sbt or Build.sbt, you'll have to include the common mark parser, the fastest and bestest:

    "com.atlassian.commonmark"   % "commonmark"  % "0.7.0",

... and that's all, folks.

Of course, perhaps a better solution is to have all your templates be markdown, so of the form mytemplate.scala.markdown but for the most part, I find that plain html with embedded markdown text works well.

p.s. This entire website is markdown.


Was this useful?    

By: Razie | 2016-10-04 | Tags: post , play , playframework


See more in: Cool Scala Subscribe

Viewed 1334 times ( | History | Print ) this page.

You need to log in to post a comment!