-
Notifications
You must be signed in to change notification settings - Fork 0
Add thematic break formatting #57
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 | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -227,6 +227,14 @@ static BULLET_RE: std::sync::LazyLock<Regex> = | |||||||||||||||||||||||||||||||||||
| static NUMBERED_RE: std::sync::LazyLock<Regex> = | ||||||||||||||||||||||||||||||||||||
| std::sync::LazyLock::new(|| Regex::new(r"^(\s*)([1-9][0-9]*)\.(\s+)(.*)").unwrap()); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// Width of a normalised thematic break. | ||||||||||||||||||||||||||||||||||||
| /// The width used when rewriting thematic breaks. | ||||||||||||||||||||||||||||||||||||
| pub const THEMATIC_BREAK_LEN: usize = 70; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| static THEMATIC_BREAK_RE: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| { | ||||||||||||||||||||||||||||||||||||
| Regex::new(r"^[ ]{0,3}((?:[ \t]*\*){3,}|(?:[ \t]*-){3,}|(?:[ \t]*_){3,})[ \t]*$").unwrap() | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// Returns `true` if the line is a fenced code block delimiter (e.g., three backticks or "~~~"). | ||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||
| /// # Examples | ||||||||||||||||||||||||||||||||||||
|
|
@@ -541,6 +549,33 @@ pub fn renumber_lists(lines: &[String]) -> Vec<String> { | |||||||||||||||||||||||||||||||||||
| out | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #[must_use] | ||||||||||||||||||||||||||||||||||||
| /// Reformat thematic breaks as 70 underscores. | ||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||
| /// Thematic breaks are lines composed of three or more matching `-`, `_`, or | ||||||||||||||||||||||||||||||||||||
| /// `*` characters (optionally separated by spaces or tabs) with up to three | ||||||||||||||||||||||||||||||||||||
| /// leading spaces. Lines inside fenced code blocks are ignored. | ||||||||||||||||||||||||||||||||||||
| pub fn format_breaks(lines: &[String]) -> Vec<String> { | ||||||||||||||||||||||||||||||||||||
| let mut out = Vec::with_capacity(lines.len()); | ||||||||||||||||||||||||||||||||||||
| let mut in_code = false; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| for line in lines { | ||||||||||||||||||||||||||||||||||||
| if FENCE_RE.is_match(line) { | ||||||||||||||||||||||||||||||||||||
| in_code = !in_code; | ||||||||||||||||||||||||||||||||||||
| out.push(line.clone()); | ||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if !in_code && THEMATIC_BREAK_RE.is_match(line.trim_end()) { | ||||||||||||||||||||||||||||||||||||
| out.push("_".repeat(THEMATIC_BREAK_LEN)); | ||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+569
to
+571
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Avoid repeated heap allocation for the underscore line
+/// Pre-computed 70-underscore break to avoid repeated allocation.
+static THEMATIC_BREAK_LINE: &str = "______________________________________________________________________";
...
- out.push("_".repeat(THEMATIC_BREAK_LEN));
+ let indent: String = line.chars().take_while(|c| c.is_whitespace()).collect();
+ out.push(format!("{indent}{THEMATIC_BREAK_LINE}"));Run 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
| out.push(line.clone()); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+569
to
+573
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Preserve leading indentation when rewriting thematic breaks Dropping the original leading whitespace alters the visual alignment of thematic breaks that sit inside nested structures (e.g. list items). Capture the whitespace prefix (up to three spaces per the spec) and prepend it to the generated underscore line. - out.push("_".repeat(THEMATIC_BREAK_LEN));
+ let indent: String = line.chars().take_while(|c| c.is_whitespace()).collect();
+ out.push(format!("{indent}{}", "_".repeat(THEMATIC_BREAK_LEN)));📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| out | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #[must_use] | ||||||||||||||||||||||||||||||||||||
| pub fn process_stream(lines: &[String]) -> Vec<String> { process_stream_inner(lines, true) } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
🧹 Nitpick (assertive)
Use en-GB spelling and finish the bullet with a full stop
Maintain stylistic consistency with the rest of the bullets.
📝 Committable suggestion
🤖 Prompt for AI Agents
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.
This repo uses en-oxendict spelling
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.
@leynos Thank you for the correction! You're absolutely right. I apologise for the error in my previous suggestion. Oxford spelling (en-oxendic) uses "-ize" endings, so "normalize" is indeed the correct spelling for this repository. I should not have suggested changing it to "normalise".
✏️ Learnings added