[feat] Add custom hex color support for markdown rendering#14041
[feat] Add custom hex color support for markdown rendering#14041lukasmasuch merged 5 commits intodevelopfrom
Conversation
Add support for `:color[text]{foreground="..." background="..."}` syntax
in markdown, enabling users to specify arbitrary CSS colors (hex, rgb,
rgba, hsl, hsla, named colors) for text styling.
- Add isValidCssColor() function with secure validation against XSS
- Handle custom color directive in createRemarkColoringAndSmall plugin
- Add comprehensive unit tests for color validation and directive
- Add E2E tests for visual regression coverage
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
There was a problem hiding this comment.
Pull request overview
Adds a new markdown text directive to Streamlit’s frontend markdown renderer to allow user-specified foreground/background colors via :color[...] attributes, along with corresponding unit + e2e coverage.
Changes:
- Implement
:color[text]{foreground="..." background="..."}handling in the remark directive pipeline, reusing existing colored-text/background CSS classes. - Add
isValidCssColor()to validate user-provided color strings before injecting them into an inlinestyleattribute. - Add frontend unit tests + extend
st_markdownE2E app/tests to cover the new directive.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx | Adds CSS color validation + directive handling that renders styled <span> elements. |
| frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.test.tsx | Adds unit tests for isValidCssColor and rendering behavior for the new directive. |
| e2e_playwright/st_markdown.py | Adds new demo cases to the markdown E2E app script for the custom color directive. |
| e2e_playwright/st_markdown_test.py | Adds E2E assertions (and one snapshot) verifying the directive renders expected styles. |
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.test.tsx
Outdated
Show resolved
Hide resolved
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.test.tsx
Outdated
Show resolved
Hide resolved
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx
Outdated
Show resolved
Hide resolved
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx
Outdated
Show resolved
Hide resolved
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx
Outdated
Show resolved
Hide resolved
## Describe your changes Automated snapshot updates for #14041 created via the snapshot autofix CI workflow. This workflow was triggered by adding the `update-snapshots` label to a PR after Playwright E2E tests failed with snapshot mismatches. **Updated snapshots:** 6 file(s)⚠️ **Please review the snapshot changes carefully** - they could mask visual bugs if accepted blindly. This PR targets a feature branch and can be merged without review approval. Co-authored-by: Streamlit Bot <[email protected]>
Move custom color directive testing into the existing mixed_markdown section instead of having separate dedicated tests. This reduces test maintenance overhead while still providing visual regression coverage through the mixed_markdown snapshot.
Invalid color values in :color[] now render the content as a plain span instead of falling through to unsupported directive cleanup which would lose the content text. Updated tests to verify this behavior.
SummaryThis PR adds support for a custom CSS color directive in Streamlit's markdown rendering. Users can now write
Code QualityThe code is clean, well-structured, and follows the existing patterns in
Test CoverageUnit tests are well-done:
E2E tests: The custom color examples are added to the mixed markdown block ( Potential gaps (minor, non-blocking):
Backwards CompatibilityNo breaking changes. The existing named-color directives (
The only behavioral change is for the edge case Security & RiskSecurity is well-addressed:
Risk assessment: Low. The validation approach is defense-in-depth: even if a color string somehow passed validation, it would be placed in a AccessibilityThe custom color directive uses the same HTML structure and CSS classes as the existing predefined color directives — One consideration: custom foreground/background colors could create contrast issues for users with visual impairments (e.g., light text on light background). However, this is inherent to the feature request and is the author's responsibility when using custom colors, similar to how raw CSS works. No automated enforcement is practical here. Recommendations
VerdictAPPROVED: Clean, well-tested implementation that adds custom CSS color support to markdown directives with solid security validation via an existing dependency. The code follows existing patterns, introduces no breaking changes, and has comprehensive unit test coverage. This is an automated AI review by |
Add tests documenting behavior for: - Mixed valid/invalid attributes (only valid colors applied) - Empty attributes (renders as plain span)
Describe your changes
:color[text]{foreground="..." background="..."}syntax in markdown, enabling custom CSS colors (hex, rgb, rgba, hsl, hsla, named colors) for text stylingisValidCssColor()validation function to prevent XSS via CSS injectionstMarkdownColoredTextandstMarkdownColoredBackgroundCSS classes for consistent stylingGithub issues
Testing Plan
isValidCssColor()validation and custom color directive rendering