Skip to content

Commit e86ccf0

Browse files
committed
Fix code highlighting in blogs & docs
This provides a better background color (for blogs) and proper light/dark highlighting, and drops the unnecessary generic 'code example' header.
1 parent 6747d99 commit e86ccf0

6 files changed

Lines changed: 123 additions & 21 deletions

File tree

src/components/modules/block-code/block-code.styles.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { styled } from '@/styles';
66
export const StyledBlockCodeWrapper = styled.div`
77
border-radius: 16px;
88
box-shadow: 0 0 0 1px ${({ theme }) => theme.colors.button.border};
9-
background-color: ${({ theme }) => theme.colors.darkGrey};
9+
background-color: var(--prism-bg);
1010
overflow: hidden;
1111
margin-top: 24px;
1212
margin-bottom: 24px;
@@ -27,6 +27,7 @@ export const StyledBlockCodeContent = styled.div`
2727
2828
& code {
2929
color: ${({ theme }) => theme.colors.lightGrey};
30+
background-color: var(--prism-bg);
3031
font-size: 13px;
3132
line-height: 19.5px;
3233
font-family: var(--font-code) !important;
@@ -37,8 +38,8 @@ export const StyledBlockCodeContent = styled.div`
3738

3839
export const StyledInlineCode = styled(Text)`
3940
&&& {
40-
color: ${({ theme }) => theme.colors.lightGrey};
41-
background: ${({ theme }) => theme.colors.darkGrey};
41+
color: var(--prism-text);
42+
background-color: var(--prism-bg);
4243
box-shadow: 0 0 0 1px ${({ theme }) => theme.colors.button.border};
4344
padding: 0 6px;
4445
border-radius: 4px;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface BlockCodeProps {
2-
title: string;
2+
title?: string;
33
content: string | React.ReactNode;
44
language?: string;
55
}

src/components/modules/block-code/components/code.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'prismjs/components/prism-docker';
1010
import 'prismjs/components/prism-go';
1111
import 'prismjs/components/prism-dart';
1212
import 'prismjs/components/prism-yaml';
13-
import 'prismjs/themes/prism-tomorrow.css';
1413
import React, { useEffect } from 'react';
1514

1615
import type { BlockCodeProps } from '../block-code.types';
@@ -37,4 +36,4 @@ export const Code = ({ children, language, title }: Component<Pick<BlockCodeProp
3736
</code>
3837
</pre>
3938
);
40-
};
39+
};

