Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: markdown alert syntax support with unsafe_allow_html = true #1908

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

hexart
Copy link
Contributor

@hexart hexart commented Feb 19, 2025

Issue

When unsafe_allow_html = true, markdown-alert component has two issues:

  1. The ::: syntax fails to parse correctly
  2. HTML alert tag is not supported at all

Solution

  1. Fixed alert syntax parsing when unsafe_allow_html = true
  2. Added HTML tag support for alerts

Verified

  1. With unsafe_allow_html = true:

    • Verify both ::: syntax and HTML tag syntax work
    • Check all alert types render correctly
    • Confirm styling and icons are consistent across both syntaxes
  2. With unsafe_allow_html = false:

    • Verify ::: syntax works as before
    • Confirm HTML tags are escaped and shown as text

Usage

await cl.Message(content="::: idea\nInnovative idea\n:::\n\n").send()
<alert type="idea">Innovative idea</alert>

Addition

Add type validation in both markdown mode and html tag mode.
If type error, alert will fall back to info type.

Testing code

Details

import chainlit as cl

MARKDOWN_DEMO = '''
# Markdown & Alert Demo

## Text Formatting
**Bold text** and *italic text*
~~Strikethrough~~ and `inline code`

## Lists
### Unordered List
- First item
- Second item
 - Nested item
 - Another nested item

### Ordered List
1. First step
2. Second step
  1. Sub-step one
  2. Sub-step two

## Table
| Header 1 | Header 2 |
|----------|----------|
| Cell 1   | Cell 2   |
| Cell 3   | Cell 4   |

## Quote
> This is the quote text.
## Alerts
#### Basic alerts
::: InFo
Information message with a list:
Item 1
Item 2
:::

::: note
Note message
:::

::: tip
Helpful tip with code
:::

::: Important
Critical information!
:::

::: warning
Warning with emphasis
:::

::: caution
Caution message
:::
#### Development related
::: debug
Debug information
:::

::: example
Example usage
:::
#### Functional alerts
::: success
Operation successful
:::

::: help
Help message
:::

::: idea
Innovative idea
:::
#### Status alerts
::: pending
Work in progress
:::

::: security
Security notice
:::

::: beta
Beta feature
:::

::: best-practice
Recommended practice
:::

::: unknown
Unknown type, falling back to info type
:::

## HTML Test
<div style="color: blue;">
  <h3>HTML Content</h3>
  <ul>
    <li>HTML list item 1</li>
    <li>HTML list item 2</li>
  </ul>
</div>

<table border="1">
  <tr>
    <th>HTML Table</th>
    <th>Header 2</th>
  </tr>
  <tr>
    <td>Data 1</td>
    <td>Data 2</td>
  </tr>
</table>
<h3>Basic alerts</h3>
<alert type="Info">Information message</alert>
<alert type="noTe">Note message</alert>
<alert type="TIP">Helpful tip with code</alert>
<alert type="important">Critical information!</alert>
<alert type="warning">Warning with emphasis</alert>
<alert type="caution">Caution message</alert>
<h3>Development related</h3>
<alert type="debug">Debug information</alert>
<alert type="example">Example usage</alert>
<h3>Functional alerts</h3>
<alert type="success">Operation successful</alert>
<alert type="help">Help message</alert>
<alert type="idea">Innovative idea</alert>
<h3>Status alerts</h3>
<alert type="pending">Work in progress</alert>
<alert type="security">Security notice</alert>
<alert type="beta">Beta feature</alert>
<alert type="Best-practice">Recommended practice</alert>
<alert type="unknown">Unknown type, falling back to info type</alert>

## LaTeX Test
Inline math: $E = mc^2$
'''

@cl.on_chat_start
async def send_demo():
    await cl.Message(content=MARKDOWN_DEMO).send()

CAUTION:
This demo will cause a bug of nested scrolling pages with latex = true.

- Fix alert syntax (:::) parsing when unsafe_allow_html=true
- Add support for HTML alert tag with type attribute: <alert type="info">content</alert>
- Move normalizeAlertType to MarkdownAlert
- Remove duplicate code in Markdown component
@hexart hexart changed the title fix: markdown alert syntax support fix: markdown alert syntax support under unsafe_allow_html = true Feb 19, 2025
@hexart hexart marked this pull request as ready for review February 19, 2025 07:18
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. frontend Pertains to the frontend. labels Feb 19, 2025
@hexart hexart mentioned this pull request Feb 19, 2025
3 tasks
@hexart hexart changed the title fix: markdown alert syntax support under unsafe_allow_html = true fix: markdown alert syntax support with unsafe_allow_html = true Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend Pertains to the frontend. size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants