Skip to content

Commit

Permalink
feat(code): Add syntax highlighting as css (instead of inline styles …
Browse files Browse the repository at this point in the history
…in js) and explicitly import specific languages (#75)
  • Loading branch information
schne324 committed Aug 27, 2020
1 parent 0f3a92f commit 2dd315b
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 162 deletions.
3 changes: 2 additions & 1 deletion docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const componentsList = [
'ExpandCollapsePanel',
'TextField',
'Link',
'Icon'
'Icon',
'Code'
].sort();

class App extends Component {
Expand Down
37 changes: 37 additions & 0 deletions docs/patterns/components/Code/index.js
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;
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
[[redirects]]
from = "/*"
to = "/index.html"

status = 200
1 change: 1 addition & 0 deletions packages/react/__tests__/hljsMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => ({});
3 changes: 2 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"keyname": "^0.1.0",
"prop-types": "^15.6.0",
"rc-tooltip": "^3.7.2",
"react-syntax-highlighter": "^6.1.1",
"react-syntax-highlighter": "^13.5.1",
"tslib": "^2.0.0",
"unique-string": "^1.0.0"
},
Expand Down Expand Up @@ -113,6 +113,7 @@
],
"moduleNameMapper": {
"\\.(css|less)$": "<rootDir>/__tests__/styleMock.js",
"react-syntax-highlighter/dist/esm/languages/hljs/(.*)": "<rootDir>/__tests__/hljsMock.js",
"\\.svg$": "<rootDir>/__tests__/svgMock.js"
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/react/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default {
output: {
dir: 'lib',
format: 'cjs',
preserveModules: true,
exports: 'auto'
},
plugins: [typescript(), commonjs(), svgr(), dynamicImportVar()]
Expand Down
31 changes: 24 additions & 7 deletions packages/react/src/components/Code/index.tsx
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)}
>
{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;
109 changes: 0 additions & 109 deletions packages/react/src/components/Code/theme.ts

This file was deleted.

84 changes: 42 additions & 42 deletions packages/react/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,13 @@
dependencies:
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.3.1":
version "7.11.2"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736"
integrity sha512-TeWkU52so0mPtDcaCTxNBI/IHiz0pZgr8VEFqXFtZWpYD08ZB6FaSwVAS8MKRQAP3bYKiVjwysOJgMFY28o6Tw==
dependencies:
regenerator-runtime "^0.13.4"

"@babel/template@^7.10.4":
version "7.10.4"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.10.4.tgz#3251996c4200ebc71d1a8fc405fba940f36ba278"
Expand Down Expand Up @@ -2542,7 +2549,7 @@ babel-preset-jest@^24.9.0:
"@babel/plugin-syntax-object-rest-spread" "^7.0.0"
babel-plugin-jest-hoist "^24.9.0"

babel-runtime@6.x, babel-runtime@^6.18.0, babel-runtime@^6.22.0, babel-runtime@^6.26.0:
babel-runtime@6.x, babel-runtime@^6.22.0, babel-runtime@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe"
integrity sha1-llxwWGaOgrVde/4E/yM3vItWR/4=
Expand Down Expand Up @@ -3920,7 +3927,7 @@ fastq@^1.6.0:
dependencies:
reusify "^1.0.4"

fault@^1.0.2:
fault@^1.0.0:
version "1.0.4"
resolved "https://registry.yarnpkg.com/fault/-/fault-1.0.4.tgz#eafcfc0a6d214fc94601e170df29954a4f842f13"
integrity sha512-CJ0HCB5tL5fYTEA7ToAq5+kTwd++Borf1/bifxd9iT70QcXr4MRrO3Llf8Ifs70q+SJcGHFtnIE/Nw6giCtECA==
Expand Down Expand Up @@ -4373,10 +4380,10 @@ hastscript@^5.0.0:
property-information "^5.0.0"
space-separated-tokens "^1.0.0"

highlight.js@~9.12.0:
version "9.12.0"
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-9.12.0.tgz#e6d9dbe57cbefe60751f02af336195870c90c01e"
integrity sha1-5tnb5Xy+/mB1HwKvM2GVhwyQwB4=
highlight.js@^10.1.1, highlight.js@~10.1.0:
version "10.1.2"
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-10.1.2.tgz#c20db951ba1c22c055010648dfffd7b2a968e00c"
integrity sha512-Q39v/Mn5mfBlMff9r+zzA+gWxRsCRKwEMvYTiisLr/XUiFI/4puWt0Ojdko3R3JCNWGdOWaA5g/Yxqa23kC5AA==

history@^4.9.0:
version "4.10.1"
Expand Down Expand Up @@ -5700,13 +5707,13 @@ loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3
dependencies:
js-tokens "^3.0.0 || ^4.0.0"

lowlight@~1.9.1:
version "1.9.2"
resolved "https://registry.yarnpkg.com/lowlight/-/lowlight-1.9.2.tgz#0b9127e3cec2c3021b7795dd81005c709a42fdd1"
integrity sha512-Ek18ElVCf/wF/jEm1b92gTnigh94CtBNWiZ2ad+vTgW7cTmQxUY3I98BjHK68gZAJEWmybGBZgx9qv3QxLQB/Q==
lowlight@^1.14.0:
version "1.14.0"
resolved "https://registry.yarnpkg.com/lowlight/-/lowlight-1.14.0.tgz#83ebc143fec0f9e6c0d3deffe01be129ce56b108"
integrity sha512-N2E7zTM7r1CwbzwspPxJvmjAbxljCPThTFawEX2Z7+P3NGrrvY54u8kyU16IY4qWfoVIxY8SYCS8jTkuG7TqYA==
dependencies:
fault "^1.0.2"
highlight.js "~9.12.0"
fault "^1.0.0"
highlight.js "~10.1.0"

magic-string@^0.25.2, magic-string@^0.25.7:
version "0.25.7"
Expand Down Expand Up @@ -6286,10 +6293,10 @@ parent-module@^1.0.0:
dependencies:
callsites "^3.0.0"

parse-entities@^1.1.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/parse-entities/-/parse-entities-1.2.2.tgz#c31bf0f653b6661354f8973559cb86dd1d5edf50"
integrity sha512-NzfpbxW/NPrzZ/yYSoQxyqUZMZXIdCfE0OIN4ESsnptHJECoUk3FZktxNuzQf4tjt5UEopnxpYJbvYuxIFDdsg==
parse-entities@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/parse-entities/-/parse-entities-2.0.0.tgz#53c6eb5b9314a1f4ec99fa0fdf7ce01ecda0cbe8"
integrity sha512-kkywGpCcRYhqQIchaWqZ875wzpS/bMKhz5HnN3p7wveJTkTtyAB/AlnS0f8DFSqYW1T82t6yEAkEcB+A1I3MbQ==
dependencies:
character-entities "^1.0.0"
character-entities-legacy "^1.0.0"
Expand Down Expand Up @@ -6572,17 +6579,10 @@ pretty-hrtime@^1.0.3:
resolved "https://registry.yarnpkg.com/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz#b7e3ea42435a4c9b2759d99e0f201eb195802ee1"
integrity sha1-t+PqQkNaTJsnWdmeDyAesZWALuE=

prismjs@^1.8.4:
version "1.20.0"
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.20.0.tgz#9b685fc480a3514ee7198eac6a3bf5024319ff03"
integrity sha512-AEDjSrVNkynnw6A+B1DsFkd6AVdTnp+/WoUixFRULlCLZVRZlVQMVWio/16jv7G1FscUxQxOQhWwApgbnxr6kQ==
optionalDependencies:
clipboard "^2.0.0"

prismjs@~1.17.0:
version "1.17.1"
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.17.1.tgz#e669fcbd4cdd873c35102881c33b14d0d68519be"
integrity sha512-PrEDJAFdUGbOP6xK/UsfkC5ghJsPJviKgnQOoxaDbBjwc8op68Quupwt1DeAFoG8GImPhiKXAvvsH7wDSLsu1Q==
prismjs@^1.21.0, prismjs@~1.21.0:
version "1.21.0"
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.21.0.tgz#36c086ec36b45319ec4218ee164c110f9fc015a3"
integrity sha512-uGdSIu1nk3kej2iZsLyDoJ7e9bnPzIgY0naW/HdknGj61zScaprVEVGHrPoXqI+M9sP0NDnTK2jpkvmldpuqDw==
optionalDependencies:
clipboard "^2.0.0"

Expand Down Expand Up @@ -6831,16 +6831,16 @@ react-side-effect@^2.1.0:
resolved "https://registry.yarnpkg.com/react-side-effect/-/react-side-effect-2.1.0.tgz#1ce4a8b4445168c487ed24dab886421f74d380d3"
integrity sha512-IgmcegOSi5SNX+2Snh1vqmF0Vg/CbkycU9XZbOHJlZ6kMzTmi3yc254oB1WCkgA7OQtIAoLmcSFuHTc/tlcqXg==

react-syntax-highlighter@^6.1.1:
version "6.1.2"
resolved "https://registry.yarnpkg.com/react-syntax-highlighter/-/react-syntax-highlighter-6.1.2.tgz#a6fd152400139dedf50dcad5e05fc07e054b4b94"
integrity sha512-ahNwcZ0FhUd8U5TQYcmAqC/pec6Q308mUAATKMcLFmNYkvGhN9wfmoqxzjACcccGb2e85d5ZnGpOiCIIzGO3yA==
react-syntax-highlighter@13:
version "13.5.1"
resolved "https://registry.yarnpkg.com/react-syntax-highlighter/-/react-syntax-highlighter-13.5.1.tgz#f21737cf6d582474a0f18b06b52613f4349c0e64"
integrity sha512-VVYTnFXF55WMRGdr3QNEzAzcypFZqH45kS7rqh90+AFeNGtui8/gV5AIOIJjwTsuP2UxcO9qvEq94Jq9BYFUhw==
dependencies:
babel-runtime "^6.18.0"
highlight.js "~9.12.0"
lowlight "~1.9.1"
prismjs "^1.8.4"
refractor "^2.0.0"
"@babel/runtime" "^7.3.1"
highlight.js "^10.1.1"
lowlight "^1.14.0"
prismjs "^1.21.0"
refractor "^3.1.0"

react-test-renderer@^16.0.0-0:
version "16.13.1"
Expand Down Expand Up @@ -6944,14 +6944,14 @@ reflect.ownkeys@^0.2.0:
resolved "https://registry.yarnpkg.com/reflect.ownkeys/-/reflect.ownkeys-0.2.0.tgz#749aceec7f3fdf8b63f927a04809e90c5c0b3460"
integrity sha1-dJrO7H8/34tj+SegSAnpDFwLNGA=

refractor@^2.0.0:
version "2.10.1"
resolved "https://registry.yarnpkg.com/refractor/-/refractor-2.10.1.tgz#166c32f114ed16fd96190ad21d5193d3afc7d34e"
integrity sha512-Xh9o7hQiQlDbxo5/XkOX6H+x/q8rmlmZKr97Ie1Q8ZM32IRRd3B/UxuA/yXDW79DBSXGWxm2yRTbcTVmAciJRw==
refractor@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/refractor/-/refractor-3.1.0.tgz#b05a43c8a1b4fccb30001ffcbd5cd781f7f06f78"
integrity sha512-bN8GvY6hpeXfC4SzWmYNQGLLF2ZakRDNBkgCL0vvl5hnpMrnyURk8Mv61v6pzn4/RBHzSWLp44SzMmVHqMGNww==
dependencies:
hastscript "^5.0.0"
parse-entities "^1.1.2"
prismjs "~1.17.0"
parse-entities "^2.0.0"
prismjs "~1.21.0"

regenerate-unicode-properties@^8.2.0:
version "8.2.0"
Expand Down
Loading

0 comments on commit 2dd315b

Please sign in to comment.