Skip to content

Commit d92eb11

Browse files
vimodeIFenton
andauthored
Add copy to clipboard in the markdown rendered content (#730)
* add lang for proper code highlighting * feat: add override for markdown code blocks * Removing dollar sign which shouldn't be there for copying --------- Co-authored-by: Isabel Fenton <ifenton@turing.ac.uk>
1 parent 1f1ae00 commit d92eb11

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

frontend/src/components/SingleView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ load_pretrained_model("${url}")`}
114114
</dt>
115115
<CopyToClipboard
116116
codeText={`pip install ${pkg_url}`}
117+
lang="language-bash"
117118
/>
118119
</div>
119120
{scivision_code}

frontend/src/pages/ScivisionPy/content.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The full documentation is [here](https://scivision.readthedocs.io/en/latest/), a
1313
From a terminal, run
1414

1515
```bash
16-
$ pip install scivision
16+
pip install scivision
1717
```
1818

1919
### Load a model

frontend/src/utils/ParseMarkdown.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ import ReactPlayer from 'react-player/youtube'
44
import { PageTitle } from '@/components/Typography'
55
import Markdown from 'markdown-to-jsx'
66
import matter from 'gray-matter'
7+
import CopyToClipboard from '@/components/CopyToClipboard'
8+
9+
type CodeAdapterProps = {
10+
children?: string
11+
className?: string
12+
}
13+
14+
const CodeAdapter = ({ children, className, ...rest }: CodeAdapterProps) => {
15+
const codeText = Array.isArray(children)
16+
? children.join('')
17+
: String(children ?? '')
18+
const lang = className || 'language-text'
19+
20+
return <CopyToClipboard codeText={codeText} lang={lang} {...rest} />
21+
}
722

823
const ParseMarkdown = (props: { markdown: string }) => {
924
const parsedMarkdown = matter(props.markdown)
@@ -17,6 +32,9 @@ const ParseMarkdown = (props: { markdown: string }) => {
1732
overrides: {
1833
Link,
1934
ReactPlayer,
35+
code: {
36+
component: CodeAdapter,
37+
},
2038
},
2139
}}
2240
>

0 commit comments

Comments
 (0)