Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline Rule end delimiter is treated the same as start delimiter when delimiters come in pairs (e.g. '{' and '}') #44

Open
SleeplessOne1917 opened this issue Sep 11, 2024 · 0 comments

Comments

@SleeplessOne1917
Copy link

I have the following code:

struct RubyScanner;

impl InlineRule for RubyScanner {
    const MARKER: char = '{';

    fn run(state: &mut InlineState) -> Option<(Node, usize)> {
        println!("START POS {}", state.pos);
        println!(
            "Relevant slice {}",
            unescape_all(&state.src[state.pos..state.pos_max])
        );

        let end_pos = state.src[state.pos..state.pos_max]
            .char_indices()
            .find_map(|(i, c)| (c == '}').then_some(i))?
            + state.pos;
        let (base_text, ruby_text) = state.src[state.pos + 1..end_pos].split_once('|')?;

        Some((
            Node::new(Ruby {
                base_text: base_text.trim().into(),
                ruby_text: ruby_text.trim().into(),
            }),
            (end_pos - state.pos) + 1,
        ))
    }
}

If I try parsing the following string with this ("\\{foo|bar}{baz|qux}"), the escape is correctly honored, but for some reason '}' is treated as a marker to start at the same as '{' is. The error output shows this:

START POS 9
Relevant slice }{baz|qux}
thread 'test::test::case_2' panicked at crates/markdown-it-ruby/src/lib.rs:66:47:
begin <= end (10 <= 9) when slicing `\{foo|bar}{baz|qux}`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I also tested this replacing '{' and '}' with '[' and ']' respectively and ran into the same issue. Interestingly, I didn't have this issue when I replaced the curly braces with parentheses.

Is this an error on my part? Because I expect MARKER - and only MARKER - to trigger the start of the scanner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant