fix: syntax highlighting for markdown files#320
Open
tenox7 wants to merge 2 commits into
Open
Conversation
Chroma's markdown lexer emits Generic* token types (GenericHeading, GenericStrong, GenericEmph) which mapTokenType did not handle, so headings, bold, and italic text rendered unstyled. Map them to existing syntax styles. GenericInserted/GenericDeleted are also mapped, giving .diff/.patch files +/- line coloring.
There was a problem hiding this comment.
Pull request overview
This PR fixes missing syntax highlighting for Markdown (and improves diff/patch highlighting) by mapping Chroma’s Generic* token types to existing term.Style* theme styles, and adds unit tests to cover the new behavior.
Changes:
- Extend
mapTokenType()to styleGenericHeading/Subheading,GenericStrong,GenericEmph, andGenericInserted/Deleted. - Add unit tests for Markdown and diff highlighting.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/core/highlight/highlighter.go | Maps Chroma Generic* token types to existing theme styles so Markdown/diff tokens aren’t dropped as StyleDefault. |
| internal/core/highlight/highlighter_test.go | Adds coverage for Markdown headings/bold/italic/inline-code and diff +/- lines. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+106
to
+109
| case t == chroma.GenericInserted: | ||
| return term.StyleSuccess | ||
| case t == chroma.GenericDeleted: | ||
| return term.StyleDanger |
Comment on lines
+101
to
+102
| assertSpanStyle(t, h, "+added line", term.StyleSuccess) | ||
| assertSpanStyle(t, h, "-removed line", term.StyleDanger) |
Use StyleDiffAdded/StyleDiffDeleted instead of StyleSuccess/StyleDanger so .diff/.patch files match the app's diff view theming.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Opening a
.mdfile shows no syntax highlighting even though chroma matches the markdown lexer.Root cause
Chroma's markdown lexer emits mostly
Generic*token types —GenericHeading(#),GenericSubheading(##+),GenericStrong(**bold**),GenericEmph(*italic*).mapTokenType()ininternal/core/highlight/highlighter.goonly handled programming-language categories (Keyword, String, Comment, Name*, ...), so all Generic tokens fell through toStyleDefaultand were dropped. Markdown files rendered almost entirely unstyled.Fix
Map Generic token types to existing theme styles:
GenericHeading/GenericSubheadingStyleSyntaxKeywordGenericStrongStyleSyntaxTypeGenericEmphStyleSyntaxStringGenericInserted/GenericDeletedStyleDiffAdded/StyleDiffDeletedThe Inserted/Deleted mapping also gives
.diff/.patchfiles +/- line coloring for free.No new styles or theme changes needed — all mappings reuse existing
term.Style*constants.Tests
Added
TestHighlightMarkdownandTestHighlightDiffunit tests covering headings, bold, italic, inline code, and diff +/- lines.Known limitation (unchanged)
Fenced code blocks (```go) still render as plain text — highlighting is per-line and fences require multi-line lexer state. Separate, larger change.