-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat(code): Add syntax highlighting as css (instead of inline styles in js) and explicitly import specific languages #75
Changes from all commits
d58aa45
fcfdd20
e2ab7a5
619dac7
1d6fd4c
8831be2
b380d21
5cf7c68
27f8bf5
0d32d39
6128c33
9b94061
a8041e4
87ac6e3
c3db55a
4f9ffd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import { Code } from '@deque/cauldron-react'; | ||
import Demo from '../../../Demo'; | ||
|
||
const CodeDemo = () => ( | ||
<Demo | ||
component={Code} | ||
states={[ | ||
{ | ||
children: 'var foo = true;', | ||
language: 'javascript', | ||
DEMO_renderBefore: <h3>javascript</h3> | ||
}, | ||
{ | ||
children: '<span class="foo">Hello world!</span>', | ||
language: 'html', | ||
DEMO_renderBefore: <h3>html</h3> | ||
}, | ||
{ | ||
children: '$ npm install --save @deque/cauldron-react', | ||
DEMO_renderBefore: <h3>(no language prop)</h3> | ||
} | ||
]} | ||
propDocs={{ | ||
children: { | ||
type: 'string', | ||
description: 'code to be syntax highlighted and rendered in code block' | ||
}, | ||
language: { | ||
type: 'string', | ||
description: '"javascript", "css" or "html"' | ||
} | ||
}} | ||
/> | ||
); | ||
|
||
export default CodeDemo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
[[redirects]] | ||
from = "/*" | ||
to = "/index.html" | ||
|
||
status = 200 | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = () => ({}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ export default { | |
output: { | ||
dir: 'lib', | ||
format: 'cjs', | ||
preserveModules: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @scurker this flag causes the build to break. For some strange reason, it emits There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old webpack code preserved the directory structure so I was trying to keep the same structure when migrating from webpack, so this option kept that output the same. The only downside to not having it is debugging issues with cauldron, you'll see something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's causing issues now because of how we're "registering" languages in the Code component. I don't fully understand it either 🤷 |
||
exports: 'auto' | ||
}, | ||
plugins: [typescript(), commonjs(), svgr(), dynamicImportVar()] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,44 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import SyntaxHighlighter, { | ||
import { | ||
Light as SyntaxHighlighter, | ||
SyntaxHighlighterProps | ||
} from 'react-syntax-highlighter'; | ||
import theme from './theme'; | ||
import classNames from 'classnames'; | ||
import js from 'react-syntax-highlighter/dist/esm/languages/hljs/javascript'; | ||
import css from 'react-syntax-highlighter/dist/esm/languages/hljs/css'; | ||
import xml from 'react-syntax-highlighter/dist/esm/languages/hljs/xml'; | ||
|
||
SyntaxHighlighter.registerLanguage('javascript', js); | ||
SyntaxHighlighter.registerLanguage('css', css); | ||
SyntaxHighlighter.registerLanguage('html', xml); | ||
|
||
interface Props extends SyntaxHighlighterProps { | ||
children: React.ReactNode; | ||
language?: 'javascript' | 'css' | 'html'; | ||
className?: string; | ||
} | ||
|
||
const Code: React.ComponentType<Props> = ({ children, ...props }) => ( | ||
<SyntaxHighlighter {...props} style={theme}> | ||
const Code: React.ComponentType<Props> = ({ | ||
children, | ||
className, | ||
...props | ||
}) => ( | ||
<SyntaxHighlighter | ||
{...props} | ||
useInlineStyles={false} | ||
className={classNames('Code', className)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
> | ||
{children} | ||
</SyntaxHighlighter> | ||
); | ||
|
||
Code.displayName = 'Code'; | ||
|
||
Code.defaultProps = { language: 'javascript' }; | ||
|
||
Code.propTypes = { | ||
children: PropTypes.string.isRequired, | ||
language: PropTypes.string | ||
language: PropTypes.oneOf(['javascript', 'css', 'html']), | ||
className: PropTypes.string | ||
}; | ||
|
||
export default Code; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes the "deployed" site, enabling it to act as a SPA. It should have been done in #76.
I know it's an unrelated change, but figured we didn't need a PR just for this.