Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Combine existing Code component with the Markdown Code comp…
- Loading branch information
1 parent
f7143ea
commit b3a1390
Showing
14 changed files
with
184 additions
and
118 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,9 +1,22 @@ | ||
import React from 'react'; | ||
import { CodeRenderer } from './CodeRenderer'; | ||
|
||
it('renderer should render code', () => { | ||
const code = '<button>OK</button>'; | ||
const actual = shallow(<CodeRenderer classes={{}}>{code}</CodeRenderer>); | ||
describe('Code blocks', () => { | ||
it('should render code', () => { | ||
const code = '<button>OK</button>'; | ||
const actual = shallow(<CodeRenderer classes={{}}>{code}</CodeRenderer>); | ||
|
||
expect(actual).toMatchSnapshot(); | ||
expect(actual).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render code to be highlighted', () => { | ||
const code = '<button>OK</button>'; | ||
const actual = render( | ||
<CodeRenderer classes={{}} className="lang-html"> | ||
{code} | ||
</CodeRenderer> | ||
); | ||
|
||
expect(actual).toMatchSnapshot(); | ||
}); | ||
}); |
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,29 +1,31 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import cx from 'classnames'; | ||
import Styled from 'rsg-components/Styled'; | ||
|
||
const styles = ({ fontFamily }) => ({ | ||
code: { | ||
display: 'inline', | ||
fontFamily: fontFamily.monospace, | ||
fontSize: 'inherit', | ||
color: 'inherit', | ||
background: 'transparent', | ||
whiteSpace: 'inherit', | ||
}, | ||
}); | ||
|
||
export function CodeRenderer({ classes, className, children }) { | ||
return ( | ||
<span className={className}> | ||
<code className={classes.code}>{children}</code> | ||
</span> | ||
); | ||
} | ||
const classNames = cx(className, classes.code); | ||
|
||
const isHighlighted = className && className.indexOf('lang-') !== -1; | ||
if (isHighlighted) { | ||
return <code className={classNames} dangerouslySetInnerHTML={{ __html: children }} />; | ||
} | ||
return <code className={classNames}>{children}</code>; | ||
} | ||
CodeRenderer.propTypes = { | ||
classes: PropTypes.object.isRequired, | ||
className: PropTypes.string, | ||
children: PropTypes.node, | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
export default Styled(styles)(CodeRenderer); |
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,9 +1,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renderer should render code 1`] = ` | ||
<span> | ||
<code> | ||
<button>OK</button> | ||
</code> | ||
</span> | ||
exports[`Code blocks should render code 1`] = ` | ||
<code | ||
className="" | ||
> | ||
<button>OK</button> | ||
</code> | ||
`; | ||
|
||
exports[`Code blocks should render code to be highlighted 1`] = ` | ||
<code | ||
class="lang-html" | ||
> | ||
<button> | ||
OK | ||
</button> | ||
</code> | ||
`; |
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,11 @@ | ||
import React from 'react'; | ||
|
||
import Pre from './index'; | ||
|
||
describe('Markdown Pre', () => { | ||
it('should render a pre', () => { | ||
const actual = render(<Pre>This is pre-formatted text.</Pre>); | ||
|
||
expect(actual).toMatchSnapshot(); | ||
}); | ||
}); |
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,30 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import Styled from 'rsg-components/Styled'; | ||
|
||
const styles = ({ space, color, fontSize, fontFamily, borderRadius }) => ({ | ||
pre: { | ||
fontFamily: fontFamily.base, | ||
fontSize: fontSize.small, | ||
lineHeight: 1.5, | ||
color: color.base, | ||
whiteSpace: 'pre', | ||
backgroundColor: color.codeBackground, | ||
padding: [[space[1], space[2]]], | ||
border: [[1, color.border, 'solid']], | ||
borderRadius, | ||
marginTop: 0, | ||
marginBottom: space[2], | ||
}, | ||
}); | ||
|
||
export function PreRenderer({ classes, children }) { | ||
return <pre className={classes.pre}>{children}</pre>; | ||
} | ||
PreRenderer.propTypes = { | ||
classes: PropTypes.object.isRequired, | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
export default Styled(styles)(PreRenderer); |
9 changes: 9 additions & 0 deletions
9
src/rsg-components/Markdown/Pre/__snapshots__/Pre.spec.js.snap
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,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Markdown Pre should render a pre 1`] = ` | ||
<pre | ||
class="rsg--pre-0" | ||
> | ||
This is pre-formatted text. | ||
</pre> | ||
`; |
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 { default } from 'rsg-components/Markdown/Pre/PreRenderer'; |
Oops, something went wrong.