@atlaskit/code 14.4.6 → 14.4.8
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 +12 -0
- package/__perf__/source-code-examples/100-line-example.tsx +5 -5
- package/dist/cjs/bidi-warning/bidi-warning-decorator.js +6 -19
- package/dist/cjs/bidi-warning/index.js +0 -2
- package/dist/cjs/bidi-warning/ui/index.js +6 -17
- package/dist/cjs/bidi-warning/ui/styled.js +6 -11
- package/dist/cjs/code-block.js +25 -34
- package/dist/cjs/code.js +11 -33
- package/dist/cjs/constants.js +0 -2
- package/dist/cjs/entry-points/block.js +0 -2
- package/dist/cjs/entry-points/constants.js +0 -1
- package/dist/cjs/entry-points/inline.js +0 -4
- package/dist/cjs/extract-react-types/code-block.js +0 -1
- package/dist/cjs/extract-react-types/code.js +0 -1
- package/dist/cjs/index.js +0 -7
- package/dist/cjs/internal/hooks/use-highlight.js +10 -24
- package/dist/cjs/internal/theme/constants.js +2 -3
- package/dist/cjs/internal/theme/get-theme.js +0 -16
- package/dist/cjs/internal/theme/styles.js +4 -23
- package/dist/cjs/internal/utils/get-normalized-language.js +2 -8
- package/dist/cjs/react-syntax-highlighter-bidi-warning-renderer.js +17 -41
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/bidi-warning/bidi-warning-decorator.js +5 -9
- package/dist/es2019/bidi-warning/ui/index.js +0 -1
- package/dist/es2019/bidi-warning/ui/styled.js +4 -5
- package/dist/es2019/code-block.js +5 -3
- package/dist/es2019/code.js +0 -7
- package/dist/es2019/internal/hooks/use-highlight.js +4 -8
- package/dist/es2019/internal/theme/constants.js +2 -1
- package/dist/es2019/internal/theme/get-theme.js +2 -3
- package/dist/es2019/internal/theme/styles.js +8 -9
- package/dist/es2019/internal/utils/get-normalized-language.js +2 -3
- package/dist/es2019/react-syntax-highlighter-bidi-warning-renderer.js +14 -21
- package/dist/es2019/version.json +1 -1
- package/dist/esm/bidi-warning/bidi-warning-decorator.js +6 -16
- package/dist/esm/bidi-warning/ui/index.js +6 -9
- package/dist/esm/bidi-warning/ui/styled.js +6 -7
- package/dist/esm/code-block.js +26 -26
- package/dist/esm/code.js +11 -21
- package/dist/esm/internal/hooks/use-highlight.js +10 -19
- package/dist/esm/internal/theme/constants.js +2 -1
- package/dist/esm/internal/theme/get-theme.js +0 -5
- package/dist/esm/internal/theme/styles.js +4 -10
- package/dist/esm/internal/utils/get-normalized-language.js +2 -3
- package/dist/esm/react-syntax-highlighter-bidi-warning-renderer.js +20 -33
- package/dist/esm/version.json +1 -1
- package/package.json +2 -2
|
@@ -2,14 +2,12 @@ import { useCallback, useMemo } from 'react';
|
|
|
2
2
|
const DEFAULT_LINE_EL_ATTR_OBJ = {
|
|
3
3
|
'data-ds--code--row': ''
|
|
4
4
|
};
|
|
5
|
-
|
|
6
5
|
const getLineStyleObject = (lineNumber, testId) => {
|
|
7
6
|
return testId ? {
|
|
8
7
|
'data-testid': `${testId}-line-${lineNumber}`,
|
|
9
8
|
...DEFAULT_LINE_EL_ATTR_OBJ
|
|
10
9
|
} : DEFAULT_LINE_EL_ATTR_OBJ;
|
|
11
10
|
};
|
|
12
|
-
|
|
13
11
|
export const useHighlightLines = ({
|
|
14
12
|
highlight = '',
|
|
15
13
|
testId
|
|
@@ -18,15 +16,14 @@ export const useHighlightLines = ({
|
|
|
18
16
|
if (!highlight) {
|
|
19
17
|
return [];
|
|
20
18
|
}
|
|
21
|
-
|
|
22
19
|
return highlight.split(',').map(num => {
|
|
23
20
|
if (num.indexOf('-') > 0) {
|
|
24
21
|
// We found a line group, e.g. 1-3
|
|
25
|
-
const [from, to] = num.split('-').map(Number)
|
|
22
|
+
const [from, to] = num.split('-').map(Number)
|
|
23
|
+
// Sort by lowest value first, highest value last.
|
|
26
24
|
.sort((a, b) => a - b);
|
|
27
25
|
return Array(to + 1).fill(undefined).map((_, index) => index).slice(from, to + 1);
|
|
28
26
|
}
|
|
29
|
-
|
|
30
27
|
return Number(num);
|
|
31
28
|
}).reduce((acc, val) => acc.concat(val), []) || [];
|
|
32
29
|
}, [highlight]);
|
|
@@ -34,16 +31,15 @@ export const useHighlightLines = ({
|
|
|
34
31
|
if (!highlight || highlightedLines.length === 0) {
|
|
35
32
|
return getLineStyleObject(lineNumber, testId);
|
|
36
33
|
}
|
|
37
|
-
|
|
38
34
|
if (highlightedLines.includes(lineNumber)) {
|
|
39
35
|
const highlightedDataAttrObj = {
|
|
40
36
|
'data-ds--code--row--highlight': ''
|
|
41
37
|
};
|
|
42
|
-
return {
|
|
38
|
+
return {
|
|
39
|
+
...highlightedDataAttrObj,
|
|
43
40
|
...getLineStyleObject(lineNumber, testId)
|
|
44
41
|
};
|
|
45
42
|
}
|
|
46
|
-
|
|
47
43
|
return getLineStyleObject(lineNumber, testId);
|
|
48
44
|
}, [highlight, testId]);
|
|
49
45
|
return {
|
|
@@ -5,6 +5,7 @@ export const HIGHLIGHT_BORDER_WIDTH = '4px';
|
|
|
5
5
|
export const SPACING = gridSize();
|
|
6
6
|
export const LINE_NUMBER_GUTTER = SPACING * 2;
|
|
7
7
|
export const VAR_CODE_LINE_NUMBER_BG_COLOR = '--ds--code--line-number-bg-color';
|
|
8
|
-
export const VAR_CODE_BG_COLOR = '--ds--code--bg-color';
|
|
8
|
+
export const VAR_CODE_BG_COLOR = '--ds--code--bg-color';
|
|
9
9
|
|
|
10
|
+
// selector for codeblock
|
|
10
11
|
export const CODE_BLOCK_SELECTOR = `data-ds--code--code-block`;
|
|
@@ -221,9 +221,8 @@ export const getColorPalette = memoizeOne(theme => {
|
|
|
221
221
|
})(akTheme)
|
|
222
222
|
};
|
|
223
223
|
});
|
|
224
|
-
|
|
225
|
-
|
|
224
|
+
const getTheme = theme => ({
|
|
225
|
+
...getBaseTheme(theme),
|
|
226
226
|
...getColorPalette(theme)
|
|
227
227
|
});
|
|
228
|
-
|
|
229
228
|
export default getTheme;
|
|
@@ -5,10 +5,8 @@ export const getLineNumWidth = numLines => {
|
|
|
5
5
|
if (!numLines) {
|
|
6
6
|
return '1ch';
|
|
7
7
|
}
|
|
8
|
-
|
|
9
8
|
return `${numLines.toFixed(0).length}ch`;
|
|
10
9
|
};
|
|
11
|
-
|
|
12
10
|
const lineNumberStyle = theme => ({
|
|
13
11
|
// width of the line number gutter
|
|
14
12
|
minWidth: `calc(${theme.lineNumberWidth} + ${LINE_NUMBER_GUTTER}px) !important`,
|
|
@@ -26,9 +24,9 @@ const lineNumberStyle = theme => ({
|
|
|
26
24
|
userSelect: 'none',
|
|
27
25
|
// this is to fix SSR spacing issue
|
|
28
26
|
display: 'block'
|
|
29
|
-
});
|
|
30
|
-
|
|
27
|
+
});
|
|
31
28
|
|
|
29
|
+
// order of these keys does matter as it will affect the css precedence
|
|
32
30
|
const syntaxKeywordColors = theme => ({
|
|
33
31
|
'.token': {
|
|
34
32
|
// this specifically stops prism css cascading.
|
|
@@ -174,11 +172,10 @@ const syntaxKeywordColors = theme => ({
|
|
|
174
172
|
}
|
|
175
173
|
}
|
|
176
174
|
});
|
|
175
|
+
|
|
177
176
|
/**
|
|
178
177
|
* Styles applied at the root element level, common across code/codeblock
|
|
179
178
|
*/
|
|
180
|
-
|
|
181
|
-
|
|
182
179
|
export const getBaseCodeStyles = theme => ({
|
|
183
180
|
fontSize: CODE_FONT_SIZE,
|
|
184
181
|
fontFamily: theme.fontFamily,
|
|
@@ -188,13 +185,13 @@ export const getBaseCodeStyles = theme => ({
|
|
|
188
185
|
borderStyle: 'none',
|
|
189
186
|
borderRadius: `${borderRadius()}px`
|
|
190
187
|
});
|
|
188
|
+
|
|
191
189
|
/**
|
|
192
190
|
* Takes an implemented CodeBlock theme, and returns styles required for
|
|
193
191
|
* react-syntax-highlighter.
|
|
194
192
|
*
|
|
195
193
|
* @param theme
|
|
196
194
|
*/
|
|
197
|
-
|
|
198
195
|
export const getCodeBlockStyles = theme => (highlightedStartText, highlightedEndText, hasLineNumbers) => ({
|
|
199
196
|
// this is required to account for prismjs styles leaking into the codeblock
|
|
200
197
|
'code[class*="language-"], pre[class*="language-"], code': {
|
|
@@ -281,7 +278,8 @@ export const getCodeStyles = globalTheme => {
|
|
|
281
278
|
const akTheme = 'theme' in globalTheme ? globalTheme.theme : globalTheme;
|
|
282
279
|
const theme = getBaseTheme(akTheme);
|
|
283
280
|
const baseStyles = getBaseCodeStyles(theme);
|
|
284
|
-
return {
|
|
281
|
+
return {
|
|
282
|
+
...baseStyles,
|
|
285
283
|
display: 'inline',
|
|
286
284
|
padding: '2px 0.5ch',
|
|
287
285
|
boxDecorationBreak: 'clone',
|
|
@@ -292,7 +290,8 @@ export const getCodeStyles = globalTheme => {
|
|
|
292
290
|
};
|
|
293
291
|
};
|
|
294
292
|
export const getCodeBlockTheme = (globalTheme, maxLines) => {
|
|
295
|
-
return {
|
|
293
|
+
return {
|
|
294
|
+
...getBaseTheme(globalTheme),
|
|
296
295
|
...getColorPalette(globalTheme),
|
|
297
296
|
lineNumberWidth: maxLines ? getLineNumWidth(maxLines) : undefined
|
|
298
297
|
};
|
|
@@ -4,10 +4,9 @@ export const normalizeLanguage = memoizeOne(language => {
|
|
|
4
4
|
if (!language) {
|
|
5
5
|
return '';
|
|
6
6
|
}
|
|
7
|
-
|
|
8
7
|
const match = SUPPORTED_LANGUAGES.find(val => {
|
|
9
8
|
return val.name === language || val.alias.includes(language);
|
|
10
|
-
});
|
|
11
|
-
|
|
9
|
+
});
|
|
10
|
+
// Fallback to plain monospaced text if language passed but not supported
|
|
12
11
|
return match ? match.value : 'text';
|
|
13
12
|
});
|
|
@@ -2,7 +2,9 @@ import _extends from "@babel/runtime/helpers/extends";
|
|
|
2
2
|
// @ts-nocheck
|
|
3
3
|
import React from 'react';
|
|
4
4
|
import CodeBidiWarning from './bidi-warning';
|
|
5
|
-
import codeBidiWarningDecorator from './bidi-warning/bidi-warning-decorator';
|
|
5
|
+
import codeBidiWarningDecorator from './bidi-warning/bidi-warning-decorator';
|
|
6
|
+
|
|
7
|
+
// File mostly vendored from react-syntax-highlighter
|
|
6
8
|
//
|
|
7
9
|
// - https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/efc3f7b7537d1729193b7a472067bcbe6cbecaf1/src/highlight.js#L272-L281
|
|
8
10
|
// - https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/efc3f7b7537d1729193b7a472067bcbe6cbecaf1/src/create-element.js
|
|
@@ -23,55 +25,47 @@ export function createBidiWarningRenderer(codeBidiWarningConfig) {
|
|
|
23
25
|
key: `code-segement${i}`
|
|
24
26
|
}));
|
|
25
27
|
};
|
|
26
|
-
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Get all possible permutations of all power sets
|
|
27
31
|
//
|
|
28
32
|
// Super simple, non-algorithmic solution since the
|
|
29
33
|
// number of class names will not be greater than 4
|
|
30
|
-
|
|
31
34
|
function powerSetPermutations(arr) {
|
|
32
35
|
const arrLength = arr.length;
|
|
33
|
-
|
|
34
36
|
if (arrLength === 0 || arrLength === 1) {
|
|
35
37
|
return arr;
|
|
36
38
|
}
|
|
37
|
-
|
|
38
39
|
if (arrLength === 2) {
|
|
39
40
|
// prettier-ignore
|
|
40
41
|
return [arr[0], arr[1], `${arr[0]}.${arr[1]}`, `${arr[1]}.${arr[0]}`];
|
|
41
42
|
}
|
|
42
|
-
|
|
43
43
|
if (arrLength === 3) {
|
|
44
44
|
return [arr[0], arr[1], arr[2], `${arr[0]}.${arr[1]}`, `${arr[0]}.${arr[2]}`, `${arr[1]}.${arr[0]}`, `${arr[1]}.${arr[2]}`, `${arr[2]}.${arr[0]}`, `${arr[2]}.${arr[1]}`, `${arr[0]}.${arr[1]}.${arr[2]}`, `${arr[0]}.${arr[2]}.${arr[1]}`, `${arr[1]}.${arr[0]}.${arr[2]}`, `${arr[1]}.${arr[2]}.${arr[0]}`, `${arr[2]}.${arr[0]}.${arr[1]}`, `${arr[2]}.${arr[1]}.${arr[0]}`];
|
|
45
45
|
}
|
|
46
|
-
|
|
47
46
|
if (arrLength >= 4) {
|
|
48
47
|
// Currently does not support more than 4 extra
|
|
49
48
|
// class names (after `.token` has been removed)
|
|
50
49
|
return [arr[0], arr[1], arr[2], arr[3], `${arr[0]}.${arr[1]}`, `${arr[0]}.${arr[2]}`, `${arr[0]}.${arr[3]}`, `${arr[1]}.${arr[0]}`, `${arr[1]}.${arr[2]}`, `${arr[1]}.${arr[3]}`, `${arr[2]}.${arr[0]}`, `${arr[2]}.${arr[1]}`, `${arr[2]}.${arr[3]}`, `${arr[3]}.${arr[0]}`, `${arr[3]}.${arr[1]}`, `${arr[3]}.${arr[2]}`, `${arr[0]}.${arr[1]}.${arr[2]}`, `${arr[0]}.${arr[1]}.${arr[3]}`, `${arr[0]}.${arr[2]}.${arr[1]}`, `${arr[0]}.${arr[2]}.${arr[3]}`, `${arr[0]}.${arr[3]}.${arr[1]}`, `${arr[0]}.${arr[3]}.${arr[2]}`, `${arr[1]}.${arr[0]}.${arr[2]}`, `${arr[1]}.${arr[0]}.${arr[3]}`, `${arr[1]}.${arr[2]}.${arr[0]}`, `${arr[1]}.${arr[2]}.${arr[3]}`, `${arr[1]}.${arr[3]}.${arr[0]}`, `${arr[1]}.${arr[3]}.${arr[2]}`, `${arr[2]}.${arr[0]}.${arr[1]}`, `${arr[2]}.${arr[0]}.${arr[3]}`, `${arr[2]}.${arr[1]}.${arr[0]}`, `${arr[2]}.${arr[1]}.${arr[3]}`, `${arr[2]}.${arr[3]}.${arr[0]}`, `${arr[2]}.${arr[3]}.${arr[1]}`, `${arr[3]}.${arr[0]}.${arr[1]}`, `${arr[3]}.${arr[0]}.${arr[2]}`, `${arr[3]}.${arr[1]}.${arr[0]}`, `${arr[3]}.${arr[1]}.${arr[2]}`, `${arr[3]}.${arr[2]}.${arr[0]}`, `${arr[3]}.${arr[2]}.${arr[1]}`, `${arr[0]}.${arr[1]}.${arr[2]}.${arr[3]}`, `${arr[0]}.${arr[1]}.${arr[3]}.${arr[2]}`, `${arr[0]}.${arr[2]}.${arr[1]}.${arr[3]}`, `${arr[0]}.${arr[2]}.${arr[3]}.${arr[1]}`, `${arr[0]}.${arr[3]}.${arr[1]}.${arr[2]}`, `${arr[0]}.${arr[3]}.${arr[2]}.${arr[1]}`, `${arr[1]}.${arr[0]}.${arr[2]}.${arr[3]}`, `${arr[1]}.${arr[0]}.${arr[3]}.${arr[2]}`, `${arr[1]}.${arr[2]}.${arr[0]}.${arr[3]}`, `${arr[1]}.${arr[2]}.${arr[3]}.${arr[0]}`, `${arr[1]}.${arr[3]}.${arr[0]}.${arr[2]}`, `${arr[1]}.${arr[3]}.${arr[2]}.${arr[0]}`, `${arr[2]}.${arr[0]}.${arr[1]}.${arr[3]}`, `${arr[2]}.${arr[0]}.${arr[3]}.${arr[1]}`, `${arr[2]}.${arr[1]}.${arr[0]}.${arr[3]}`, `${arr[2]}.${arr[1]}.${arr[3]}.${arr[0]}`, `${arr[2]}.${arr[3]}.${arr[0]}.${arr[1]}`, `${arr[2]}.${arr[3]}.${arr[1]}.${arr[0]}`, `${arr[3]}.${arr[0]}.${arr[1]}.${arr[2]}`, `${arr[3]}.${arr[0]}.${arr[2]}.${arr[1]}`, `${arr[3]}.${arr[1]}.${arr[0]}.${arr[2]}`, `${arr[3]}.${arr[1]}.${arr[2]}.${arr[0]}`, `${arr[3]}.${arr[2]}.${arr[0]}.${arr[1]}`, `${arr[3]}.${arr[2]}.${arr[1]}.${arr[0]}`];
|
|
51
50
|
}
|
|
52
51
|
}
|
|
53
|
-
|
|
54
52
|
const classNameCombinations = {};
|
|
55
|
-
|
|
56
53
|
function getClassNameCombinations(classNames) {
|
|
57
54
|
if (classNames.length === 0 || classNames.length === 1) {
|
|
58
55
|
return classNames;
|
|
59
56
|
}
|
|
60
|
-
|
|
61
57
|
const key = classNames.join('.');
|
|
62
|
-
|
|
63
58
|
if (!classNameCombinations[key]) {
|
|
64
59
|
classNameCombinations[key] = powerSetPermutations(classNames);
|
|
65
60
|
}
|
|
66
|
-
|
|
67
61
|
return classNameCombinations[key];
|
|
68
62
|
}
|
|
69
|
-
|
|
70
63
|
export function createStyleObject(classNames, elementStyle = {}, stylesheet) {
|
|
71
64
|
const nonTokenClassNames = classNames.filter(className => className !== 'token');
|
|
72
65
|
const classNamesCombinations = getClassNameCombinations(nonTokenClassNames);
|
|
73
66
|
return classNamesCombinations.reduce((styleObject, className) => {
|
|
74
|
-
return {
|
|
67
|
+
return {
|
|
68
|
+
...styleObject,
|
|
75
69
|
...stylesheet[className]
|
|
76
70
|
};
|
|
77
71
|
}, elementStyle);
|
|
@@ -92,7 +86,6 @@ export function createChildren(stylesheet, useInlineStyles, codeBidiWarningConfi
|
|
|
92
86
|
}));
|
|
93
87
|
};
|
|
94
88
|
}
|
|
95
|
-
|
|
96
89
|
function createElement({
|
|
97
90
|
node,
|
|
98
91
|
stylesheet,
|
|
@@ -107,7 +100,6 @@ function createElement({
|
|
|
107
100
|
tagName: TagName,
|
|
108
101
|
value
|
|
109
102
|
} = node;
|
|
110
|
-
|
|
111
103
|
if (type === 'text') {
|
|
112
104
|
// occasionally react-syntax-highlighter passes a numeric value when the
|
|
113
105
|
// type is text
|
|
@@ -125,9 +117,9 @@ function createElement({
|
|
|
125
117
|
} else if (TagName) {
|
|
126
118
|
const childrenCreator = createChildren(stylesheet, useInlineStyles, codeBidiWarningConfig);
|
|
127
119
|
let props;
|
|
128
|
-
|
|
129
120
|
if (!useInlineStyles) {
|
|
130
|
-
props = {
|
|
121
|
+
props = {
|
|
122
|
+
...properties,
|
|
131
123
|
className: createClassNameString(properties.className)
|
|
132
124
|
};
|
|
133
125
|
} else {
|
|
@@ -138,16 +130,17 @@ function createElement({
|
|
|
138
130
|
}
|
|
139
131
|
});
|
|
140
132
|
return classes;
|
|
141
|
-
}, []);
|
|
133
|
+
}, []);
|
|
142
134
|
|
|
135
|
+
// For compatibility with older versions of react-syntax-highlighter
|
|
143
136
|
const startingClassName = properties.className && properties.className.includes('token') ? ['token'] : [];
|
|
144
137
|
const className = properties.className && startingClassName.concat(properties.className.filter(className => !allStylesheetSelectors.includes(className)));
|
|
145
|
-
props = {
|
|
138
|
+
props = {
|
|
139
|
+
...properties,
|
|
146
140
|
className: createClassNameString(className) || undefined,
|
|
147
141
|
style: createStyleObject(properties.className, Object.assign({}, properties.style, style), stylesheet)
|
|
148
142
|
};
|
|
149
143
|
}
|
|
150
|
-
|
|
151
144
|
const children = childrenCreator(node.children);
|
|
152
145
|
return /*#__PURE__*/React.createElement(TagName, _extends({
|
|
153
146
|
key: key
|
package/dist/es2019/version.json
CHANGED
|
@@ -1,46 +1,37 @@
|
|
|
1
1
|
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
-
|
|
3
2
|
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
4
|
-
|
|
5
3
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
6
|
-
|
|
7
4
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
8
|
-
|
|
9
5
|
export var bidiCharacterRegex = /[\u202A-\u202E\u2066-\u2069]/g;
|
|
10
6
|
export default function codeBidiWarningDecorator(originalText, decorate) {
|
|
11
7
|
var matches = _toConsumableArray(originalText.matchAll(bidiCharacterRegex));
|
|
12
|
-
|
|
13
8
|
if (matches.length === 0) {
|
|
14
9
|
// No matches encountered, so we return the originalText value
|
|
15
10
|
return originalText;
|
|
16
11
|
}
|
|
17
|
-
|
|
18
12
|
var children = [];
|
|
19
13
|
var mappedTo = 0;
|
|
20
|
-
|
|
21
14
|
var _iterator = _createForOfIteratorHelper(matches),
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
_step;
|
|
24
16
|
try {
|
|
25
17
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
26
18
|
var match = _step.value;
|
|
27
|
-
|
|
28
19
|
if (mappedTo !== match.index) {
|
|
29
20
|
// There were unmatched characters prior to this match which haven't been
|
|
30
21
|
// mapped to the children.
|
|
31
22
|
// Add them as plain text.
|
|
32
23
|
children.push(originalText.substring(mappedTo, match.index));
|
|
33
24
|
}
|
|
34
|
-
|
|
35
25
|
children.push(decorate({
|
|
36
26
|
bidiCharacter: match[0],
|
|
37
27
|
index: match.index
|
|
38
|
-
}));
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
// While index is guaranteed to be present, it needs to be asserted due
|
|
39
31
|
// to a limitation of typescripts regex handling
|
|
40
32
|
//
|
|
41
33
|
// https://github.com/microsoft/TypeScript/issues/36788
|
|
42
34
|
// Decorate bidi character
|
|
43
|
-
|
|
44
35
|
mappedTo = match.index + match[0].length;
|
|
45
36
|
}
|
|
46
37
|
} catch (err) {
|
|
@@ -48,14 +39,13 @@ export default function codeBidiWarningDecorator(originalText, decorate) {
|
|
|
48
39
|
} finally {
|
|
49
40
|
_iterator.f();
|
|
50
41
|
}
|
|
51
|
-
|
|
52
42
|
if (mappedTo !== originalText.length) {
|
|
53
43
|
// There is text following the final match, which needs to be mapped
|
|
54
44
|
// to the children.
|
|
55
45
|
// Added as plain text.
|
|
56
46
|
children.push(originalText.substring(mappedTo, originalText.length));
|
|
57
|
-
}
|
|
58
|
-
|
|
47
|
+
}
|
|
59
48
|
|
|
49
|
+
// return the mapped children with decorated bidi characters
|
|
60
50
|
return children;
|
|
61
51
|
}
|
|
@@ -6,12 +6,11 @@ import Tooltip from '@atlaskit/tooltip';
|
|
|
6
6
|
import { Decorator } from './styled';
|
|
7
7
|
export default function BidiWarning(_ref) {
|
|
8
8
|
var testId = _ref.testId,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
bidiCharacter = _ref.bidiCharacter,
|
|
10
|
+
skipChildren = _ref.skipChildren,
|
|
11
|
+
tooltipEnabled = _ref.tooltipEnabled,
|
|
12
|
+
_ref$label = _ref.label,
|
|
13
|
+
label = _ref$label === void 0 ? 'Bidirectional characters change the order that text is rendered. This could be used to obscure malicious code.' : _ref$label;
|
|
15
14
|
if (tooltipEnabled) {
|
|
16
15
|
return (
|
|
17
16
|
/*#__PURE__*/
|
|
@@ -26,7 +25,6 @@ export default function BidiWarning(_ref) {
|
|
|
26
25
|
}, skipChildren ? null : bidiCharacter))
|
|
27
26
|
);
|
|
28
27
|
}
|
|
29
|
-
|
|
30
28
|
return /*#__PURE__*/React.createElement(Decorator, {
|
|
31
29
|
testId: testId,
|
|
32
30
|
bidiCharacter: bidiCharacter
|
|
@@ -34,8 +32,7 @@ export default function BidiWarning(_ref) {
|
|
|
34
32
|
}
|
|
35
33
|
var CustomizedTagWithRef = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
36
34
|
var children = props.children,
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
rest = _objectWithoutProperties(props, _excluded);
|
|
39
36
|
return /*#__PURE__*/React.createElement("span", _extends({}, rest, {
|
|
40
37
|
ref: ref
|
|
41
38
|
}), children);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** @jsx jsx */
|
|
2
|
+
|
|
2
3
|
import { css, jsx } from '@emotion/react';
|
|
3
4
|
import { Y75 } from '@atlaskit/theme/colors';
|
|
4
5
|
var decoration = css({
|
|
@@ -10,7 +11,7 @@ var decoration = css({
|
|
|
10
11
|
position: 'relative',
|
|
11
12
|
':before': {
|
|
12
13
|
display: 'inline-flex',
|
|
13
|
-
padding: "var(--ds-
|
|
14
|
+
padding: "var(--ds-space-0, 0px)".concat(" ", "var(--ds-space-050, 4px)"),
|
|
14
15
|
alignItems: 'center',
|
|
15
16
|
justifyContent: 'center',
|
|
16
17
|
flexDirection: 'row',
|
|
@@ -20,7 +21,6 @@ var decoration = css({
|
|
|
20
21
|
fontSize: "var(--ds-font-size-100, 14px)",
|
|
21
22
|
fontStyle: 'normal',
|
|
22
23
|
lineHeight: '18px',
|
|
23
|
-
|
|
24
24
|
/**
|
|
25
25
|
* Ensures the decoration receives pointer events when it occurs with
|
|
26
26
|
* an ancestor that disables them.
|
|
@@ -34,15 +34,16 @@ var decoration = css({
|
|
|
34
34
|
});
|
|
35
35
|
export function Decorator(_ref) {
|
|
36
36
|
var bidiCharacter = _ref.bidiCharacter,
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
children = _ref.children,
|
|
38
|
+
testId = _ref.testId;
|
|
39
39
|
var bidiCharacterCode = getBidiCharacterCode(bidiCharacter);
|
|
40
40
|
return jsx("span", {
|
|
41
41
|
"aria-label": bidiCharacterCode
|
|
42
42
|
}, jsx("span", {
|
|
43
43
|
css: decoration,
|
|
44
44
|
"data-testid": testId,
|
|
45
|
-
"data-bidi-character-code": bidiCharacterCode
|
|
45
|
+
"data-bidi-character-code": bidiCharacterCode
|
|
46
|
+
// This is set to true so that the content is not read out by
|
|
46
47
|
// screen readers as the content includes angle brackets for
|
|
47
48
|
// visual decoration purposes.
|
|
48
49
|
// We use a span with the aria-label set to the bidi character code
|
|
@@ -51,10 +52,8 @@ export function Decorator(_ref) {
|
|
|
51
52
|
"aria-hidden": "true"
|
|
52
53
|
}, children));
|
|
53
54
|
}
|
|
54
|
-
|
|
55
55
|
function getBidiCharacterCode(bidiCharacter) {
|
|
56
56
|
var _bidiCharacter$codePo;
|
|
57
|
-
|
|
58
57
|
var bidiCharacterCode = (_bidiCharacter$codePo = bidiCharacter.codePointAt(0)) === null || _bidiCharacter$codePo === void 0 ? void 0 : _bidiCharacter$codePo.toString(16);
|
|
59
58
|
return "U+".concat(bidiCharacterCode);
|
|
60
59
|
}
|
package/dist/esm/code-block.js
CHANGED
|
@@ -7,6 +7,7 @@ import { useHighlightLines } from './internal/hooks/use-highlight';
|
|
|
7
7
|
import { getCodeBlockStyles, getCodeBlockTheme } from './internal/theme/styles';
|
|
8
8
|
import { normalizeLanguage } from './internal/utils/get-normalized-language';
|
|
9
9
|
import { createBidiWarningRenderer } from './react-syntax-highlighter-bidi-warning-renderer';
|
|
10
|
+
|
|
10
11
|
/**
|
|
11
12
|
* __Code block__
|
|
12
13
|
*
|
|
@@ -16,25 +17,24 @@ import { createBidiWarningRenderer } from './react-syntax-highlighter-bidi-warni
|
|
|
16
17
|
* - [Code](https://atlassian.design/components/code/code-block/code)
|
|
17
18
|
* - [Usage](https://atlassian.design/components/code/code-block/usage)
|
|
18
19
|
*/
|
|
19
|
-
|
|
20
20
|
var CodeBlock = /*#__PURE__*/memo(function CodeBlock(_ref) {
|
|
21
21
|
var _ref$showLineNumbers = _ref.showLineNumbers,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
22
|
+
showLineNumbers = _ref$showLineNumbers === void 0 ? true : _ref$showLineNumbers,
|
|
23
|
+
_ref$language = _ref.language,
|
|
24
|
+
providedLanguage = _ref$language === void 0 ? 'text' : _ref$language,
|
|
25
|
+
_ref$highlight = _ref.highlight,
|
|
26
|
+
highlight = _ref$highlight === void 0 ? '' : _ref$highlight,
|
|
27
|
+
_ref$highlightedStart = _ref.highlightedStartText,
|
|
28
|
+
highlightedStartText = _ref$highlightedStart === void 0 ? 'Highlight start' : _ref$highlightedStart,
|
|
29
|
+
_ref$highlightedEndTe = _ref.highlightedEndText,
|
|
30
|
+
highlightedEndText = _ref$highlightedEndTe === void 0 ? 'Highlight end' : _ref$highlightedEndTe,
|
|
31
|
+
testId = _ref.testId,
|
|
32
|
+
text = _ref.text,
|
|
33
|
+
_ref$codeBidiWarnings = _ref.codeBidiWarnings,
|
|
34
|
+
codeBidiWarnings = _ref$codeBidiWarnings === void 0 ? true : _ref$codeBidiWarnings,
|
|
35
|
+
codeBidiWarningLabel = _ref.codeBidiWarningLabel,
|
|
36
|
+
_ref$codeBidiWarningT = _ref.codeBidiWarningTooltipEnabled,
|
|
37
|
+
codeBidiWarningTooltipEnabled = _ref$codeBidiWarningT === void 0 ? true : _ref$codeBidiWarningT;
|
|
38
38
|
var numLines = (text || '').split('\n').length;
|
|
39
39
|
var globalTheme = useGlobalTheme();
|
|
40
40
|
var theme = useMemo(function () {
|
|
@@ -46,21 +46,20 @@ var CodeBlock = /*#__PURE__*/memo(function CodeBlock(_ref) {
|
|
|
46
46
|
var styles = useMemo(function () {
|
|
47
47
|
return css(getStyles(highlightedStartText, highlightedEndText, showLineNumbers));
|
|
48
48
|
}, [highlightedStartText, highlightedEndText, showLineNumbers, getStyles]);
|
|
49
|
-
|
|
50
49
|
var _useHighlightLines = useHighlightLines({
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
highlight: highlight,
|
|
51
|
+
testId: testId
|
|
52
|
+
}),
|
|
53
|
+
getHighlightStyles = _useHighlightLines.getHighlightStyles,
|
|
54
|
+
highlightedLines = _useHighlightLines.highlightedLines;
|
|
57
55
|
var getLineProps = useCallback(function (line) {
|
|
58
56
|
return getHighlightStyles(line, highlightedLines);
|
|
59
57
|
}, [getHighlightStyles, highlightedLines]);
|
|
60
58
|
var language = useMemo(function () {
|
|
61
59
|
return normalizeLanguage(providedLanguage);
|
|
62
|
-
}, [providedLanguage]);
|
|
60
|
+
}, [providedLanguage]);
|
|
63
61
|
|
|
62
|
+
// https://product-fabric.atlassian.net/browse/DST-2472
|
|
64
63
|
var languageToUse = text ? language : 'text';
|
|
65
64
|
var renderer = codeBidiWarnings ? createBidiWarningRenderer({
|
|
66
65
|
codeBidiWarningLabel: codeBidiWarningLabel,
|
|
@@ -73,7 +72,8 @@ var CodeBlock = /*#__PURE__*/memo(function CodeBlock(_ref) {
|
|
|
73
72
|
css: styles,
|
|
74
73
|
language: languageToUse,
|
|
75
74
|
PreTag: "span",
|
|
76
|
-
showLineNumbers: showLineNumbers
|
|
75
|
+
showLineNumbers: showLineNumbers
|
|
76
|
+
// Wrap lines is needed to set styles on the line when highlighting.
|
|
77
77
|
,
|
|
78
78
|
wrapLines: highlight.length > 0 || !!testId,
|
|
79
79
|
lineProps: getLineProps,
|
package/dist/esm/code.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import _extends from "@babel/runtime/helpers/extends";
|
|
2
2
|
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
3
3
|
var _excluded = ["testId"],
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
_excluded2 = ["children", "codeBidiWarnings", "codeBidiWarningLabel", "codeBidiWarningTooltipEnabled"];
|
|
6
5
|
/** @jsx jsx */
|
|
7
6
|
import React, { forwardRef, memo, useMemo } from 'react';
|
|
8
7
|
import { css, jsx } from '@emotion/react';
|
|
@@ -10,7 +9,6 @@ import { useGlobalTheme } from '@atlaskit/theme/components';
|
|
|
10
9
|
import CodeBidiWarning from './bidi-warning';
|
|
11
10
|
import codeBidiWarningDecorator from './bidi-warning/bidi-warning-decorator';
|
|
12
11
|
import { getCodeStyles } from './internal/theme/styles';
|
|
13
|
-
|
|
14
12
|
/**
|
|
15
13
|
* __Code__
|
|
16
14
|
*
|
|
@@ -22,21 +20,18 @@ import { getCodeStyles } from './internal/theme/styles';
|
|
|
22
20
|
*/
|
|
23
21
|
var Code = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Code(_ref, ref) {
|
|
24
22
|
var testId = _ref.testId,
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
|
27
24
|
var theme = useGlobalTheme();
|
|
28
25
|
var styles = useMemo(function () {
|
|
29
26
|
return css(getCodeStyles(theme));
|
|
30
27
|
}, [theme]);
|
|
31
|
-
|
|
32
28
|
var children = props.children,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
29
|
+
_props$codeBidiWarnin = props.codeBidiWarnings,
|
|
30
|
+
codeBidiWarnings = _props$codeBidiWarnin === void 0 ? true : _props$codeBidiWarnin,
|
|
31
|
+
codeBidiWarningLabel = props.codeBidiWarningLabel,
|
|
32
|
+
_props$codeBidiWarnin2 = props.codeBidiWarningTooltipEnabled,
|
|
33
|
+
codeBidiWarningTooltipEnabled = _props$codeBidiWarnin2 === void 0 ? true : _props$codeBidiWarnin2,
|
|
34
|
+
otherProps = _objectWithoutProperties(props, _excluded2);
|
|
40
35
|
var decoratedChildren = codeBidiWarnings ? jsx(RenderCodeChildrenWithBidiWarnings, {
|
|
41
36
|
codeBidiWarningLabel: codeBidiWarningLabel,
|
|
42
37
|
codeBidiWarningTooltipEnabled: codeBidiWarningTooltipEnabled
|
|
@@ -47,16 +42,15 @@ var Code = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Code(_ref, ref) {
|
|
|
47
42
|
css: styles
|
|
48
43
|
}, otherProps), decoratedChildren);
|
|
49
44
|
}));
|
|
50
|
-
|
|
51
45
|
function RenderCodeChildrenWithBidiWarnings(_ref2) {
|
|
52
46
|
var children = _ref2.children,
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
codeBidiWarningLabel = _ref2.codeBidiWarningLabel,
|
|
48
|
+
codeBidiWarningTooltipEnabled = _ref2.codeBidiWarningTooltipEnabled;
|
|
55
49
|
var replacedChildren = React.Children.map(children, function (childNode) {
|
|
56
50
|
if (typeof childNode === 'string') {
|
|
57
51
|
var decorated = codeBidiWarningDecorator(childNode, function (_ref3) {
|
|
58
52
|
var bidiCharacter = _ref3.bidiCharacter,
|
|
59
|
-
|
|
53
|
+
index = _ref3.index;
|
|
60
54
|
return jsx(CodeBidiWarning, {
|
|
61
55
|
bidiCharacter: bidiCharacter,
|
|
62
56
|
key: index,
|
|
@@ -66,7 +60,6 @@ function RenderCodeChildrenWithBidiWarnings(_ref2) {
|
|
|
66
60
|
});
|
|
67
61
|
return decorated;
|
|
68
62
|
}
|
|
69
|
-
|
|
70
63
|
if (isReactElement(childNode) && childNode.props.children) {
|
|
71
64
|
// eslint-disable-next-line @repo/internal/react/no-clone-element
|
|
72
65
|
var newChildNode = /*#__PURE__*/React.cloneElement(childNode, {
|
|
@@ -77,16 +70,13 @@ function RenderCodeChildrenWithBidiWarnings(_ref2) {
|
|
|
77
70
|
});
|
|
78
71
|
return newChildNode;
|
|
79
72
|
}
|
|
80
|
-
|
|
81
73
|
return childNode;
|
|
82
74
|
});
|
|
83
75
|
return jsx(React.Fragment, null, replacedChildren);
|
|
84
76
|
}
|
|
85
|
-
|
|
86
77
|
function isReactElement(child) {
|
|
87
78
|
return !!child.type;
|
|
88
79
|
}
|
|
89
|
-
|
|
90
80
|
Code.displayName = 'Code';
|
|
91
81
|
export { getCodeStyles };
|
|
92
82
|
export default Code;
|