diff options
| Commit | 681b6ce5fb1c5d903f70bb81fb6f210689f0859e (patch) | |
|---|---|---|
| Author | 2025-01-01 14:43:58 +0200 | |
| Committer | 2025-01-01 14:43:58 +0200 | |
| Files | e318d32b711acac4115cda2fe0a4f725f7ce95d7 /single.awk | |
| csg-681b6ce5fb1c5d903f70bb81fb6f210689f0859e.tar csg-681b6ce5fb1c5d903f70bb81fb6f210689f0859e.tar.gz csg-681b6ce5fb1c5d903f70bb81fb6f210689f0859e.zip | ||
| Parent | cb888c3ddad432a4a66106834d7655592becee3b (diff) | |
feat(single): Implement fenced codeblocks
1 files changed, 28 insertions, 5 deletions
| -rw-r--r-- | single.awk | 33 |
@@ -15,7 +15,7 @@ # [ ] Definition lists # [ ] Single line # [ ] Multi-line -# [ ] Fenced code blocks ```lang ``` +# [x] Fenced code blocks ```lang ``` # [ ] Tables # [ ] Links # [ ] Link destination (<link> inside anchors) @@ -155,12 +155,22 @@ blockquote_level > 0 && empty0 { } /^>$/ { $0 = ""; not_paragraph = 1 } +# Fenced code blocks +fenced_codeblock { + $0 = " " $0 +} + # Code blocks (ending) -inside_codeblock && ! /^[ \t]*$)/ { +inside_codeblock && (/ ```/ || ! /^( |\t|[ \t]*$)/) { PNappnd("</code></pre>") inside_codeblock = 0 emptyP1 = 1 not_paragraph = 1 + if (fenced_codeblock) { + fenced_codeblock = 0 + fenced_class = "" + next + } } # Lists @@ -239,10 +249,20 @@ list_started && /^\[[x ]\][ \t]/ { "><label>" substr($0, 5) "</label>" } -# Code blocks -/^( |\t)/ { +# Fenced code blocks +!inside_codeblock && /^[ \t]*```/ { + fenced_codeblock = 1 + + match(P1, /```[^ \t]*/) + if (RLENGTH > 3) { + fenced_class = " class=\"language-" tolower(substr(P1, RSTART + 3, RLENGTH - 3)) "\"" } + next +} + +# Code blocks +/^( |\t)/ { ## Autoescape & and < for (i = 1; match(substr($0, i), /&/); i += RSTART) { RSTART += i - 1 @@ -251,8 +271,11 @@ list_started && /^\[[x ]\][ \t]/ { gsub(/</, "\\<") gsub(/>/, "\\>") + if (fenced_codeblock) { + $0 = substr($0, 5) + } if (!inside_codeblock) { - $0 = "<pre><code>" $0 + $0 = "<pre><code" fenced_class ">" $0 inside_codeblock = 1 } |
