Skip to content

Commit

Permalink
Fix codeblock in codeblock
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Dec 21, 2023
1 parent a89f4dd commit fdaba81
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion i18n-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,11 @@ pub fn reconstruct_markdown(
group: &[(usize, Event)],
state: Option<State<'static>>,
) -> (String, State<'static>) {
let code_block_token_count = check_code_block_token_count(group);
let events = group.iter().map(|(_, event)| event);
let mut markdown = String::new();
let options = Options {
code_block_token_count: 3,
code_block_token_count,
list_token: '-',
emphasis_token: '_',
strong_token: "**",
Expand Down Expand Up @@ -556,6 +557,39 @@ pub fn reconstruct_markdown(
(String::from(markdown.trim_start_matches('\n')), new_state)
}

/// Check appropriate codeblock token count
fn check_code_block_token_count(group: &[(usize, Event)]) -> usize {
let events = group.iter().map(|(_, event)| event);
let mut in_codeblock = false;
let mut max_token_count = 0;
for event in events {
match event {
Event::Start(Tag::CodeBlock(_)) => in_codeblock = true,
Event::End(Tag::CodeBlock(_)) => in_codeblock = false,
Event::Text(x) if in_codeblock => {
let mut token_count = 0;
for c in x.chars() {
if c == '`' {
token_count += 1;
} else {
max_token_count = std::cmp::max(max_token_count, token_count);
token_count = 0;
}
}
max_token_count = std::cmp::max(max_token_count, token_count);
}
_ => (),
}
}
if max_token_count < 3 {
// default code block token is "```" which is 3
3
} else {
// If there is "```" in codeblock, codeblock token should be extended.
max_token_count + 1
}
}

/// Extract translatable strings from `document`.
///
/// # Examples
Expand Down Expand Up @@ -1483,4 +1517,18 @@ def g(x):
)],
);
}

#[test]
fn extract_messages_codeblock_in_codeblock() {
assert_extract_messages(
r#"
````
```
// codeblock in codeblock
```
````
"#,
&[(2, "````\n```\n// codeblock in codeblock\n```\n````")],
);
}
}

0 comments on commit fdaba81

Please sign in to comment.