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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
import Prism from 'prismjs';
import { useEffect, useMemo } from 'react';
import { useIntl } from 'react-intl';
const PRISM_PLUGINS = [
'autoloader',
'color-scheme',
'command-line',
'copy-to-clipboard',
'diff-highlight',
'inline-color',
'line-highlight',
'line-numbers',
'match-braces',
'normalize-whitespace',
'show-language',
'toolbar',
] as const;
export type PrismPlugin = (typeof PRISM_PLUGINS)[number];
export type DefaultPrismPlugin = Extract<
PrismPlugin,
| 'autoloader'
| 'color-scheme'
| 'copy-to-clipboard'
| 'match-braces'
| 'normalize-whitespace'
| 'show-language'
| 'toolbar'
>;
export type OptionalPrismPlugin = Exclude<PrismPlugin, DefaultPrismPlugin>;
export type PrismLanguage =
| 'apacheconf'
| 'bash'
| 'css'
| 'diff'
| 'docker'
| 'editorconfig'
| 'ejs'
| 'git'
| 'graphql'
| 'html'
| 'ignore'
| 'ini'
| 'javascript'
| 'jsdoc'
| 'json'
| 'jsx'
| 'makefile'
| 'markup'
| 'php'
| 'phpdoc'
| 'regex'
| 'scss'
| 'shell-session'
| 'smarty'
| 'tcl'
| 'toml'
| 'tsx'
| 'twig'
| 'yaml';
export type PrismAttributes = {
'data-prismjs-copy': string;
'data-prismjs-copy-success': string;
'data-prismjs-copy-error': string;
'data-prismjs-color-scheme-dark': string;
'data-prismjs-color-scheme-light': string;
};
export type UsePrismProps = {
language?: PrismLanguage;
plugins: OptionalPrismPlugin[];
};
export type UsePrismReturn = {
attributes: PrismAttributes;
className: string;
};
/**
* Import and configure all given Prism plugins.
*
* @param {PrismPlugin[]} plugins - The Prism plugins to activate.
*/
const loadPrismPlugins = async (plugins: PrismPlugin[]) => {
for (const plugin of plugins) {
try {
if (plugin === 'color-scheme') {
await import(`../plugins/prism-${plugin}`);
} else {
await import(`prismjs/plugins/${plugin}/prism-${plugin}.min.js`);
}
if (plugin === 'autoloader') {
Prism.plugins.autoloader.languages_path = '/prism/';
}
} catch (error) {
console.error('usePrism: an error occurred while loading Prism plugins.');
console.error(error);
}
}
};
/**
* Use Prism and its plugins.
*
* @param {UsePrismProps} props - An object of options.
* @returns {UsePrismReturn} An object of data.
*/
export const usePrism = ({
language,
plugins,
}: UsePrismProps): UsePrismReturn => {
/**
* The order matter. Toolbar must be loaded before some other plugins.
*/
const defaultPlugins: DefaultPrismPlugin[] = useMemo(
() => [
'toolbar',
'autoloader',
'show-language',
'copy-to-clipboard',
'color-scheme',
'match-braces',
'normalize-whitespace',
],
[]
);
useEffect(() => {
loadPrismPlugins([...defaultPlugins, ...plugins]).then(() => {
Prism.highlightAll();
});
}, [defaultPlugins, plugins]);
const defaultClassName = 'match-braces';
const languageClassName = language ? `language-${language}` : '';
const pluginsClassName = plugins.join(' ');
const className = `${defaultClassName} ${pluginsClassName} ${languageClassName}`;
const intl = useIntl();
const copyText = intl.formatMessage({
defaultMessage: 'Copy',
description: 'usePrism: copy button text (not clicked)',
id: '6GySNl',
});
const copiedText = intl.formatMessage({
defaultMessage: 'Copied!',
description: 'usePrism: copy button text (clicked)',
id: 'nsw6Th',
});
const errorText = intl.formatMessage({
defaultMessage: 'Use Ctrl+c to copy',
description: 'usePrism: copy button error text',
id: 'lKhTGM',
});
const darkTheme = intl.formatMessage({
defaultMessage: 'Dark Theme 🌙',
description: 'usePrism: toggle dark theme button text',
id: 'QLisK6',
});
const lightTheme = intl.formatMessage({
defaultMessage: 'Light Theme 🌞',
description: 'usePrism: toggle light theme button text',
id: 'hHVgW3',
});
const attributes = {
'data-prismjs-copy': copyText,
'data-prismjs-copy-success': copiedText,
'data-prismjs-copy-error': errorText,
'data-prismjs-color-scheme-dark': darkTheme,
'data-prismjs-color-scheme-light': lightTheme,
};
return {
attributes,
className,
};
};
|