Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.simiacryptus.skyenet.util
import com.simiacryptus.skyenet.AgentPatterns.displayMapInTabs
import com.simiacryptus.skyenet.webui.application.ApplicationInterface
import com.vladsch.flexmark.ext.tables.TablesExtension
import com.vladsch.flexmark.html.HtmlRenderer
import com.vladsch.flexmark.parser.Parser
import com.vladsch.flexmark.util.data.MutableDataSet
import org.apache.commons.text.StringEscapeUtils
import java.nio.file.Files
import java.util.*
object MarkdownUtil {
fun renderMarkdown(
markdown: String,
options: MutableDataSet = defaultOptions(),
tabs: Boolean = true,
ui: ApplicationInterface? = null,
): String {
if (markdown.isBlank()) return ""
val parser = Parser.builder(options).build()
val renderer = HtmlRenderer.builder(options).build()
val document = parser.parse(markdown)
val html = renderer.render(document)
val mermaidRegex =
Regex("
]*>(.*?)
", RegexOption.DOT_MATCHES_ALL)
val matches = mermaidRegex.findAll(html)
var htmlContent = html
matches.forEach { match ->
var mermaidCode = match.groups[1]!!.value
// HTML Decode mermaidCode
val fixedMermaidCode = fixupMermaidCode(mermaidCode)
var mermaidDiagramHTML = """
$fixedMermaidCode
"""
try {
if(true){
val svg = renderMermaidToSVG(fixedMermaidCode)
if (null != ui) {
val newTask = ui.newTask(false)
newTask.complete(svg)
mermaidDiagramHTML = newTask.placeholder
} else {
mermaidDiagramHTML = svg
}
}
} catch (e: Exception) {
log.warn("Failed to render Mermaid diagram", e)
}
val replacement = if (tabs) """
|
|
|
|
|
|
$mermaidDiagramHTML
|
$fixedMermaidCode
|
|""".trimMargin() else """
|$mermaidDiagramHTML
|""".trimMargin()
htmlContent = htmlContent.replace(match.value, replacement)
}
//language=HTML
return if (tabs) {
displayMapInTabs(
mapOf(
"HTML" to htmlContent,
"Markdown" to """