-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for serializing math nodes as markdown
Closes GH-148. Reviewed-by: Titus Wormer <[email protected]>
- Loading branch information
Showing
9 changed files
with
505 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! JS equivalent: https://github.com/syntax-tree/mdast-util-math/blob/main/lib/index.js#L241 | ||
|
||
use super::Handle; | ||
use crate::state::{Info, State}; | ||
use alloc::format; | ||
use markdown::{ | ||
mdast::{InlineMath, Node}, | ||
message::Message, | ||
}; | ||
use regex::Regex; | ||
|
||
impl Handle for InlineMath { | ||
fn handle( | ||
&self, | ||
state: &mut State, | ||
_info: &Info, | ||
_parent: Option<&Node>, | ||
_node: &Node, | ||
) -> Result<alloc::string::String, Message> { | ||
let mut size: usize = if !state.options.single_dollar_text_math { | ||
2 | ||
} else { | ||
1 | ||
}; | ||
|
||
let pattern = format!("(^|[^$]){}([^$]|$)", "\\$".repeat(size)); | ||
let mut dollar_sign_match = Regex::new(&pattern).unwrap(); | ||
while dollar_sign_match.is_match(&self.value) { | ||
size += 1; | ||
let pattern = format!("(^|[^$]){}([^$]|$)", "\\$".repeat(size)); | ||
dollar_sign_match = Regex::new(&pattern).unwrap(); | ||
} | ||
|
||
let sequence = "$".repeat(size); | ||
|
||
let no_whitespaces = !self.value.chars().all(char::is_whitespace); | ||
let starts_with_whitespace = self.value.starts_with(char::is_whitespace); | ||
let ends_with_whitespace = self.value.ends_with(char::is_whitespace); | ||
let starts_with_dollar = self.value.starts_with('$'); | ||
let ends_with_dollar = self.value.ends_with('$'); | ||
|
||
let mut value = self.value.clone(); | ||
if no_whitespaces | ||
&& ((starts_with_whitespace && ends_with_whitespace) | ||
|| starts_with_dollar | ||
|| ends_with_dollar) | ||
{ | ||
value = format!(" {} ", value); | ||
} | ||
|
||
for pattern in &mut state.r#unsafe { | ||
if !pattern.at_break { | ||
continue; | ||
} | ||
|
||
State::compile_pattern(pattern); | ||
|
||
if let Some(regex) = &pattern.compiled { | ||
while let Some(m) = regex.find(&value) { | ||
let position = m.start(); | ||
|
||
let position = if position > 0 | ||
&& &value[position..m.len()] == "\n" | ||
&& &value[position - 1..position] == "\r" | ||
{ | ||
position - 1 | ||
} else { | ||
position | ||
}; | ||
|
||
value.replace_range(position..m.start() + 1, " "); | ||
} | ||
} | ||
} | ||
|
||
Ok(format!("{}{}{}", sequence, value, sequence)) | ||
} | ||
} | ||
|
||
pub fn peek_inline_math() -> char { | ||
'$' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! JS equivalent: https://github.com/syntax-tree/mdast-util-math/blob/main/lib/index.js#L204 | ||
|
||
use super::Handle; | ||
use crate::{ | ||
construct_name::ConstructName, | ||
state::{Info, State}, | ||
util::{longest_char_streak::longest_char_streak, safe::SafeConfig}, | ||
}; | ||
use alloc::string::String; | ||
use markdown::{ | ||
mdast::{Math, Node}, | ||
message::Message, | ||
}; | ||
|
||
impl Handle for Math { | ||
fn handle( | ||
&self, | ||
state: &mut State, | ||
_info: &Info, | ||
_parent: Option<&Node>, | ||
_node: &Node, | ||
) -> Result<alloc::string::String, Message> { | ||
let sequence = "$".repeat((longest_char_streak(&self.value, '$') + 1).max(2)); | ||
state.enter(ConstructName::MathFlow); | ||
|
||
let mut value = String::new(); | ||
value.push_str(&sequence); | ||
|
||
if let Some(meta) = &self.meta { | ||
state.enter(ConstructName::MathFlowMeta); | ||
value.push_str(&state.safe(meta, &SafeConfig::new(&value, "\n", Some('$')))); | ||
state.exit(); | ||
} | ||
|
||
value.push('\n'); | ||
|
||
if !self.value.is_empty() { | ||
value.push_str(&self.value); | ||
value.push('\n'); | ||
} | ||
|
||
value.push_str(&sequence); | ||
state.exit(); | ||
Ok(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.