markdown: Fix inline HTML block highlighting (#61212)

# Objective

Fixes #59304

Right now inline html tags don't render with any kind of highlighting,
despite the fact that non-inline html tags do get highlighting.

## Solution

Right now zed already has tree-sitter rules that identifies the inline
blocks so we can add a new tree-sitter grammar rule,
```
((html_tag) @injection.content
  (#set! injection.language "html")
  (#set! injection.combined))
```
that passes these blocks to the html language server.

## Testing

I did some manual testing:


https://github.com/user-attachments/assets/92e5b322-bbc1-48c3-a615-0d32acc2e7d6

And also added a test:
test_markdown_inline_html_highlighting


## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

---

Release Notes:

- markdown: Fixed inline html block highlighting.
This commit is contained in:
Finn Eitreim 2026-07-19 23:52:32 -04:00 committed by GitHub
parent ac09219cad
commit 8677759c7a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 102 additions and 0 deletions

View file

@ -1,2 +1,6 @@
((html_tag) @injection.content
(#set! injection.language "html")
(#set! injection.combined))
((latex_block) @injection.content
(#set! injection.language "latex"))

View file

@ -2981,6 +2981,104 @@ fn test_language_at_for_markdown_code_block(cx: &mut App) {
});
}
#[gpui::test]
async fn test_markdown_inline_html_highlighting(cx: &mut TestAppContext) {
let markdown_language = markdown_lang();
let markdown_inline_language = Arc::new(
Language::new(
LanguageConfig {
name: "markdown-inline".into(),
grammar: Some("markdown-inline".into()),
..Default::default()
},
Some(tree_sitter_md::INLINE_LANGUAGE.into()),
)
.with_highlights_query(include_str!(
"../../grammars/src/markdown-inline/highlights.scm"
))
.unwrap()
.with_injection_query(include_str!(
"../../grammars/src/markdown-inline/injections.scm"
))
.unwrap(),
);
let html_language = Arc::new(
Language::new(
LanguageConfig {
name: "HTML".into(),
..Default::default()
},
Some(tree_sitter_html::LANGUAGE.into()),
)
.with_highlights_query("(comment) @comment (tag_name) @tag")
.unwrap(),
);
let syntax_theme = SyntaxTheme::new([
("comment".to_string(), gpui::rgba(0xffffffff).into()),
("tag".to_string(), gpui::rgba(0xff0000ff).into()),
]);
markdown_language.set_theme(&syntax_theme);
markdown_inline_language.set_theme(&syntax_theme);
html_language.set_theme(&syntax_theme);
let language_registry = Arc::new(LanguageRegistry::test(cx.background_executor.clone()));
language_registry.add(markdown_language.clone());
language_registry.add(markdown_inline_language);
language_registry.add(html_language);
let text = "<!--Annotation from the start is OK-->\n\n\
Annotation in the middle <!--is rendered badly.-->\n\n\
An inline comment can span <!--multiple\nlines--> within a paragraph.\n\n\
Ordinary inline HTML: <em>emphasized</em>.";
let buffer = cx.new(|cx| {
let mut buffer = Buffer::local(text, cx);
buffer.set_language_registry(language_registry);
buffer.set_language(Some(markdown_language), cx);
buffer
});
cx.run_until_parked();
buffer.read_with(cx, |buffer, _cx| {
let snapshot = buffer.snapshot();
let highlighted_text = |capture_name: &str| {
let highlight_id = syntax_theme
.highlight_id(capture_name)
.map(HighlightId::new);
assert!(highlight_id.is_some(), "{capture_name} not in test theme");
let mut runs: Vec<String> = Vec::new();
let mut previous_chunk_matched = false;
let chunks = snapshot.chunks(
0..snapshot.len(),
LanguageAwareStyling {
tree_sitter: true,
diagnostics: false,
},
);
for chunk in chunks {
let chunk_matches = chunk.syntax_highlight_id == highlight_id;
if chunk_matches {
match runs.last_mut() {
Some(last_run) if previous_chunk_matched => last_run.push_str(chunk.text),
_ => runs.push(chunk.text.to_string()),
}
}
previous_chunk_matched = chunk_matches;
}
runs
};
assert_eq!(
highlighted_text("comment"),
vec![
"<!--Annotation from the start is OK-->",
"<!--is rendered badly.-->",
"<!--multiple\nlines-->",
]
);
assert_eq!(highlighted_text("tag"), vec!["em", "em"]);
});
}
#[gpui::test]
fn test_syntax_layer_at_for_combined_injections(cx: &mut App) {
init_settings(cx, |_| {});