@apify/ui-library 0.65.3-featuresyntaxhighlighter-48cb44.38 → 0.65.3
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/CHANGELOG.md +8 -0
- package/dist/src/components/code/code_block/code_block.d.ts.map +1 -1
- package/dist/src/components/code/code_block/code_block.js +20 -14
- 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 +2 -8
- 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 +68 -14
- package/dist/src/components/code/code_block/code_block.styled.js.map +1 -1
- package/dist/src/components/code/code_block/utils.d.ts +1 -5
- package/dist/src/components/code/code_block/utils.d.ts.map +1 -1
- package/dist/src/components/code/code_block/utils.js +12 -23
- package/dist/src/components/code/code_block/utils.js.map +1 -1
- package/dist/src/components/code/index.d.ts +0 -1
- package/dist/src/components/code/index.d.ts.map +1 -1
- package/dist/src/components/code/index.js +0 -1
- 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 +23 -5
- package/dist/src/components/code/one_line_code/one_line_code.js.map +1 -1
- package/dist/src/components/code/syntax_highlighter.d.ts +15 -0
- package/dist/src/components/code/syntax_highlighter.d.ts.map +1 -0
- package/dist/src/components/code/syntax_highlighter.js +103 -0
- package/dist/src/components/code/syntax_highlighter.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/components/code/code_block/code_block.styled.tsx +69 -21
- package/src/components/code/code_block/code_block.tsx +36 -25
- package/src/components/code/code_block/utils.ts +19 -0
- package/src/components/code/index.ts +0 -1
- package/src/components/code/one_line_code/one_line_code.tsx +28 -9
- package/src/components/code/syntax_highlighter.tsx +134 -0
- package/dist/src/components/code/prism_highlighter.d.ts +0 -15
- package/dist/src/components/code/prism_highlighter.d.ts.map +0 -1
- package/dist/src/components/code/prism_highlighter.js +0 -116
- package/dist/src/components/code/prism_highlighter.js.map +0 -1
- package/src/components/code/code_block/utils.tsx +0 -50
- package/src/components/code/prism_highlighter.tsx +0 -178
|
@@ -1,50 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,178 +0,0 @@
|
|
|
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
|
-
code {
|
|
63
|
-
min-width: 100%;
|
|
64
|
-
display: inline-block;
|
|
65
|
-
vertical-align: middle;
|
|
66
|
-
|
|
67
|
-
*,
|
|
68
|
-
*::before,
|
|
69
|
-
*::after {
|
|
70
|
-
text-shadow: none !important;
|
|
71
|
-
font-style: normal !important;
|
|
72
|
-
font-family: inherit !important;
|
|
73
|
-
font-size: inherit !important;
|
|
74
|
-
font-weight: inherit !important;
|
|
75
|
-
line-height: inherit !important;
|
|
76
|
-
background-color: transparent !important;
|
|
77
|
-
}
|
|
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).attrs({ as: 'span' })`
|
|
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
|
-
});
|