@atlaskit/code 14.4.8 → 14.5.1

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.
Files changed (81) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/cjs/bidi-warning/bidi-warning-decorator.js +8 -0
  3. package/dist/cjs/bidi-warning/ui/index.js +8 -0
  4. package/dist/cjs/code-block.js +22 -20
  5. package/dist/cjs/internal/theme/styles.js +20 -9
  6. package/dist/cjs/syntax-highlighter/async.js +88 -0
  7. package/dist/cjs/syntax-highlighter/index.js +44 -0
  8. package/dist/cjs/syntax-highlighter/lib/highlight.js +91 -0
  9. package/dist/cjs/syntax-highlighter/lib/process/create-line-element.js +74 -0
  10. package/dist/cjs/syntax-highlighter/lib/process/create-line.js +32 -0
  11. package/dist/cjs/syntax-highlighter/lib/process/flatten-code-tree.js +29 -0
  12. package/dist/cjs/syntax-highlighter/lib/process/get-code-tree.js +22 -0
  13. package/dist/cjs/syntax-highlighter/lib/process/get-inline-line-number.js +23 -0
  14. package/dist/cjs/syntax-highlighter/lib/process/index.js +122 -0
  15. package/dist/cjs/syntax-highlighter/lib/react-renderer/create-children.js +21 -0
  16. package/dist/cjs/syntax-highlighter/lib/react-renderer/create-element.js +57 -0
  17. package/dist/cjs/syntax-highlighter/lib/react-renderer/index.js +30 -0
  18. package/dist/cjs/syntax-highlighter/types.js +31 -0
  19. package/dist/cjs/version.json +1 -1
  20. package/dist/es2019/bidi-warning/bidi-warning-decorator.js +8 -0
  21. package/dist/es2019/bidi-warning/ui/index.js +8 -0
  22. package/dist/es2019/code-block.js +18 -18
  23. package/dist/es2019/internal/theme/styles.js +20 -9
  24. package/dist/es2019/syntax-highlighter/async.js +51 -0
  25. package/dist/es2019/syntax-highlighter/index.js +38 -0
  26. package/dist/es2019/syntax-highlighter/lib/highlight.js +73 -0
  27. package/dist/es2019/syntax-highlighter/lib/process/create-line-element.js +65 -0
  28. package/dist/es2019/syntax-highlighter/lib/process/create-line.js +23 -0
  29. package/dist/es2019/syntax-highlighter/lib/process/flatten-code-tree.js +18 -0
  30. package/dist/es2019/syntax-highlighter/lib/process/get-code-tree.js +16 -0
  31. package/dist/es2019/syntax-highlighter/lib/process/get-inline-line-number.js +17 -0
  32. package/dist/es2019/syntax-highlighter/lib/process/index.js +114 -0
  33. package/dist/es2019/syntax-highlighter/lib/react-renderer/create-children.js +12 -0
  34. package/dist/es2019/syntax-highlighter/lib/react-renderer/create-element.js +53 -0
  35. package/dist/es2019/syntax-highlighter/lib/react-renderer/index.js +23 -0
  36. package/dist/es2019/syntax-highlighter/types.js +9 -0
  37. package/dist/es2019/version.json +1 -1
  38. package/dist/esm/bidi-warning/bidi-warning-decorator.js +8 -0
  39. package/dist/esm/bidi-warning/ui/index.js +8 -0
  40. package/dist/esm/code-block.js +20 -19
  41. package/dist/esm/internal/theme/styles.js +20 -9
  42. package/dist/esm/syntax-highlighter/async.js +76 -0
  43. package/dist/esm/syntax-highlighter/index.js +38 -0
  44. package/dist/esm/syntax-highlighter/lib/highlight.js +84 -0
  45. package/dist/esm/syntax-highlighter/lib/process/create-line-element.js +67 -0
  46. package/dist/esm/syntax-highlighter/lib/process/create-line.js +24 -0
  47. package/dist/esm/syntax-highlighter/lib/process/flatten-code-tree.js +22 -0
  48. package/dist/esm/syntax-highlighter/lib/process/get-code-tree.js +16 -0
  49. package/dist/esm/syntax-highlighter/lib/process/get-inline-line-number.js +17 -0
  50. package/dist/esm/syntax-highlighter/lib/process/index.js +115 -0
  51. package/dist/esm/syntax-highlighter/lib/react-renderer/create-children.js +14 -0
  52. package/dist/esm/syntax-highlighter/lib/react-renderer/create-element.js +50 -0
  53. package/dist/esm/syntax-highlighter/lib/react-renderer/index.js +24 -0
  54. package/dist/esm/syntax-highlighter/types.js +24 -0
  55. package/dist/esm/version.json +1 -1
  56. package/dist/types/bidi-warning/bidi-warning-decorator.d.ts +7 -0
  57. package/dist/types/bidi-warning/ui/index.d.ts +8 -0
  58. package/dist/types/bidi-warning/ui/types.d.ts +11 -15
  59. package/dist/types/extract-react-types/code-block.d.ts +24 -14
  60. package/dist/types/internal/theme/styles.d.ts +1 -1
  61. package/dist/types/internal/types.d.ts +31 -18
  62. package/dist/types/syntax-highlighter/async.d.ts +3 -0
  63. package/dist/types/syntax-highlighter/index.d.ts +36 -0
  64. package/dist/types/syntax-highlighter/lib/highlight.d.ts +12 -0
  65. package/dist/types/syntax-highlighter/lib/process/create-line-element.d.ts +9 -0
  66. package/dist/types/syntax-highlighter/lib/process/create-line.d.ts +3 -0
  67. package/dist/types/syntax-highlighter/lib/process/flatten-code-tree.d.ts +2 -0
  68. package/dist/types/syntax-highlighter/lib/process/get-code-tree.d.ts +2 -0
  69. package/dist/types/syntax-highlighter/lib/process/get-inline-line-number.d.ts +2 -0
  70. package/dist/types/syntax-highlighter/lib/process/index.d.ts +17 -0
  71. package/dist/types/syntax-highlighter/lib/react-renderer/create-children.d.ts +3 -0
  72. package/dist/types/syntax-highlighter/lib/react-renderer/create-element.d.ts +7 -0
  73. package/dist/types/syntax-highlighter/lib/react-renderer/index.d.ts +15 -0
  74. package/dist/types/syntax-highlighter/types.d.ts +129 -0
  75. package/dist/types/types.d.ts +10 -11
  76. package/package.json +14 -10
  77. package/report.api.md +1 -0
  78. package/dist/cjs/react-syntax-highlighter-bidi-warning-renderer.js +0 -156
  79. package/dist/es2019/react-syntax-highlighter-bidi-warning-renderer.js +0 -149
  80. package/dist/esm/react-syntax-highlighter-bidi-warning-renderer.js +0 -155
  81. package/dist/types/react-syntax-highlighter-bidi-warning-renderer.d.ts +0 -9
