Skip to content

Commit

Permalink
Fix markdown toc link when head includes code (#1087)
Browse files Browse the repository at this point in the history
* Fix markdown toc link when head includes code

* Fix clippy
  • Loading branch information
liuchengxu committed Jul 15, 2024
1 parent b2bfdcf commit 7a25ebd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
31 changes: 29 additions & 2 deletions crates/maple_markdown/src/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl Heading {
&& config.max_depth.map(|d| self.depth <= d).unwrap_or(true)
{
let Self { depth, title } = self;
let title_link = strip_backticks(title);
let indent_before_bullet = " "
.repeat(config.indent)
.repeat(depth.saturating_sub(config.min_depth));
Expand All @@ -90,12 +91,12 @@ impl Heading {
let title = cap.get(1).map(|x| x.as_str())?;
Some(format!(
"{indent_before_bullet}{bullet}{indent_after_bullet}[{title}](#{})",
slugify(title)
slugify(&title_link)
))
} else {
Some(format!(
"{indent_before_bullet}{bullet}{indent_after_bullet}[{title}](#{})",
slugify(title)
slugify(&title_link)
))
}
} else {
Expand Down Expand Up @@ -191,3 +192,29 @@ pub fn find_toc_range(input_file: impl AsRef<Path>) -> std::io::Result<Option<(u

Ok(None)
}

fn strip_backticks(input: &str) -> String {
// Define a regex to match text enclosed in backticks
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"`([^`]*)`").unwrap());

// Replace the matched pattern, keeping the inner text unchanged
RE.replace_all(input, "$1").to_string()
}

#[test]
fn test_heading() {
let heading: Heading = "### run-`subcoin import-blocks`".parse().unwrap();
assert_eq!(
heading.title.clone(),
"run-`subcoin import-blocks`".to_string()
);
assert_eq!(
heading
.format(&TocConfig {
max_depth: Some(4),
..Default::default()
})
.unwrap(),
" * [run-`subcoin import-blocks`](#run-subcoin-import-blocks)".to_string()
);
}
21 changes: 21 additions & 0 deletions crates/matcher/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,24 @@ fn test_rank() {
println!("{matched_item:?}");
}
}

#[test]
fn test_grep() {
let items = vec![
(
"substrate/primitives/wasm-interface/src/lib.rs",
"is_major_syncing: &str",
),
(
"substrate/client/network/sync/src/strategy.rs",
"pub fn is_major_syncing(&self) -> bool {",
),
];
// let query: Query = "\"is_major_syncing 'fn \"str".into();
let query: Query = "is_major_syncing 'fn 'strategy".into();
println!("Query: {query:?}");
let matcher = MatcherBuilder::new().build(query);
for (path, line) in items {
println!("{:?}", matcher.match_file_result(path.as_ref(), line));
}
}

0 comments on commit 7a25ebd

Please sign in to comment.