From 65069abba06212316af016fc14953e4dbdba3119 Mon Sep 17 00:00:00 2001 From: frozolotl Date: Mon, 29 Jan 2024 00:41:59 +0100 Subject: [PATCH 1/3] Unescape escaped backticks and switch to ZWJ --- bot/src/bot.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/bot/src/bot.rs b/bot/src/bot.rs index 28bc528..84fd2da 100644 --- a/bot/src/bot.rs +++ b/bot/src/bot.rs @@ -12,10 +12,11 @@ use tokio::sync::{mpsc, Mutex}; use crate::worker::Worker; use crate::SOURCE_URL; -/// Prevent garbled output from codeblocks unwittingly terminated by their own content. -/// -/// U+200C is a zero-width non-joiner. +/// U+200D is a zero-width joiner. /// It prevents the triple backtick from being interpreted as a codeblock but retains ligature support. +const ZERO_WIDTH_JOINER: char = '\u{200D}'; + +/// Prevent garbled output from codeblocks unwittingly terminated by their own content. fn sanitize_code_block(raw: &str) -> impl Display + '_ { struct Helper<'a>(&'a str); @@ -27,7 +28,9 @@ fn sanitize_code_block(raw: &str) -> impl Display + '_ { .map_or((section, false), |safe| (safe, true)); formatter.write_str(safe)?; if should_append { - formatter.write_str("``\u{200c}`")?; + formatter.write_str("``")?; + formatter.write_char(ZERO_WIDTH_JOINER)?; + formatter.write_str("`")?; } } @@ -254,6 +257,16 @@ impl<'a> poise::PopArgument<'a> for CodeBlock { if code_block.language.as_deref() == Some("ansi") { source = strip_ansi_escapes::strip_str(source); } + + // Remove all occurences of the ZWJ when used if surrounded by backticks. + // This is used to enter Typst code blocks within Discord-markdown code blocks. + // Two replace calls are needed to remove all patterns of `ABA`: ABABABA => AABAA => AAAA. + let pattern = format!("`{ZERO_WIDTH_JOINER}`"); + let replacement = "``"; + source = source + .replace(&pattern, replacement) + .replace(&pattern, replacement); + Ok((rest, attachment_index, CodeBlock { source })) } } From 910be400b6a4b18478c1521f7261485f412366b7 Mon Sep 17 00:00:00 2001 From: frozolotl Date: Mon, 29 Jan 2024 00:51:18 +0100 Subject: [PATCH 2/3] Improve wording --- bot/src/bot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/src/bot.rs b/bot/src/bot.rs index 84fd2da..5f0d63a 100644 --- a/bot/src/bot.rs +++ b/bot/src/bot.rs @@ -258,7 +258,7 @@ impl<'a> poise::PopArgument<'a> for CodeBlock { source = strip_ansi_escapes::strip_str(source); } - // Remove all occurences of the ZWJ when used if surrounded by backticks. + // Remove all occurences of zero width joiners surrounded by backticks. // This is used to enter Typst code blocks within Discord-markdown code blocks. // Two replace calls are needed to remove all patterns of `ABA`: ABABABA => AABAA => AAAA. let pattern = format!("`{ZERO_WIDTH_JOINER}`"); From 6eb507e134b187c09a04d3659af92e598664147c Mon Sep 17 00:00:00 2001 From: Matt Fellenz Date: Sun, 28 Jan 2024 15:55:45 -0800 Subject: [PATCH 3/3] Clean up --- bot/src/bot.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/bot/src/bot.rs b/bot/src/bot.rs index 84fd2da..1bf548f 100644 --- a/bot/src/bot.rs +++ b/bot/src/bot.rs @@ -28,9 +28,7 @@ fn sanitize_code_block(raw: &str) -> impl Display + '_ { .map_or((section, false), |safe| (safe, true)); formatter.write_str(safe)?; if should_append { - formatter.write_str("``")?; - formatter.write_char(ZERO_WIDTH_JOINER)?; - formatter.write_str("`")?; + write!(formatter, "``{ZERO_WIDTH_JOINER}`")?; } } @@ -221,13 +219,13 @@ To remove the preamble entirely, use `pagesize=default theme=transparent`. ``` ?render `hello, world!` -?render pagesize=default theme=light ``‌` +?render pagesize=default theme=light ``‍` = Heading! And some text. #lorem(100) -``‌` +``‍` ?render `#myfunc()` I don't understand this code, can anyone help? ```" @@ -258,7 +256,7 @@ impl<'a> poise::PopArgument<'a> for CodeBlock { source = strip_ansi_escapes::strip_str(source); } - // Remove all occurences of the ZWJ when used if surrounded by backticks. + // Remove all occurrences of the ZWJ when used if surrounded by backticks. // This is used to enter Typst code blocks within Discord-markdown code blocks. // Two replace calls are needed to remove all patterns of `ABA`: ABABABA => AABAA => AAAA. let pattern = format!("`{ZERO_WIDTH_JOINER}`"); @@ -401,13 +399,13 @@ async fn source(ctx: Context<'_>) -> Result<(), PoiseError> { /// ``` /// ?ast `hello, world!` /// -/// ?ast ``‌` +/// ?ast ``‍` /// = Heading! /// /// And some text. /// /// #lorem(100) -/// ``‌` +/// ``‍` /// /// ?ast `#((3): 4)` Interesting parse result here. /// ```