-
Notifications
You must be signed in to change notification settings - Fork 6
feat: hide code block lines prepended with #
#87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ use std::fmt::Write; | |||||||||||||||||
| use comrak::html::{ | ||||||||||||||||||
| ChildRendering, Context, format_document_with_formatter, format_node_default, render_sourcepos, | ||||||||||||||||||
| }; | ||||||||||||||||||
| use comrak::nodes::{AstNode, NodeLink, NodeValue}; | ||||||||||||||||||
| use comrak::nodes::{AstNode, NodeCodeBlock, NodeLink, NodeValue}; | ||||||||||||||||||
| use comrak::options::Plugins; | ||||||||||||||||||
| use comrak::{Arena, Options, parse_document}; | ||||||||||||||||||
| use cot_site_common::Version; | ||||||||||||||||||
|
|
@@ -47,10 +47,36 @@ fn format_node_custom<'a>( | |||||||||||||||||
| match node.data.borrow().value { | ||||||||||||||||||
| NodeValue::Table(_) => render_table_custom(context, node, entering), | ||||||||||||||||||
| NodeValue::Link(ref ln) => render_link_custom(context, node, entering, ln), | ||||||||||||||||||
| NodeValue::CodeBlock(ref cb) => render_code_block_custom(context, node, entering, cb), | ||||||||||||||||||
| _ => format_node_default(context, node, entering), | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| 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) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| fn remove_hidden_lines(input: &str) -> String { | ||||||||||||||||||
| let mut literal = String::new(); | ||||||||||||||||||
| for line in input.lines() { | ||||||||||||||||||
| if !line.starts_with("# ") { | ||||||||||||||||||
| literal.push_str(line); | ||||||||||||||||||
| literal.push('\n'); | ||||||||||||||||||
|
Comment on lines
+71
to
+74
|
||||||||||||||||||
| 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
render_code_block_customclones the entireNodeCodeBlockand allocates a newStringfor 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 toformat_node_defaulton the original node when no filtering is needed.