Skip to content

Commit 01e2148

Browse files
committed
fix(markdown): address PR #275 review feedback
1 parent 35db652 commit 01e2148

2 files changed

Lines changed: 12 additions & 30 deletions

File tree

webview-ui/src/components/common/MarkdownBlock.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -369,26 +369,22 @@ const MarkdownBlock = memo(({ markdown }: MarkdownBlockProps) => {
369369
</code>
370370
)
371371
},
372-
blockquote: ({ children, className, ...props }: any) => {
372+
blockquote: ({ children, className, "data-alert-type": alertType, ..._rest }: any) => {
373373
// The remarkGithubAlerts plugin tags alert blockquotes with a
374374
// `data-alert-type` attribute and `markdown-alert*` classes.
375375
// Anything without that attribute is a normal blockquote and
376376
// must render unchanged.
377-
const alertType = props["data-alert-type"] as AlertType | undefined
377+
const typedAlertType = alertType as AlertType | undefined
378378

379-
if (!alertType || !(alertType in ALERT_ICONS)) {
380-
return (
381-
<blockquote className={className} {...props}>
382-
{children}
383-
</blockquote>
384-
)
379+
if (!typedAlertType || !(typedAlertType in ALERT_ICONS)) {
380+
return <blockquote className={className}>{children}</blockquote>
385381
}
386382

387383
return (
388-
<blockquote className={className} {...props}>
384+
<blockquote className={className} data-alert-type={typedAlertType}>
389385
<div className="markdown-alert-title">
390-
<span className={`codicon ${ALERT_ICONS[alertType]}`} aria-hidden="true" />
391-
<span>{ALERT_LABELS[alertType]}</span>
386+
<span className={`codicon ${ALERT_ICONS[typedAlertType]}`} aria-hidden="true" />
387+
<span>{ALERT_LABELS[typedAlertType]}</span>
392388
</div>
393389
{children}
394390
</blockquote>

webview-ui/src/utils/markdown.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { visit } from "unist-util-visit"
2+
13
/**
24
* Counts the number of markdown headings in the given text.
35
* Matches headings from level 1 to 6 (e.g. #, ##, ###, etc.).
@@ -32,7 +34,7 @@ export type AlertType = (typeof ALERT_TYPES)[number]
3234

3335
// Matches a leading alert marker like "[!NOTE]" (case-insensitive) optionally
3436
// followed by trailing whitespace/newline on the first line of a blockquote.
35-
const ALERT_MARKER_REGEX = /^\[!(note|tip|important|warning|caution)\][^\S\r\n]*\r?\n?/i
37+
const ALERT_MARKER_REGEX = new RegExp(`^\\[!(${ALERT_TYPES.join("|")})\\][^\\S\\r\\n]*\\r?\\n?`, "i")
3638

3739
/**
3840
* remark plugin that detects GitHub-style alerts inside blockquotes
@@ -47,24 +49,8 @@ const ALERT_MARKER_REGEX = /^\[!(note|tip|important|warning|caution)\][^\S\r\n]*
4749
* normal blockquotes continue to render exactly as before.
4850
*/
4951
export function remarkGithubAlerts() {
50-
return (tree: any) => {
51-
walkAlertBlockquotes(tree)
52-
}
53-
}
54-
55-
function walkAlertBlockquotes(node: any): void {
56-
if (!node || typeof node !== "object") {
57-
return
58-
}
59-
60-
if (node.type === "blockquote") {
61-
annotateAlertBlockquote(node)
62-
}
63-
64-
if (Array.isArray(node.children)) {
65-
for (const child of node.children) {
66-
walkAlertBlockquotes(child)
67-
}
52+
return (tree: Parameters<typeof visit>[0]) => {
53+
visit(tree, "blockquote", annotateAlertBlockquote)
6854
}
6955
}
7056

0 commit comments

Comments
 (0)