Skip to content

Commit

Permalink
feat: support Icon in MDXA
Browse files Browse the repository at this point in the history
  • Loading branch information
kuizuo committed Dec 30, 2023
1 parent 1d07704 commit 1c862b4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/css/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ article {
.markdown-body a:hover::before {
width: 100%;
}

.a-icon {
display: none;
}

p > span {
> .a-icon {
display: block;
}

> a {
line-height: 1.5rem;
}
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/theme/MDXComponents/A.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react'
import Link from '@docusaurus/Link'
import type { Props } from '@theme/MDXComponents/A'

import { Icon } from '@iconify/react'

export default function MDXA(props: Props): JSX.Element {
const href = props.href

if (!href) return <Link {...props} />

const iconMappings = {
'github.com': 'simple-icons:github',
'twitter.com': 'logos:twitter',
}

const foundKey = Object.keys(iconMappings).find(key => {
const prefixRegex = new RegExp(`^https?://${key}`)
return prefixRegex.test(href)
})

const icon = foundKey ? iconMappings[foundKey] : null

if (icon) {
return (
<span style={{ display: 'inline-flex', gap: '0.25rem' }}>
{icon && (
<Icon
className="a-icon"
style={{ alignSelf: 'center' }}
icon={icon}
width={16}
height={16}
></Icon>
)}
<Link {...props} />
</span>
)
}

return <Link {...props} />
}

1 comment on commit 1c862b4

@vercel
Copy link

@vercel vercel bot commented on 1c862b4 Dec 30, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

blog – ./

blog-kuizuo1.vercel.app
kuizuo.cn
blog-kuizuo.vercel.app
blog-git-main-kuizuo1.vercel.app

Please sign in to comment.