Skip to content

Commit be06f10

Browse files
gganchganchimeg
andauthored
in order to keep formatting for pasting in codeblocks added a new line (#343)
* in order to keep formatting for pasting in codeblocks added a new line * simplified code and deleted unused code for better copy paste behavior Co-authored-by: ganchimeg <[email protected]>
1 parent 5a67f4b commit be06f10

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/components/mdx/code-block.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@ import * as frontMatterParser from "gray-matter"
44

55
import Highlight, { defaultProps } from "prism-react-renderer"
66

7-
import { languageMappings, prismLanguages, transformToken } from "./custom-syntax-highlighting"
7+
import {
8+
languageMappings,
9+
prismLanguages,
10+
transformToken,
11+
} from "./custom-syntax-highlighting"
812

913
// Additional language support (See https://git.io/JJuZX)
1014
import Prism from "prism-react-renderer/prism"
11-
(typeof global !== "undefined" ? global : window).Prism = Prism
15+
;(typeof global !== "undefined" ? global : window).Prism = Prism
1216
require("prismjs/components/prism-toml")
1317
require("prismjs/components/prism-rust")
1418
Prism.languages.sh = prismLanguages.sh
1519

16-
const codeBlockClassName = theme => {
17-
const themeClassName = theme === "dark" ? "" : " CodeBlock-is-light-in-light-theme"
20+
const codeBlockClassName = (theme) => {
21+
const themeClassName =
22+
theme === "dark" ? "" : " CodeBlock-is-light-in-light-theme"
1823
return `CodeBlock CodeBlock-with-rows CodeBlock-scrolls-horizontally${themeClassName}`
1924
}
2025

21-
const addNewlineToEmptyLine = line => {
26+
const addNewlineToEmptyLine = (line) => {
2227
if (line && line.length === 1 && line[0].empty) {
2328
// Improves copy/paste behavior
2429
line[0].content = "\n"
@@ -27,11 +32,11 @@ const addNewlineToEmptyLine = line => {
2732
return line
2833
}
2934

30-
const CodeBlock = props => {
35+
const CodeBlock = (props) => {
3136
const { className, children } = props.children.props
3237

3338
if (props.className) {
34-
return (<pre {...props}/>)
39+
return <pre {...props} />
3540
}
3641

3742
let language = className ? className.split("-")[1] : "js"
@@ -47,16 +52,16 @@ const CodeBlock = props => {
4752

4853
tokens.forEach((token, i) => {
4954
token = transformToken({ token, children, language })
50-
if (token.indexOf("language-") !== 0) token = `token-${ token }`
51-
className += ` CodeBlock--${ token }`
55+
if (token.indexOf("language-") !== 0) token = `token-${token}`
56+
className += ` CodeBlock--${token}`
5257
})
5358

5459
className = className.trim()
5560

5661
return {
5762
key,
5863
children,
59-
className
64+
className,
6065
}
6166
}
6267

@@ -81,31 +86,49 @@ const CodeBlock = props => {
8186
return (
8287
<Highlight {...defaultProps} code={code} language={language}>
8388
{({ className, style, tokens, getLineProps, getTokenProps }) => (
84-
<pre className={codeBlockClassName(theme) + " CodeBlock--language-" + language} language={language}>
89+
<pre
90+
className={
91+
codeBlockClassName(theme) + " CodeBlock--language-" + language
92+
}
93+
language={language}
94+
>
8595
{codeFrontmatter.header && (
8696
<span className="CodeBlock--header">{codeFrontmatter.header}</span>
8797
)}
8898
{codeFrontmatter.filename && !codeFrontmatter.header && (
89-
<span className="CodeBlock--filename">{codeFrontmatter.filename}</span>
99+
<span className="CodeBlock--filename">
100+
{codeFrontmatter.filename}
101+
</span>
90102
)}
91103
<code>
92104
<span className="CodeBlock--rows">
93105
<span className="CodeBlock--rows-content">
94106
{tokens.map((line, i) => (
95107
<span
96108
key={i}
97-
className={"CodeBlock--row" + (
98-
codeFrontmatter.highlight &&
109+
className={
110+
"CodeBlock--row" +
111+
(codeFrontmatter.highlight &&
99112
codeFrontmatter.highlight.length &&
100-
codeFrontmatter.highlight.includes(i + 1) ?
101-
" CodeBlock--row-is-highlighted" : ""
102-
)}
113+
codeFrontmatter.highlight.includes(i + 1)
114+
? " CodeBlock--row-is-highlighted"
115+
: "")
116+
}
103117
>
104118
<span className="CodeBlock--row-indicator"></span>
105119
<span className="CodeBlock--row-content">
106120
{addNewlineToEmptyLine(line).map((token, key) => (
107-
<span key={key} {...tokenProps(getTokenProps({ token, key }))}/>
121+
<span
122+
key={key}
123+
{...tokenProps(getTokenProps({ token, key }))}
124+
/>
108125
))}
126+
{/* Forced newline added to the markup here for each line */}
127+
{/* This doesn't add an additional newline to be shown but */}
128+
{/* makes copying and pasting more consistent */}
129+
<span className="CodeBlock--token-plain">
130+
<br />
131+
</span>
109132
</span>
110133
</span>
111134
))}

0 commit comments

Comments
 (0)