feat: hide code block lines prepended with # #87
Conversation
There was a problem hiding this comment.
Pull request overview
Adds support in the markdown renderer used by cot-site-macros to hide code-block lines that are prepended with # (needed for cot-rs/cot#552), so those lines don’t appear in the generated HTML.
Changes:
- Intercept Comrak
CodeBlockrendering and filter out lines starting with#before rendering. - Add a helper (
remove_hidden_lines) to perform the filtering. - Add a unit test asserting that “hidden” lines are removed from the output HTML.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for line in input.lines() { | ||
| if !line.starts_with("# ") { | ||
| literal.push_str(line); | ||
| literal.push('\n'); |
There was a problem hiding this comment.
remove_hidden_lines rebuilds the code block text via input.lines() and always appends \n for each retained line. This can change the original code block content beyond hiding # lines (e.g., it drops trailing blank lines and may force a trailing newline even if the literal didn’t have one). Consider filtering while preserving the original line terminators (e.g., iterate with split_inclusive('\n') / track ends_with('\n')) so only the intended lines are removed and whitespace/newline structure is unchanged.
| for line in input.lines() { | |
| if !line.starts_with("# ") { | |
| literal.push_str(line); | |
| literal.push('\n'); | |
| for line in input.split_inclusive('\n') { | |
| let line_content = line.strip_suffix('\n').unwrap_or(line); | |
| if !line_content.starts_with("# ") { | |
| literal.push_str(line); |
| fn render_code_block_custom<'a>( | ||
| context: &mut Context<PageContext>, | ||
| _node: &'a AstNode<'a>, | ||
| entering: bool, | ||
| cb: &NodeCodeBlock, | ||
| ) -> Result<ChildRendering, std::fmt::Error> { | ||
| let mut new_cb = cb.clone(); | ||
| new_cb.literal = remove_hidden_lines(&cb.literal); | ||
|
|
||
| let node = AstNode::from(NodeValue::CodeBlock(Box::new(new_cb))); | ||
|
|
||
| format_node_default(context, &node, entering) |
There was a problem hiding this comment.
render_code_block_custom clones the entire NodeCodeBlock and allocates a new String for every code block, even when there are no hidden # lines to remove. You can avoid the extra work by doing a quick check (e.g., whether the literal starts with # or contains \n# ) and falling back to format_node_default on the original node when no filtering is needed.
This is required for cot-rs/cot#552.