-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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(www): add copy button to code snippets #15834
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
d9905de
feat(www): add standalone pre component with copy functionality
DSchau ed4801a
chore: make it vaguely appealing
DSchau 246eb4f
chore: make even prettier
DSchau d5068a8
chore: keep iterating
DSchau b8bb626
chore: make it work nicely again
DSchau b63020c
chore: get syntax highlighting working; todo line highlighting
DSchau bcaf18a
feat: improve accessibility (not done!)
DSchau 16d3cbe
feat: add screenreader text
DSchau 5755947
chore: swap to name
DSchau d4f06ee
chore: add missing status
DSchau a6fe77e
feat: get line highlighting mostly working
DSchau 2ea5c81
feat: get hide directive working too
DSchau 253eef5
feat: iron out highlights and use correct aria-role
DSchau a8eff91
chore: iron-out hide
DSchau 083c71f
feat: add support for braces in language
DSchau 1919a03
style: implement flo's new design
DSchau d37f914
test: get tests passing
DSchau fc095b7
Merge branch 'master' of github.com:gatsbyjs/gatsby into www/copy
DSchau e05e395
chore: restore monospace stack
DSchau 7db21a8
feat: get {} working again
DSchau 82ec189
chore: make sure to trim
DSchau e1feaea
chore: add some tests
DSchau e08c990
test: finish tests
DSchau d86d8a4
test: more of 'em of course
DSchau 45d92a4
chore: more fixes
DSchau 7a89649
test: more tests; and calling this done!
DSchau 1742d2b
chore: tiny fix
DSchau 1be3da0
chore: add back missing autolink headers
DSchau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from "react" | ||
import CodeBlock from ".." | ||
import { fireEvent, render } from "react-testing-library" | ||
|
||
beforeEach(() => { | ||
document.execCommand = jest.fn() | ||
}) | ||
|
||
describe(`basic functionality`, () => { | ||
describe(`copy`, () => { | ||
it(`renders a copy button`, () => { | ||
const { queryByText } = render( | ||
<CodeBlock language="jsx">{`var a = 'b'`}</CodeBlock> | ||
) | ||
|
||
expect(queryByText(`copy`)).toBeDefined() | ||
}) | ||
|
||
it(`copies text to clipboard`, () => { | ||
const text = `alert('hello world')` | ||
const { queryByText } = render( | ||
<CodeBlock language="jsx">{text}</CodeBlock> | ||
) | ||
|
||
const copyButton = queryByText(`Copy`) | ||
|
||
fireEvent.click(copyButton) | ||
|
||
expect(document.execCommand).toHaveBeenCalledWith(`copy`) | ||
}) | ||
}) | ||
|
||
describe(`highlighting`, () => { | ||
let instance | ||
const hidden = `var a = 'i will be hidden'` | ||
const highlighted = ` | ||
<div> | ||
<h1>Oh shit waddup</h1> | ||
</div> | ||
`.trim() | ||
beforeEach(() => { | ||
const text = ` | ||
import React from 'react' | ||
|
||
${hidden} // hide-line | ||
|
||
export default function HelloWorld() { | ||
return ( | ||
{/* highlight-start */} | ||
${highlighted} | ||
{/* highlight-end */} | ||
) | ||
} | ||
`.trim() | ||
instance = render(<CodeBlock language="jsx">{text}</CodeBlock>) | ||
}) | ||
|
||
it(`hides lines appropriately`, () => { | ||
expect(instance.queryByText(hidden)).toBeNull() | ||
}) | ||
|
||
it(`highlights lines appropriately`, () => { | ||
const lines = highlighted.split(`\n`) | ||
expect( | ||
instance.container.querySelectorAll(`.gatsby-highlight-code-line`) | ||
).toHaveLength(lines.length) | ||
}) | ||
}) | ||
}) |
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,203 @@ | ||
import normalize from "../normalize" | ||
|
||
describe(`highlighting`, () => { | ||
it(`highlight-start`, () => { | ||
expect( | ||
normalize( | ||
` | ||
// highlight-start | ||
var a = 'b' | ||
var b = 'c' | ||
// highlight-end | ||
`.trim(), | ||
`jsx` | ||
) | ||
).toEqual([expect.any(String), { 0: true, 1: true }]) | ||
}) | ||
|
||
it(`highlight-start, without end`, () => { | ||
expect( | ||
normalize( | ||
` | ||
var a = 'b' | ||
// highlight-start | ||
var b = 'c' | ||
var d = 'e' | ||
`.trim(), | ||
`jsx` | ||
) | ||
).toEqual([expect.any(String), { 1: true, 2: true }]) | ||
}) | ||
|
||
it(`highlight-line`, () => { | ||
expect( | ||
normalize( | ||
` | ||
var a = 'b' // highlight-line | ||
`.trim(), | ||
`jsx` | ||
) | ||
).toEqual([expect.any(String), { 0: true }]) | ||
}) | ||
|
||
it(`highlight-next-line`, () => { | ||
expect( | ||
normalize( | ||
` | ||
var a = 'b' // highlight-next-line | ||
var b = 'c' | ||
`.trim(), | ||
`jsx` | ||
) | ||
).toEqual([expect.any(String), { 1: true }]) | ||
}) | ||
|
||
it(`curly brace format`, () => { | ||
expect( | ||
normalize( | ||
` | ||
\`\`\` | ||
var a = 'i am highlighted' | ||
var b = 'i am not' | ||
\`\`\` | ||
`.trim(), | ||
`jsx{1}` | ||
) | ||
).toEqual([expect.any(String), { 0: true }]) | ||
}) | ||
}) | ||
|
||
describe(`languages`, () => { | ||
it(`handles js`, () => { | ||
expect( | ||
normalize( | ||
` | ||
function () { | ||
alert('hi') /* highlight-linen */ | ||
} | ||
`.trim(), | ||
`html` | ||
) | ||
).toEqual([expect.any(String), { 1: true }]) | ||
}) | ||
|
||
it(`handles html`, () => { | ||
expect( | ||
normalize( | ||
` | ||
<div> | ||
<h1>Oh shit waddup</h1> <!-- highlight-line --> | ||
</div> | ||
`.trim(), | ||
`html` | ||
) | ||
).toEqual([expect.any(String), { 1: true }]) | ||
}) | ||
|
||
it(`handles yaml`, () => { | ||
expect( | ||
normalize( | ||
` | ||
something: true | ||
highlighted: you bedda believe it # highlight-line | ||
`.trim(), | ||
`html` | ||
) | ||
).toEqual([expect.any(String), { 1: true }]) | ||
}) | ||
|
||
it(`handles css`, () => { | ||
expect( | ||
normalize( | ||
` | ||
p { | ||
color: red; // highlight-line | ||
} | ||
`.trim(), | ||
`css` | ||
) | ||
).toEqual([expect.any(String), { 1: true }]) | ||
}) | ||
|
||
it(`handles graphql`, () => { | ||
expect( | ||
normalize( | ||
` | ||
query whatever { | ||
field # highlight-line | ||
} | ||
`.trim(), | ||
`graphql` | ||
) | ||
).toEqual([expect.any(String), { 1: true }]) | ||
}) | ||
}) | ||
|
||
describe(`hiding`, () => { | ||
it(`hide-line`, () => { | ||
expect( | ||
normalize( | ||
` | ||
var a = 'b' // hide-line | ||
var b = 'c' | ||
`.trim(), | ||
`jsx` | ||
) | ||
).toEqual([`var b = 'c'`, expect.any(Object)]) | ||
}) | ||
|
||
it(`hide-start`, () => { | ||
expect( | ||
normalize( | ||
` | ||
// hide-start | ||
var a = 'b' | ||
var b = 'c' | ||
// hide-end | ||
`.trim(), | ||
`jsx` | ||
) | ||
).toEqual([``, expect.any(Object)]) | ||
}) | ||
|
||
it(`hide-start without end`, () => { | ||
expect( | ||
normalize( | ||
` | ||
var a = 'b' | ||
// hide-start | ||
var b = 'c' | ||
var d = 'e' | ||
`.trim(), | ||
`jsx` | ||
) | ||
).toEqual([`var a = 'b'`, expect.any(Object)]) | ||
}) | ||
|
||
describe(`next-line`, () => { | ||
it(`on same line`, () => { | ||
expect( | ||
normalize( | ||
` | ||
var a = 'b' // hide-next-line | ||
var b = 'c' | ||
`.trim(), | ||
`jsx` | ||
) | ||
).toEqual([`var a = 'b'`, expect.any(Object)]) | ||
}) | ||
|
||
it(`on next line`, () => { | ||
expect( | ||
normalize( | ||
` | ||
var a = 'b' | ||
// hide-next-line | ||
var b = 'c' | ||
`.trim(), | ||
`jsx` | ||
) | ||
).toEqual([`var a = 'b'`, expect.any(Object)]) | ||
}) | ||
}) | ||
}) |
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,96 @@ | ||
import React from "react" | ||
import Highlight, { defaultProps } from "prism-react-renderer" | ||
|
||
import Copy from "../copy" | ||
import normalize from "./normalize" | ||
import { fontSizes, radii, space } from "../../utils/presets" | ||
|
||
const getParams = (name = ``) => { | ||
const [lang, params = ``] = name.split(`:`) | ||
return [ | ||
lang | ||
.split(`language-`) | ||
.pop() | ||
.split(`{`) | ||
.shift(), | ||
].concat( | ||
params.split(`&`).reduce((merged, param) => { | ||
const [key, value] = param.split(`=`) | ||
merged[key] = value | ||
return merged | ||
}, {}) | ||
) | ||
} | ||
|
||
/* | ||
* MDX passes the code block as JSX | ||
* we un-wind it a bit to get the string content | ||
* but keep it extensible so it can be used with just children (string) and className | ||
*/ | ||
export default ({ | ||
children, | ||
className = children.props ? children.props.className : ``, | ||
}) => { | ||
const [language, { title = `` }] = getParams(className) | ||
const [content, highlights] = normalize( | ||
children.props && children.props.children | ||
? children.props.children | ||
: children, | ||
className | ||
) | ||
|
||
return ( | ||
<Highlight | ||
{...defaultProps} | ||
code={content} | ||
language={language} | ||
theme={undefined} | ||
> | ||
{({ tokens, getLineProps, getTokenProps }) => ( | ||
<div className="gatsby-highlight"> | ||
{title && ( | ||
<div className="gatsby-highlight-header"> | ||
<div | ||
className="gatsby-code-title" | ||
css={{ fontSize: fontSizes[0] }} | ||
> | ||
{title} | ||
</div> | ||
</div> | ||
)} | ||
<pre className={`language-${language}`}> | ||
<Copy | ||
fileName={title} | ||
css={{ | ||
position: `absolute`, | ||
right: space[1], | ||
top: space[1], | ||
borderRadius: `${radii[2]}px ${radii[2]}px`, | ||
}} | ||
content={content} | ||
/> | ||
{tokens.map((line, i) => { | ||
const lineProps = getLineProps({ line, key: i }) | ||
const className = [lineProps.className] | ||
.concat(highlights[i] && `gatsby-highlight-code-line`) | ||
.filter(Boolean) | ||
.join(` `) | ||
return ( | ||
<div | ||
key={i} | ||
{...Object.assign({}, lineProps, { | ||
className, | ||
})} | ||
> | ||
{line.map((token, key) => ( | ||
<span key={key} {...getTokenProps({ token, key })} /> | ||
))} | ||
</div> | ||
) | ||
})} | ||
</pre> | ||
</div> | ||
)} | ||
</Highlight> | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@DSchau I love it 😍 and plan to use some of that to add that nice copy feature to coding4.gaiama.org too 👍 🚀
By the way
mdx
passes additional space separated props as well so you could get thetitle
fromprops.title
if you pass the title like soinstead of
then it won't end up in className and you wouldn't have to extract
Yet it's working as is and would be quite a pain to migrate all docs I guess 😅
Update: just realized this PR is quite old and my comment might've not worked back in the days ^^
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.
FYI: i found your described behaviour here in the mdx docs:
https://mdxjs.com/guides/live-code#code-block-meta-string
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.
Uh yeah thanks for the link that's where I know this from 😁🙏