Skip to content

Commit

Permalink
Documents: Use proper title and description in DTO
Browse files Browse the repository at this point in the history
Signed-off-by: Till Kottmann <me@deletescape.ch>
  • Loading branch information
nyancrimew committed Dec 5, 2019
1 parent 566e6b4 commit 2ab351c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
23 changes: 18 additions & 5 deletions app/src/dog/del/app/dto/DocumentDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,24 @@ open class FrontendDocumentDto : KoinComponent, PebbleModel {
val statsUrl: String? get() = reporter.getUrl(slug)
val showCount: Boolean get() = reporter.showCount
val lines get() = content?.lineCount ?: 0
open val description get() = docContent?.take(100) ?: "The sexiest pastebin and url-shortener ever"
open val title = "dogbin - $slug"
var description = "The sexiest pastebin and url-shortener ever"
protected set
var title = "dogbin"
protected set
open val editing = false
protected var docContent: String? = null

open suspend fun applyFrom(document: XdDocument, call: ApplicationCall? = null): FrontendDocumentDto =
coroutineScope {
slug = store.transactional(readonly = true) { document.slug }
title = "dogbin - $slug"
if (reporter.showCount) {
viewCountDeferred = async { reporter.getImpressions(slug) }
}
store.transactional(readonly = true) {
type = DocumentTypeDto.fromXdDocumentType(document.type)
docContent = document.stringContent
description = docContent?.take(100) ?: description
content = docContent
owner = UserDto.fromUser(document.owner)
created = document.created.formatShort(call?.locale)
Expand Down Expand Up @@ -114,7 +118,10 @@ class MarkdownDocumentDto : FrontendDocumentDto() {
super.applyFrom(document, call)

if (docContent != null) {
content = mdRenderer.render(docContent!!)
val mdContent = mdRenderer.render(docContent!!)
title = mdContent.title ?: title
description = mdContent.description ?: description
content = mdContent.content
}
return this
}
Expand Down Expand Up @@ -160,13 +167,19 @@ class HighlightedDocumentDto(val redirectToFull: Boolean = true) : FrontendDocum
}

class EditDocumentDto : FrontendDocumentDto() {
override val title: String
get() = "Editing - ${super.title}"
override var redirectTo: String?
get() = if (!editable) viewUrl else null
set(value) {}
override val editing = true

override suspend fun applyFrom(document: XdDocument, call: ApplicationCall?): FrontendDocumentDto {
super.applyFrom(document, call)

title = "Editing - $slug"

return this
}

override suspend fun toModel(): Map<String, Any> {
return super.toModel() + mapOf(
"initialValue" to (content ?: ""),
Expand Down
25 changes: 22 additions & 3 deletions app/src/dog/del/app/markdown/MarkdownRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@ import com.vladsch.flexmark.ext.autolink.AutolinkExtension
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension
import com.vladsch.flexmark.ext.tables.TablesExtension
import com.vladsch.flexmark.ext.yaml.front.matter.AbstractYamlFrontMatterVisitor
import com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterExtension
import com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterNode
import com.vladsch.flexmark.html.HtmlRenderer
import com.vladsch.flexmark.parser.Parser
import dog.del.app.markdown.iframely.IframelyExtensions
import dog.del.app.markdown.utils.mutableDataSetOf
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope

// TODO: cache rendered markdown
class MarkdownRenderer {
private val parser = Parser.builder(options).build()
private val renderer = HtmlRenderer.builder(options).build()

fun render(markdown: String): String {
suspend fun render(markdown: String): MarkdownRenderResult = coroutineScope {
val document = parser.parse(markdown)
// TODO: extract front matter
return renderer.render(document)
val renderDeferred = async { renderer.render(document) }
val visitor = AbstractYamlFrontMatterVisitor()
visitor.visit(document)
val frontMatter = visitor.data
MarkdownRenderResult(
title = frontMatter["title"]?.get(0),
description = frontMatter["description"]?.get(0),
coverImage = frontMatter["coverImage"]?.get(0),
content = renderDeferred.await()
)
}

companion object {
Expand All @@ -45,4 +57,11 @@ class MarkdownRenderer {
HtmlRenderer.GENERATE_HEADER_ID to true
).toImmutable()
}

data class MarkdownRenderResult(
val title: String?,
val description: String?,
val coverImage: String?,
val content: String
)
}

0 comments on commit 2ab351c

Please sign in to comment.