src/components/modules/block-code/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ export const InlineCode = ({ children }: Component) => {
2424
export const BlockCode = ({ title, content, language = 'javascript' }: BlockCodeProps) => {
2525
return (
2626
<StyledBlockCodeWrapper className={dmCodeFont.variable}>
27-
<StyledBlockCodeTitleWrapper>
28-
<Text fontSize="m" fontWeight="bold" color="white">
29-
{title}
30-
</Text>
31-
</StyledBlockCodeTitleWrapper>
27+
{ title &&
28+
<StyledBlockCodeTitleWrapper>
29+
<Text fontSize="m" fontWeight="bold" color="white">
30+
{title}
31+
</Text>
32+
</StyledBlockCodeTitleWrapper>
33+
}
3234
<StyledBlockCodeContent>
3335
<Code title={title} language={language}>
3436
{content}

src/components/sections/rich-text/components/index.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ export const defaultComponents: MDXComponents = {
6767
ol({ children }: Component) {
6868
return <StyledOL>{children}</StyledOL>;
6969
},
70-
code({ children, className }) {
71-
if (!className) {
72-
return <InlineCode>{children}</InlineCode>;
73-
}
70+
// Triple-tick ``` code renders as <pre><code>... blocks, while inline code
71+
// renders as a single <code> element.
72+
pre: ({ children }: { children?: React.ReactNode }) => {
73+
const codeElement = children as React.ReactElement<{ children?: React.ReactNode; className?: string }>;
74+
const { children: content, className } = codeElement.props;
7475

75-
return <BlockCode content={children} language={className} title="Code example" />;
76+
return <pre>
77+
<BlockCode content={content} language={className} />
78+
</pre>;
79+
},
80+
code({ children }) {
81+
return <InlineCode>{children}</InlineCode>;
7682
},
7783
...Icons,
7884
};

src/styles/index.ts

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ export const GlobalStyles = createGlobalStyle`
209209
--background-gradient: url('/images/backgrounds/gradient_dark.svg');
210210
--background-func-gradient: url('/images/backgrounds/func_gradient_dark.png');
211211
--hero-box-shadow: 0 0 20px 52px inset rgb(30 32 40 / 55%);
212+
213+
--prism-bg: var(--ink-black);
214+
--prism-comment: #999;
215+
--prism-punctuation: #ccc;
216+
--prism-tag: #e2777a;
217+
--prism-function-name: #6196cc;
218+
--prism-boolean: #f08d49;
219+
--prism-property: #f8c555;
220+
--prism-selector: #cc99cd;
221+
--prism-string: #7ec699;
222+
--prism-operator: #67cdcc;
223+
--prism-inserted: green;
212224
}
213225
214226
[data-theme="light"] {
@@ -252,6 +264,18 @@ export const GlobalStyles = createGlobalStyle`
252264
--background-gradient: url('/images/backgrounds/gradient_light.svg');
253265
--background-func-gradient: url('/images/backgrounds/func-gradient-dark.png');
254266
--hero-box-shadow: transparent;
267+
268+
--prism-bg: var(--ink-black);
269+
--prism-comment: #7d8b99;
270+
--prism-punctuation: #5f6364;
271+
--prism-tag: #c92c2c;
272+
--prism-function-name: #c92c2c;
273+
--prism-boolean: #c92c2c;
274+
--prism-property: #c92c2c;
275+
--prism-selector: #2f9c0a;
276+
--prism-string: #2f9c0a;
277+
--prism-operator: #a67f59;
278+
--prism-inserted: #2f9c0a;
255279
}
256280
257281
[data-theme='dark'] [data-hide-on-theme='dark'],
@@ -332,10 +356,80 @@ export const GlobalStyles = createGlobalStyle`
332356
white-space: nowrap; /* added line to stop words getting smushed together (as they go onto seperate lines and some screen readers do not understand line feeds as a space */
333357
}
334358
335-
/* Override Auth0's style choices */
336-
.auth0-lock {
337-
/* It's always light - this fixes the browser UI */
338-
color-scheme: light;
339-
font-family: ${theme.fontFamily.dmSans};
359+
/* PrismJS syntax highlighting, based on prism-tomorrow but linked to vars */
360+
pre[class*="language-"] {
361+
background: var(--prism-bg);
362+
color: var(--prism-text);
363+
}
364+
365+
.token.comment,
366+
.token.block-comment,
367+
.token.prolog,
368+
.token.doctype,
369+
.token.cdata {
370+
color: var(--prism-comment);
371+
}
372+
373+
.token.punctuation {
374+
color: var(--prism-punctuation);
375+
}
376+
377+
.token.tag,
378+
.token.attr-name,
379+
.token.namespace,
380+
.token.deleted {
381+
color: var(--prism-tag);
382+
}
383+
384+
.token.function-name {
385+
color: var(--prism-function-name);
386+
}
387+
388+
.token.boolean,
389+
.token.number,
390+
.token.function {
391+
color: var(--prism-boolean);
392+
}
393+
394+
.token.property,
395+
.token.class-name,
396+
.token.constant,
397+
.token.symbol {
398+
color: var(--prism-property);
399+
}
400+
401+
.token.selector,
402+
.token.important,
403+
.token.atrule,
404+
.token.keyword,
405+
.token.builtin {
406+
color: var(--prism-selector);
407+
}
408+
409+
.token.string,
410+
.token.char,
411+
.token.attr-value,
412+
.token.regex,
413+
.token.variable {
414+
color: var(--prism-string);
415+
}
416+
417+
.token.operator,
418+
.token.entity,
419+
.token.url {
420+
color: var(--prism-operator);
421+
}
422+
423+
.token.inserted {
424+
color: var(--prism-inserted);
425+
}
426+
427+
.token.important,
428+
.token.bold {
429+
font-weight: bold;
430+
}
431+
432+
.token.italic {
433+
font-style: italic;
340434
}
341435
`;

0 commit comments

Comments
 (0)