Skip to content

Instantly share code, notes, and snippets.

@eltimn
Last active December 15, 2015 16:59
Show Gist options
  • Save eltimn/5293540 to your computer and use it in GitHub Desktop.
Save eltimn/5293540 to your computer and use it in GitHub Desktop.
A lift Record Field for storing and converting Markdown. Uses Actuarius - https://github.com/chenkelmann/actuarius
package code.model
import scala.xml._
import net.liftweb.util._
import net.liftweb.common._
import net.liftweb.http.S
import net.liftweb.record._
import net.liftweb.record.field._
import S._
import Helpers._
// https://github.com/chenkelmann/actuarius
import eu.henkelmann.actuarius._
class NoVerbatimTransformer extends Transformer with Decorator {
override def deco = this
override def allowVerbatimXml: Boolean = false
}
trait MarkdownTypedField extends TextareaTypedField {
lazy val transformer = new NoVerbatimTransformer
override def asHtml: NodeSeq = valueBox
.flatMap { v => tryo(XML.loadString("<div>%s</div>".format(transformer(v)))) }
.openOr(<div>Error converting text to HTML</div>)
}
class MarkdownField[OwnerType <: Record[OwnerType]](rec: OwnerType, maxLength: Int)
extends StringField(rec, maxLength) with MarkdownTypedField
class OptionalMarkdownField[OwnerType <: Record[OwnerType]](rec: OwnerType, maxLength: Int)
extends OptionalStringField(rec, maxLength) with MarkdownTypedField
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment