-
-
Notifications
You must be signed in to change notification settings - Fork 18
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: add PrismFormatted #303
feat: add PrismFormatted #303
Conversation
a3a8700
to
108947c
Compare
108947c
to
3f721f0
Compare
plugins: [ | ||
["transform-react-remove-prop-types", { removeImport: true }], | ||
[ | ||
"prismjs", |
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.
Setup the babel-plugin-prismjs
plugin.
The config is copied from https://github.com/freeCodeCamp/freeCodeCamp/blob/f1931399a1663f134a80744d3bcb6777d261f96c/client/.babelrc.js#L37-L58.
@@ -18,7 +18,7 @@ export const parameters = { | |||
}, | |||
{ | |||
name: "dark-palette", | |||
value: "#0a0a23", | |||
value: "#1b1b32", |
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.
// Stub out CSS as Jest would try to parse the imported stylesheets as JS modules | ||
// and throw an error as it can't transpile the code. | ||
// Ref: https://github.com/jestjs/jest/issues/3094#issuecomment-385164816 | ||
moduleNameMapper: { | ||
"\\.css": "<rootDir>/__mocks__/styleMock.ts", | ||
}, |
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.
@@ -29,7 +29,7 @@ | |||
@apply bg-background-tertiary text-foreground-tertiary; | |||
} | |||
:not(pre) > code { | |||
@apply border-1 border-gray-450; | |||
@apply border-1 border-gray-450 px-[4px] py-[1px]; |
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.
Add some padding to inline code.
Site note (mostly for future me): This :not(pre) > code
rule is to cover the case where the consumers don't use PrismFormatted
to render strings that contain <code>
tags.
@@ -40,6 +40,7 @@ | |||
h6, | |||
p { | |||
margin-bottom: 12.5px; | |||
color: var(--foreground-primary); |
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.
Add a default text color to text elements.
/* Without this, the above selector takes precedence and messes up the answer | ||
padding in night mode */ | ||
.dark-palette .video-quiz-option > pre { | ||
padding: 0; | ||
margin: 0; | ||
} |
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.
I think this rule should be removed as the video-quiz-option
class is specific to /learn. Though I'll need to integrate PrismFormatted into Quiz/QuizQuestion so the rule could possibly be moved there.
I'm keeping the rule for now to be safe.
/** | ||
* PrismFormatted is used to render code blocks with syntax highlighting. | ||
* |
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.
text, | ||
useSpan, | ||
noAria, | ||
codeBlockAriaLabel, |
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.
In /learn, PrismFormatted automatically creates an aria label with i18next
: https://github.com/freeCodeCamp/freeCodeCamp/blob/f1931399a1663f134a80744d3bcb6777d261f96c/client/src/templates/Challenges/utils/index.ts#L70-L75.
fcc/ui doesn't have localization so it needs the string to be passed in.
useSpan, | ||
noAria, | ||
codeBlockAriaLabel, | ||
hasLineNumbers, |
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.
To enable line numbers, we need to pass the line-numbers
class to PrismJS (doc).
The PrismFormatted component in /learn also expects the consumers to pass in the class. But I find this a little error-prone (typo happens), so I decided to turn it into a boolean prop.
@@ -0,0 +1,78 @@ | |||
@import "../colors.css"; | |||
|
|||
.light-palette pre[class*="language-"]::selection, |
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.
The main different between this file and the original one is that:
- The original uses the
:not(.dark-palette)
selector - This file uses the
.light-palette
selector
I guess we use :not(.dark-palette)
in /learn because we need to account for both (.default
and .light-palette
). I think that was the case in the past, but it's probably standardized now, and we just use .light-palette
or .dark-palette
(https://github.com/freeCodeCamp/freeCodeCamp/blob/f1931399a1663f134a80744d3bcb6777d261f96c/client/src/components/layouts/default.tsx#L109-L114).
Note: this .light-palette pre[class*="language-"]::selection
selector brings a discrepancy.
In /learn, we are still having this selector with .default
, which has no effect, and allows PrismJS to apply its color: https://github.com/freeCodeCamp/freeCodeCamp/blob/f1931399a1663f134a80744d3bcb6777d261f96c/client/src/components/layouts/prism.css#L10-L15.
Changing .default
to .light-palette
allows the override to take effect.
a01e0b2
to
0ab27bd
Compare
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.
Nice work, @huyenltnguyen. I'm not the best judge of css changes, but from what I can see everything looks great.
Thanks for the hard work and the comments. |
Checklist:
Update index.md
)This PR adds
PrismFormatted
component to the library.This component is cloned from the main repo (https://github.com/freeCodeCamp/freeCodeCamp/blob/main/client/src/templates/Challenges/components/prism-formatted.tsx) with some small adjustments.
Screenshots