This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
multi-code-block.js
127 lines (111 loc) · 3.03 KB
/
multi-code-block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import PropTypes from 'prop-types';
import React, {createContext, useContext, useMemo} from 'react';
import styled from '@emotion/styled';
import {trackCustomEvent} from '../utils';
export const GA_EVENT_CATEGORY_CODE_BLOCK = 'Code Block';
export const MultiCodeBlockContext = createContext({});
export const SelectedLanguageContext = createContext();
const Container = styled.div({
position: 'relative'
});
const langLabels = {
js: 'JavaScript',
ts: 'TypeScript',
'hooks-js': 'Hooks (JS)',
'hooks-ts': 'Hooks (TS)'
};
function getUnifiedLang(language) {
switch (language) {
case 'js':
case 'jsx':
case 'javascript':
return 'js';
case 'ts':
case 'tsx':
case 'typescript':
return 'ts';
default:
return language;
}
}
function getLang(child) {
return getUnifiedLang(child.props['data-language']);
}
export function MultiCodeBlock(props) {
const {codeBlocks, titles} = useMemo(() => {
const defaultState = {
codeBlocks: {},
titles: {}
};
if (!Array.isArray(props.children)) {
return defaultState;
}
return props.children.reduce((acc, child, index, array) => {
const lang = getLang(child);
if (lang) {
return {
...acc,
codeBlocks: {
...acc.codeBlocks,
[lang]: child
}
};
}
if (child.props.className === 'gatsby-code-title') {
const nextNode = array[index + 1];
const title = child.props.children;
const lang = getLang(nextNode);
if (nextNode && title && lang) {
return {
...acc,
titles: {
...acc.titles,
[lang]: title
}
};
}
}
return acc;
}, defaultState);
}, [props.children]);
const languages = useMemo(() => Object.keys(codeBlocks), [codeBlocks]);
const [selectedLanguage, setSelectedLanguage] = useContext(
SelectedLanguageContext
);
if (!languages.length) {
return props.children;
}
function handleLanguageChange(language) {
setSelectedLanguage(language);
trackCustomEvent({
category: GA_EVENT_CATEGORY_CODE_BLOCK,
action: 'Change language',
label: language
});
}
const defaultLanguage = languages[0];
const renderedLanguage =
selectedLanguage in codeBlocks ? selectedLanguage : defaultLanguage;
return (
<Container>
<MultiCodeBlockContext.Provider
value={{
selectedLanguage: renderedLanguage,
languages: languages.map(lang => ({
lang,
label:
// try to find a label or capitalize the provided lang
langLabels[lang] || lang.charAt(0).toUpperCase() + lang.slice(1)
})),
onLanguageChange: handleLanguageChange
}}
>
<div className="gatsby-code-title">{titles[renderedLanguage]}</div>
{codeBlocks[renderedLanguage]}
</MultiCodeBlockContext.Provider>
</Container>
);
}
MultiCodeBlock.propTypes = {
children: PropTypes.node.isRequired
};