Skip to content

Commit

Permalink
fix(markdown): ignore block quotes
Browse files Browse the repository at this point in the history
Block quotes can contain code, raw text, or actual quotes where we do
not want to lint the content.
  • Loading branch information
ronnychevalier committed Aug 7, 2024
1 parent 134879f commit 269befa
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
- Avoid false positive with sqlite prepared statements (e.g., `SELECT a FROM b WHERE c = ?1 AND d = ?2`)
- Avoid false positives when something prints a string that looks like a condition or an expression (e.g., `a | !c` or`d = !(z && b)`)
- Markdown: avoid false positives with images (e.g., `![image](image.png)`)
- Markdown: ignore block quotes

### 📚 Documentation

Expand Down
60 changes: 60 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ lang-c = ["dep:tree-sitter-c"]
lang-cpp = ["dep:tree-sitter-cpp"]
lang-go = ["dep:tree-sitter-go"]
lang-json = ["dep:tree-sitter-json"]
lang-markdown = ["dep:tree-sitter-md"]
lang-markdown = ["dep:tree-sitter-md", "dep:btree-range-map"]
lang-python = ["dep:tree-sitter-python"]
lang-rust = ["dep:tree-sitter-rust"]
lang-toml = ["dep:tree-sitter-toml-ng"]
Expand All @@ -38,6 +38,7 @@ lang-kotlin = ["dep:tree-sitter-kotlin"]

[dependencies]
anyhow = "1.0.86"
btree-range-map = { version = "0.7.2", optional = true }
clap = { version = "4.5.9", features = ["derive"] }
ignore = "0.4.22"
miette = { version = "7.2.0", features = ["fancy"] }
Expand Down
49 changes: 48 additions & 1 deletion src/lang/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::iter::FlatMap;

use btree_range_map::RangeSet;

use tree_sitter::Tree;

use tree_sitter_md::MarkdownTree;
Expand Down Expand Up @@ -43,10 +45,20 @@ type MarkdownTraversal<'t> = FlatMap<
pub struct IterMarkdown<'t> {
traversals: MarkdownTraversal<'t>,
tree_sitter_types: &'static [&'static str],
block_quote_ranges: RangeSet<usize>,
}

impl<'t> IterMarkdown<'t> {
fn new(parsed: &'t ParsedMarkdown) -> Self {
let block_quote_ranges = PreorderTraversal::from(parsed.tree.block_tree())
.filter_map(|node| {
if node.kind() != "block_quote" {
return None;
}

Some(node.byte_range())
})
.collect::<RangeSet<_>>();
let traversals = parsed
.tree
.inline_trees()
Expand All @@ -55,6 +67,7 @@ impl<'t> IterMarkdown<'t> {
Self {
traversals,
tree_sitter_types: parsed.tree_sitter_types,
block_quote_ranges,
}
}
}
Expand All @@ -64,7 +77,11 @@ impl<'t> Iterator for IterMarkdown<'t> {

fn next(&mut self) -> Option<Self::Item> {
loop {
let node = self.traversals.next().map(LintableNode::from)?;
let node = self.traversals.next()?;
if self.block_quote_ranges.intersects(node.byte_range()) {
continue;
}

let kind = node.kind();
if node.byte_range().len() <= 3 {
continue;
Expand All @@ -73,6 +90,8 @@ impl<'t> Iterator for IterMarkdown<'t> {
if !self.tree_sitter_types.contains(&kind) {
continue;
}

let node = LintableNode::from(node);
let node =
node.ignore_children_ranges(|node| ["code_span", "image"].contains(&node.kind()));

Expand Down Expand Up @@ -172,4 +191,32 @@ hello
},]
);
}

#[test]
fn block_quote() {
let markdown = r#"# Block Quotes
> Should not be lintable
> > This line as well
> > And this one
Something else `hmm`
"#;
let markdown = SharedSource::new("file.md", markdown.as_bytes().to_vec());
let mut parsed = Language::markdown().parse(&markdown).unwrap();
let strings = parsed.strings(markdown.as_ref()).collect::<Vec<_>>();
assert_eq!(
strings,
[
LintableString {
offset: 2,
value: "Block Quotes".into()
},
LintableString {
offset: 81,
value: "Something else ".into()
},
]
);
}
}

0 comments on commit 269befa

Please sign in to comment.