Skip to content

Commit

Permalink
Basic link rendering in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Oct 28, 2024
1 parent 9290c34 commit 2c9592f
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions sample_programs/build_site.gdn
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,48 @@ fun render_markdown_header(src: String): String {
}

fun render_inline(src: String): String {
let src_parts = src.split("`")
let in_code = False

let parts: List<String> = []
for src_part in src_parts {
if in_code {
parts = parts.append("".join(["<code>", src_part, "</code>"]))
} else {
parts = parts.append(src_part)
}

in_code = in_code.not()
while True {
match split_inline_punct(src) {
Some(src_parts) => {
let (before, after) = src_parts
parts = parts.append(before)

if after.starts_with("`") {
let (rendered, new_src) = render_backtick(after)
parts = parts.append(rendered)
src = new_src
} else if after.ends_with("[") {
let (rendered, new_src) = render_link(after)
parts = parts.append(rendered)
src = new_src
} else {
assert(False)
}
}
None => {
parts = parts.append(src)
break
}
}
}

"".join(parts)
}

fun split_inline_punct(src: String): Option<(String, String)> {
let i = 0
while (i + 1) < src.len() {
let char = src.substring(i, i + 1)
if (char == "`") || (char == "[") {
return Some((src.substring(0, i), src.substring(i, src.len())))
}
}

None
}

// Render the next backtick text as code, and return the remainder of
// the text to process.
fun render_backtick(src: String): (String, String) {
Expand Down

0 comments on commit 2c9592f

Please sign in to comment.