package/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # @atlaskit/code
2
2
 
3
+ ## 14.5.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [`261420360ec`](https://bitbucket.org/atlassian/atlassian-frontend/commits/261420360ec) - Upgrades component types to support React 18.
8
+ - Updated dependencies
9
+
10
+ ## 14.5.0
11
+
12
+ ### Minor Changes
13
+
14
+ - [`f5ca132a05e`](https://bitbucket.org/atlassian/atlassian-frontend/commits/f5ca132a05e) - [ux] CodeBlock has had some significant under-the-hood changes in this version. While technically it is a minor update as all API changes are additive and backwards-compatible, this is a heads up that there are some small visual changes that may require visual regression snapshots to be updated.
15
+
16
+ - Adds a new `shouldWrapLongLines` prop that allows you to wrap long lines of code over multiple lines, instead of always forcing the container to scroll horizontally.
17
+
18
+ - Fixes a frustrating bug where copying code from `CodeBlock` would also copy line numbers — this no longer occurs!
19
+
20
+ - Replaces the dependency of `react-syntax-highlighter` with our own implementation that is more maintainable and performant. This also lead to a bump in an underlying dependency `prismjs` which added new syntax highlighting features for some languages. This may cause some blocks of code to look slightly different, potentially impacting VR tests — some changes are expected! Reach out to #help-design-system if you have any questions.
21
+
22
+ - Improves the presentation and security of `CodeBlock`'s with bidirectional characters to present the source code exactly how it was written. The bidirectional characters themselves are still highlighted in the same way as before with a warning and a tooltip. This may also impact VR tests, but is expected.
23
+
24
+ ### Patch Changes
25
+
26
+ - Updated dependencies
27
+
3
28
  ## 14.4.8
4
29
 
5
30
  ### Patch Changes
@@ -11,6 +11,14 @@ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol
11
11
  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); }
12
12
  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; }
13
13
  var bidiCharacterRegex = /[\u202A-\u202E\u2066-\u2069]/g;
14
+
15
+ /**
16
+ * __Code Bidi Warning Decorator__
17
+ *
18
+ * Checks the code to see if it contains any bidi characters.
19
+ * In case if bidi characters found - returns children with decorated
20
+ * bidi characters. If no bidi characters found - original text returned.
21
+ */
14
22
  exports.bidiCharacterRegex = bidiCharacterRegex;
15
23
  function codeBidiWarningDecorator(originalText, decorate) {
16
24
  var matches = (0, _toConsumableArray2.default)(originalText.matchAll(bidiCharacterRegex));
@@ -11,6 +11,14 @@ var _react = _interopRequireDefault(require("react"));
11
11
  var _tooltip = _interopRequireDefault(require("@atlaskit/tooltip"));
12
12
  var _styled = require("./styled");
13
13
  var _excluded = ["children"];
14
+ /**
15
+ * __Bidi Warning__
16
+ *
17
+ * A component used to render a bidi character warning.
18
+ * A bidi character can be used to perform a "bidi override attack".
19
+ *
20
+ * See https://hello.atlassian.net/wiki/spaces/PRODSEC/pages/1347434677/PSHELP-2943+Investigate+Trojan+Source+Attack+Vulnerability#1)-Providing-visual-cues-for-our-Customers-in-our-affected-products
21
+ */
14
22
  function BidiWarning(_ref) {
15
23
  var testId = _ref.testId,
16
24
  bidiCharacter = _ref.bidiCharacter,
@@ -1,17 +1,17 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
6
7
  exports.default = void 0;
7
8
  var _react = require("react");
8
9
  var _react2 = require("@emotion/react");
9
- var _reactSyntaxHighlighter = require("react-syntax-highlighter");
10
10
  var _components = require("@atlaskit/theme/components");
11
11
  var _useHighlight = require("./internal/hooks/use-highlight");
12
12
  var _styles = require("./internal/theme/styles");
13
13
  var _getNormalizedLanguage = require("./internal/utils/get-normalized-language");
14
- var _reactSyntaxHighlighterBidiWarningRenderer = require("./react-syntax-highlighter-bidi-warning-renderer");
14
+ var _syntaxHighlighter = _interopRequireDefault(require("./syntax-highlighter"));
15
15
  /** @jsx jsx */
16
16
 
17
17
  /**
@@ -40,7 +40,9 @@ var CodeBlock = /*#__PURE__*/(0, _react.memo)(function CodeBlock(_ref) {
40
40
  codeBidiWarnings = _ref$codeBidiWarnings === void 0 ? true : _ref$codeBidiWarnings,
41
41
  codeBidiWarningLabel = _ref.codeBidiWarningLabel,
42
42
  _ref$codeBidiWarningT = _ref.codeBidiWarningTooltipEnabled,
43
- codeBidiWarningTooltipEnabled = _ref$codeBidiWarningT === void 0 ? true : _ref$codeBidiWarningT;
43
+ codeBidiWarningTooltipEnabled = _ref$codeBidiWarningT === void 0 ? true : _ref$codeBidiWarningT,
44
+ _ref$shouldWrapLongLi = _ref.shouldWrapLongLines,
45
+ shouldWrapLongLines = _ref$shouldWrapLongLi === void 0 ? false : _ref$shouldWrapLongLi;
44
46
  var numLines = (text || '').split('\n').length;
45
47
  var globalTheme = (0, _components.useGlobalTheme)();
46
48
  var theme = (0, _react.useMemo)(function () {
@@ -50,8 +52,8 @@ var CodeBlock = /*#__PURE__*/(0, _react.memo)(function CodeBlock(_ref) {
50
52
  return (0, _styles.getCodeBlockStyles)(theme);
51
53
  }, [theme]);
52
54
  var styles = (0, _react.useMemo)(function () {
53
- return (0, _react2.css)(getStyles(highlightedStartText, highlightedEndText, showLineNumbers));
54
- }, [highlightedStartText, highlightedEndText, showLineNumbers, getStyles]);
55
+ return (0, _react2.css)(getStyles(highlightedStartText, highlightedEndText, showLineNumbers, shouldWrapLongLines));
56
+ }, [highlightedStartText, highlightedEndText, showLineNumbers, shouldWrapLongLines, getStyles]);
55
57
  var _useHighlightLines = (0, _useHighlight.useHighlightLines)({
56
58
  highlight: highlight,
57
59
  testId: testId
@@ -67,25 +69,25 @@ var CodeBlock = /*#__PURE__*/(0, _react.memo)(function CodeBlock(_ref) {
67
69
 
68
70
  // https://product-fabric.atlassian.net/browse/DST-2472
69
71
  var languageToUse = text ? language : 'text';
70
- var renderer = codeBidiWarnings ? (0, _reactSyntaxHighlighterBidiWarningRenderer.createBidiWarningRenderer)({
71
- codeBidiWarningLabel: codeBidiWarningLabel,
72
- codeBidiWarningTooltipEnabled: codeBidiWarningTooltipEnabled
73
- }) : undefined;
74
- return (0, _react2.jsx)(_reactSyntaxHighlighter.PrismAsync, {
75
- "data-testid": testId,
72
+ return (0, _react2.jsx)(_syntaxHighlighter.default, {
76
73
  "data-code-lang": language,
77
74
  "data-ds--code--code-block": "",
78
- css: styles,
75
+ testId: testId,
79
76
  language: languageToUse,
80
- PreTag: "span",
81
- showLineNumbers: showLineNumbers
82
- // Wrap lines is needed to set styles on the line when highlighting.
77
+ css: styles,
78
+ showLineNumbers: showLineNumbers,
79
+ lineProps: getLineProps
80
+ // shouldCreateParentElementForLines is needed to pass down props to each line.
81
+ // This is necessary for both line highlighting and testId's, as each of
82
+ // these rely on a data attribute being passed down to lines.
83
83
  ,
84
- wrapLines: highlight.length > 0 || !!testId,
85
- lineProps: getLineProps,
86
- useInlineStyles: false,
87
- renderer: renderer
88
- }, text);
84
+ shouldCreateParentElementForLines: highlight.length > 0 || !!testId,
85
+ shouldWrapLongLines: shouldWrapLongLines,
86
+ codeBidiWarnings: codeBidiWarnings,
87
+ codeBidiWarningLabel: codeBidiWarningLabel,
88
+ codeBidiWarningTooltipEnabled: codeBidiWarningTooltipEnabled,
89
+ text: text
90
+ });
89
91
  });
90
92
  CodeBlock.displayName = 'CodeBlock';
91
93
  var _default = CodeBlock;
@@ -35,7 +35,13 @@ var lineNumberStyle = function lineNumberStyle(theme) {
35
35
  textAlign: 'right',
36
36
  userSelect: 'none',
37
37
  // this is to fix SSR spacing issue
38
- display: 'block'
38
+ display: 'block',
39
+ // This is how we are preventing line numbers being copied to clipboard.
40
+ // (`user-select: none;` was not sufficent).
41
+ // https://product-fabric.atlassian.net/browse/DSP-2729
42
+ '&::after': {
43
+ content: "attr(data-ds--line-number)"
44
+ }
39
45
  };
40
46
  };
41
47
 
@@ -211,12 +217,12 @@ var getBaseCodeStyles = function getBaseCodeStyles(theme) {
211
217
  */
212
218
  exports.getBaseCodeStyles = getBaseCodeStyles;
213
219
  var getCodeBlockStyles = function getCodeBlockStyles(theme) {
214
- return function (highlightedStartText, highlightedEndText, hasLineNumbers) {
220
+ return function (highlightedStartText, highlightedEndText, showLineNumbers, shouldWrapLongLines) {
215
221
  return _objectSpread(_objectSpread(_objectSpread({
216
222
  // this is required to account for prismjs styles leaking into the codeblock
217
223
  'code[class*="language-"], pre[class*="language-"], code': {
218
224
  all: 'unset',
219
- padding: hasLineNumbers ? "".concat(_constants2.SPACING, "px 0") : _constants2.SPACING
225
+ padding: showLineNumbers ? "".concat(_constants2.SPACING, "px 0") : _constants2.SPACING
220
226
  },
221
227
  display: 'flex',
222
228
  lineHeight: _constants2.CODE_LINE_HEIGHT,
@@ -225,14 +231,14 @@ var getCodeBlockStyles = function getCodeBlockStyles(theme) {
225
231
  direction: 'ltr'
226
232
  }, getBaseCodeStyles(theme)), syntaxKeywordColors(theme)), {}, {
227
233
  // this is to account for SSR spacing issue once loaded in browser
228
- '& .linenumber, .react-syntax-highlighter-line-number': lineNumberStyle(theme),
234
+ '& .linenumber, .ds-sh-line-number': lineNumberStyle(theme),
229
235
  '& .linenumber': {
230
236
  display: 'inline-block !important',
231
237
  float: 'left'
232
238
  },
233
239
  // these styles are for line highlighting
234
240
  '& [data-ds--code--row]': {
235
- display: 'block',
241
+ display: showLineNumbers ? 'flex' : 'block',
236
242
  paddingRight: "".concat(_constants2.SPACING, "px !important"),
237
243
  marginRight: "-".concat(_constants2.SPACING, "px")
238
244
  },
@@ -248,11 +254,12 @@ var getCodeBlockStyles = function getCodeBlockStyles(theme) {
248
254
  whiteSpace: 'nowrap',
249
255
  width: '1px'
250
256
  },
257
+ // The formatting here is an accessibility convention
251
258
  '&::before': {
252
- content: "\", ".concat(highlightedStartText, ", \"")
259
+ content: "\" [".concat(highlightedStartText, "] \"")
253
260
  },
254
261
  '&::after': {
255
- content: "\", ".concat(highlightedEndText, ", \"")
262
+ content: "\" [".concat(highlightedEndText, "] \"")
256
263
  }
257
264
  },
258
265
  '& [data-ds--code--row--highlight] .linenumber': {
@@ -277,14 +284,18 @@ var getCodeBlockStyles = function getCodeBlockStyles(theme) {
277
284
  },
278
285
  '& code:first-of-type': {
279
286
  paddingRight: "0px !important",
280
- backgroundImage: hasLineNumbers ? "linear-gradient(to right, var(".concat(_constants2.VAR_CODE_LINE_NUMBER_BG_COLOR, ",").concat(theme.lineNumberBgColor, "), var(").concat(_constants2.VAR_CODE_LINE_NUMBER_BG_COLOR, ",").concat(theme.lineNumberBgColor, ")\n calc(").concat(theme.lineNumberWidth, " + ").concat(_constants2.LINE_NUMBER_GUTTER, "px), transparent calc(").concat(theme.lineNumberWidth, " + ").concat(_constants2.LINE_NUMBER_GUTTER, "px), transparent)") : undefined
287
+ backgroundImage: showLineNumbers ? "linear-gradient(to right, var(".concat(_constants2.VAR_CODE_LINE_NUMBER_BG_COLOR, ",").concat(theme.lineNumberBgColor, "), var(").concat(_constants2.VAR_CODE_LINE_NUMBER_BG_COLOR, ",").concat(theme.lineNumberBgColor, ")\n calc(").concat(theme.lineNumberWidth, " + ").concat(_constants2.LINE_NUMBER_GUTTER, "px), transparent calc(").concat(theme.lineNumberWidth, " + ").concat(_constants2.LINE_NUMBER_GUTTER, "px), transparent)") : undefined
281
288
  },
282
289
  // we need to use last-of-type because when Code is SSR'd
283
290
  // 2 <code> elements are created and we don't want this style
284
291
  // applied to the first one
285
292
  '& code:last-of-type': {
286
293
  paddingRight: "".concat(_constants2.SPACING, "px !important"),
287
- flex: '1 0 auto'
294
+ flexBasis: 'auto',
295
+ flexGrow: 1,
296
+ // Needed for the highlight line to extend full-width
297
+ flexShrink: shouldWrapLongLines ? 1 : 0,
298
+ wordBreak: 'break-word'
288
299
  },
289
300
  // Prevents empty code blocks from vertically collapsing
290
301
  'code > span:only-child:empty:before, code > span:only-child > span:only-child:empty:before': {
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof = require("@babel/runtime/helpers/typeof");
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
10
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
12
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
13
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
14
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
15
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
16
+ var _react = _interopRequireDefault(require("react"));
17
+ var _highlight = _interopRequireDefault(require("./lib/highlight"));
18
+ 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); }
19
+ 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; }
20
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
21
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
22
+ // Uses the loader method of async bundling
23
+ // Instantiates highligher as a singleton, loading refractor only once per page (refractor/prism are singleton modules)
24
+ var generator = function generator(options) {
25
+ var loader = options.loader;
26
+
27
+ // eslint-disable-next-line @repo/internal/react/no-class-components
28
+ var AsyncHighlighter = /*#__PURE__*/function (_React$PureComponent) {
29
+ (0, _inherits2.default)(AsyncHighlighter, _React$PureComponent);
30
+ var _super = _createSuper(AsyncHighlighter);
31
+ function AsyncHighlighter() {
32
+ (0, _classCallCheck2.default)(this, AsyncHighlighter);
33
+ return _super.apply(this, arguments);
34
+ }
35
+ (0, _createClass2.default)(AsyncHighlighter, [{
36
+ key: "componentDidMount",
37
+ value: function componentDidMount() {
38
+ var _this = this;
39
+ if (!AsyncHighlighter.astGeneratorPromise) {
40
+ AsyncHighlighter.loadAstGenerator();
41
+ }
42
+ if (!AsyncHighlighter.astGenerator && AsyncHighlighter.astGeneratorPromise) {
43
+ AsyncHighlighter.astGeneratorPromise.then(function () {
44
+ _this.forceUpdate();
45
+ });
46
+ }
47
+ }
48
+ }, {
49
+ key: "render",
50
+ value: function render() {
51
+ return /*#__PURE__*/_react.default.createElement(AsyncHighlighter.highlightInstance, (0, _extends2.default)({}, this.props, {
52
+ astGenerator: AsyncHighlighter.astGenerator
53
+ }));
54
+ }
55
+ }], [{
56
+ key: "preload",
57
+ value:
58
+ // Useful in tests
59
+ function preload() {
60
+ return AsyncHighlighter.loadAstGenerator();
61
+ }
62
+ }, {
63
+ key: "loadAstGenerator",
64
+ value: function loadAstGenerator() {
65
+ AsyncHighlighter.astGeneratorPromise = loader().then(function (astGenerator) {
66
+ AsyncHighlighter.astGenerator = astGenerator;
67
+ return astGenerator;
68
+ });
69
+ return AsyncHighlighter.astGeneratorPromise;
70
+ }
71
+ }]);
72
+ return AsyncHighlighter;
73
+ }(_react.default.PureComponent);
74
+ (0, _defineProperty2.default)(AsyncHighlighter, "astGenerator", null);
75
+ (0, _defineProperty2.default)(AsyncHighlighter, "highlightInstance", _highlight.default);
76
+ return AsyncHighlighter;
77
+ };
78
+ var _default = generator({
79
+ loader: function loader() {
80
+ return Promise.resolve().then(function () {
81
+ return _interopRequireWildcard(require('refractor'));
82
+ }).then(function (module) {
83
+ // Webpack 3 returns module.exports as default as module, but webpack 4 returns module.exports as module.default
84
+ return module.default || module;
85
+ });
86
+ }
87
+ });
88
+ exports.default = _default;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _async = _interopRequireDefault(require("./async"));
9
+ /**
10
+ * Original library: https://github.com/react-syntax-highlighter/react-syntax-highlighter
11
+ * All modifications by Atlassian
12
+ */
13
+ /**
14
+ * MIT License
15
+ *
16
+ * Copyright (c) 2019 Conor Hastings
17
+ *
18
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
19
+ * of this software and associated documentation files (the "Software"), to deal
20
+ * in the Software without restriction, including without limitation the rights
21
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22
+ * copies of the Software, and to permit persons to whom the Software is
23
+ * furnished to do so, subject to the following conditions:
24
+ *
25
+ * The above copyright notice and this permission notice shall be included in all
26
+ * copies or substantial portions of the Software.
27
+ *
28
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
+ * SOFTWARE.
35
+ */
36
+ /**
37
+ * __Syntax Highlighter__
38
+ *
39
+ * A syntax highlighter based on top of [refractor](https://github.com/wooorm/refractor).
40
+ * @atlaskit/code uses this under-the-hood, but it is not intended for public consumption.
41
+ * You should be using @atlaskit/code directly.
42
+ */
43
+ var _default = _async.default;
44
+ exports.default = _default;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
10
+ var _react = require("react");
11
+ var _process = _interopRequireDefault(require("./process"));
12
+ var _reactRenderer = _interopRequireDefault(require("./react-renderer"));
13
+ var _excluded = ["language", "testId", "text", "codeTagProps", "showLineNumbers", "shouldCreateParentElementForLines", "shouldWrapLongLines", "lineProps", "codeBidiWarnings", "codeBidiWarningLabel", "codeBidiWarningTooltipEnabled", "astGenerator"];
14
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
15
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
16
+ /**
17
+ * Takes in a code string and (in the default behaviour):
18
+ * - Uses refractor to turn it into a tree structure with highlighting metadata
19
+ * - Collapses this tree into lines for a renderer
20
+ * - Passes these lines to a React renderer
21
+ *
22
+ * In future, the final step could have a custom renderer.
23
+ */
24
+ var Highlight = /*#__PURE__*/(0, _react.memo)(function Highlight(_ref) {
25
+ var _ref$language = _ref.language,
26
+ language = _ref$language === void 0 ? 'text' : _ref$language,
27
+ testId = _ref.testId,
28
+ _ref$text = _ref.text,
29
+ text = _ref$text === void 0 ? '' : _ref$text,
30
+ _ref$codeTagProps = _ref.codeTagProps,
31
+ codeTagProps = _ref$codeTagProps === void 0 ? {
32
+ className: "language-".concat(language)
33
+ } : _ref$codeTagProps,
34
+ _ref$showLineNumbers = _ref.showLineNumbers,
35
+ showLineNumbers = _ref$showLineNumbers === void 0 ? false : _ref$showLineNumbers,
36
+ _ref$shouldCreatePare = _ref.shouldCreateParentElementForLines,
37
+ shouldCreateParentElementForLines = _ref$shouldCreatePare === void 0 ? false : _ref$shouldCreatePare,
38
+ _ref$shouldWrapLongLi = _ref.shouldWrapLongLines,
39
+ shouldWrapLongLines = _ref$shouldWrapLongLi === void 0 ? false : _ref$shouldWrapLongLi,
40
+ _ref$lineProps = _ref.lineProps,
41
+ lineProps = _ref$lineProps === void 0 ? {} : _ref$lineProps,
42
+ codeBidiWarnings = _ref.codeBidiWarnings,
43
+ codeBidiWarningLabel = _ref.codeBidiWarningLabel,
44
+ codeBidiWarningTooltipEnabled = _ref.codeBidiWarningTooltipEnabled,
45
+ _ref$astGenerator = _ref.astGenerator,
46
+ astGenerator = _ref$astGenerator === void 0 ? null : _ref$astGenerator,
47
+ rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
48
+ // TODO maybe we call this code or text everywhere; for now we match the API in
49
+ // @atlaskit/codeblock
50
+ var code = text;
51
+ var generatorClassName = 'prismjs';
52
+ var containerProps = _objectSpread(_objectSpread({}, rest), {}, {
53
+ 'data-testid': testId,
54
+ className: rest.className ? "".concat(generatorClassName, " ").concat(rest.className) : generatorClassName
55
+ });
56
+ if (shouldWrapLongLines) {
57
+ codeTagProps.style = {
58
+ whiteSpace: 'pre-wrap',
59
+ wordBreak: 'break-word'
60
+ };
61
+ } else {
62
+ codeTagProps.style = {
63
+ whiteSpace: 'pre'
64
+ };
65
+ }
66
+ var codeBidiWarningConfig = {
67
+ codeBidiWarnings: codeBidiWarnings,
68
+ codeBidiWarningLabel: codeBidiWarningLabel,
69
+ codeBidiWarningTooltipEnabled: codeBidiWarningTooltipEnabled
70
+ };
71
+
72
+ // Tree + logic into rows
73
+ var rows = (0, _process.default)({
74
+ astGenerator: astGenerator,
75
+ code: code,
76
+ language: language,
77
+ shouldCreateParentElementForLines: shouldCreateParentElementForLines || !!shouldWrapLongLines,
78
+ lineProps: lineProps,
79
+ showLineNumbers: showLineNumbers
80
+ });
81
+
82
+ // Rows + logic into a renderer
83
+ return (0, _reactRenderer.default)({
84
+ containerProps: containerProps,
85
+ codeTagProps: codeTagProps,
86
+ rows: rows,
87
+ codeBidiWarningConfig: codeBidiWarningConfig
88
+ });
89
+ });
90
+ var _default = Highlight;
91
+ exports.default = _default;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = createLineElement;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
+ var _getInlineLineNumber = _interopRequireDefault(require("./get-inline-line-number"));
10
+ function createLineElement(_ref) {
11
+ var children = _ref.children,
12
+ lineNumber = _ref.lineNumber,
13
+ showLineNumbers = _ref.showLineNumbers,
14
+ _ref$lineProps = _ref.lineProps,
15
+ lineProps = _ref$lineProps === void 0 ? {} : _ref$lineProps,
16
+ _ref$className = _ref.className,
17
+ className = _ref$className === void 0 ? [] : _ref$className;
18
+ var properties = typeof lineProps === 'function' ? lineProps(lineNumber) : lineProps;
19
+ properties.className = className;
20
+ var currentChildren = children;
21
+ if (lineNumber && showLineNumbers) {
22
+ // When syntax highlighting is NOT turned on, the entire LOC is just a single
23
+ // child. We can then happily create the line number and the LOC as siblings...
24
+ if (currentChildren.length === 1) {
25
+ currentChildren = [(0, _getInlineLineNumber.default)(lineNumber)].concat((0, _toConsumableArray2.default)(currentChildren));
26
+ } else {
27
+ // ... However, when syntax highlighting IS on, a span is created for each
28
+ // tokenised node in the AST (eg. <span>import</span><span>React</span>).
29
+ // When shouldWrapLongLines is true, the row becomes `display: flex` and
30
+ // forces all its children to be equal height. This can be a source of
31
+ // visual bugs where a LOC is broken up weirdly into segments.
32
+ //
33
+ // +---------------------------------------+
34
+ // | row |
35
+ // | +----+ +-------+ +--------+ +------+ |
36
+ // | |line| |key | |keywords| |key | |
37
+ // | |no. | |word | | | |word | |
38
+ // | | | | | | | | | |
39
+ // | +----+ +-------+ +--------+ +------+ |
40
+ // +---------------------------------------+
41
+ //
42
+ // Nesting the children one layer deeper (i.e. creating an extra span)
43
+ // ensures that the line number and the ENTIRE line of code are aligned by
44
+ // the parent flexbox (the row).
45
+ //
46
+ // +---------------------------------------------+
47
+ // | row |
48
+ // | +----+ +--------------------------------+ |
49
+ // | |line| | extra span we are creating | |
50
+ // | |no. | | +-------+ +--------+ +------+ | |
51
+ // | | | | |key | |keywords| |key | | |
52
+ // | | | | |word | | | |word | | |
53
+ // | | | | | | | | | | | |
54
+ // | | | | +-------+ +--------+ +------+ | |
55
+ // | +----+ +--------------------------------+ |
56
+ // +---------------------------------------------+
57
+ //
58
+ currentChildren = [(0, _getInlineLineNumber.default)(lineNumber), {
59
+ type: 'element',
60
+ tagName: 'span',
61
+ properties: {
62
+ className: []
63
+ },
64
+ children: currentChildren
65
+ }];
66
+ }
67
+ }
68
+ return {
69
+ type: 'element',
70
+ tagName: 'span',
71
+ properties: properties,
72
+ children: currentChildren
73
+ };
74
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _createLineElement = _interopRequireDefault(require("./create-line-element"));
9
+ var _getInlineLineNumber = _interopRequireDefault(require("./get-inline-line-number"));
10
+ function createLineGenerator(lineProps, shouldCreateParentElementForLines, showLineNumbers) {
11
+ return function (children, lineNumber) {
12
+ var className = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
13
+ // Needed for more complex line creation
14
+ if (shouldCreateParentElementForLines || className.length > 0) {
15
+ return (0, _createLineElement.default)({
16
+ children: children,
17
+ lineNumber: lineNumber,
18
+ showLineNumbers: showLineNumbers,
19
+ lineProps: lineProps,
20
+ className: className
21
+ });
22
+ } else {
23
+ // Simple line creation without the bells and whistles
24
+ if (showLineNumbers && lineNumber) {
25
+ children.unshift((0, _getInlineLineNumber.default)(lineNumber));
26
+ }
27
+ return children;
28
+ }
29
+ };
30
+ }
31
+ var _default = createLineGenerator;
32
+ exports.default = _default;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = flattenCodeTree;
8
+ var _createLineElement = _interopRequireDefault(require("./create-line-element"));
9
+ function flattenCodeTree(tree) {
10
+ var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
11
+ var className = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
12
+ var newTree = [];
13
+ for (var i = 0; i < tree.length; i++) {
14
+ var node = tree[i];
15
+ if (node.type === 'text') {
16
+ newTree.push((0, _createLineElement.default)({
17
+ children: [node],
18
+ lineNumber: offset,
19
+ className: className
20
+ }));
21
+ } else if (node.children) {
22
+ var classNames = className.concat(node.properties.className || []);
23
+ flattenCodeTree(node.children, offset + 1, classNames).forEach(function (i) {
24
+ return newTree.push(i);
25
+ });
26
+ }
27
+ }
28
+ return newTree;
29
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getCodeTree;
7
+ function getCodeTree(language, code, astGenerator) {
8
+ if (language === 'text' || !astGenerator) {
9
+ return [{
10
+ type: 'text',
11
+ value: code
12
+ }];
13
+ }
14
+ try {
15
+ return astGenerator.highlight(code, language);
16
+ } catch (e) {
17
+ return [{
18
+ type: 'text',
19
+ value: code
20
+ }];
21
+ }
22
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getInlineLineNumber;
7
+ function getInlineLineNumber(lineNumber) {
8
+ return {
9
+ type: 'element',
10
+ tagName: 'span',
11
+ properties: {
12
+ key: "line-number--".concat(lineNumber),
13
+ className: ['comment', 'linenumber', 'ds-line-number'],
14
+ // We're placing the lineNumber in a data-attr on an empty span (hence the
15
+ // empty children array below). This allows CodeBlock to use CSS to
16
+ // generate the content in a pseudo-element, which is a fix for a bug
17
+ // where line numbers were being copied (CSS content can't be copied)
18
+ // https://product-fabric.atlassian.net/browse/DSP-2729
19
+ 'data-ds--line-number': lineNumber
20
+ },
21
+ children: []
22
+ };
23
+ }