Skip to content

Commit 8720b08

Browse files
committed
set some groundwork to make it possible to change codeHighlighting theme
1 parent ecac753 commit 8720b08

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

src/MarkdownRenderer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CodeBlock extends React.PureComponent<
3737
{
3838
value: string,
3939
language: string,
40+
theme: string,
4041
},
4142
{
4243
tokenInfo: TokenInfo | Promise<TokenInfo>,
@@ -46,6 +47,7 @@ class CodeBlock extends React.PureComponent<
4647
tokenInfo: fetchTokenInfo({
4748
code: this.props.value,
4849
language: this.props.language,
50+
theme: this.props.theme,
4951
}),
5052
};
5153

@@ -309,7 +311,7 @@ const defaultRenderers = ({
309311
}
310312
return (
311313
<Box margin={{vertical: 'small'}}>
312-
<CodeBlock {...props} />
314+
<CodeBlock theme={Config.codeTheme} {...props} />
313315
</Box>
314316
);
315317
},

src/lib/codeHighlight.js

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,34 @@ export type TokenInfo = {
1919
// Assumes identical code blocks should match the same lanugage
2020
export const TOKEN_INFO_CACHE: Map<string, TokenInfo> = new Map();
2121

22+
function makeCacheKey({
23+
code,
24+
language,
25+
theme,
26+
}: {
27+
code: string,
28+
language: ?string,
29+
theme: string,
30+
}) {
31+
return `${code}:${language || ''}:${theme}`;
32+
}
33+
2234
export function registerTokenInfo({
2335
code,
36+
language,
37+
theme,
2438
tokenInfo,
2539
}: {
2640
code: string,
41+
language: ?string,
42+
theme: string,
2743
tokenInfo: TokenInfo,
2844
}) {
29-
if (TOKEN_INFO_CACHE.get(code)) {
45+
const cacheKey = makeCacheKey({code, language, theme});
46+
if (TOKEN_INFO_CACHE.has(cacheKey)) {
3047
return;
3148
}
32-
TOKEN_INFO_CACHE.set(code, tokenInfo);
49+
TOKEN_INFO_CACHE.set(cacheKey, tokenInfo);
3350
if (TOKEN_INFO_CACHE.size > 5000) {
3451
const firstKey = TOKEN_INFO_CACHE.keys().next().value;
3552
if (firstKey) {
@@ -41,17 +58,20 @@ export function registerTokenInfo({
4158
export function fetchTokenInfo({
4259
code,
4360
language,
61+
theme,
4462
}: {
4563
code: string,
46-
language?: ?string,
64+
language: ?string,
65+
theme: string,
4766
}): TokenInfo | Promise<TokenInfo> {
48-
const fromCache = TOKEN_INFO_CACHE.get(code);
67+
const cacheKey = makeCacheKey({code, language, theme});
68+
const fromCache = TOKEN_INFO_CACHE.get(cacheKey);
4969
if (fromCache) {
5070
return fromCache;
5171
}
5272
const getUrl = `https://sourcecodeshots.com/api/token-info?code=${encodeURIComponent(
5373
code,
54-
)}&theme=${Config.codeTheme}&language=${language ? language : ''}`;
74+
)}&theme=${theme}&language=${language ? language : ''}`;
5575
const resp =
5676
getUrl.length < 2083
5777
? fetch(getUrl, {
@@ -67,13 +87,13 @@ export function fetchTokenInfo({
6787
body: JSON.stringify({
6888
code,
6989
language: language,
70-
theme: Config.codeTheme,
90+
theme,
7191
}),
7292
});
7393
return resp
7494
.then((r) => r.json())
7595
.then((tokenInfo) => {
76-
registerTokenInfo({code, tokenInfo});
96+
registerTokenInfo({code, theme, language, tokenInfo});
7797
return tokenInfo;
7898
});
7999
}
@@ -100,17 +120,26 @@ export async function tokenInfosFromMarkdowns({
100120
markdowns,
101121
}: {
102122
markdowns: Array<string>,
103-
}): {[code: string]: TokenInfo} {
123+
}): Array<{
124+
code: string,
125+
language: ?string,
126+
theme: string,
127+
tokenInfo: TokenInfo,
128+
}> {
129+
const theme = Config.codeTheme;
104130
const nodes = findCodeNodes(markdowns.map(parseMarkdown));
105-
const entries = await Promise.all(
131+
return await Promise.all(
106132
nodes.map(async (node) => {
107133
const code = node.value;
108134
const language = node.lang;
109-
const tokenInfo = await fetchTokenInfo({code: node.value, language});
110-
return [code, tokenInfo];
135+
const tokenInfo = await fetchTokenInfo({
136+
code: node.value,
137+
language,
138+
theme,
139+
});
140+
return {code, language, theme, tokenInfo};
111141
}),
112142
);
113-
return Object.fromEntries(entries);
114143
}
115144

116145
export const defaultThemeColors = {
@@ -131,6 +160,16 @@ export const defaultThemeColors = {
131160
'kimbie-dark': {backgroundColor: '#221a0f', foregroundColor: '#d3af86'},
132161
'dimmed-monokai': {backgroundColor: '#1e1e1e', foregroundColor: '#c5c8c6'},
133162
monokai: {backgroundColor: '#272822', foregroundColor: '#f8f8f2'},
163+
'night-owl': {backgroundColor: '#011627', foregroundColor: '#d6deeb'},
164+
'night-owl-no-italic': {
165+
backgroundColor: '#011627',
166+
foregroundColor: '#d6deeb',
167+
},
168+
'night-owl-light': {backgroundColor: '#FBFBFB', foregroundColor: '#403f53'},
169+
'night-owl-light-no-italic': {
170+
backgroundColor: '#FBFBFB',
171+
foregroundColor: '#403f53',
172+
},
134173
quietlight: {backgroundColor: '#F5F5F5', foregroundColor: 'white'},
135174
red: {backgroundColor: '#390000', foregroundColor: '#F8F8F8'},
136175
'solarized-dark': {backgroundColor: '#002B36', foregroundColor: 'white'},

src/pages/_app.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {useRouter} from 'next/router';
1717
import {query as loginQuery} from '../LoginQuery';
1818
import * as trk from '../lib/trk';
1919
import {registerTokenInfo} from '../lib/codeHighlight';
20+
import Config from '../config';
2021

2122
function AppComponent({
2223
Component,
@@ -99,8 +100,14 @@ function App({Component, pageProps}: any) {
99100
});
100101

101102
if (pageProps.tokenInfos) {
102-
for (const code of Object.keys(pageProps.tokenInfos)) {
103-
registerTokenInfo({code, tokenInfo: pageProps.tokenInfos[code]});
103+
for (const {code, theme, language, tokenInfo} of pageProps.tokenInfos) {
104+
console.log('theme', theme, language, 'language');
105+
registerTokenInfo({
106+
code,
107+
theme,
108+
language,
109+
tokenInfo,
110+
});
104111
}
105112
}
106113

src/pages/api/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import Config from '../../config';
44

5-
export default (req, res) => {
5+
export default (req: any, res: any) => {
66
res.setHeader('access-control-allow-origin', '*');
77
res.json({
88
version: '1.0.0',

0 commit comments

Comments
 (0)