pub struct CodeBlockUtils;Expand description
Utility functions for detecting and handling code blocks in Markdown
Implementations§
Source§impl CodeBlockUtils
impl CodeBlockUtils
Sourcepub fn detect_code_blocks(content: &str) -> Vec<(usize, usize)>
pub fn detect_code_blocks(content: &str) -> Vec<(usize, usize)>
Detect all code blocks in the content (NOT including inline code spans)
Uses pulldown-cmark for spec-compliant CommonMark parsing. This correctly handles:
- Fenced code blocks (``` and ~~~)
- Indented code blocks (4 spaces or tab)
- Code blocks inside lists, blockquotes, and other containers
- Edge cases like backticks in info strings (which invalidate the fence)
Returns a sorted vector of (start, end) byte offset tuples.
Sourcepub fn detect_code_blocks_and_spans(content: &str) -> ParseResult
pub fn detect_code_blocks_and_spans(content: &str) -> ParseResult
Returns code block ranges, inline code span ranges, and detailed code block info in a single pulldown-cmark pass.
Sourcepub fn is_in_code_block_or_span(blocks: &[(usize, usize)], pos: usize) -> bool
pub fn is_in_code_block_or_span(blocks: &[(usize, usize)], pos: usize) -> bool
Check if a position is within a code block (for compatibility)
Sourcepub fn is_in_code_block(blocks: &[(usize, usize)], pos: usize) -> bool
pub fn is_in_code_block(blocks: &[(usize, usize)], pos: usize) -> bool
Check if a byte position falls within any of the given sorted, non-overlapping ranges.
Uses binary search on the sorted block ranges for O(log n) lookup.
The blocks slice must be sorted by start position (as returned by
detect_code_blocks and detect_code_blocks_and_spans).
Sourcepub fn analyze_code_block_context(
lines: &[LineInfo],
line_idx: usize,
min_continuation_indent: usize,
) -> CodeBlockContext
pub fn analyze_code_block_context( lines: &[LineInfo], line_idx: usize, min_continuation_indent: usize, ) -> CodeBlockContext
Analyze code block context relative to list parsing This is the core function implementing Design #3’s three-tier classification
Sourcepub fn calculate_min_continuation_indent(
content: &str,
lines: &[LineInfo],
current_line_idx: usize,
) -> usize
pub fn calculate_min_continuation_indent( content: &str, lines: &[LineInfo], current_line_idx: usize, ) -> usize
Calculate minimum indentation required for code block to continue a list Based on the most recent list item’s marker width
Sourcepub fn detect_markdown_code_blocks(content: &str) -> Vec<MarkdownCodeBlock>
pub fn detect_markdown_code_blocks(content: &str) -> Vec<MarkdownCodeBlock>
Detect fenced code blocks with markdown/md language tag.
Returns a vector of MarkdownCodeBlock containing byte ranges for the
content between the fences (excluding the fence lines themselves).
Only detects fenced code blocks (``` or ~~~), not indented code blocks, since indented blocks don’t have a language tag.