-
Notifications
You must be signed in to change notification settings - Fork 7
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
Initial work on header for code blocks #40
Draft
fcooper8472
wants to merge
1
commit into
main
Choose a base branch
from
code-blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−40
Draft
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ import remarkGfm from 'remark-gfm' | |
import rehypeKatex from 'rehype-katex' | ||
import 'katex/dist/katex.min.css' // `rehype-katex` does not import the CSS for you | ||
|
||
|
||
import { startCase } from 'lodash'; | ||
|
||
import Challenge from './Challenge'; | ||
import Solution from './Solution'; | ||
|
@@ -74,50 +74,75 @@ const challenge = (sectionStr: string) => { | |
} | ||
|
||
function code({node, inline, className, children, ...props}: CodeProps): JSX.Element { | ||
const match = /language-(\w+)/.exec(className || '') | ||
const code = String(children).replace(/\n$/, '') | ||
if (!inline) { | ||
if (match) { | ||
return ( | ||
<div className="relative m-0"> | ||
<CopyToClipboard text={code}> | ||
<button className="group absolute top-0 right-0 bg-transparent text-xs text-grey-700 hover:bg-grey-900 px-2 py-1 rounded flex items-center space-x-1"> | ||
<FaClipboard className='group-hover:text-white' /><span className='group-hover:text-white'>Copy</span> | ||
</button> | ||
</CopyToClipboard> | ||
<SyntaxHighlighter | ||
style={codeStyle} | ||
codeTagProps={{className: "text-sm"}} | ||
language={match[1]} | ||
customStyle={{margin: 0}} | ||
PreTag="div" | ||
{...props} | ||
> | ||
{code} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
} else { | ||
return ( | ||
<div className="p-3 relative"> | ||
<CopyToClipboard text={code}> | ||
<button className="group absolute top-0 right-0 bg-transparent text-xs text-grey-700 hover:text-grey-900 px-2 py-1 rounded flex items-center space-x-1"> | ||
<FaClipboard className='group-hover:text-white' /><span className='group-hover:text-white'>Copy</span> | ||
</button> | ||
</CopyToClipboard> | ||
<code className={className} {...props}> | ||
{children} | ||
</code> | ||
</div> | ||
) | ||
} | ||
} else { | ||
|
||
if (inline) { | ||
return ( | ||
<code className={className} {...props}> | ||
{children} | ||
</code> | ||
) | ||
) | ||
} | ||
|
||
const code = String(children).replace(/\n$/, '') | ||
|
||
const languageMatch = /language-(\w+)/.exec(className || '') | ||
const explicitTitleMatch = /title="([^"]*)"/.exec(String(node?.data?.meta) || ''); | ||
|
||
let languageString = 'text'; | ||
if (languageMatch && languageMatch[1]) { | ||
languageString = languageMatch[1] | ||
} | ||
|
||
let titleString = ''; | ||
if (explicitTitleMatch && explicitTitleMatch[1]) { | ||
titleString = explicitTitleMatch[1]; | ||
} else if (languageMatch && languageMatch[1]) { | ||
titleString = startCase(languageMatch[1]); | ||
} | ||
|
||
const borderTextColor = 'text-black dark:text-white' | ||
const borderHoverColor = 'group-hover:text-neutral-700 dark:group-hover:text-neutral-300' | ||
const borderColor = 'border-neutral-300 dark:border-neutral-700' | ||
|
||
let headerColor = 'bg-neutral-300 dark:bg-neutral-700' | ||
if (titleString == 'Error') { | ||
headerColor = 'bg-red-300 dark:bg-red-700' | ||
} | ||
|
||
const copyToClipboardButton = ( | ||
<CopyToClipboard text={code}> | ||
<button | ||
className={`group absolute top-0 right-0 bg-transparent text-xs ${borderTextColor} px-2 py-1 rounded flex items-center space-x-1`}> | ||
<FaClipboard className={`${borderTextColor} ${borderHoverColor}`}/><span className={`${borderTextColor} ${borderHoverColor}`}>Copy</span> | ||
</button> | ||
</CopyToClipboard> | ||
) | ||
|
||
const headerBox = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again this could be a separate HeaderBox component. Note that I realise I've been a bit lazy and not done this in many places :) so I'm not saying do this now, just that in the future we should build up a library of components we us across the site, so the styling is consistent |
||
<div className={`border rounded-t-md ${headerColor} ${borderColor} mb-4`}> | ||
<div className={`flex items-center justify-between rounded-t-md ${headerColor} pl-1`}> | ||
<h4 className={`w-full mx-2 my-0 ${borderTextColor}`}>{titleString}</h4> | ||
{copyToClipboardButton} | ||
</div> | ||
</div> | ||
) | ||
|
||
return ( | ||
<div className="relative m-0"> | ||
{headerBox} | ||
<SyntaxHighlighter | ||
style={codeStyle} | ||
codeTagProps={{className: "text-sm"}} | ||
language={languageString} | ||
customStyle={{margin: 0}} | ||
showLineNumbers={false} | ||
PreTag="div" | ||
{...props} | ||
> | ||
{code} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
} | ||
|
||
//function transformImageUri(src, alt, title) { | ||
|
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
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.
its generally good to refactor these things into their own react component, e.g.
Button
. We are actually already using a component library called https://www.flowbite-react.com/, which provides such a button, but I wrote this function before I started using it.