markup: support alerts in Markdown render - #8400
Open
Nahid-NHB wants to merge 1 commit into
Open
Conversation
Block quotes that start with `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]`, or `[!CAUTION]` on their own line are now rendered as callouts with a colored border, an icon, and a title, the same way GitHub renders them. Anything else keeps rendering as a plain block quote. Closes gogs#8388
|
https://github.com/orgs/community/discussions/204814 FYI, only GitHub has chosen to make Caution's color red instead of yellow. (All other similar decisions have been made under the influence of GitHub.) |
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.
Describe the pull request
Block quotes that start with
[!NOTE],[!TIP],[!IMPORTANT],[!WARNING], or[!CAUTION]on their own line now render as callouts with a colored border, an icon, and a title, the way GitHub renders them. Everything else keeps rendering as a plain block quote.Link to the issue: #8388
Root cause
Markdown goes through blackfriday v1, which has no GFM alerts extension, and
MarkdownRenderernever overrodeBlockQuote. So> [!NOTE]fell through to the stock renderer and came out as a normal block quote with the marker still sitting there as text.What changed
MarkdownRenderer.BlockQuotechecks whether a paragraph of the block quote starts with an alert marker on its own line, and emits an alert box if it does. Anything that isn't an alert still goes through the stock renderer, so plain block quotes are byte for byte what they were before.One wrinkle worth knowing about: blackfriday merges block quotes that are only separated by a blank line into a single one, so the five alerts in the issue arrive as one block quote with five paragraphs. The renderer therefore looks at every paragraph instead of just the first, and content that follows a marker (lists, code blocks, more paragraphs) stays inside that alert.
The marker has to take up the whole first line, same as GitHub, so
> [!NOTE] text on the same linestays a plain quote. Matching is case insensitive and works withENABLE_HARD_LINE_BREAKeither on or off.bluemonday strips
classby default, so the sanitizer needed an allowlist for the alert classes. The patterns only accept the five alert types and the five octicons used here, nothing broader.Styling lives in
_markdown.lesswith the matching rules added togogs.min.css. Colors are GitHub's, all at least 4.5:1 against white. Icons come from the octicons font that head.tmpl already loads, and they carryaria-hiddensince the title text is what conveys the meaning. Octicons 4.3.0 has noreporticon, so[!IMPORTANT]usesmegaphone.Notes for reviewers
A plain
>quote written right after an alert with only a blank line between them ends up inside that alert. blackfriday has already merged the two into one block quote by the time the renderer runs, so there is nothing left to tell them apart. Same root cause as the alert merging above, and it only really goes away with a different Markdown library.Also, I see the contributing guide asks for a proposal in Discussions before new features. Happy to move this over there if you'd rather discuss the approach first, or to close it if the direction isn't what you want.
Checklist
Test plan
Automated:
Test_MarkdownAlertcovers all five types, a lowercase marker, consecutive alerts, block content inside an alert, a marker with no body, a plain quote followed by an alert, a marker that isn't on its own line, an unknown type, and a plain block quote.Test_Sanitizergained cases for the accepted and the rejected classes. Both fail onmainand pass with this change.Manual:
Create a repository and put this in its
README.md:Open the repository home page. Each block renders as a callout: blue Note, green Tip, purple Important, amber Warning, red Caution, each with its octicon and a colored left border.
Add
> A regular quote.somewhere separated by other content and confirm it still renders as a gray block quote.