refactor: replace inline markdown with markdown package dependency
Remove 606 lines of hand-rolled markdown parsing from main.lux and the unused markdown.lux split file. Replace with `import markdown` using the new markdown package (path dependency at ../markdown). This fixes the heading-in-list rendering bug where `- ### Title` was showing literal `### ` text. Now renders as `<li><h3>Title</h3></li>`. Also adds strikethrough support (~~text~~). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
245
main.lux
245
main.lux
@@ -1,3 +1,5 @@
|
||||
import markdown
|
||||
|
||||
type SiteConfig =
|
||||
| SiteConfig(String, String, String, String, String, String, String)
|
||||
|
||||
@@ -13,9 +15,6 @@ type Page =
|
||||
type FMState =
|
||||
| FMState(Bool, Bool, String, String, String, String, String)
|
||||
|
||||
type BState =
|
||||
| BState(String, String, Bool, String, String, Bool, String, Bool, String, Bool)
|
||||
|
||||
type TagEntry =
|
||||
| TagEntry(String, String, String, String, String)
|
||||
|
||||
@@ -197,10 +196,6 @@ fn teSection(e: TagEntry): String =
|
||||
TagEntry(_, _, _, _, s) => s,
|
||||
}
|
||||
|
||||
fn escapeHtml(s: String): String = String.replace(String.replace(String.replace(s, "&", "&"), "<", "<"), ">", ">")
|
||||
|
||||
fn escapeHtmlCode(s: String): String = String.replace(String.replace(String.replace(s, "&", "&"), "<", "<"), ">", ">")
|
||||
|
||||
fn slugFromFilename(filename: String): String = if String.endsWith(filename, ".md") then String.substring(filename, 0, String.length(filename) - 3) else filename
|
||||
|
||||
fn formatDate(isoDate: String): String = {
|
||||
@@ -239,241 +234,7 @@ fn insertByDate(sorted: List<Page>, item: Page): List<Page> = {
|
||||
}
|
||||
}
|
||||
|
||||
fn findClosingFrom(text: String, i: Int, len: Int, delim: String, delimLen: Int): Option<Int> = if i + delimLen > len then None else if String.substring(text, i, i + delimLen) == delim then Some(i) else findClosingFrom(text, i + 1, len, delim, delimLen)
|
||||
|
||||
fn findClosing(text: String, start: Int, len: Int, delim: String): Option<Int> = findClosingFrom(text, start, len, delim, String.length(delim))
|
||||
|
||||
fn isLetterOrSlash(ch: String): Bool = ch == "/" || ch == "!" || ch == "a" || ch == "b" || ch == "c" || ch == "d" || ch == "e" || ch == "f" || ch == "g" || ch == "h" || ch == "i" || ch == "j" || ch == "k" || ch == "l" || ch == "m" || ch == "n" || ch == "o" || ch == "p" || ch == "q" || ch == "r" || ch == "s" || ch == "t" || ch == "u" || ch == "v" || ch == "w" || ch == "x" || ch == "y" || ch == "z" || ch == "A" || ch == "B" || ch == "C" || ch == "D" || ch == "E" || ch == "F" || ch == "G" || ch == "H" || ch == "I" || ch == "J" || ch == "K" || ch == "L" || ch == "M" || ch == "N" || ch == "O" || ch == "P" || ch == "Q" || ch == "R" || ch == "S" || ch == "T" || ch == "U" || ch == "V" || ch == "W" || ch == "X" || ch == "Y" || ch == "Z"
|
||||
|
||||
fn processInlineFrom(text: String, i: Int, len: Int, acc: String): String = {
|
||||
if i >= len then acc else {
|
||||
let ch = String.substring(text, i, i + 1)
|
||||
if ch == "*" then if i + 1 < len then if String.substring(text, i + 1, i + 2) == "*" then match findClosing(text, i + 2, len, "**") {
|
||||
Some(end) => processInlineFrom(text, end + 2, len, acc + "<strong>" + processInline(String.substring(text, i + 2, end)) + "</strong>"),
|
||||
None => processInlineFrom(text, i + 1, len, acc + ch),
|
||||
} else match findClosing(text, i + 1, len, "*") {
|
||||
Some(end) => processInlineFrom(text, end + 1, len, acc + "<em>" + processInline(String.substring(text, i + 1, end)) + "</em>"),
|
||||
None => processInlineFrom(text, i + 1, len, acc + ch),
|
||||
} else acc + ch else if ch == "_" then if i + 1 < len then match findClosing(text, i + 1, len, "_") {
|
||||
Some(end) => processInlineFrom(text, end + 1, len, acc + "<em>" + processInline(String.substring(text, i + 1, end)) + "</em>"),
|
||||
None => processInlineFrom(text, i + 1, len, acc + ch),
|
||||
} else acc + ch else if ch == "`" then match findClosing(text, i + 1, len, "`") {
|
||||
Some(end) => processInlineFrom(text, end + 1, len, acc + "<code>" + escapeHtml(String.substring(text, i + 1, end)) + "</code>"),
|
||||
None => processInlineFrom(text, i + 1, len, acc + ch),
|
||||
} else if ch == "!" then if i + 1 < len then if String.substring(text, i + 1, i + 2) == "[" then match findClosing(text, i + 2, len, "](") {
|
||||
Some(end) => match findClosing(text, end + 2, len, ")") {
|
||||
Some(urlEnd) => {
|
||||
let imgTag = acc + "<img src=\"" + String.substring(text, end + 2, urlEnd) + "\" alt=\"" + String.substring(text, i + 2, end) + "\">"
|
||||
processInlineFrom(text, urlEnd + 1, len, imgTag)
|
||||
},
|
||||
None => processInlineFrom(text, i + 1, len, acc + ch),
|
||||
},
|
||||
None => processInlineFrom(text, i + 1, len, acc + ch),
|
||||
} else processInlineFrom(text, i + 1, len, acc + ch) else acc + ch else if ch == "[" then match findClosing(text, i + 1, len, "](") {
|
||||
Some(end) => match findClosing(text, end + 2, len, ")") {
|
||||
Some(urlEnd) => {
|
||||
let linkTag = acc + "<a href=\"" + String.substring(text, end + 2, urlEnd) + "\">" + processInline(String.substring(text, i + 1, end)) + "</a>"
|
||||
processInlineFrom(text, urlEnd + 1, len, linkTag)
|
||||
},
|
||||
None => processInlineFrom(text, i + 1, len, acc + ch),
|
||||
},
|
||||
None => processInlineFrom(text, i + 1, len, acc + ch),
|
||||
} else if ch == "<" then if i + 1 < len then if isLetterOrSlash(String.substring(text, i + 1, i + 2)) then match findClosing(text, i + 1, len, ">") {
|
||||
Some(end) => processInlineFrom(text, end + 1, len, acc + String.substring(text, i, end + 1)),
|
||||
None => processInlineFrom(text, i + 1, len, acc + "<"),
|
||||
} else processInlineFrom(text, i + 1, len, acc + "<") else acc + "<" else if ch == ">" then processInlineFrom(text, i + 1, len, acc + ">") else processInlineFrom(text, i + 1, len, acc + ch)
|
||||
}
|
||||
}
|
||||
|
||||
fn processInline(text: String): String = processInlineFrom(text, 0, String.length(text), "")
|
||||
|
||||
fn bsHtml(s: BState): String =
|
||||
match s {
|
||||
BState(h, _, _, _, _, _, _, _, _, _) => h,
|
||||
}
|
||||
|
||||
fn bsPara(s: BState): String =
|
||||
match s {
|
||||
BState(_, p, _, _, _, _, _, _, _, _) => p,
|
||||
}
|
||||
|
||||
fn bsInCode(s: BState): Bool =
|
||||
match s {
|
||||
BState(_, _, c, _, _, _, _, _, _, _) => c,
|
||||
}
|
||||
|
||||
fn bsCodeLang(s: BState): String =
|
||||
match s {
|
||||
BState(_, _, _, l, _, _, _, _, _, _) => l,
|
||||
}
|
||||
|
||||
fn bsCodeLines(s: BState): String =
|
||||
match s {
|
||||
BState(_, _, _, _, cl, _, _, _, _, _) => cl,
|
||||
}
|
||||
|
||||
fn bsInBq(s: BState): Bool =
|
||||
match s {
|
||||
BState(_, _, _, _, _, bq, _, _, _, _) => bq,
|
||||
}
|
||||
|
||||
fn bsBqLines(s: BState): String =
|
||||
match s {
|
||||
BState(_, _, _, _, _, _, bl, _, _, _) => bl,
|
||||
}
|
||||
|
||||
fn bsInList(s: BState): Bool =
|
||||
match s {
|
||||
BState(_, _, _, _, _, _, _, il, _, _) => il,
|
||||
}
|
||||
|
||||
fn bsListItems(s: BState): String =
|
||||
match s {
|
||||
BState(_, _, _, _, _, _, _, _, li, _) => li,
|
||||
}
|
||||
|
||||
fn bsOrdered(s: BState): Bool =
|
||||
match s {
|
||||
BState(_, _, _, _, _, _, _, _, _, o) => o,
|
||||
}
|
||||
|
||||
fn flushPara(html: String, para: String): String =
|
||||
if para == "" then html else html + "<p>" + processInline(String.trim(para)) + "</p>
|
||||
"
|
||||
|
||||
fn flushBq(html: String, bqLines: String): String =
|
||||
if bqLines == "" then html else html + "<blockquote>
|
||||
" + convertMd(bqLines) + "</blockquote>
|
||||
"
|
||||
|
||||
fn flushList(html: String, listItems: String, ordered: Bool): String =
|
||||
if listItems == "" then html else {
|
||||
let tag = if ordered then "ol" else "ul"
|
||||
html + "<" + tag + ">
|
||||
" + listItems + "</" + tag + ">
|
||||
"
|
||||
}
|
||||
|
||||
fn isOrderedListItem(line: String): Bool = {
|
||||
let trimmed = String.trim(line)
|
||||
if String.length(trimmed) < 3 then false else {
|
||||
let first = String.substring(trimmed, 0, 1)
|
||||
let isDigit = first == "0" || first == "1" || first == "2" || first == "3" || first == "4" || first == "5" || first == "6" || first == "7" || first == "8" || first == "9"
|
||||
if isDigit then String.contains(trimmed, ". ") else false
|
||||
}
|
||||
}
|
||||
|
||||
fn processBlockLine(html: String, para: String, inList: Bool, listItems: String, ordered: Bool, line: String): BState = {
|
||||
let trimmed = String.trim(line)
|
||||
if trimmed == "" then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
BState(h3, "", false, "", "", false, "", false, "", false)
|
||||
} else if String.startsWith(trimmed, "#### ") then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
BState(h3 + "<h4>" + processInline(String.substring(trimmed, 5, String.length(trimmed))) + "</h4>
|
||||
", "", false, "", "", false, "", false, "", false)
|
||||
} else if String.startsWith(trimmed, "### ") then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
BState(h3 + "<h3>" + processInline(String.substring(trimmed, 4, String.length(trimmed))) + "</h3>
|
||||
", "", false, "", "", false, "", false, "", false)
|
||||
} else if String.startsWith(trimmed, "## ") then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
BState(h3 + "<h2>" + processInline(String.substring(trimmed, 3, String.length(trimmed))) + "</h2>
|
||||
", "", false, "", "", false, "", false, "", false)
|
||||
} else if String.startsWith(trimmed, "# ") then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
BState(h3 + "<h1>" + processInline(String.substring(trimmed, 2, String.length(trimmed))) + "</h1>
|
||||
", "", false, "", "", false, "", false, "", false)
|
||||
} else if trimmed == "---" || trimmed == "***" || trimmed == "___" then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
BState(h3 + "<hr>
|
||||
", "", false, "", "", false, "", false, "", false)
|
||||
} else if String.startsWith(trimmed, "<") then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
BState(h3 + trimmed + "
|
||||
", "", false, "", "", false, "", false, "", false)
|
||||
} else if String.startsWith(trimmed, "![") then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
BState(h3 + "<p>" + processInline(trimmed) + "</p>
|
||||
", "", false, "", "", false, "", false, "", false)
|
||||
} else if inList then {
|
||||
let h2 = flushList(html, listItems, ordered)
|
||||
BState(h2, para + trimmed + " ", false, "", "", false, "", false, "", false)
|
||||
} else BState(html, para + trimmed + " ", false, "", "", false, "", false, "", false)
|
||||
}
|
||||
|
||||
fn blockFoldLine(state: BState, line: String): BState = {
|
||||
let html = bsHtml(state)
|
||||
let para = bsPara(state)
|
||||
let inCode = bsInCode(state)
|
||||
let codeLang = bsCodeLang(state)
|
||||
let codeLines = bsCodeLines(state)
|
||||
let inBq = bsInBq(state)
|
||||
let bqLines = bsBqLines(state)
|
||||
let inList = bsInList(state)
|
||||
let listItems = bsListItems(state)
|
||||
let ordered = bsOrdered(state)
|
||||
if inCode then if String.startsWith(line, "```") then {
|
||||
let codeHtml = if codeLang == "" then "<pre><code>" + codeLines + "</code></pre>
|
||||
" else "<pre><code class=\"language-" + codeLang + "\">" + codeLines + "</code></pre>
|
||||
"
|
||||
BState(html + codeHtml, "", false, "", "", false, "", false, "", false)
|
||||
} else BState(html, para, true, codeLang, codeLines + escapeHtmlCode(line) + "
|
||||
", false, "", false, "", false) else if String.startsWith(line, "```") then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushBq(h2, bqLines)
|
||||
let h4 = flushList(h3, listItems, ordered)
|
||||
let lang = String.trim(String.substring(line, 3, String.length(line)))
|
||||
BState(h4, "", true, lang, "", false, "", false, "", false)
|
||||
} else if String.startsWith(line, "> ") then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
let bqContent = String.substring(line, 2, String.length(line))
|
||||
BState(h3, "", false, "", "", true, bqLines + bqContent + "
|
||||
", false, "", false)
|
||||
} else if String.trim(line) == ">" then {
|
||||
let h2 = flushPara(html, para)
|
||||
let h3 = flushList(h2, listItems, ordered)
|
||||
BState(h3, "", false, "", "", true, bqLines + "
|
||||
", false, "", false)
|
||||
} else if inBq then {
|
||||
let h2 = flushBq(html, bqLines)
|
||||
processBlockLine(h2, para, inList, listItems, ordered, line)
|
||||
} else if String.startsWith(line, "- ") then {
|
||||
let h2 = flushPara(html, para)
|
||||
let item = String.substring(line, 2, String.length(line))
|
||||
BState(h2, "", false, "", "", false, "", true, listItems + "<li>" + processInline(item) + "</li>
|
||||
", false)
|
||||
} else if isOrderedListItem(line) then {
|
||||
let h2 = flushPara(html, para)
|
||||
let dotIdx = match String.indexOf(line, ". ") {
|
||||
Some(idx) => idx,
|
||||
None => 0,
|
||||
}
|
||||
let item = String.substring(line, dotIdx + 2, String.length(line))
|
||||
BState(h2, "", false, "", "", false, "", true, listItems + "<li>" + processInline(item) + "</li>
|
||||
", true)
|
||||
} else processBlockLine(html, para, inList, listItems, ordered, line)
|
||||
}
|
||||
|
||||
fn parseBlocks(text: String): String = {
|
||||
let lines = String.lines(text)
|
||||
let init = BState("", "", false, "", "", false, "", false, "", false)
|
||||
let final = List.fold(lines, init, blockFoldLine)
|
||||
let h = flushPara(bsHtml(final), bsPara(final))
|
||||
let h2 = flushBq(h, bsBqLines(final))
|
||||
flushList(h2, bsListItems(final), bsOrdered(final))
|
||||
}
|
||||
|
||||
fn convertMd(markdown: String): String = parseBlocks(markdown)
|
||||
fn convertMd(text: String): String = markdown.toHtml(text)
|
||||
|
||||
fn htmlHead(title: String, description: String): String = "<!doctype html><html lang=\"en\"><head>" + "<title>" + title + "</title>" + "<meta charset=\"utf-8\">" + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" + "<meta name=\"description\" content=\"" + description + "\">" + "<meta property=\"og:title\" content=\"" + title + "\">" + "<meta property=\"og:description\" content=\"" + description + "\">" + "<meta property=\"og:type\" content=\"website\">" + "<meta property=\"og:url\" content=\"https://blu.cx\">" + "<meta property=\"og:image\" content=\"https://blu.cx/images/social-card.png\">" + "<meta property=\"og:site_name\" content=\"Brandon Lucas\">" + "<meta name=\"twitter:card\" content=\"summary_large_image\">" + "<meta name=\"twitter:title\" content=\"" + title + "\">" + "<meta name=\"twitter:description\" content=\"" + description + "\">" + "<link rel=\"canonical\" href=\"https://blu.cx\">" + "<link rel=\"preload\" href=\"/fonts/EBGaramond-Regular.woff2\" as=\"font\" type=\"font/woff2\" crossorigin=\"\">" + "<link rel=\"preload\" href=\"/fonts/UnifrakturMaguntia-Regular.woff2\" as=\"font\" type=\"font/woff2\" crossorigin=\"\">" + "<link href=\"/styles.css\" rel=\"stylesheet\" type=\"text/css\">" + "<link href=\"/highlight/tokyo-night-dark.min.css\" rel=\"stylesheet\" type=\"text/css\">" + "<script src=\"/highlight/highlight.min.js\" defer=\"\"></script>" + "<script>document.addEventListener('DOMContentLoaded', function() \{ hljs.highlightAll(); \});</script>" + "</head>"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user