Skip to content

Commit

Permalink
Merge pull request #18 from frozolotl/unescape-backticks
Browse files Browse the repository at this point in the history
Unescape escaped backticks and switch to ZWJ
  • Loading branch information
mattfbacon authored Jan 28, 2024
2 parents a864f1b + 7de2182 commit 9ac4650
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions bot/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -27,7 +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("``\u{200c}`")?;
write!(formatter, "``{ZERO_WIDTH_JOINER}`")?;
}
}

Expand Down Expand Up @@ -218,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?
```"
Expand Down Expand Up @@ -254,6 +255,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 occurrences 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}`");
let replacement = "``";
source = source
.replace(&pattern, replacement)
.replace(&pattern, replacement);

Ok((rest, attachment_index, CodeBlock { source }))
}
}
Expand Down Expand Up @@ -388,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.
/// ```
Expand Down

0 comments on commit 9ac4650

Please sign in to comment.