Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/mdparser/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ pub static INLINE_MATH_DOLLAR_REGEX: Lazy<FancyRegex> = Lazy::new(|| {
pub static DISPLAY_MATH_DOLLAR_REGEX: Lazy<FancyRegex> =
Lazy::new(|| FancyRegex::new(r"(?s)^\$\$(?:\n)?(?P<math>.*?)(?<!\\)(?:\n)?\$\$").unwrap());

// mirror INLINE_MATH_PAREN_PATTERN from postprocess.py
// match `\(math\)`, where the opening/closing backslashes are not themselves escaped
pub static INLINE_MATH_PAREN_REGEX: Lazy<FancyRegex> =
Lazy::new(|| FancyRegex::new(r"^\\\((?P<math>.*?)(?<!\\)\\\)").unwrap());

// mirror DISPLAY_MATH_BRACKET_PATTERN from postprocess.py
// display math can span multiple lines, so enable dotall mode
pub static DISPLAY_MATH_BRACKET_REGEX: Lazy<FancyRegex> =
Lazy::new(|| FancyRegex::new(r"(?s)^\\\[(?P<math>.*?)(?<!\\)\\\]").unwrap());

pub static CITATION_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"【\d+】").unwrap());

// mirror SINGLE_BACKTICK_PATTERN from postprocess.py
Expand Down
45 changes: 44 additions & 1 deletion src/plugins/kagi_plugins/math_display.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::mdparser::inline::{InlineRule, InlineState};
use crate::mdparser::preprocess::DISPLAY_MATH_DOLLAR_REGEX;
use crate::mdparser::preprocess::{DISPLAY_MATH_BRACKET_REGEX, DISPLAY_MATH_DOLLAR_REGEX};
use crate::plugin_config::DisplayMathExtensionPlugin;
use crate::plugins::cmark::inline::escape::EscapeScanner;
use crate::plugins::kagi_plugins::math::{math_render, math_render_cached};
use crate::{MarkdownIt, Node, NodeValue, Renderer};
use html_escape::decode_html_entities;
Expand Down Expand Up @@ -62,7 +63,49 @@ impl InlineRule for MathDisplayScanner {
}
}

struct MathDisplayBracketScanner;

impl InlineRule for MathDisplayBracketScanner {
const MARKER: char = '\\';

fn run(state: &mut InlineState) -> Option<(Node, usize)> {
let input = &state.src[state.pos..state.pos_max];
if !input.starts_with("\\[") {
return None;
}

// reject if the opening backslash is itself escaped by a preceding backslash
let preceeding_char = state
.src
.get(..state.pos)
.and_then(|s| s.chars().rev().next());
if let Some('\\') = preceeding_char {
return None;
}

let config = state.md.ext.get::<DisplayMathExtensionPlugin>().unwrap();

if let Ok(Some(caps)) = DISPLAY_MATH_BRACKET_REGEX.captures(input) {
let complete_match = &caps[0];
let math = caps.name("math")?.as_str();
Some((
Node::new(DisplayMath {
math: decode_html_entities(math).to_string(),
cache: config.cache,
}),
complete_match.len(),
))
} else {
None
}
}
}

pub fn add(md: &mut MarkdownIt, config: DisplayMathExtensionPlugin) {
md.ext.insert(config);
md.inline.add_rule::<MathDisplayScanner>();
// must run before EscapeScanner, which would otherwise consume `\[` as a plain escape
md.inline
.add_rule::<MathDisplayBracketScanner>()
.before::<EscapeScanner>();
}
45 changes: 44 additions & 1 deletion src/plugins/kagi_plugins/math_inline.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::mdparser::inline::{InlineRule, InlineState};
use crate::mdparser::preprocess::INLINE_MATH_DOLLAR_REGEX;
use crate::mdparser::preprocess::{INLINE_MATH_DOLLAR_REGEX, INLINE_MATH_PAREN_REGEX};
use crate::plugin_config::InlineMathExtensionPlugin;
use crate::plugins::cmark::inline::escape::EscapeScanner;
use crate::plugins::kagi_plugins::math::{math_render, math_render_cached};
use crate::{MarkdownIt, Node, NodeValue, Renderer};
use html_escape::decode_html_entities;
Expand Down Expand Up @@ -61,7 +62,49 @@ impl InlineRule for MathInlineScanner {
}
}

struct MathInlineParenScanner;

impl InlineRule for MathInlineParenScanner {
const MARKER: char = '\\';

fn run(state: &mut InlineState) -> Option<(Node, usize)> {
let input = &state.src[state.pos..state.pos_max];
if !input.starts_with("\\(") {
return None;
}

// reject if the opening backslash is itself escaped by a preceding backslash
let preceeding_char = state
.src
.get(..state.pos)
.and_then(|s| s.chars().rev().next());
if let Some('\\') = preceeding_char {
return None;
}

let config = state.md.ext.get::<InlineMathExtensionPlugin>().unwrap();

if let Ok(Some(caps)) = INLINE_MATH_PAREN_REGEX.captures(input) {
let complete_match = &caps[0];
let math = caps.name("math")?.as_str();
Some((
Node::new(InlineMath {
math: decode_html_entities(math).to_string(),
cache: config.cache,
}),
complete_match.len(),
))
} else {
None
}
}
}

pub fn add(md: &mut MarkdownIt, config: InlineMathExtensionPlugin) {
md.ext.insert(config);
md.inline.add_rule::<MathInlineScanner>();
// must run before EscapeScanner, which would otherwise consume `\(` as a plain escape
md.inline
.add_rule::<MathInlineParenScanner>()
.before::<EscapeScanner>();
}