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

Refactor GitHub alert capitalization styling #4088

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 15 additions & 6 deletions packages/nextra/src/client/hocs/with-github-alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,32 @@ export function withGitHubAlert(
if (Array.isArray(props.children)) {
const str = props.children[1].props.children
if (typeof str === 'string') {
const alertName = str
.match(GITHUB_ALERT_RE)
?.groups?.name!.toLowerCase()
const match = str.match(GITHUB_ALERT_RE)?.groups?.name

if (!match) {
return <Component {...props} />
}

const alertName = match.toLowerCase()

if (alertName) {
if (!GITHUB_ALERT_TYPES.has(alertName)) {
throw new Error(
`Invalid GitHub alert type: "${alertName}". Should be one of: ${GITHUB_ALERTS.join(', ')}.`
)
}
const capitalizedName =
alertName[0]!.toUpperCase() + alertName.slice(1)
return fn({
...props,
type: alertName as T,
children: [
<b key={0}>{capitalizedName}</b>,
<b
key={0}
style={{
textTransform: 'capitalize'
}}
>
{alertName}
</b>,
...props.children.slice(2)
]
})
Expand Down
Loading