generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
159 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,14 @@ | ||
import { CheckOutlined, CopyOutlined } from '@ant-design/icons'; | ||
import { Button, ConfigProvider, Tooltip } from 'antd'; | ||
import copy from 'copy-to-clipboard'; | ||
import { FC } from 'react'; | ||
|
||
import { Highlighter } from '../../components/Highlighter'; | ||
import { useCopied } from '../../hooks/useCopied'; | ||
import { useStyles } from './style'; | ||
|
||
interface SourceCodeProps { | ||
lang: string; | ||
children: string; | ||
} | ||
|
||
const SourceCode: FC<SourceCodeProps> = ({ children, lang }) => { | ||
const { copied, setCopied } = useCopied(); | ||
const { styles, theme } = useStyles(); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<ConfigProvider theme={{ token: { colorBgContainer: theme.colorBgElevated } }}> | ||
<Tooltip | ||
placement={'left'} | ||
arrow={false} | ||
title={ | ||
copied ? ( | ||
<> | ||
<CheckOutlined style={{ color: theme.colorSuccess }} /> 复制成功 | ||
</> | ||
) : ( | ||
'复制' | ||
) | ||
} | ||
> | ||
<Button | ||
icon={<CopyOutlined />} | ||
className={styles.button} | ||
onClick={() => { | ||
copy(children); | ||
setCopied(); | ||
}} | ||
/> | ||
</Tooltip> | ||
</ConfigProvider> | ||
|
||
<Highlighter language={lang}>{children}</Highlighter> | ||
</div> | ||
); | ||
return <Highlighter language={lang}>{children}</Highlighter>; | ||
}; | ||
|
||
export default SourceCode; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,29 @@ | ||
import { createStyles } from 'antd-style'; | ||
|
||
export const useStyles = createStyles(({ css, cx, prefixCls }) => { | ||
export const useStyles = createStyles(({ css, token, cx, prefixCls }) => { | ||
const prefix = `${prefixCls}-highlighter`; | ||
|
||
return { | ||
container: cx( | ||
prefix, | ||
css` | ||
position: relative; | ||
pre { | ||
margin: 0 !important; | ||
} | ||
`, | ||
), | ||
|
||
shiki: cx( | ||
`${prefix}-shiki`, | ||
css` | ||
.shiki { | ||
overflow: scroll; | ||
.line { | ||
font-family: ${token.fontFamilyHighlighter}; | ||
} | ||
} | ||
`, | ||
), | ||
|
||
prism: cx(`${prefix}-prism`), | ||
|
||
loading: css` | ||
position: absolute; | ||
bottom: 8px; | ||
right: 8px; | ||
right: 12px; | ||
color: ${token.colorTextTertiary}; | ||
`, | ||
}; | ||
}); |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { CheckOutlined, CopyOutlined } from '@ant-design/icons'; | ||
import { Button, ConfigProvider, Tooltip } from 'antd'; | ||
import copy from 'copy-to-clipboard'; | ||
import { CSSProperties, FC } from 'react'; | ||
|
||
import { useCopied } from '../../hooks/useCopied'; | ||
import SyntaxHighlighter from './Highlighter'; | ||
import { LanguageKeys } from './language'; | ||
import { useStyles } from './style'; | ||
export { Prism } from './Prism'; | ||
|
||
export interface HighlighterProps { | ||
children: string; | ||
language: LanguageKeys | string; | ||
/** | ||
* 语法高亮器类型 | ||
* @default 'shiki' | ||
*/ | ||
type?: 'shiki' | 'prism'; | ||
/** | ||
* 是否显示背景容器 | ||
* @default true | ||
*/ | ||
background?: boolean; | ||
className?: string; | ||
style?: CSSProperties; | ||
} | ||
|
||
export const Highlighter: FC<HighlighterProps> = ({ | ||
children, | ||
language, | ||
background = true, | ||
type, | ||
className, | ||
style, | ||
}) => { | ||
const { copied, setCopied } = useCopied(); | ||
const { styles, theme, cx } = useStyles(); | ||
const container = cx(styles.container, background && styles.withBackground, className); | ||
|
||
return ( | ||
<div | ||
// 用于标记是 markdown 中的代码块,避免和普通 code 的样式混淆 | ||
data-code-type="highlighter" | ||
className={container} | ||
style={style} | ||
> | ||
<ConfigProvider theme={{ token: { colorBgContainer: theme.colorBgElevated } }}> | ||
<Tooltip | ||
placement={'left'} | ||
arrow={false} | ||
title={ | ||
copied ? ( | ||
<> | ||
<CheckOutlined style={{ color: theme.colorSuccess }} /> 复制成功 | ||
</> | ||
) : ( | ||
'复制' | ||
) | ||
} | ||
> | ||
<Button | ||
icon={<CopyOutlined />} | ||
className={styles.button} | ||
onClick={() => { | ||
copy(children); | ||
setCopied(); | ||
}} | ||
/> | ||
</Tooltip> | ||
</ConfigProvider> | ||
|
||
<SyntaxHighlighter language={language} type={type}> | ||
{children} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Highlighter; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
import bash from 'react-syntax-highlighter/dist/cjs/languages/prism/bash'; | ||
import css from 'react-syntax-highlighter/dist/cjs/languages/prism/css'; | ||
import diff from 'react-syntax-highlighter/dist/cjs/languages/prism/diff'; | ||
import javascript from 'react-syntax-highlighter/dist/cjs/languages/prism/javascript'; | ||
import json from 'react-syntax-highlighter/dist/cjs/languages/prism/json'; | ||
import jsx from 'react-syntax-highlighter/dist/cjs/languages/prism/jsx'; | ||
import less from 'react-syntax-highlighter/dist/cjs/languages/prism/less'; | ||
import markdown from 'react-syntax-highlighter/dist/cjs/languages/prism/markdown'; | ||
import tsx from 'react-syntax-highlighter/dist/cjs/languages/prism/tsx'; | ||
import typescript from 'react-syntax-highlighter/dist/cjs/languages/prism/typescript'; | ||
|
||
export const languageMap = { | ||
javascript: javascript, | ||
jsx: javascript, | ||
json: json, | ||
markdown: markdown, | ||
less: less, | ||
typescript: typescript, | ||
javascript, | ||
js: javascript, | ||
jsx, | ||
json, | ||
markdown, | ||
md: markdown, | ||
less, | ||
css, | ||
typescript, | ||
ts: typescript, | ||
tsx: tsx, | ||
diff: diff, | ||
bash: bash, | ||
tsx, | ||
diff, | ||
bash, | ||
}; | ||
|
||
export type LanguageKeys = keyof typeof languageMap; |
This file contains 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
This file contains 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
This file contains 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