Skip to content

Add support for rendering alerts in README #11441

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

Merged
merged 2 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions app/styles/crate/version.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,105 @@
border: 0;
}
}

/* alerts */
:global(.markdown-alert) {
--fg-color-note: #4494f8;
--fg-color-tip: #3fb950;
--fg-color-important: #ab7df8;
--fg-color-warning: #d29922;
--fg-color-caution: #f85149;

padding: 0.5rem 1rem;
margin-bottom: 1rem;
color: inherit;
border-left: .25em solid var(--gray-border);

& > :first-child {
margin-top: 0;
}

& > :last-child {
margin-bottom: 0;
}

:global(.markdown-alert-title) {
display: flex;
font-weight: 500;
align-items: center;
line-height: 1;
}

& > :global(.markdown-alert-title)::before {
content: '';
margin-right: .5rem;
background-color: var(--gray-border);
width: 1em;
height: 1em;
}

&:global(.markdown-alert-note) {
border-left-color: var(--fg-color-note);

& > :global(.markdown-alert-title) {
color: var(--fg-color-note);

&:before {
mask: url("/assets/alert-note.svg");
background-color: var(--fg-color-note);
}
}
}

&:global(.markdown-alert-tip) {
border-left-color: var(--fg-color-tip);

& > :global(.markdown-alert-title) {
color: var(--fg-color-tip);

&:before {
mask: url("/assets/alert-tip.svg");
background-color: var(--fg-color-tip);
}
}
}

&:global(.markdown-alert-important) {
border-left-color: var(--fg-color-important);

& > :global(.markdown-alert-title) {
color: var(--fg-color-important);

&:before {
mask: url("/assets/alert-important.svg");
background-color: var(--fg-color-important);
}
}
}

&:global(.markdown-alert-warning) {
border-left-color: var(--fg-color-warning);

& > :global(.markdown-alert-title) {
color: var(--fg-color-warning);

&:before {
mask: url("/assets/alert-warning.svg");
background-color: var(--fg-color-warning);
}
}
}

&:global(.markdown-alert-caution) {
border-left-color: var(--fg-color-caution);

& > :global(.markdown-alert-title) {
color: var(--fg-color-caution);

&:before {
mask: url("/assets/alert-caution.svg");
background-color: var(--fg-color-caution);
}
}
}
}
48 changes: 47 additions & 1 deletion crates/crates_io_markdown/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ impl<'a> MarkdownRenderer<'a> {
]),
),
("section", hashset(&["footnotes"])),
(
"div",
hashset(&[
"markdown-alert",
"markdown-alert-note",
"markdown-alert-tip",
"markdown-alert-important",
"markdown-alert-warning",
"markdown-alert-caution",
]),
),
("p", hashset(&["markdown-alert-title"])),
]);
let sanitize_url = UrlRelative::Custom(Box::new(SanitizeUrl::new(base_url, base_dir)));

Expand Down Expand Up @@ -77,7 +89,9 @@ impl<'a> MarkdownRenderer<'a> {
.build();

let extension_options = ComrakExtensionOptions::builder()
.alerts(true)
.autolink(true)
.multiline_block_quotes(true)
.strikethrough(true)
.table(true)
.tagfilter(true)
Expand Down Expand Up @@ -429,7 +443,7 @@ mod tests {
#[test]
fn text_with_forbidden_class_attribute() {
let text = "<p class='bad-class'>Hello World!</p>";
assert_snapshot!(markdown_to_html(text, None, ""), @"<p>Hello World!</p>");
assert_snapshot!(markdown_to_html(text, None, ""), @r#"<p class="">Hello World!</p>"#);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be caused by the allowed_classes. I'm not sure if there's any way to get around it.

}

#[test]
Expand Down Expand Up @@ -684,4 +698,36 @@ There can also be some text in between!
</ul>
"##);
}

#[test]
fn alerts_note() {
let text = r#"
> [!note]
> Hello, world!
"#;
assert_snapshot!(markdown_to_html(text, None, ""), @r#"
<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<p>Hello, world!</p>
</div>
"#);
}

#[test]
fn alerts_note_multiline_block_quotes() {
let text = r#"
>>> [!note]
Hello,

world!
>>>
"#;
assert_snapshot!(markdown_to_html(text, None, ""), @r#"
<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<p>Hello,</p>
<p>world!</p>
</div>
"#);
}
}
33 changes: 33 additions & 0 deletions e2e/acceptance/readme-rendering.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@ import { expect, test } from '@/e2e/helper';
import { http, HttpResponse } from 'msw';

const README_HTML = `
<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<p>Useful information that users should know, even when skimming content.</p>
</div>
<div class="markdown-alert markdown-alert-tip">
<p class="markdown-alert-title">Tip</p>
<p>Helpful advice for doing things better or more easily.</p>
</div>
<div class="markdown-alert markdown-alert-important">
<p class="markdown-alert-title">Important</p>
<p>Key information users need to know to achieve their goal.</p>
</div>
<div class="markdown-alert markdown-alert-warning">
<p class="markdown-alert-title">Warning</p>
<p>Urgent info that needs immediate user attention to avoid problems.</p>
</div>
<div class="markdown-alert markdown-alert-caution">
<p class="markdown-alert-title">Caution</p>
<p>Advises about risks or negative outcomes of certain actions.</p>
</div>

<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<div class="markdown-alert markdown-alert-important">
<p class="markdown-alert-title">Important</p>
<div class="markdown-alert markdown-alert-caution">
<p class="markdown-alert-title">Caution</p>
<p>Rick roll</p>
<p>Never gonna give you up</p>
</div>
</div>
</div>

<p><strong>Serde is a framework for <em>ser</em>ializing and <em>de</em>serializing Rust data structures efficiently and generically.</strong></p>
<hr>
<p>You may be looking for:</p>
Expand Down
3 changes: 3 additions & 0 deletions public/assets/alert-caution.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/assets/alert-important.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/assets/alert-note.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/assets/alert-tip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/assets/alert-warning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions tests/acceptance/readme-rendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ import { setupApplicationTest } from 'crates-io/tests/helpers';
import { visit } from '../helpers/visit-ignoring-abort';

const README_HTML = `
<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<p>Useful information that users should know, even when skimming content.</p>
</div>
<div class="markdown-alert markdown-alert-tip">
<p class="markdown-alert-title">Tip</p>
<p>Helpful advice for doing things better or more easily.</p>
</div>
<div class="markdown-alert markdown-alert-important">
<p class="markdown-alert-title">Important</p>
<p>Key information users need to know to achieve their goal.</p>
</div>
<div class="markdown-alert markdown-alert-warning">
<p class="markdown-alert-title">Warning</p>
<p>Urgent info that needs immediate user attention to avoid problems.</p>
</div>
<div class="markdown-alert markdown-alert-caution">
<p class="markdown-alert-title">Caution</p>
<p>Advises about risks or negative outcomes of certain actions.</p>
</div>

<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<div class="markdown-alert markdown-alert-important">
<p class="markdown-alert-title">Important</p>
<div class="markdown-alert markdown-alert-caution">
<p class="markdown-alert-title">Caution</p>
<p>Rick roll</p>
<p>Never gonna give you up</p>
</div>
</div>
</div>

<p><strong>Serde is a framework for <em>ser</em>ializing and <em>de</em>serializing Rust data structures efficiently and generically.</strong></p>
<hr>
<p>You may be looking for:</p>
Expand Down
Loading