@apify/ui-library 0.65.2 → 0.65.3-featuresyntaxhighlighter-48cb44.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/components/code/code_block/code_block.d.ts.map +1 -1
- package/dist/src/components/code/code_block/code_block.js +14 -20
- package/dist/src/components/code/code_block/code_block.js.map +1 -1
- package/dist/src/components/code/code_block/code_block.styled.d.ts +8 -2
- package/dist/src/components/code/code_block/code_block.styled.d.ts.map +1 -1
- package/dist/src/components/code/code_block/code_block.styled.js +14 -68
- package/dist/src/components/code/code_block/code_block.styled.js.map +1 -1
- package/dist/src/components/code/code_block/utils.d.ts +5 -1
- package/dist/src/components/code/code_block/utils.d.ts.map +1 -1
- package/dist/src/components/code/code_block/utils.js +23 -12
- package/dist/src/components/code/code_block/utils.js.map +1 -1
- package/dist/src/components/code/index.d.ts +1 -0
- package/dist/src/components/code/index.d.ts.map +1 -1
- package/dist/src/components/code/index.js +1 -0
- package/dist/src/components/code/index.js.map +1 -1
- package/dist/src/components/code/one_line_code/one_line_code.d.ts.map +1 -1
- package/dist/src/components/code/one_line_code/one_line_code.js +5 -23
- package/dist/src/components/code/one_line_code/one_line_code.js.map +1 -1
- package/dist/src/components/code/prism_highlighter.d.ts +13 -0
- package/dist/src/components/code/prism_highlighter.d.ts.map +1 -0
- package/dist/src/components/code/prism_highlighter.js +116 -0
- package/dist/src/components/code/prism_highlighter.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/components/code/code_block/code_block.styled.tsx +21 -69
- package/src/components/code/code_block/code_block.tsx +25 -36
- package/src/components/code/code_block/utils.tsx +50 -0
- package/src/components/code/index.ts +1 -0
- package/src/components/code/one_line_code/one_line_code.tsx +9 -28
- package/src/components/code/prism_highlighter.tsx +178 -0
- package/dist/src/components/code/syntax_highlighter.d.ts +0 -15
- package/dist/src/components/code/syntax_highlighter.d.ts.map +0 -1
- package/dist/src/components/code/syntax_highlighter.js +0 -103
- package/dist/src/components/code/syntax_highlighter.js.map +0 -1
- package/src/components/code/code_block/utils.ts +0 -19
- package/src/components/code/syntax_highlighter.tsx +0 -134
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apify/ui-library",
|
|
3
|
-
"version": "0.65.
|
|
3
|
+
"version": "0.65.3-featuresyntaxhighlighter-48cb44.36+8cc051d6e85",
|
|
4
4
|
"description": "React UI library used by apify.com",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -33,9 +33,10 @@
|
|
|
33
33
|
"fast-average-color": "^9.4.0",
|
|
34
34
|
"history": "^5.3.0",
|
|
35
35
|
"lodash": "^4.17.21",
|
|
36
|
+
"prism-react-renderer": "^2.4.1",
|
|
37
|
+
"prismjs": "^1.30.0",
|
|
36
38
|
"query-string": "^8.1.0",
|
|
37
39
|
"react-markdown": "^8.0.5",
|
|
38
|
-
"react-syntax-highlighter": "^15.5.0",
|
|
39
40
|
"rehype-raw": "^6.1.1",
|
|
40
41
|
"rehype-sanitize": "^6.0.0",
|
|
41
42
|
"remark-gfm": "^3.0.1",
|
|
@@ -54,7 +55,6 @@
|
|
|
54
55
|
"@types/lodash": "^4.14.200",
|
|
55
56
|
"@types/node": "^22.10.5",
|
|
56
57
|
"@types/react": "^18.2.74",
|
|
57
|
-
"@types/react-syntax-highlighter": "^15.5.9",
|
|
58
58
|
"@types/styled-components": "^5.1.34",
|
|
59
59
|
"eslint": "^9.22.0",
|
|
60
60
|
"eslint-plugin-import": "2.26.0",
|
|
@@ -65,5 +65,5 @@
|
|
|
65
65
|
"typescript": "^5.1.6",
|
|
66
66
|
"typescript-eslint": "^8.24.0"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "8cc051d6e85edbd8cd3c994fd90725e5b4f5cf8e"
|
|
69
69
|
}
|
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
import styled, { css } from 'styled-components';
|
|
2
2
|
|
|
3
3
|
import { theme } from '../../../design_system/theme.js';
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
4
|
+
import { Box } from '../../box.js';
|
|
5
|
+
import type { SharedTextProps, SharedTextSize } from '../../text/text_shared.js';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
export type SyntaxHighlighterBaseStylesWrapperProps = SharedTextProps & {
|
|
8
|
+
$fullWidth?: boolean;
|
|
9
|
+
$fullHeight?: string;
|
|
10
|
+
$showLineNumbers?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const SyntaxHighlighterBaseStylesWrapper = styled(Box)<SyntaxHighlighterBaseStylesWrapperProps>`
|
|
14
|
+
background-color: ${theme.color.neutral.backgroundMuted} !important;
|
|
15
|
+
border: 1px solid ${theme.color.neutral.border};
|
|
16
|
+
border-radius: ${theme.radius.radius12};
|
|
17
|
+
overflow: hidden;
|
|
18
|
+
|
|
19
|
+
width: 100%;
|
|
20
|
+
${({ $fullWidth }) => !$fullWidth && css`max-width: 860px;`}
|
|
21
|
+
${({ $fullHeight }) => !$fullHeight && css`max-height: 600px;`}
|
|
22
|
+
|
|
23
|
+
background-color: transparent;
|
|
24
|
+
position: relative;
|
|
25
|
+
`;
|
|
8
26
|
|
|
9
27
|
export const CodeBlockWrapper = styled(SyntaxHighlighterBaseStylesWrapper) <{
|
|
10
28
|
$hasHeader: boolean;
|
|
@@ -105,72 +123,6 @@ export const CodeBlockWrapper = styled(SyntaxHighlighterBaseStylesWrapper) <{
|
|
|
105
123
|
}
|
|
106
124
|
}
|
|
107
125
|
|
|
108
|
-
.CodePre {
|
|
109
|
-
z-index: 2;
|
|
110
|
-
padding: ${theme.space.space16} 0 !important;
|
|
111
|
-
|
|
112
|
-
code {
|
|
113
|
-
& > span {
|
|
114
|
-
display: block;
|
|
115
|
-
padding-left: ${({ $showLineNumbers }) => ($showLineNumbers ? theme.space.space8 : theme.space.space16)};
|
|
116
|
-
padding-right: ${theme.space.space16};
|
|
117
|
-
&:hover {
|
|
118
|
-
background-color: ${HIGHLIGHT_BACKGROUND_COLOR} !important;
|
|
119
|
-
border-left: 6px solid ${theme.color.neutral.border} !important;
|
|
120
|
-
padding-left: ${({ $showLineNumbers }) => ($showLineNumbers ? '0.2' : '1')}rem;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Common styles
|
|
125
|
-
[data*='bash']{
|
|
126
|
-
&::before {
|
|
127
|
-
color: ${theme.color.lavender.base};
|
|
128
|
-
padding-right: ${theme.space.space16};
|
|
129
|
-
min-width: ${theme.space.space24};
|
|
130
|
-
user-select: none;
|
|
131
|
-
-webkit-user-select: none;
|
|
132
|
-
font-size: 1em;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
[data*='bashStartCommand'] {
|
|
137
|
-
&::before {
|
|
138
|
-
content: '$';
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
[data*='bashCommand'] {
|
|
143
|
-
&::before {
|
|
144
|
-
content: '>';
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
[data*='bashEmptyLine'] {
|
|
149
|
-
&::before {
|
|
150
|
-
content: ' ';
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
[data*='highlighted'] {
|
|
155
|
-
background-color: ${HIGHLIGHT_BACKGROUND_COLOR} !important;
|
|
156
|
-
border-left: 6px solid ${theme.color.neutral.border} !important;
|
|
157
|
-
padding-left: ${({ $showLineNumbers }) => ($showLineNumbers ? '0.2' : '1')}rem;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
.linenumber {
|
|
162
|
-
color: ${theme.color.neutral.textSubtle} !important;
|
|
163
|
-
padding-right: ${theme.space.space16} !important;
|
|
164
|
-
min-width: ${theme.space.space24} !important;
|
|
165
|
-
box-sizing: unset !important;
|
|
166
|
-
user-select: none;
|
|
167
|
-
-webkit-user-select: none;
|
|
168
|
-
|
|
169
|
-
${({ $showLineNumbers }) => (!$showLineNumbers && css`display: none !important;`)}
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
126
|
|
|
175
127
|
.CodeBlock-TopRightActionsWrapper {
|
|
176
128
|
position: absolute;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import clsx from 'clsx';
|
|
2
|
-
import React, {
|
|
2
|
+
import React, { useMemo, useRef, useState } from 'react';
|
|
3
3
|
|
|
4
4
|
import type { RegularBoxProps } from '../../box.js';
|
|
5
5
|
import { ColorWheelGradient } from '../../color_wheel_gradient.js';
|
|
@@ -7,9 +7,9 @@ import { Link } from '../../link.js';
|
|
|
7
7
|
import { Text } from '../../text/index.js';
|
|
8
8
|
import type { SharedTextSize } from '../../text/text_shared.js';
|
|
9
9
|
import { ActionButton, CopyButton } from '../action_button.js';
|
|
10
|
-
import {
|
|
10
|
+
import { PrismSyntaxHighlighter } from '../prism_highlighter.js';
|
|
11
11
|
import { CodeBlockWrapper } from './code_block.styled.js';
|
|
12
|
-
import {
|
|
12
|
+
import { getBashLinePrefixes, getNumberLinePrefixes } from './utils.js';
|
|
13
13
|
|
|
14
14
|
type CodeTab = {
|
|
15
15
|
key: string;
|
|
@@ -149,9 +149,7 @@ export const CodeBlock = ({
|
|
|
149
149
|
|
|
150
150
|
const isBash = currentLanguage === 'bash';
|
|
151
151
|
|
|
152
|
-
const showLineNumbers = !hideLineNumbers
|
|
153
|
-
&& !LANGUAGES_WITHOUT_LINE_NUMBERS.includes(currentLanguage ?? '');
|
|
154
|
-
const showBashPrompt = isBash && !hideBashPromptPrefixes;
|
|
152
|
+
const showLineNumbers = !hideLineNumbers && !LANGUAGES_WITHOUT_LINE_NUMBERS.includes(currentLanguage ?? '');
|
|
155
153
|
const showBashHeader = isBash && !hideBashHeader && !isMultiTab;
|
|
156
154
|
|
|
157
155
|
const showHeader = isMultiTab || !!title || showBashHeader;
|
|
@@ -164,28 +162,20 @@ export const CodeBlock = ({
|
|
|
164
162
|
syntaxHighlighterRef.current?.scrollTo(0, 0);
|
|
165
163
|
};
|
|
166
164
|
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (showBashPrompt) {
|
|
182
|
-
data.push(currentBashLinesPrefixes[lineNumber - 1]);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return { data: data.join(' ') };
|
|
186
|
-
},
|
|
187
|
-
[currentHighlightLines, showBashPrompt, currentBashLinesPrefixes],
|
|
188
|
-
);
|
|
165
|
+
const showBashPrompt = isBash && !hideBashPromptPrefixes;
|
|
166
|
+
const effectiveBashCommandsStart = currentTab?.bashCommandsStart ?? bashCommandsStart;
|
|
167
|
+
|
|
168
|
+
const linePrefixes = useMemo(() => {
|
|
169
|
+
if (showBashPrompt) {
|
|
170
|
+
return getBashLinePrefixes(currentCode, effectiveBashCommandsStart);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (showLineNumbers) {
|
|
174
|
+
return getNumberLinePrefixes(currentCode);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {};
|
|
178
|
+
}, [showLineNumbers, showBashPrompt, currentCode, effectiveBashCommandsStart]);
|
|
189
179
|
|
|
190
180
|
return (
|
|
191
181
|
<CodeBlockWrapper
|
|
@@ -208,16 +198,15 @@ export const CodeBlock = ({
|
|
|
208
198
|
/>
|
|
209
199
|
)}
|
|
210
200
|
|
|
211
|
-
<
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
201
|
+
<PrismSyntaxHighlighter
|
|
202
|
+
code={currentCode}
|
|
203
|
+
linePrefixes={linePrefixes}
|
|
204
|
+
highlightLines={currentHighlightLines}
|
|
205
|
+
language={currentLanguage || 'text'}
|
|
215
206
|
size={size}
|
|
216
|
-
lineProps={getLineProps}
|
|
217
207
|
ref={syntaxHighlighterRef}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
</SyntaxHighlighter>
|
|
208
|
+
isSingleLine={false}
|
|
209
|
+
/>
|
|
221
210
|
|
|
222
211
|
<div className="CodeBlock-TopRightActionsWrapper">
|
|
223
212
|
{showActionButton && <ActionButton successStyle onClick={onActionButtonClick}>{actionButtonLabel ?? 'Run on Apify'}</ActionButton>}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
import { theme } from '../../../design_system/theme.js';
|
|
4
|
+
import { CodeHighlighterLinePrefix } from '../prism_highlighter.js';
|
|
5
|
+
|
|
6
|
+
// input: [2, 4, 5, 6, 8, 10, 11, 12]
|
|
7
|
+
// returns string indicating what type of line it is (bashStartCommand, bashCommand, bashEmptyLine)
|
|
8
|
+
// commands start -> bashStartCommand
|
|
9
|
+
// every next line after command start until next command start or empty line -> bashCommand
|
|
10
|
+
export const getBashLinePrefixes = (code: string, bashCommandsStart: number[] = []) => {
|
|
11
|
+
const lines = code.split('\n').map((line) => line.trim());
|
|
12
|
+
|
|
13
|
+
const bashLinePrefixesArray = lines.map((line, i) => {
|
|
14
|
+
const isEmptyLine = line.startsWith('#') || !line;
|
|
15
|
+
|
|
16
|
+
// by default each line that is not empty or comment is a command start
|
|
17
|
+
if (bashCommandsStart.length === 0) {
|
|
18
|
+
return isEmptyLine ? (
|
|
19
|
+
<CodeHighlighterLinePrefix />
|
|
20
|
+
) : (
|
|
21
|
+
<CodeHighlighterLinePrefix color={theme.color.lavender.base}>$</CodeHighlighterLinePrefix>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (bashCommandsStart.includes(i + 1)) {
|
|
26
|
+
return (
|
|
27
|
+
<CodeHighlighterLinePrefix color={theme.color.lavender.base}>$</CodeHighlighterLinePrefix>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (isEmptyLine) {
|
|
32
|
+
return (
|
|
33
|
+
<CodeHighlighterLinePrefix />
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return (
|
|
38
|
+
<CodeHighlighterLinePrefix color={theme.color.lavender.base}><</CodeHighlighterLinePrefix>
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return Object.fromEntries(bashLinePrefixesArray.map((linePrefix, i) => [i + 1, linePrefix]));
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const getNumberLinePrefixes = (code: string): Record<number, ReactNode> => {
|
|
46
|
+
const numberOfLines = code.split('\n').length;
|
|
47
|
+
const numbersArray = Array.from({ length: numberOfLines }, (_, i) => i + 1);
|
|
48
|
+
|
|
49
|
+
return Object.fromEntries(numbersArray.map((number) => [number, <CodeHighlighterLinePrefix>{number}</CodeHighlighterLinePrefix>]));
|
|
50
|
+
};
|
|
@@ -8,8 +8,9 @@ import type { BoxProps } from '../../box.js';
|
|
|
8
8
|
import { Text } from '../../text/index.js';
|
|
9
9
|
import type { SharedTextSize } from '../../text/text_shared.js';
|
|
10
10
|
import { ActionButton, CopyButton } from '../action_button.js';
|
|
11
|
-
import type { SyntaxHighlighterBaseStylesWrapperProps } from '../
|
|
12
|
-
import {
|
|
11
|
+
import type { SyntaxHighlighterBaseStylesWrapperProps } from '../code_block/code_block.styled.js';
|
|
12
|
+
import { SyntaxHighlighterBaseStylesWrapper } from '../code_block/code_block.styled.js';
|
|
13
|
+
import { PrismSyntaxHighlighter } from '../prism_highlighter.js';
|
|
13
14
|
|
|
14
15
|
export const oneLineCodeClassNames = {
|
|
15
16
|
gradientWrapper: 'OneLineCode-GradientWrapper',
|
|
@@ -92,6 +93,7 @@ const OneLineCodeWrapper = styled(SyntaxHighlighterBaseStylesWrapper)<OneLineCod
|
|
|
92
93
|
top: 0;
|
|
93
94
|
bottom: 0;
|
|
94
95
|
width: 4rem;
|
|
96
|
+
z-index: 5;
|
|
95
97
|
background: linear-gradient(
|
|
96
98
|
to right,
|
|
97
99
|
transparent,
|
|
@@ -114,32 +116,12 @@ const OneLineCodeWrapper = styled(SyntaxHighlighterBaseStylesWrapper)<OneLineCod
|
|
|
114
116
|
scrollbar-color: ${theme.color.neutral.border} transparent;
|
|
115
117
|
|
|
116
118
|
|
|
117
|
-
|
|
119
|
+
pre {
|
|
118
120
|
overflow: unset !important;
|
|
119
121
|
display: flex;
|
|
120
122
|
flex: 1;
|
|
121
123
|
align-items: center;
|
|
122
124
|
height: 2.8rem;
|
|
123
|
-
|
|
124
|
-
code {
|
|
125
|
-
& > span {
|
|
126
|
-
text-overflow: ellipsis;
|
|
127
|
-
overflow: hidden;
|
|
128
|
-
white-space: nowrap;
|
|
129
|
-
vertical-align: text-top;
|
|
130
|
-
${({ $showBashPrefixes }) => $showBashPrefixes && css`
|
|
131
|
-
&::before {
|
|
132
|
-
color: ${theme.color.lavender.base};
|
|
133
|
-
padding-right: ${theme.space.space16};
|
|
134
|
-
min-width: ${theme.space.space24};
|
|
135
|
-
user-select: none;
|
|
136
|
-
-webkit-user-select: none;
|
|
137
|
-
content: "$";
|
|
138
|
-
font-size: 1em;
|
|
139
|
-
}
|
|
140
|
-
`}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
125
|
}
|
|
144
126
|
}
|
|
145
127
|
`;
|
|
@@ -197,13 +179,12 @@ export function OneLineCode({
|
|
|
197
179
|
{httpMethod}
|
|
198
180
|
</Text>
|
|
199
181
|
)}
|
|
200
|
-
<
|
|
182
|
+
<PrismSyntaxHighlighter
|
|
201
183
|
language={language}
|
|
202
184
|
size={size}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
</SyntaxHighlighter>
|
|
185
|
+
code={code}
|
|
186
|
+
isSingleLine={true}
|
|
187
|
+
/>
|
|
207
188
|
</div>
|
|
208
189
|
<div className={oneLineCodeClassNames.gradient} />
|
|
209
190
|
</div>
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import clsx from 'clsx';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
import type { HighlightProps } from 'prism-react-renderer';
|
|
4
|
+
import { Highlight, Prism, themes } from 'prism-react-renderer';
|
|
5
|
+
import type { ReactNode } from 'react';
|
|
6
|
+
import { forwardRef, useRef } from 'react';
|
|
7
|
+
import styled, { css } from 'styled-components';
|
|
8
|
+
|
|
9
|
+
import { theme } from '../../design_system/theme.js';
|
|
10
|
+
import { useSharedUiDependencies } from '../../ui_dependency_provider.js';
|
|
11
|
+
import { Text } from '../text/index.js';
|
|
12
|
+
import type { SharedTextSize } from '../text/text_shared.js';
|
|
13
|
+
|
|
14
|
+
(typeof global !== 'undefined' ? global : window).Prism = Prism;
|
|
15
|
+
|
|
16
|
+
const HIGHLIGHT_BACKGROUND_COLOR = 'rgba(22, 114, 235, 0.05)';
|
|
17
|
+
|
|
18
|
+
const loadLanguages = async () => {
|
|
19
|
+
await Promise.all([
|
|
20
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
21
|
+
import('prismjs/components/prism-bash.js'),
|
|
22
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
23
|
+
import('prismjs/components/prism-rust.js'),
|
|
24
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
25
|
+
import('prismjs/components/prism-bash.js'),
|
|
26
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
27
|
+
import('prismjs/components/prism-docker.js'),
|
|
28
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
29
|
+
import('prismjs/components/prism-http.js'),
|
|
30
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
31
|
+
import('prismjs/components/prism-javascript.js'),
|
|
32
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
33
|
+
import('prismjs/components/prism-json.js'),
|
|
34
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
35
|
+
import('prismjs/components/prism-markup.js'),
|
|
36
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
37
|
+
import('prismjs/components/prism-python.js'),
|
|
38
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
39
|
+
import('prismjs/components/prism-typescript.js'),
|
|
40
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
41
|
+
import('prismjs/components/prism-xml-doc.js'),
|
|
42
|
+
// @ts-expect-error The library is badly typed and does not export the type
|
|
43
|
+
import('prismjs/components/prism-yaml.js'),
|
|
44
|
+
]);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
void loadLanguages();
|
|
48
|
+
|
|
49
|
+
type StyledPreProps = {
|
|
50
|
+
$hasLinePrefixes: boolean,
|
|
51
|
+
$isSingleLine: boolean
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const StyledPre = styled.pre<StyledPreProps>`
|
|
55
|
+
width: 100%;
|
|
56
|
+
position: relative;
|
|
57
|
+
overflow: auto;
|
|
58
|
+
z-index: 2;
|
|
59
|
+
padding: ${({ $isSingleLine }) => ($isSingleLine ? 0 : `${theme.space.space16} 0`)};
|
|
60
|
+
margin: 0;
|
|
61
|
+
|
|
62
|
+
*,
|
|
63
|
+
*::before,
|
|
64
|
+
*::after {
|
|
65
|
+
text-shadow: none !important;
|
|
66
|
+
font-style: normal !important;
|
|
67
|
+
font-family: inherit !important;
|
|
68
|
+
font-size: inherit !important;
|
|
69
|
+
font-weight: inherit !important;
|
|
70
|
+
line-height: inherit !important;
|
|
71
|
+
background-color: transparent !important;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
code {
|
|
75
|
+
min-width: 100%;
|
|
76
|
+
display: inline-block;
|
|
77
|
+
vertical-align: middle;
|
|
78
|
+
|
|
79
|
+
& > div {
|
|
80
|
+
display: block;
|
|
81
|
+
|
|
82
|
+
&.highlighted {
|
|
83
|
+
background-color: ${HIGHLIGHT_BACKGROUND_COLOR} !important;
|
|
84
|
+
border-left: 6px solid ${theme.color.neutral.border} !important;
|
|
85
|
+
padding-left: ${({ $hasLinePrefixes }) => ($hasLinePrefixes ? '0.2' : '1')}rem;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
${({ $isSingleLine }) => !$isSingleLine && css<{$hasLinePrefixes: boolean}>`
|
|
89
|
+
&:hover {
|
|
90
|
+
background-color: ${HIGHLIGHT_BACKGROUND_COLOR} !important;
|
|
91
|
+
border-left: 6px solid ${theme.color.neutral.border} !important;
|
|
92
|
+
padding-left: ${({ $hasLinePrefixes }) => ($hasLinePrefixes ? '0.2' : '1')}rem;
|
|
93
|
+
}
|
|
94
|
+
`}
|
|
95
|
+
|
|
96
|
+
${({ $isSingleLine }) => ($isSingleLine ? css<StyledPreProps>`
|
|
97
|
+
text-overflow: ellipsis;
|
|
98
|
+
overflow: hidden;
|
|
99
|
+
white-space: nowrap;
|
|
100
|
+
vertical-align: text-top;
|
|
101
|
+
` : css<StyledPreProps>`
|
|
102
|
+
padding-left: ${({ $hasLinePrefixes }) => ($hasLinePrefixes ? theme.space.space8 : theme.space.space16)};
|
|
103
|
+
padding-right: ${theme.space.space16};
|
|
104
|
+
|
|
105
|
+
&:hover {
|
|
106
|
+
background-color: ${HIGHLIGHT_BACKGROUND_COLOR} !important;
|
|
107
|
+
border-left: 6px solid ${theme.color.neutral.border} !important;
|
|
108
|
+
padding-left: ${({ $hasLinePrefixes }) => ($hasLinePrefixes ? '0.2' : '1')}rem;
|
|
109
|
+
}
|
|
110
|
+
`)}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
`;
|
|
114
|
+
|
|
115
|
+
export const CodeHighlighterLinePrefix = styled(Text)`
|
|
116
|
+
display: inline-block;
|
|
117
|
+
margin-right: ${theme.space.space16};
|
|
118
|
+
text-align: right;
|
|
119
|
+
min-width: ${theme.space.space24};
|
|
120
|
+
color: ${({ color }) => color || theme.color.neutral.textSubtle};
|
|
121
|
+
user-select: none;
|
|
122
|
+
-webkit-user-select: none;
|
|
123
|
+
`;
|
|
124
|
+
|
|
125
|
+
type SyntaxHighlighterProps = Omit<HighlightProps, 'children' | 'language'> & {
|
|
126
|
+
size?: SharedTextSize,
|
|
127
|
+
linePrefixes?: Record<string, ReactNode>,
|
|
128
|
+
highlightLines?: number[],
|
|
129
|
+
language?: string,
|
|
130
|
+
className?: string,
|
|
131
|
+
isSingleLine: boolean,
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const PrismSyntaxHighlighter = forwardRef<HTMLPreElement, SyntaxHighlighterProps>(({
|
|
135
|
+
size = 'regular',
|
|
136
|
+
linePrefixes = {},
|
|
137
|
+
highlightLines = [],
|
|
138
|
+
language = 'javascript',
|
|
139
|
+
className,
|
|
140
|
+
isSingleLine,
|
|
141
|
+
...rest
|
|
142
|
+
}, ref) => {
|
|
143
|
+
const { uiTheme } = useSharedUiDependencies();
|
|
144
|
+
const hasLinePrefixes = !_.isEmpty(linePrefixes);
|
|
145
|
+
const highlightedLinesObject = useRef<Record<number, boolean>>(Object.fromEntries(highlightLines.map((e) => [e, true])));
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<Highlight
|
|
149
|
+
theme={uiTheme === 'DARK' ? themes.nightOwl : themes.oneLight}
|
|
150
|
+
language={language}
|
|
151
|
+
{...rest}
|
|
152
|
+
>
|
|
153
|
+
{({ className: prismClassName, tokens, getLineProps, getTokenProps }) => (
|
|
154
|
+
<StyledPre
|
|
155
|
+
className={clsx(className, prismClassName)}
|
|
156
|
+
$hasLinePrefixes={hasLinePrefixes}
|
|
157
|
+
$isSingleLine={isSingleLine}
|
|
158
|
+
ref={ref}
|
|
159
|
+
>
|
|
160
|
+
<Text
|
|
161
|
+
size={size}
|
|
162
|
+
type='code'
|
|
163
|
+
as='code'
|
|
164
|
+
>
|
|
165
|
+
{tokens.map((line, i) => (
|
|
166
|
+
<div key={i} {...getLineProps({ line, className: highlightedLinesObject.current[i + 1] ? 'highlighted' : '' })}>
|
|
167
|
+
{hasLinePrefixes && (linePrefixes[i + 1] || <CodeHighlighterLinePrefix />)}
|
|
168
|
+
{line.map((token, key) => (
|
|
169
|
+
<span key={key} {...getTokenProps({ token })} />
|
|
170
|
+
))}
|
|
171
|
+
</div>
|
|
172
|
+
))}
|
|
173
|
+
</Text>
|
|
174
|
+
</StyledPre>
|
|
175
|
+
)}
|
|
176
|
+
</Highlight>
|
|
177
|
+
);
|
|
178
|
+
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { SyntaxHighlighterProps as ReactSyntaxHighlighterProps } from 'react-syntax-highlighter';
|
|
2
|
-
import { type SharedTextProps } from '../text/index.js';
|
|
3
|
-
import type { SharedTextSize } from '../text/text_shared.js';
|
|
4
|
-
type SyntaxHighlighterProps = ReactSyntaxHighlighterProps & {
|
|
5
|
-
size: SharedTextSize;
|
|
6
|
-
};
|
|
7
|
-
export declare const SyntaxHighlighter: import("react").ForwardRefExoticComponent<Omit<SyntaxHighlighterProps, "ref"> & import("react").RefAttributes<HTMLPreElement>>;
|
|
8
|
-
export type SyntaxHighlighterBaseStylesWrapperProps = SharedTextProps & {
|
|
9
|
-
$fullWidth?: boolean;
|
|
10
|
-
$fullHeight?: string;
|
|
11
|
-
$showLineNumbers?: boolean;
|
|
12
|
-
};
|
|
13
|
-
export declare const SyntaxHighlighterBaseStylesWrapper: import("styled-components").StyledComponent<import("react").ForwardRefExoticComponent<import("../box.js").BoxProps & import("react").RefAttributes<HTMLElement>>, any, SyntaxHighlighterBaseStylesWrapperProps, never>;
|
|
14
|
-
export {};
|
|
15
|
-
//# sourceMappingURL=syntax_highlighter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"syntax_highlighter.d.ts","sourceRoot":"","sources":["../../../../src/components/code/syntax_highlighter.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,sBAAsB,IAAI,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAiCtG,OAAO,EAAE,KAAK,eAAe,EAAQ,MAAM,kBAAkB,CAAC;AAC9D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAoD7D,KAAK,sBAAsB,GAAG,2BAA2B,GAAG;IAAE,IAAI,EAAE,cAAc,CAAA;CAAE,CAAC;AAErF,eAAO,MAAM,iBAAiB,gIAsB5B,CAAC;AAEH,MAAM,MAAM,uCAAuC,GAAG,eAAe,GAAG;IACpE,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAA;AAED,eAAO,MAAM,kCAAkC,wNAY9C,CAAC"}
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
3
|
-
import { forwardRef } from 'react';
|
|
4
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
5
|
-
import bash from 'react-syntax-highlighter/dist/esm/languages/prism/bash.js';
|
|
6
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
7
|
-
import dockerFile from 'react-syntax-highlighter/dist/esm/languages/prism/docker.js';
|
|
8
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
9
|
-
import http from 'react-syntax-highlighter/dist/esm/languages/prism/http.js';
|
|
10
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
11
|
-
import js from 'react-syntax-highlighter/dist/esm/languages/prism/javascript.js';
|
|
12
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
13
|
-
import json from 'react-syntax-highlighter/dist/esm/languages/prism/json.js';
|
|
14
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
15
|
-
import html from 'react-syntax-highlighter/dist/esm/languages/prism/markup.js';
|
|
16
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
17
|
-
import python from 'react-syntax-highlighter/dist/esm/languages/prism/python.js';
|
|
18
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
19
|
-
import ts from 'react-syntax-highlighter/dist/esm/languages/prism/typescript.js';
|
|
20
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
21
|
-
import xml from 'react-syntax-highlighter/dist/esm/languages/prism/xml-doc.js';
|
|
22
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
23
|
-
import yaml from 'react-syntax-highlighter/dist/esm/languages/prism/yaml.js';
|
|
24
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
25
|
-
import ReactSyntaxHighlighter from 'react-syntax-highlighter/dist/esm/prism-light.js';
|
|
26
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
27
|
-
import dark from 'react-syntax-highlighter/dist/esm/styles/prism/night-owl.js';
|
|
28
|
-
// @ts-ignore The library is badly typed and does not export the type
|
|
29
|
-
import light from 'react-syntax-highlighter/dist/esm/styles/prism/prism.js';
|
|
30
|
-
/* tslint:enable */
|
|
31
|
-
import styled, { css } from 'styled-components';
|
|
32
|
-
import { theme } from '../../design_system/theme.js';
|
|
33
|
-
import { useSharedUiDependencies } from '../../ui_dependency_provider.js';
|
|
34
|
-
import { Box } from '../box.js';
|
|
35
|
-
import { Text } from '../text/index.js';
|
|
36
|
-
ReactSyntaxHighlighter.registerLanguage('typescript', ts);
|
|
37
|
-
ReactSyntaxHighlighter.registerLanguage('javascript', js);
|
|
38
|
-
ReactSyntaxHighlighter.registerLanguage('json', json);
|
|
39
|
-
ReactSyntaxHighlighter.registerLanguage('bash', bash);
|
|
40
|
-
ReactSyntaxHighlighter.registerLanguage('docker', dockerFile);
|
|
41
|
-
ReactSyntaxHighlighter.registerLanguage('html', html);
|
|
42
|
-
ReactSyntaxHighlighter.registerLanguage('xml', xml);
|
|
43
|
-
ReactSyntaxHighlighter.registerLanguage('yaml', yaml);
|
|
44
|
-
ReactSyntaxHighlighter.registerLanguage('python', python);
|
|
45
|
-
ReactSyntaxHighlighter.registerLanguage('http', http);
|
|
46
|
-
ReactSyntaxHighlighter.registerLanguage('jsonp', json);
|
|
47
|
-
ReactSyntaxHighlighter.registerLanguage('jsonl', json);
|
|
48
|
-
ReactSyntaxHighlighter.registerLanguage('rss', xml);
|
|
49
|
-
const StyledPre = styled(Text) `
|
|
50
|
-
width: 100%;
|
|
51
|
-
position: relative;
|
|
52
|
-
|
|
53
|
-
*,
|
|
54
|
-
*::before,
|
|
55
|
-
*::after {
|
|
56
|
-
text-shadow: none !important;
|
|
57
|
-
font-style: normal !important;
|
|
58
|
-
font-family: inherit !important;
|
|
59
|
-
font-size: inherit !important;
|
|
60
|
-
font-weight: inherit !important;
|
|
61
|
-
line-height: inherit !important;
|
|
62
|
-
background-color: transparent !important;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
code {
|
|
66
|
-
min-width: 100%;
|
|
67
|
-
display: inline-block;
|
|
68
|
-
vertical-align: middle;
|
|
69
|
-
overflow: hidden;
|
|
70
|
-
}
|
|
71
|
-
`;
|
|
72
|
-
// Syntax highlighter uses inline style a lot which makes it hard to override them. To do so, we make the library not
|
|
73
|
-
// use styles that we wish to set on our own (font-based styles, spacing, background, ...),
|
|
74
|
-
const inlineCssReset = {
|
|
75
|
-
fontFamily: undefined,
|
|
76
|
-
fontSize: undefined,
|
|
77
|
-
fontWeight: undefined,
|
|
78
|
-
lineHeight: undefined,
|
|
79
|
-
background: undefined,
|
|
80
|
-
margin: undefined,
|
|
81
|
-
padding: undefined,
|
|
82
|
-
};
|
|
83
|
-
export const SyntaxHighlighter = forwardRef(({ size, children, ...rest }, ref) => {
|
|
84
|
-
const { uiTheme } = useSharedUiDependencies();
|
|
85
|
-
const PreTag = (preProps) => {
|
|
86
|
-
return _jsx(StyledPre, { ...preProps, size: size, forwardedAs: 'pre', type: 'code', ref: ref });
|
|
87
|
-
};
|
|
88
|
-
return (_jsx(ReactSyntaxHighlighter, { PreTag: PreTag, customStyle: inlineCssReset, className: "CodePre", style: uiTheme === 'DARK' ? dark : light, ...rest, children: children }));
|
|
89
|
-
});
|
|
90
|
-
export const SyntaxHighlighterBaseStylesWrapper = styled(Box) `
|
|
91
|
-
background-color: ${theme.color.neutral.backgroundMuted} !important;
|
|
92
|
-
border: 1px solid ${theme.color.neutral.border};
|
|
93
|
-
border-radius: ${theme.radius.radius12};
|
|
94
|
-
overflow: hidden;
|
|
95
|
-
|
|
96
|
-
width: 100%;
|
|
97
|
-
${({ $fullWidth }) => !$fullWidth && css `max-width: 860px;`}
|
|
98
|
-
${({ $fullHeight }) => !$fullHeight && css `max-height: 600px;`}
|
|
99
|
-
|
|
100
|
-
background-color: transparent;
|
|
101
|
-
position: relative;
|
|
102
|
-
`;
|
|
103
|
-
//# sourceMappingURL=syntax_highlighter.js.map
|