Skip to content

feat: hide code block lines prepended with # #87

Merged
seqre merged 1 commit intomasterfrom
code-blocks-hide-lines
Apr 27, 2026
Merged

feat: hide code block lines prepended with # #87
seqre merged 1 commit intomasterfrom
code-blocks-hide-lines

Conversation

@m4tx
Copy link
Copy Markdown
Member

@m4tx m4tx commented Apr 27, 2026

This is required for cot-rs/cot#552.

@m4tx m4tx requested review from a team and Copilot April 27, 2026 16:38
@seqre seqre merged commit c316d0c into master Apr 27, 2026
14 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 CodeBlock rendering 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.

Comment on lines +71 to +74
for line in input.lines() {
if !line.starts_with("# ") {
literal.push_str(line);
literal.push('\n');
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
Comment on lines +55 to +66
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)
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@seqre seqre deleted the code-blocks-hide-lines branch April 27, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants