@atlaskit/code 14.1.2 → 14.2.0
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 +24 -0
- package/bidi-warning/package.json +7 -0
- package/bidi-warning-decorator/package.json +7 -0
- package/dist/cjs/bidi-warning/bidi-warning-decorator.js +73 -0
- package/dist/cjs/bidi-warning/index.js +15 -0
- package/dist/cjs/bidi-warning/ui/index.js +46 -0
- package/dist/cjs/bidi-warning/ui/styled.js +74 -0
- package/dist/cjs/bidi-warning/ui/types.js +5 -0
- package/dist/cjs/code-block.js +9 -2
- package/dist/cjs/code.js +59 -2
- package/dist/cjs/internal/hooks/use-highlight.js +1 -2
- package/dist/cjs/react-syntax-highlighter-bidi-warning-renderer.js +185 -0
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/bidi-warning/bidi-warning-decorator.js +42 -0
- package/dist/es2019/bidi-warning/index.js +1 -0
- package/dist/es2019/bidi-warning/ui/index.js +32 -0
- package/dist/es2019/bidi-warning/ui/styled.js +65 -0
- package/dist/es2019/bidi-warning/ui/types.js +1 -0
- package/dist/es2019/code-block.js +7 -2
- package/dist/es2019/code.js +49 -2
- package/dist/es2019/react-syntax-highlighter-bidi-warning-renderer.js +155 -0
- package/dist/es2019/version.json +1 -1
- package/dist/esm/bidi-warning/bidi-warning-decorator.js +61 -0
- package/dist/esm/bidi-warning/index.js +1 -0
- package/dist/esm/bidi-warning/ui/index.js +32 -0
- package/dist/esm/bidi-warning/ui/styled.js +64 -0
- package/dist/esm/bidi-warning/ui/types.js +1 -0
- package/dist/esm/code-block.js +8 -2
- package/dist/esm/code.js +50 -2
- package/dist/esm/internal/hooks/use-highlight.js +1 -2
- package/dist/esm/react-syntax-highlighter-bidi-warning-renderer.js +167 -0
- package/dist/esm/version.json +1 -1
- package/dist/types/bidi-warning/bidi-warning-decorator.d.ts +5 -0
- package/dist/types/bidi-warning/index.d.ts +1 -0
- package/dist/types/bidi-warning/ui/index.d.ts +2 -0
- package/dist/types/bidi-warning/ui/styled.d.ts +7 -0
- package/dist/types/bidi-warning/ui/types.d.ts +21 -0
- package/dist/types/code.d.ts +3 -2
- package/dist/types/internal/theme/get-theme.d.ts +1 -1
- package/dist/types/internal/types.d.ts +11 -0
- package/dist/types/internal/utils/get-normalized-language.d.ts +1 -1
- package/dist/types/react-syntax-highlighter-bidi-warning-renderer.d.ts +4 -0
- package/dist/types/types.d.ts +11 -0
- package/package.json +10 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @atlaskit/code
|
|
2
2
|
|
|
3
|
+
## 14.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`c80c71b537d`](https://bitbucket.org/atlassian/atlassian-frontend/commits/c80c71b537d) - [ux] ED-13860 add bidi warning decorations to code components
|
|
8
|
+
|
|
9
|
+
## 14.1.5
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [`b85e7ce12cd`](https://bitbucket.org/atlassian/atlassian-frontend/commits/b85e7ce12cd) - Internal upgrade of memoize-one to 6.0.0
|
|
14
|
+
|
|
15
|
+
## 14.1.4
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies
|
|
20
|
+
|
|
21
|
+
## 14.1.3
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- [`378d1cef00f`](https://bitbucket.org/atlassian/atlassian-frontend/commits/378d1cef00f) - Bump `@atlaskit/theme` to version `^11.3.0`.
|
|
26
|
+
|
|
3
27
|
## 14.1.2
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@atlaskit/code/bidi-warning-decorator",
|
|
3
|
+
"main": "../dist/cjs/bidi-warning/bidi-warning-decorator.js",
|
|
4
|
+
"module": "../dist/esm/bidi-warning/bidi-warning-decorator.js",
|
|
5
|
+
"module:es2019": "../dist/es2019/bidi-warning/bidi-warning-decorator.js",
|
|
6
|
+
"types": "../dist/types/bidi-warning/bidi-warning-decorator.d.ts"
|
|
7
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = codeBidiWarningDecorator;
|
|
9
|
+
exports.bidiCharacterRegex = void 0;
|
|
10
|
+
|
|
11
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
12
|
+
|
|
13
|
+
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; } } }; }
|
|
14
|
+
|
|
15
|
+
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); }
|
|
16
|
+
|
|
17
|
+
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; }
|
|
18
|
+
|
|
19
|
+
var bidiCharacterRegex = /[\u202A-\u202E\u2066-\u2069]/g;
|
|
20
|
+
exports.bidiCharacterRegex = bidiCharacterRegex;
|
|
21
|
+
|
|
22
|
+
function codeBidiWarningDecorator(originalText, decorate) {
|
|
23
|
+
var matches = (0, _toConsumableArray2.default)(originalText.matchAll(bidiCharacterRegex));
|
|
24
|
+
|
|
25
|
+
if (matches.length === 0) {
|
|
26
|
+
// No matches encountered, so we return the originalText value
|
|
27
|
+
return originalText;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var children = [];
|
|
31
|
+
var mappedTo = 0;
|
|
32
|
+
|
|
33
|
+
var _iterator = _createForOfIteratorHelper(matches),
|
|
34
|
+
_step;
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
38
|
+
var match = _step.value;
|
|
39
|
+
|
|
40
|
+
if (mappedTo !== match.index) {
|
|
41
|
+
// There were unmatched characters prior to this match which haven't been
|
|
42
|
+
// mapped to the children.
|
|
43
|
+
// Add them as plain text.
|
|
44
|
+
children.push(originalText.substring(mappedTo, match.index));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
children.push(decorate({
|
|
48
|
+
bidiCharacter: match[0],
|
|
49
|
+
index: match.index
|
|
50
|
+
})); // While index is guaranteed to be present, it needs to be asserted due
|
|
51
|
+
// to a limitation of typescripts regex handling
|
|
52
|
+
//
|
|
53
|
+
// https://github.com/microsoft/TypeScript/issues/36788
|
|
54
|
+
// Decorate bidi character
|
|
55
|
+
|
|
56
|
+
mappedTo = match.index + match[0].length;
|
|
57
|
+
}
|
|
58
|
+
} catch (err) {
|
|
59
|
+
_iterator.e(err);
|
|
60
|
+
} finally {
|
|
61
|
+
_iterator.f();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (mappedTo !== originalText.length) {
|
|
65
|
+
// There is text following the final match, which needs to be mapped
|
|
66
|
+
// to the children.
|
|
67
|
+
// Added as plain text.
|
|
68
|
+
children.push(originalText.substring(mappedTo, originalText.length));
|
|
69
|
+
} // return the mapped children with decorated bidi characters
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
return children;
|
|
73
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports, "default", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function get() {
|
|
11
|
+
return _ui.default;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
var _ui = _interopRequireDefault(require("./ui"));
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = BidiWarning;
|
|
9
|
+
|
|
10
|
+
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
11
|
+
|
|
12
|
+
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
13
|
+
|
|
14
|
+
var _react = _interopRequireDefault(require("react"));
|
|
15
|
+
|
|
16
|
+
var _tooltip = _interopRequireDefault(require("@atlaskit/tooltip"));
|
|
17
|
+
|
|
18
|
+
var _styled = require("./styled");
|
|
19
|
+
|
|
20
|
+
function BidiWarning(_ref) {
|
|
21
|
+
var testId = _ref.testId,
|
|
22
|
+
bidiCharacter = _ref.bidiCharacter,
|
|
23
|
+
skipChildren = _ref.skipChildren,
|
|
24
|
+
_ref$label = _ref.label,
|
|
25
|
+
label = _ref$label === void 0 ? 'Bidirectional characters change the order that text is rendered. This could be used to obscure malicious code.' : _ref$label;
|
|
26
|
+
return (
|
|
27
|
+
/*#__PURE__*/
|
|
28
|
+
// Following patches, this should be updated to use the render props signature which will provide aria attributes.
|
|
29
|
+
// Note: this should be tested, as initial testing did not see attributes work with current tooltip implementation.
|
|
30
|
+
_react.default.createElement(_tooltip.default, {
|
|
31
|
+
content: label,
|
|
32
|
+
tag: CustomizedTagWithRef
|
|
33
|
+
}, /*#__PURE__*/_react.default.createElement(_styled.Decorator, {
|
|
34
|
+
testId: testId,
|
|
35
|
+
bidiCharacter: bidiCharacter
|
|
36
|
+
}, skipChildren ? null : bidiCharacter))
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
var CustomizedTagWithRef = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
|
|
41
|
+
var children = props.children,
|
|
42
|
+
rest = (0, _objectWithoutProperties2.default)(props, ["children"]);
|
|
43
|
+
return /*#__PURE__*/_react.default.createElement("span", (0, _extends2.default)({}, rest, {
|
|
44
|
+
ref: ref
|
|
45
|
+
}), children);
|
|
46
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Decorator = Decorator;
|
|
7
|
+
|
|
8
|
+
var _core = require("@emotion/core");
|
|
9
|
+
|
|
10
|
+
var _colors = require("@atlaskit/theme/colors");
|
|
11
|
+
|
|
12
|
+
/** @jsx jsx */
|
|
13
|
+
var decoration = (0, _core.css)({
|
|
14
|
+
// Required as otherwise the following bidi characters cause the span
|
|
15
|
+
// to not receive hover events.
|
|
16
|
+
//
|
|
17
|
+
// U+2066 LEFT-TO-RIGHT ISOLATE (when using pseudo element before)
|
|
18
|
+
// U+202E RIGHT-TO-LEFT OVERRIDE' (when using pseudo element after)
|
|
19
|
+
position: 'relative',
|
|
20
|
+
':before': {
|
|
21
|
+
/* layout */
|
|
22
|
+
display: 'inline-flex',
|
|
23
|
+
flexDirection: 'row',
|
|
24
|
+
justifyContent: 'center',
|
|
25
|
+
alignItems: 'center',
|
|
26
|
+
padding: '0 4px',
|
|
27
|
+
fontSize: '14px',
|
|
28
|
+
lineHeight: '18px',
|
|
29
|
+
background: _colors.Y75,
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Ensures the decoration receives pointer events when it occurs with
|
|
33
|
+
* an ancestor that disables them.
|
|
34
|
+
*/
|
|
35
|
+
pointerEvents: 'auto',
|
|
36
|
+
|
|
37
|
+
/* contents */
|
|
38
|
+
content: '"<"attr(data-bidi-character-code)">"',
|
|
39
|
+
// This color is Y800 which is not yet rolled out
|
|
40
|
+
// https://hello.atlassian.net/wiki/spaces/~tswan/pages/1366555782?focusedCommentId=1370387374#comment-1370387374
|
|
41
|
+
color: '#7F5F01',
|
|
42
|
+
fontStyle: 'normal'
|
|
43
|
+
},
|
|
44
|
+
':hover:before': {
|
|
45
|
+
color: '#533F04'
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
function Decorator(_ref) {
|
|
50
|
+
var bidiCharacter = _ref.bidiCharacter,
|
|
51
|
+
children = _ref.children,
|
|
52
|
+
testId = _ref.testId;
|
|
53
|
+
var bidiCharacterCode = getBidiCharacterCode(bidiCharacter);
|
|
54
|
+
return (0, _core.jsx)("span", {
|
|
55
|
+
"aria-label": bidiCharacterCode
|
|
56
|
+
}, (0, _core.jsx)("span", {
|
|
57
|
+
css: decoration,
|
|
58
|
+
"data-testid": testId,
|
|
59
|
+
"data-bidi-character-code": bidiCharacterCode // This is set to true so that the content is not read out by
|
|
60
|
+
// screen readers as the content includes angle brackets for
|
|
61
|
+
// visual decoration purposes.
|
|
62
|
+
// We use a span with the aria-label set to the bidi character code
|
|
63
|
+
// above this span for screen readers.
|
|
64
|
+
,
|
|
65
|
+
"aria-hidden": "true"
|
|
66
|
+
}, children));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getBidiCharacterCode(bidiCharacter) {
|
|
70
|
+
var _bidiCharacter$codePo;
|
|
71
|
+
|
|
72
|
+
var bidiCharacterCode = (_bidiCharacter$codePo = bidiCharacter.codePointAt(0)) === null || _bidiCharacter$codePo === void 0 ? void 0 : _bidiCharacter$codePo.toString(16);
|
|
73
|
+
return "U+".concat(bidiCharacterCode);
|
|
74
|
+
}
|
package/dist/cjs/code-block.js
CHANGED
|
@@ -19,6 +19,8 @@ var _styles = require("./internal/theme/styles");
|
|
|
19
19
|
|
|
20
20
|
var _getNormalizedLanguage = require("./internal/utils/get-normalized-language");
|
|
21
21
|
|
|
22
|
+
var _reactSyntaxHighlighterBidiWarningRenderer = require("./react-syntax-highlighter-bidi-warning-renderer");
|
|
23
|
+
|
|
22
24
|
/** @jsx jsx */
|
|
23
25
|
var CodeBlock = /*#__PURE__*/(0, _react.memo)(function CodeBlock(_ref) {
|
|
24
26
|
var _ref$showLineNumbers = _ref.showLineNumbers,
|
|
@@ -32,7 +34,10 @@ var CodeBlock = /*#__PURE__*/(0, _react.memo)(function CodeBlock(_ref) {
|
|
|
32
34
|
_ref$highlightedEndTe = _ref.highlightedEndText,
|
|
33
35
|
highlightedEndText = _ref$highlightedEndTe === void 0 ? 'Highlight end' : _ref$highlightedEndTe,
|
|
34
36
|
testId = _ref.testId,
|
|
35
|
-
text = _ref.text
|
|
37
|
+
text = _ref.text,
|
|
38
|
+
_ref$codeBidiWarnings = _ref.codeBidiWarnings,
|
|
39
|
+
codeBidiWarnings = _ref$codeBidiWarnings === void 0 ? true : _ref$codeBidiWarnings,
|
|
40
|
+
codeBidiWarningLabel = _ref.codeBidiWarningLabel;
|
|
36
41
|
var numLines = (text || '').split('\n').length;
|
|
37
42
|
var globalTheme = (0, _components.useGlobalTheme)();
|
|
38
43
|
var theme = (0, _react.useMemo)(function () {
|
|
@@ -60,6 +65,7 @@ var CodeBlock = /*#__PURE__*/(0, _react.memo)(function CodeBlock(_ref) {
|
|
|
60
65
|
}, [providedLanguage]); // https://product-fabric.atlassian.net/browse/DST-2472
|
|
61
66
|
|
|
62
67
|
var languageToUse = text ? language : 'text';
|
|
68
|
+
var renderer = codeBidiWarnings ? (0, _reactSyntaxHighlighterBidiWarningRenderer.createBidiWarningRenderer)(codeBidiWarningLabel) : undefined;
|
|
63
69
|
return (0, _core.jsx)(_reactSyntaxHighlighter.PrismAsync, {
|
|
64
70
|
"data-testid": testId,
|
|
65
71
|
"data-code-lang": language,
|
|
@@ -71,7 +77,8 @@ var CodeBlock = /*#__PURE__*/(0, _react.memo)(function CodeBlock(_ref) {
|
|
|
71
77
|
,
|
|
72
78
|
wrapLines: highlight.length > 0 || !!testId,
|
|
73
79
|
lineProps: getLineProps,
|
|
74
|
-
useInlineStyles: false
|
|
80
|
+
useInlineStyles: false,
|
|
81
|
+
renderer: renderer
|
|
75
82
|
}, text);
|
|
76
83
|
});
|
|
77
84
|
CodeBlock.displayName = 'CodeBlock';
|
package/dist/cjs/code.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
4
|
|
|
5
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
6
|
+
|
|
5
7
|
Object.defineProperty(exports, "__esModule", {
|
|
6
8
|
value: true
|
|
7
9
|
});
|
|
@@ -17,14 +19,22 @@ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")
|
|
|
17
19
|
|
|
18
20
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
19
21
|
|
|
20
|
-
var _react = require("react");
|
|
22
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
21
23
|
|
|
22
24
|
var _core = require("@emotion/core");
|
|
23
25
|
|
|
24
26
|
var _components = require("@atlaskit/theme/components");
|
|
25
27
|
|
|
28
|
+
var _bidiWarning = _interopRequireDefault(require("./bidi-warning"));
|
|
29
|
+
|
|
30
|
+
var _bidiWarningDecorator = _interopRequireDefault(require("./bidi-warning/bidi-warning-decorator"));
|
|
31
|
+
|
|
26
32
|
var _styles = require("./internal/theme/styles");
|
|
27
33
|
|
|
34
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
35
|
+
|
|
36
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
37
|
+
|
|
28
38
|
/** @jsx jsx */
|
|
29
39
|
var Code = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.forwardRef)(function Code(_ref, ref) {
|
|
30
40
|
var testId = _ref.testId,
|
|
@@ -33,12 +43,59 @@ var Code = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.forwardRef)(fu
|
|
|
33
43
|
var styles = (0, _react.useMemo)(function () {
|
|
34
44
|
return (0, _styles.getCodeStyles)(theme);
|
|
35
45
|
}, [theme]);
|
|
46
|
+
var children = props.children,
|
|
47
|
+
_props$codeBidiWarnin = props.codeBidiWarnings,
|
|
48
|
+
codeBidiWarnings = _props$codeBidiWarnin === void 0 ? true : _props$codeBidiWarnin,
|
|
49
|
+
codeBidiWarningLabel = props.codeBidiWarningLabel,
|
|
50
|
+
otherProps = (0, _objectWithoutProperties2.default)(props, ["children", "codeBidiWarnings", "codeBidiWarningLabel"]);
|
|
51
|
+
var decoratedChildren = codeBidiWarnings ? (0, _core.jsx)(RenderCodeChildrenWithBidiWarnings, {
|
|
52
|
+
codeBidiWarningLabel: codeBidiWarningLabel
|
|
53
|
+
}, children) : children;
|
|
36
54
|
return (0, _core.jsx)("code", (0, _extends2.default)({
|
|
37
55
|
ref: ref,
|
|
38
56
|
"data-testid": testId,
|
|
39
57
|
css: styles
|
|
40
|
-
},
|
|
58
|
+
}, otherProps), decoratedChildren);
|
|
41
59
|
}));
|
|
60
|
+
|
|
61
|
+
function RenderCodeChildrenWithBidiWarnings(_ref2) {
|
|
62
|
+
var children = _ref2.children,
|
|
63
|
+
codeBidiWarningLabel = _ref2.codeBidiWarningLabel;
|
|
64
|
+
|
|
65
|
+
var replacedChildren = _react.default.Children.map(children, function (childNode) {
|
|
66
|
+
if (typeof childNode === 'string') {
|
|
67
|
+
var decorated = (0, _bidiWarningDecorator.default)(childNode, function (_ref3) {
|
|
68
|
+
var bidiCharacter = _ref3.bidiCharacter,
|
|
69
|
+
index = _ref3.index;
|
|
70
|
+
return (0, _core.jsx)(_bidiWarning.default, {
|
|
71
|
+
bidiCharacter: bidiCharacter,
|
|
72
|
+
key: index,
|
|
73
|
+
label: codeBidiWarningLabel
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
return decorated;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (isReactElement(childNode) && childNode.props.children) {
|
|
80
|
+
var newChildNode = /*#__PURE__*/_react.default.cloneElement(childNode, {
|
|
81
|
+
children: (0, _core.jsx)(RenderCodeChildrenWithBidiWarnings, {
|
|
82
|
+
codeBidiWarningLabel: codeBidiWarningLabel
|
|
83
|
+
}, childNode.props.children)
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return newChildNode;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return childNode;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return (0, _core.jsx)(_react.default.Fragment, null, replacedChildren);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function isReactElement(child) {
|
|
96
|
+
return !!child.type;
|
|
97
|
+
}
|
|
98
|
+
|
|
42
99
|
Code.displayName = 'Code';
|
|
43
100
|
var _default = Code;
|
|
44
101
|
exports.default = _default;
|
|
@@ -40,8 +40,7 @@ var useHighlightLines = function useHighlightLines(_ref) {
|
|
|
40
40
|
if (num.indexOf('-') > 0) {
|
|
41
41
|
// We found a line group, e.g. 1-3
|
|
42
42
|
var _num$split$map$sort = num.split('-').map(Number) // Sort by lowest value first, highest value last.
|
|
43
|
-
.
|
|
44
|
-
sort(function (a, b) {
|
|
43
|
+
.sort(function (a, b) {
|
|
45
44
|
return a - b;
|
|
46
45
|
}),
|
|
47
46
|
_num$split$map$sort2 = (0, _slicedToArray2.default)(_num$split$map$sort, 2),
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.createBidiWarningRenderer = createBidiWarningRenderer;
|
|
9
|
+
exports.createStyleObject = createStyleObject;
|
|
10
|
+
exports.createClassNameString = createClassNameString;
|
|
11
|
+
exports.createChildren = createChildren;
|
|
12
|
+
|
|
13
|
+
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
14
|
+
|
|
15
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
16
|
+
|
|
17
|
+
var _react = _interopRequireDefault(require("react"));
|
|
18
|
+
|
|
19
|
+
var _bidiWarning = _interopRequireDefault(require("./bidi-warning"));
|
|
20
|
+
|
|
21
|
+
var _bidiWarningDecorator = _interopRequireDefault(require("./bidi-warning/bidi-warning-decorator"));
|
|
22
|
+
|
|
23
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
24
|
+
|
|
25
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
26
|
+
|
|
27
|
+
// File mostly vendored from react-syntax-highlighter
|
|
28
|
+
//
|
|
29
|
+
// - https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/efc3f7b7537d1729193b7a472067bcbe6cbecaf1/src/highlight.js#L272-L281
|
|
30
|
+
// - https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/efc3f7b7537d1729193b7a472067bcbe6cbecaf1/src/create-element.js
|
|
31
|
+
//
|
|
32
|
+
// Patching react syntax-highlighter with a decoration feature is likely preferable
|
|
33
|
+
function createBidiWarningRenderer(codeBidiWarningLabel) {
|
|
34
|
+
return function bidiWarningRenderer(_ref) {
|
|
35
|
+
var rows = _ref.rows,
|
|
36
|
+
stylesheet = _ref.stylesheet,
|
|
37
|
+
useInlineStyles = _ref.useInlineStyles;
|
|
38
|
+
return rows.map(function (node, i) {
|
|
39
|
+
return createElement({
|
|
40
|
+
node: node,
|
|
41
|
+
stylesheet: stylesheet,
|
|
42
|
+
useInlineStyles: useInlineStyles,
|
|
43
|
+
codeBidiWarningLabel: codeBidiWarningLabel,
|
|
44
|
+
key: "code-segement".concat(i)
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
} // Get all possible permutations of all power sets
|
|
49
|
+
//
|
|
50
|
+
// Super simple, non-algorithmic solution since the
|
|
51
|
+
// number of class names will not be greater than 4
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
function powerSetPermutations(arr) {
|
|
55
|
+
var arrLength = arr.length;
|
|
56
|
+
|
|
57
|
+
if (arrLength === 0 || arrLength === 1) {
|
|
58
|
+
return arr;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (arrLength === 2) {
|
|
62
|
+
// prettier-ignore
|
|
63
|
+
return [arr[0], arr[1], "".concat(arr[0], ".").concat(arr[1]), "".concat(arr[1], ".").concat(arr[0])];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (arrLength === 3) {
|
|
67
|
+
return [arr[0], arr[1], arr[2], "".concat(arr[0], ".").concat(arr[1]), "".concat(arr[0], ".").concat(arr[2]), "".concat(arr[1], ".").concat(arr[0]), "".concat(arr[1], ".").concat(arr[2]), "".concat(arr[2], ".").concat(arr[0]), "".concat(arr[2], ".").concat(arr[1]), "".concat(arr[0], ".").concat(arr[1], ".").concat(arr[2]), "".concat(arr[0], ".").concat(arr[2], ".").concat(arr[1]), "".concat(arr[1], ".").concat(arr[0], ".").concat(arr[2]), "".concat(arr[1], ".").concat(arr[2], ".").concat(arr[0]), "".concat(arr[2], ".").concat(arr[0], ".").concat(arr[1]), "".concat(arr[2], ".").concat(arr[1], ".").concat(arr[0])];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (arrLength >= 4) {
|
|
71
|
+
// Currently does not support more than 4 extra
|
|
72
|
+
// class names (after `.token` has been removed)
|
|
73
|
+
return [arr[0], arr[1], arr[2], arr[3], "".concat(arr[0], ".").concat(arr[1]), "".concat(arr[0], ".").concat(arr[2]), "".concat(arr[0], ".").concat(arr[3]), "".concat(arr[1], ".").concat(arr[0]), "".concat(arr[1], ".").concat(arr[2]), "".concat(arr[1], ".").concat(arr[3]), "".concat(arr[2], ".").concat(arr[0]), "".concat(arr[2], ".").concat(arr[1]), "".concat(arr[2], ".").concat(arr[3]), "".concat(arr[3], ".").concat(arr[0]), "".concat(arr[3], ".").concat(arr[1]), "".concat(arr[3], ".").concat(arr[2]), "".concat(arr[0], ".").concat(arr[1], ".").concat(arr[2]), "".concat(arr[0], ".").concat(arr[1], ".").concat(arr[3]), "".concat(arr[0], ".").concat(arr[2], ".").concat(arr[1]), "".concat(arr[0], ".").concat(arr[2], ".").concat(arr[3]), "".concat(arr[0], ".").concat(arr[3], ".").concat(arr[1]), "".concat(arr[0], ".").concat(arr[3], ".").concat(arr[2]), "".concat(arr[1], ".").concat(arr[0], ".").concat(arr[2]), "".concat(arr[1], ".").concat(arr[0], ".").concat(arr[3]), "".concat(arr[1], ".").concat(arr[2], ".").concat(arr[0]), "".concat(arr[1], ".").concat(arr[2], ".").concat(arr[3]), "".concat(arr[1], ".").concat(arr[3], ".").concat(arr[0]), "".concat(arr[1], ".").concat(arr[3], ".").concat(arr[2]), "".concat(arr[2], ".").concat(arr[0], ".").concat(arr[1]), "".concat(arr[2], ".").concat(arr[0], ".").concat(arr[3]), "".concat(arr[2], ".").concat(arr[1], ".").concat(arr[0]), "".concat(arr[2], ".").concat(arr[1], ".").concat(arr[3]), "".concat(arr[2], ".").concat(arr[3], ".").concat(arr[0]), "".concat(arr[2], ".").concat(arr[3], ".").concat(arr[1]), "".concat(arr[3], ".").concat(arr[0], ".").concat(arr[1]), "".concat(arr[3], ".").concat(arr[0], ".").concat(arr[2]), "".concat(arr[3], ".").concat(arr[1], ".").concat(arr[0]), "".concat(arr[3], ".").concat(arr[1], ".").concat(arr[2]), "".concat(arr[3], ".").concat(arr[2], ".").concat(arr[0]), "".concat(arr[3], ".").concat(arr[2], ".").concat(arr[1]), "".concat(arr[0], ".").concat(arr[1], ".").concat(arr[2], ".").concat(arr[3]), "".concat(arr[0], ".").concat(arr[1], ".").concat(arr[3], ".").concat(arr[2]), "".concat(arr[0], ".").concat(arr[2], ".").concat(arr[1], ".").concat(arr[3]), "".concat(arr[0], ".").concat(arr[2], ".").concat(arr[3], ".").concat(arr[1]), "".concat(arr[0], ".").concat(arr[3], ".").concat(arr[1], ".").concat(arr[2]), "".concat(arr[0], ".").concat(arr[3], ".").concat(arr[2], ".").concat(arr[1]), "".concat(arr[1], ".").concat(arr[0], ".").concat(arr[2], ".").concat(arr[3]), "".concat(arr[1], ".").concat(arr[0], ".").concat(arr[3], ".").concat(arr[2]), "".concat(arr[1], ".").concat(arr[2], ".").concat(arr[0], ".").concat(arr[3]), "".concat(arr[1], ".").concat(arr[2], ".").concat(arr[3], ".").concat(arr[0]), "".concat(arr[1], ".").concat(arr[3], ".").concat(arr[0], ".").concat(arr[2]), "".concat(arr[1], ".").concat(arr[3], ".").concat(arr[2], ".").concat(arr[0]), "".concat(arr[2], ".").concat(arr[0], ".").concat(arr[1], ".").concat(arr[3]), "".concat(arr[2], ".").concat(arr[0], ".").concat(arr[3], ".").concat(arr[1]), "".concat(arr[2], ".").concat(arr[1], ".").concat(arr[0], ".").concat(arr[3]), "".concat(arr[2], ".").concat(arr[1], ".").concat(arr[3], ".").concat(arr[0]), "".concat(arr[2], ".").concat(arr[3], ".").concat(arr[0], ".").concat(arr[1]), "".concat(arr[2], ".").concat(arr[3], ".").concat(arr[1], ".").concat(arr[0]), "".concat(arr[3], ".").concat(arr[0], ".").concat(arr[1], ".").concat(arr[2]), "".concat(arr[3], ".").concat(arr[0], ".").concat(arr[2], ".").concat(arr[1]), "".concat(arr[3], ".").concat(arr[1], ".").concat(arr[0], ".").concat(arr[2]), "".concat(arr[3], ".").concat(arr[1], ".").concat(arr[2], ".").concat(arr[0]), "".concat(arr[3], ".").concat(arr[2], ".").concat(arr[0], ".").concat(arr[1]), "".concat(arr[3], ".").concat(arr[2], ".").concat(arr[1], ".").concat(arr[0])];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
var classNameCombinations = {};
|
|
78
|
+
|
|
79
|
+
function getClassNameCombinations(classNames) {
|
|
80
|
+
if (classNames.length === 0 || classNames.length === 1) {
|
|
81
|
+
return classNames;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
var key = classNames.join('.');
|
|
85
|
+
|
|
86
|
+
if (!classNameCombinations[key]) {
|
|
87
|
+
classNameCombinations[key] = powerSetPermutations(classNames);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return classNameCombinations[key];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function createStyleObject(classNames) {
|
|
94
|
+
var elementStyle = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
95
|
+
var stylesheet = arguments.length > 2 ? arguments[2] : undefined;
|
|
96
|
+
var nonTokenClassNames = classNames.filter(function (className) {
|
|
97
|
+
return className !== 'token';
|
|
98
|
+
});
|
|
99
|
+
var classNamesCombinations = getClassNameCombinations(nonTokenClassNames);
|
|
100
|
+
return classNamesCombinations.reduce(function (styleObject, className) {
|
|
101
|
+
return _objectSpread(_objectSpread({}, styleObject), stylesheet[className]);
|
|
102
|
+
}, elementStyle);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function createClassNameString(classNames) {
|
|
106
|
+
return classNames.join(' ');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function createChildren(stylesheet, useInlineStyles, codeBidiWarningLabel) {
|
|
110
|
+
var childrenCount = 0;
|
|
111
|
+
return function (children) {
|
|
112
|
+
childrenCount += 1;
|
|
113
|
+
return children.map(function (child, i) {
|
|
114
|
+
return createElement({
|
|
115
|
+
node: child,
|
|
116
|
+
stylesheet: stylesheet,
|
|
117
|
+
useInlineStyles: useInlineStyles,
|
|
118
|
+
codeBidiWarningLabel: codeBidiWarningLabel,
|
|
119
|
+
key: "code-segment-".concat(childrenCount, "-").concat(i)
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function createElement(_ref2) {
|
|
126
|
+
var node = _ref2.node,
|
|
127
|
+
stylesheet = _ref2.stylesheet,
|
|
128
|
+
_ref2$style = _ref2.style,
|
|
129
|
+
style = _ref2$style === void 0 ? {} : _ref2$style,
|
|
130
|
+
useInlineStyles = _ref2.useInlineStyles,
|
|
131
|
+
codeBidiWarningLabel = _ref2.codeBidiWarningLabel,
|
|
132
|
+
key = _ref2.key;
|
|
133
|
+
var properties = node.properties,
|
|
134
|
+
type = node.type,
|
|
135
|
+
TagName = node.tagName,
|
|
136
|
+
value = node.value;
|
|
137
|
+
|
|
138
|
+
if (type === 'text') {
|
|
139
|
+
// occasionally react-syntax-highlighter passes a numeric value when the
|
|
140
|
+
// type is text
|
|
141
|
+
var textValue = value + '';
|
|
142
|
+
var decorated = (0, _bidiWarningDecorator.default)(textValue, function (_ref3) {
|
|
143
|
+
var bidiCharacter = _ref3.bidiCharacter,
|
|
144
|
+
index = _ref3.index;
|
|
145
|
+
return /*#__PURE__*/_react.default.createElement(_bidiWarning.default, {
|
|
146
|
+
bidiCharacter: bidiCharacter,
|
|
147
|
+
key: index,
|
|
148
|
+
label: codeBidiWarningLabel
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
return decorated;
|
|
152
|
+
} else if (TagName) {
|
|
153
|
+
var childrenCreator = createChildren(stylesheet, useInlineStyles, codeBidiWarningLabel);
|
|
154
|
+
var props;
|
|
155
|
+
|
|
156
|
+
if (!useInlineStyles) {
|
|
157
|
+
props = _objectSpread(_objectSpread({}, properties), {}, {
|
|
158
|
+
className: createClassNameString(properties.className)
|
|
159
|
+
});
|
|
160
|
+
} else {
|
|
161
|
+
var allStylesheetSelectors = Object.keys(stylesheet).reduce(function (classes, selector) {
|
|
162
|
+
selector.split('.').forEach(function (className) {
|
|
163
|
+
if (!classes.includes(className)) {
|
|
164
|
+
classes.push(className);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
return classes;
|
|
168
|
+
}, []); // For compatibility with older versions of react-syntax-highlighter
|
|
169
|
+
|
|
170
|
+
var startingClassName = properties.className && properties.className.includes('token') ? ['token'] : [];
|
|
171
|
+
var className = properties.className && startingClassName.concat(properties.className.filter(function (className) {
|
|
172
|
+
return !allStylesheetSelectors.includes(className);
|
|
173
|
+
}));
|
|
174
|
+
props = _objectSpread(_objectSpread({}, properties), {}, {
|
|
175
|
+
className: createClassNameString(className) || undefined,
|
|
176
|
+
style: createStyleObject(properties.className, Object.assign({}, properties.style, style), stylesheet)
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
var children = childrenCreator(node.children);
|
|
181
|
+
return /*#__PURE__*/_react.default.createElement(TagName, (0, _extends2.default)({
|
|
182
|
+
key: key
|
|
183
|
+
}, props), children);
|
|
184
|
+
}
|
|
185
|
+
}
|
package/dist/cjs/version.json
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export const bidiCharacterRegex = /[\u202A-\u202E\u2066-\u2069]/g;
|
|
2
|
+
export default function codeBidiWarningDecorator(originalText, decorate) {
|
|
3
|
+
const matches = [...originalText.matchAll(bidiCharacterRegex)];
|
|
4
|
+
|
|
5
|
+
if (matches.length === 0) {
|
|
6
|
+
// No matches encountered, so we return the originalText value
|
|
7
|
+
return originalText;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let children = [];
|
|
11
|
+
let mappedTo = 0;
|
|
12
|
+
|
|
13
|
+
for (const match of matches) {
|
|
14
|
+
if (mappedTo !== match.index) {
|
|
15
|
+
// There were unmatched characters prior to this match which haven't been
|
|
16
|
+
// mapped to the children.
|
|
17
|
+
// Add them as plain text.
|
|
18
|
+
children.push(originalText.substring(mappedTo, match.index));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
children.push(decorate({
|
|
22
|
+
bidiCharacter: match[0],
|
|
23
|
+
index: match.index
|
|
24
|
+
})); // While index is guaranteed to be present, it needs to be asserted due
|
|
25
|
+
// to a limitation of typescripts regex handling
|
|
26
|
+
//
|
|
27
|
+
// https://github.com/microsoft/TypeScript/issues/36788
|
|
28
|
+
// Decorate bidi character
|
|
29
|
+
|
|
30
|
+
mappedTo = match.index + match[0].length;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (mappedTo !== originalText.length) {
|
|
34
|
+
// There is text following the final match, which needs to be mapped
|
|
35
|
+
// to the children.
|
|
36
|
+
// Added as plain text.
|
|
37
|
+
children.push(originalText.substring(mappedTo, originalText.length));
|
|
38
|
+
} // return the mapped children with decorated bidi characters
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
return children;
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './ui';
|