Skip to content

Commit 6ff91a4

Browse files
authored
[Fix Accessibility Bug] Refactor code.js to improve syntax highlighting colors (#1305)
## WHAT This pull request includes changes to improve the syntax highlighting in the `src/mdx/code.js` file by introducing a color mapping for different token types and simplifying the code structure. Improvements to syntax highlighting: * [`src/mdx/code.js`](diffhunk://#diff-f3f4e897983cba6337e020c78ef03c356c88f8868bcd19c465eb3c07e8b96625R46-R50): Added a `colorMap` object to define custom colors for specific token types such as comments, parameter variables, and functions. Code simplification: * [`src/mdx/code.js`](diffhunk://#diff-f3f4e897983cba6337e020c78ef03c356c88f8868bcd19c465eb3c07e8b96625L116-R128): Refactored the `Code` component to use the `colorMap` for setting token colors, simplifying the logic for applying styles to tokens. ## WHY ### BEFORE <img width="835" alt="screen shot before" src="https://github.com/user-attachments/assets/5eabe1a8-58f2-4aea-8be2-85c378d47129"> <img width="229" alt="colour red before" src="https://github.com/user-attachments/assets/6fa14348-e459-4adb-8d40-3b82eeb27fa4"> <img width="229" alt="colour blue before" src="https://github.com/user-attachments/assets/8875765f-c878-4081-8055-de51c2af162d"> ### AFTER <img width="835" alt="screen shot after" src="https://github.com/user-attachments/assets/1a9ab2f5-4f65-4f78-840e-b9205e5ca6ca"> <img width="229" alt="colour red after" src="https://github.com/user-attachments/assets/769626c3-358f-4c97-a474-45c6a313c008"> <img width="229" alt="colour blue after" src="https://github.com/user-attachments/assets/474b603f-8b72-4c08-994b-26f6986bdb5d">
1 parent 23167cb commit 6ff91a4

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/mdx/code.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ export const InlineCode = styled.code`
4343
background-color: ${themeGet('colors.neutral.muted')};
4444
border-radius: ${themeGet('radii.2')};
4545
`
46+
const colorMap = {
47+
'token comment': '#747458',
48+
'token function': '#cf3846',
49+
'token parameter variable': '#277d7b',
50+
}
4651

4752
const MonoText = props => <Text sx={{fontFamily: 'mono', fontSize: 1}} {...props} />
4853

@@ -113,18 +118,14 @@ function Code({className = '', prompt, children}) {
113118
<CodeBlock className={highlightClassName} style={style} code={code}>
114119
{tokens.map((line, i) => (
115120
<Box key={i} {...getLineProps({line, key: i})}>
116-
{line.map((token, key) => (
117-
<MonoText
118-
key={key}
119-
{...{
120-
...getTokenProps({token, key}),
121-
style:
122-
getTokenProps({token, key}).className === 'token comment'
123-
? {...getTokenProps({token, key}).style, color: '#747458'}
124-
: getTokenProps({token, key}).style,
125-
}}
126-
/>
127-
))}
121+
{line.map((token, key) => {
122+
const tokenProps = getTokenProps({token, key})
123+
const tokenStyle = colorMap[tokenProps.className]
124+
? {...tokenProps.style, color: colorMap[tokenProps.className]}
125+
: tokenProps.style
126+
127+
return <MonoText key={key} {...tokenProps} style={tokenStyle} />
128+
})}
128129
</Box>
129130
))}
130131
</CodeBlock>

0 commit comments

Comments
 (0)