-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: syntahighlighter and markdown code format
- Loading branch information
1 parent
3bf353c
commit 022d2f2
Showing
16 changed files
with
154 additions
and
53 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
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,3 +1,2 @@ | ||
export * from './ControlsTable'; | ||
export * from './StorySource'; | ||
export * from './Source'; |
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
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,19 +1,24 @@ | ||
import React from 'react'; | ||
import { Markdown } from './Markdown'; | ||
import { ThemeProvider } from '../ThemeContext'; | ||
|
||
export default { | ||
title: 'Components/Markdown', | ||
component: Markdown, | ||
}; | ||
|
||
export const simple = () => ( | ||
<Markdown>{` | ||
<ThemeProvider> | ||
<Markdown>{` | ||
# Header H1 | ||
## Header H2 | ||
### Header H3 | ||
#### Header H4 | ||
##### Header H5 | ||
some text | ||
\`@theme-ui\` | ||
`}</Markdown> | ||
</ThemeProvider> | ||
); |
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,14 +1,28 @@ | ||
/* eslint-disable react/display-name */ | ||
import React, { FC } from 'react'; | ||
import MarkdownToJSX from 'markdown-to-jsx'; | ||
import { MDXProvider, Components } from '@mdx-js/react'; | ||
import MarkdownToJSX, { MarkdownOptions } from 'markdown-to-jsx'; | ||
import { SyntaxHighlighter } from '../SyntaxHighlighter'; | ||
|
||
export interface MarkdownProps { | ||
children: string; | ||
components?: Components; | ||
components?: MarkdownOptions['overrides']; | ||
} | ||
|
||
const defaultComponents: MarkdownOptions['overrides'] = { | ||
code: SyntaxHighlighter, | ||
}; | ||
|
||
/** | ||
* MDX display component that works at run time | ||
* uses `markdown-to-jsx` to compile MDX | ||
*/ | ||
export const Markdown: FC<MarkdownProps> = ({ children, components }) => ( | ||
<MDXProvider components={components}> | ||
<MarkdownToJSX>{children}</MarkdownToJSX> | ||
</MDXProvider> | ||
<MarkdownToJSX | ||
options={{ | ||
forceBlock: true, | ||
overrides: { ...defaultComponents, ...components }, | ||
}} | ||
> | ||
{children} | ||
</MarkdownToJSX> | ||
); |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** @jsx jsx */ | ||
/* eslint react/jsx-key: 0 */ | ||
import { jsx } from 'theme-ui'; | ||
import React, { FC, MouseEvent } from 'react'; | ||
import copy from 'copy-to-clipboard'; | ||
import { | ||
SyntaxHighlighter, | ||
SyntaxHighlighterProps, | ||
} from '../SyntaxHighlighter'; | ||
import { BlockContainer, BlockContainerProps } from '../BlockContainer'; | ||
|
||
export type SourceProps = SyntaxHighlighterProps & BlockContainerProps; | ||
/** | ||
* Source component used to display source code | ||
* | ||
*/ | ||
export const Source: FC<SourceProps> = ({ | ||
children = '', | ||
actions, | ||
title, | ||
as = 'div', | ||
...props | ||
}) => { | ||
const [copied, setCopied] = React.useState(false); | ||
const onCopy = (e: MouseEvent<HTMLButtonElement>) => { | ||
e.preventDefault(); | ||
setCopied(true); | ||
copy(children as string); | ||
window.setTimeout(() => setCopied(false), 1500); | ||
}; | ||
|
||
const actionsItems = Array.isArray(actions) ? [...actions] : []; | ||
|
||
actionsItems.push({ title: copied ? 'copied' : 'copy', onClick: onCopy }); | ||
return ( | ||
<BlockContainer actions={actionsItems} title={title}> | ||
<SyntaxHighlighter | ||
as={as} | ||
{...props} | ||
style={{ | ||
padding: '16px 10px 10px', | ||
display: 'block', | ||
}} | ||
> | ||
{children} | ||
</SyntaxHighlighter> | ||
</BlockContainer> | ||
); | ||
}; |
File renamed without changes.
31 changes: 31 additions & 0 deletions
31
ui/components/src/SyntaxHighlighter/SyntaxHighlighter.stories.tsx
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,31 @@ | ||
import React from 'react'; | ||
import shadesOfPurple from 'prism-react-renderer/themes/shadesOfPurple'; | ||
import { SyntaxHighlighter, SyntaxHighlighterProps } from './SyntaxHighlighter'; | ||
|
||
export default { | ||
title: 'Components/SyntaxHighlighter', | ||
component: SyntaxHighlighter, | ||
}; | ||
|
||
const source = `import { Button } from 'theme-ui';`; | ||
export const simpleSource = ({ children, dark }: SyntaxHighlighterProps) => { | ||
return <SyntaxHighlighter dark={dark}>{children}</SyntaxHighlighter>; | ||
}; | ||
|
||
simpleSource.story = { | ||
parameters: { | ||
controls: { | ||
children: { | ||
type: 'text', | ||
rows: 10, | ||
value: source, | ||
data: null, | ||
}, | ||
dark: { type: 'boolean' }, | ||
}, | ||
}, | ||
}; | ||
|
||
export const theme = () => ( | ||
<SyntaxHighlighter theme={shadesOfPurple}>{source}</SyntaxHighlighter> | ||
); |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './SyntaxHighlighter'; |
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