From 681b6ce5fb1c5d903f70bb81fb6f210689f0859e Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 1 Jan 2025 14:43:58 +0200 Subject: feat(single): Implement fenced codeblocks --- single.awk | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'single.awk') diff --git a/single.awk b/single.awk index 0752b9f..94801be 100644 --- a/single.awk +++ b/single.awk @@ -15,7 +15,7 @@ # [ ] Definition lists # [ ] Single line # [ ] Multi-line -# [ ] Fenced code blocks ```lang ``` +# [x] Fenced code blocks ```lang ``` # [ ] Tables # [ ] Links # [ ] Link destination ( 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("") 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]/ { ">" } -# 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(//, "\\>") + if (fenced_codeblock) { + $0 = substr($0, 5) + } if (!inside_codeblock) { - $0 = "
" $0
+		$0 = "
" $0
 		inside_codeblock = 1
 	}
 
-- 
cgit v1.3.1-23-gd82d