@redocly/theme 0.45.1 → 0.45.2-rc.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.
@@ -37,7 +37,15 @@ const CodeBlockControls_1 = require("../../components/CodeBlock/CodeBlockControl
37
37
  function CodeBlock({ lang, source, externalSource, header, dataTestId = 'source-code', codeBlockRef, highlightedHtml, withLineNumbers, startLineNumber, className, codeBlockMaxHeight, tabs, hideCodeColors, wrapContents = false, children, }) {
38
38
  var _a, _b, _c, _d, _e, _f;
39
39
  const [sourceCode, setSourceCode] = (0, react_1.useState)((_c = (source || ((_b = (_a = externalSource === null || externalSource === void 0 ? void 0 : externalSource.sample) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a, externalSource)))) !== null && _c !== void 0 ? _c : '');
40
- const highlightedCode = highlightedHtml || (children ? null : (0, utils_1.highlight)(sourceCode, lang));
40
+ const { useCodeHighlight } = (0, hooks_1.useThemeHooks)();
41
+ const { highlight } = useCodeHighlight() || {};
42
+ const highlightedCode = highlightedHtml
43
+ ? withLineNumbers
44
+ ? (0, utils_1.addLineNumbers)(highlightedHtml, startLineNumber)
45
+ : highlightedHtml
46
+ : children
47
+ ? null
48
+ : highlight === null || highlight === void 0 ? void 0 : highlight(sourceCode, lang, { withLineNumbers, startLineNumber });
41
49
  // The same initial value should be returned for ssr and frontend to avoid issues
42
50
  // Because we don't have session storage in ssr and can't get the security details there
43
51
  // Issue for more details https://github.com/Redocly/reference-docs/issues/888
@@ -54,11 +62,9 @@ function CodeBlock({ lang, source, externalSource, header, dataTestId = 'source-
54
62
  return (react_1.default.createElement(CodeBlockWrapper, { "data-component-name": "CodeBlock/CodeBlock", className: className },
55
63
  react_1.default.createElement(ContainerWrapper, null,
56
64
  react_1.default.createElement(CodeBlockControls_1.CodeBlockControls, { tabs: tabs, className: header === null || header === void 0 ? void 0 : header.className, title: header === null || header === void 0 ? void 0 : header.title, controls: controls }),
57
- react_1.default.createElement(CodeBlockContainer_1.CodeBlockContainer, { ref: codeBlockRef, className: withLineNumbers ? 'line-numbers' : '', dangerouslySetInnerHTML: highlightedCode
65
+ react_1.default.createElement(CodeBlockContainer_1.CodeBlockContainer, { ref: codeBlockRef, withLineNumbers: withLineNumbers, dangerouslySetInnerHTML: highlightedCode
58
66
  ? {
59
- __html: withLineNumbers
60
- ? (0, utils_1.addLineNumbers)(highlightedCode, startLineNumber)
61
- : highlightedCode,
67
+ __html: highlightedCode,
62
68
  }
63
69
  : undefined, suppressHydrationWarning // TODO: investigate issue
64
70
  : true, "data-testid": dataTestId, hideCodeColors: hideCodeColors, maxHeight: codeBlockMaxHeight, wrapContents: wrapContents, tabIndex: 0 }, children),
@@ -4,7 +4,7 @@ export type CodeBlockContainerProps = PropsWithChildren<{
4
4
  maxHeight?: string;
5
5
  wrapContents?: boolean;
6
6
  ref?: (instance: HTMLPreElement | null) => void;
7
- className?: string;
7
+ withLineNumbers?: boolean;
8
8
  dangerouslySetInnerHTML?: {
9
9
  __html: string;
10
10
  };
@@ -127,29 +127,243 @@ const CodeBlockContainerComponent = styled_components_1.default.pre `
127
127
  cursor: help;
128
128
  }
129
129
 
130
- .code-line {
131
- &:before {
132
- content: attr(data-line-number);
133
- display: inline-block;
134
- min-width: 2em;
135
- padding-right: 0.8em;
136
- text-align: right;
137
- pointer-events: none;
138
- user-select: none;
139
- }
130
+ .line-number {
131
+ display: inline-block;
132
+ min-width: 2.5em;
133
+ padding-right: 0.8em;
134
+ text-align: right;
135
+ pointer-events: none;
136
+ user-select: none;
140
137
  }
141
138
 
142
139
  .highlighted {
143
140
  background: var(--tag-basic-bg-color);
144
141
  }
145
142
 
143
+ /*
144
+ * Shiki styles
145
+ */
146
+
147
+ .line {
148
+ display: block;
149
+ }
150
+
151
+ .has-diff {
152
+ > .line {
153
+ &:before {
154
+ content: ' ';
155
+ display: inline-block;
156
+ min-width: 1em;
157
+ padding-right: 0.4em;
158
+ text-align: right;
159
+ pointer-events: none;
160
+ user-select: none;
161
+ }
162
+ }
163
+ }
164
+
165
+ .diff {
166
+ &.add {
167
+ background: var(--color-grass-2);
168
+ &:before {
169
+ content: '+';
170
+ }
171
+ }
172
+ &.remove {
173
+ background: var(--color-red-2);
174
+
175
+ &:before {
176
+ content: '-';
177
+ }
178
+ }
179
+ }
180
+
181
+ .highlighted-word {
182
+ background: var(--color-carrot-2);
183
+ border: 1px solid var(--color-carrot-5);
184
+ border-radius: 4px;
185
+ }
186
+
187
+ .has-focused {
188
+ .line {
189
+ opacity: 0.7;
190
+ filter: blur(0.095rem);
191
+ transition:
192
+ filter 0.35s,
193
+ opacity 0.35s;
194
+ &.focused {
195
+ opacity: 1;
196
+ filter: blur(0);
197
+ }
198
+ }
199
+
200
+ &:hover {
201
+ .line {
202
+ opacity: 1;
203
+ filter: blur(0);
204
+ }
205
+ }
206
+ }
207
+
208
+ /*
209
+ * Tree view styles
210
+ */
211
+
212
+ .tree-view-line {
213
+ display: flex;
214
+ }
215
+
216
+ .tree-view-branch {
217
+ color: var(--code-block-tree-view-lines-color);
218
+ }
219
+
220
+ .tree-view-file {
221
+ display: inline-flex;
222
+ &::before {
223
+ content: '\\ea01';
224
+ margin-right: 0.5rem;
225
+ font-family: 'TreeViewIcons';
226
+ }
227
+
228
+ &.no-file {
229
+ &::before {
230
+ content: '';
231
+ margin-right: 0;
232
+ }
233
+ }
234
+
235
+ &.ext-folder {
236
+ &::before {
237
+ content: '\\ea02';
238
+ }
239
+ }
240
+
241
+ &.ext-img,
242
+ &.ext-png,
243
+ &.ext-jpg,
244
+ &.ext-jpeg,
245
+ &.ext-gif,
246
+ &.ext-svg {
247
+ &::before {
248
+ content: '\\ea03';
249
+ }
250
+ }
251
+
252
+ &.ext-mp2,
253
+ &.ext-mp3,
254
+ &.ext-wav,
255
+ &.ext-ogg,
256
+ &.ext-flac {
257
+ &::before {
258
+ content: '\\ea04';
259
+ margin-right: 0.5rem;
260
+ }
261
+ }
262
+
263
+ &.ext-mp4,
264
+ &.ext-mkv,
265
+ &.ext-avi,
266
+ &.ext-mov,
267
+ &.ext-wmv,
268
+ &.ext-flv {
269
+ &::before {
270
+ content: '\\ea05';
271
+ margin-right: 0.5rem;
272
+ }
273
+ }
274
+
275
+ &.ext-txt,
276
+ &.ext-text,
277
+ &.ext-md,
278
+ &.ext-markdown {
279
+ &::before {
280
+ content: '\\ea06';
281
+ margin-right: 0.5rem;
282
+ }
283
+ }
284
+
285
+ &.ext-js,
286
+ &.ext-ts,
287
+ &.ext-jsx,
288
+ &.ext-tsx,
289
+ &.ext-json,
290
+ &.ext-yaml,
291
+ &.ext-yml,
292
+ &.ext-xml,
293
+ &.ext-html,
294
+ &.ext-css,
295
+ &.ext-scss,
296
+ &.ext-less,
297
+ &.ext-sass,
298
+ &.ext-java,
299
+ &.ext-c,
300
+ &.ext-cpp,
301
+ &.ext-cs,
302
+ &.ext-php,
303
+ &.ext-py,
304
+ &.ext-rb,
305
+ &.ext-go,
306
+ &.ext-swift,
307
+ &.ext-sql,
308
+ &.ext-perl,
309
+ &.ext-lua,
310
+ &.ext-scala,
311
+ &.ext-sh {
312
+ &::before {
313
+ content: '\\ea07';
314
+ margin-right: 0.5rem;
315
+ }
316
+ }
317
+
318
+ &.ext-zip,
319
+ &.ext-rar,
320
+ &.ext-tar,
321
+ &.ext-gz,
322
+ &.ext-iso {
323
+ &::before {
324
+ content: '\\ea08';
325
+ }
326
+ }
327
+
328
+ &.ext-pdf,
329
+ &.ext-PDF {
330
+ &::before {
331
+ content: '\\ea09';
332
+ }
333
+ }
334
+
335
+ &.ext-excel,
336
+ &.ext-xls,
337
+ &.ext-xlsx {
338
+ &::before {
339
+ content: '\\ea0a';
340
+ }
341
+ }
342
+
343
+ &.ext-powerpoint,
344
+ &.ext-ppt,
345
+ &.ext-pptx {
346
+ &::before {
347
+ content: '\\ea0b';
348
+ }
349
+ }
350
+
351
+ &.ext-doc,
352
+ &.ext-docx,
353
+ &.ext-rtf {
354
+ &::before {
355
+ content: '\\ea0c';
356
+ }
357
+ }
358
+ }
359
+
146
360
  ${(0, utils_1.generateCodeBlockTokens)()}
147
361
 
148
362
  ${({ hideCodeColors }) => hideCodeColors &&
149
363
  (0, styled_components_1.css) `
150
- .code-line:not(.highlighted),
151
- .code-line:not(.highlighted) > span,
152
- .code-line:not(.highlighted) > span > span {
364
+ .line-number:not(.highlighted),
365
+ .line-number:not(.highlighted) > span,
366
+ .line-number:not(.highlighted) > span > span {
153
367
  color: grey;
154
368
  }
155
369
  `}
@@ -3,6 +3,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.code = void 0;
4
4
  const styled_components_1 = require("styled-components");
5
5
  exports.code = (0, styled_components_1.css) `
6
+ /**
7
+ * Tree view icons font
8
+ */
9
+ @font-face {
10
+ font-family: 'TreeViewIcons';
11
+ /**
12
+ * Use the following escape sequences to refer to a specific icon:
13
+ *
14
+ * - \\ea01 file
15
+ * - \\ea02 folder
16
+ * - \\ea03 image
17
+ * - \\ea04 audio
18
+ * - \\ea05 video
19
+ * - \\ea06 text
20
+ * - \\ea07 code
21
+ * - \\ea08 archive
22
+ * - \\ea09 pdf
23
+ * - \\ea0a excel
24
+ * - \\ea0b powerpoint
25
+ * - \\ea0c word
26
+ */
27
+ src: url('data:application/font-woff;base64,d09GRgABAAAAAAgYAAsAAAAAEGAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1VCAAABCAAAADsAAABUIIslek9TLzIAAAFEAAAAPwAAAFY1UkH9Y21hcAAAAYQAAAB/AAACCtvO7yxnbHlmAAACBAAAA+MAAAlACm1VqmhlYWQAAAXoAAAAKgAAADZfxj5jaGhlYQAABhQAAAAYAAAAJAFbAMFobXR4AAAGLAAAAA4AAAA0CGQAAGxvY2EAAAY8AAAAHAAAABwM9A9CbWF4cAAABlgAAAAfAAAAIAEgAHZuYW1lAAAGeAAAATcAAAJSfUrk+HBvc3QAAAewAAAAZgAAAIka0DSfeJxjYGRgYOBiMGCwY2BycfMJYeDLSSzJY5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JMKJqoVgrejQ3CP5Yubt0JwxNJa96Oypr6j621VSOMQKG+uP36eKmHylcb0MAeJxtwdEOgjAMBdBeWEFR/Mdl7bTJtMsygc/nwVfPoYF+QP+tGDAigDFhxgVXLLjhjhUPCtmKTtmLaGN7x6dy/Io5bybqoevRQ3LRObb0sk3HKpn1SFqW6ru26vbpYfcmRCccJhqsAAA=') format('woff');
28
+ }
29
+
6
30
  /**
7
31
  * @tokens Code base styles
8
32
  */
@@ -90,6 +114,12 @@ exports.code = (0, styled_components_1.css) `
90
114
  --code-block-tokens-class-name-color: var(--color-carrot-7); // @presenter Color
91
115
  --code-block-tokens-function-color: var(--color-carrot-7); // @presenter Color
92
116
 
117
+ /**
118
+ * @tokens Code Block tree view
119
+ */
120
+ --code-block-tree-view-icon-font-family: 'TreeViewIcons'; // @presenter FontFamily
121
+ --code-block-tree-view-lines-color: var(--border-color-primary); // @presenter Color
122
+
93
123
  // @tokens End
94
124
  `;
95
125
  //# sourceMappingURL=variables.js.map
@@ -95,6 +95,12 @@ export type ThemeHooks = {
95
95
  };
96
96
  useUserTeams: () => string[];
97
97
  usePageProps: <T extends Record<string, unknown>>() => PageProps & T;
98
+ useCodeHighlight: () => {
99
+ highlight: (code: string, language: string | undefined, { withLineNumbers, startLineNumber }: {
100
+ withLineNumbers?: boolean;
101
+ startLineNumber?: number;
102
+ }) => string;
103
+ };
98
104
  };
99
105
  export type L10nConfig = {
100
106
  currentLocale: string;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @param highlightedCode - A string with new line breaks; the line numbers will be added before each line.
3
+ * @param start - The number to start the line numbering from; default is 1
4
+ * @returns A string with line numbers added before each line
5
+ */
6
+ export declare function addLineNumbers(highlightedCode: string, start?: number): string;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addLineNumbers = addLineNumbers;
4
+ const NEW_LINE_EXP = /\n(?!$)/g;
5
+ /**
6
+ * @param highlightedCode - A string with new line breaks; the line numbers will be added before each line.
7
+ * @param start - The number to start the line numbering from; default is 1
8
+ * @returns A string with line numbers added before each line
9
+ */
10
+ function addLineNumbers(highlightedCode, start = 1) {
11
+ console.log('from addLineNumbers ---> ', highlightedCode);
12
+ const codeLines = highlightedCode.split(NEW_LINE_EXP);
13
+ return codeLines
14
+ .map((line, i) => {
15
+ return `<span class='line-number'>${start + i}</span>${line}`;
16
+ })
17
+ .join('\n');
18
+ }
19
+ //# sourceMappingURL=add-line-numbers.js.map
@@ -1,6 +1,6 @@
1
1
  export * from '../../core/utils/clipboard-service';
2
2
  export * from '../../core/utils/css-variables';
3
- export * from '../../core/utils/highlight';
3
+ export * from '../../core/utils/add-line-numbers';
4
4
  export * from '../../core/utils/media-css';
5
5
  export * from '../../core/utils/theme-helpers';
6
6
  export * from '../../core/utils/class-names';
@@ -16,7 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("../../core/utils/clipboard-service"), exports);
18
18
  __exportStar(require("../../core/utils/css-variables"), exports);
19
- __exportStar(require("../../core/utils/highlight"), exports);
19
+ __exportStar(require("../../core/utils/add-line-numbers"), exports);
20
20
  __exportStar(require("../../core/utils/media-css"), exports);
21
21
  __exportStar(require("../../core/utils/theme-helpers"), exports);
22
22
  __exportStar(require("../../core/utils/class-names"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redocly/theme",
3
- "version": "0.45.1",
3
+ "version": "0.45.2-rc.0",
4
4
  "description": "Shared UI components lib",
5
5
  "keywords": [
6
6
  "theme",
@@ -27,7 +27,6 @@
27
27
  "peerDependencies": {
28
28
  "@markdoc/markdoc": "0.4.0",
29
29
  "lodash.throttle": "^4.1.1",
30
- "prismjs": "^1.28.0",
31
30
  "react": "^17.0.0 || ^18.0.0",
32
31
  "react-dom": "^17.0.0 || ^18.0.0",
33
32
  "react-router-dom": "^6.21.1",
@@ -44,7 +43,6 @@
44
43
  "@types/jest-when": "3.5.5",
45
44
  "@types/lodash.throttle": "4.1.9",
46
45
  "@types/node": "18.19.3",
47
- "@types/prismjs": "1.26.4",
48
46
  "@types/react": "18.3.9",
49
47
  "@types/react-dom": "18.2.25",
50
48
  "@types/styled-components": "5.1.34",
@@ -4,8 +4,8 @@ import styled from 'styled-components';
4
4
  import type { CodeBlockControlsProps } from '@redocly/theme/components/CodeBlock/CodeBlockControls';
5
5
  import type { ReportDialogProps } from '@redocly/theme/components/Feedback/ReportDialog';
6
6
 
7
- import { highlight, addLineNumbers } from '@redocly/theme/core/utils';
8
- import { useModalScrollLock, useReportDialog } from '@redocly/theme/core/hooks';
7
+ import { addLineNumbers } from '@redocly/theme/core/utils';
8
+ import { useModalScrollLock, useReportDialog, useThemeHooks } from '@redocly/theme/core/hooks';
9
9
  import { ReportDialog } from '@redocly/theme/components/Feedback/ReportDialog';
10
10
  import { CodeBlockContainer } from '@redocly/theme/components/CodeBlock/CodeBlockContainer';
11
11
  import { CodeBlockControls } from '@redocly/theme/components/CodeBlock/CodeBlockControls';
@@ -68,8 +68,16 @@ export function CodeBlock({
68
68
  const [sourceCode, setSourceCode] = useState<string>(
69
69
  (source || externalSource?.sample?.get?.(externalSource)) ?? '',
70
70
  );
71
+ const { useCodeHighlight } = useThemeHooks();
72
+ const { highlight } = useCodeHighlight() || {};
71
73
 
72
- const highlightedCode = highlightedHtml || (children ? null : highlight(sourceCode, lang));
74
+ const highlightedCode = highlightedHtml
75
+ ? withLineNumbers
76
+ ? addLineNumbers(highlightedHtml, startLineNumber)
77
+ : highlightedHtml
78
+ : children
79
+ ? null
80
+ : highlight?.(sourceCode, lang, { withLineNumbers, startLineNumber });
73
81
 
74
82
  // The same initial value should be returned for ssr and frontend to avoid issues
75
83
  // Because we don't have session storage in ssr and can't get the security details there
@@ -102,13 +110,11 @@ export function CodeBlock({
102
110
  />
103
111
  <CodeBlockContainer
104
112
  ref={codeBlockRef}
105
- className={withLineNumbers ? 'line-numbers' : ''}
113
+ withLineNumbers={withLineNumbers}
106
114
  dangerouslySetInnerHTML={
107
115
  highlightedCode
108
116
  ? {
109
- __html: withLineNumbers
110
- ? addLineNumbers(highlightedCode, startLineNumber)
111
- : highlightedCode,
117
+ __html: highlightedCode,
112
118
  }
113
119
  : undefined
114
120
  }
@@ -8,7 +8,7 @@ export type CodeBlockContainerProps = PropsWithChildren<{
8
8
  maxHeight?: string;
9
9
  wrapContents?: boolean;
10
10
  ref?: (instance: HTMLPreElement | null) => void;
11
- className?: string;
11
+ withLineNumbers?: boolean;
12
12
  dangerouslySetInnerHTML?: {
13
13
  __html: string;
14
14
  };
@@ -117,30 +117,244 @@ const CodeBlockContainerComponent = styled.pre<CodeBlockContainerProps>`
117
117
  cursor: help;
118
118
  }
119
119
 
120
- .code-line {
121
- &:before {
122
- content: attr(data-line-number);
123
- display: inline-block;
124
- min-width: 2em;
125
- padding-right: 0.8em;
126
- text-align: right;
127
- pointer-events: none;
128
- user-select: none;
129
- }
120
+ .line-number {
121
+ display: inline-block;
122
+ min-width: 2.5em;
123
+ padding-right: 0.8em;
124
+ text-align: right;
125
+ pointer-events: none;
126
+ user-select: none;
130
127
  }
131
128
 
132
129
  .highlighted {
133
130
  background: var(--tag-basic-bg-color);
134
131
  }
135
132
 
133
+ /*
134
+ * Shiki styles
135
+ */
136
+
137
+ .line {
138
+ display: block;
139
+ }
140
+
141
+ .has-diff {
142
+ > .line {
143
+ &:before {
144
+ content: ' ';
145
+ display: inline-block;
146
+ min-width: 1em;
147
+ padding-right: 0.4em;
148
+ text-align: right;
149
+ pointer-events: none;
150
+ user-select: none;
151
+ }
152
+ }
153
+ }
154
+
155
+ .diff {
156
+ &.add {
157
+ background: var(--color-grass-2);
158
+ &:before {
159
+ content: '+';
160
+ }
161
+ }
162
+ &.remove {
163
+ background: var(--color-red-2);
164
+
165
+ &:before {
166
+ content: '-';
167
+ }
168
+ }
169
+ }
170
+
171
+ .highlighted-word {
172
+ background: var(--color-carrot-2);
173
+ border: 1px solid var(--color-carrot-5);
174
+ border-radius: 4px;
175
+ }
176
+
177
+ .has-focused {
178
+ .line {
179
+ opacity: 0.7;
180
+ filter: blur(0.095rem);
181
+ transition:
182
+ filter 0.35s,
183
+ opacity 0.35s;
184
+ &.focused {
185
+ opacity: 1;
186
+ filter: blur(0);
187
+ }
188
+ }
189
+
190
+ &:hover {
191
+ .line {
192
+ opacity: 1;
193
+ filter: blur(0);
194
+ }
195
+ }
196
+ }
197
+
198
+ /*
199
+ * Tree view styles
200
+ */
201
+
202
+ .tree-view-line {
203
+ display: flex;
204
+ }
205
+
206
+ .tree-view-branch {
207
+ color: var(--code-block-tree-view-lines-color);
208
+ }
209
+
210
+ .tree-view-file {
211
+ display: inline-flex;
212
+ &::before {
213
+ content: '\\ea01';
214
+ margin-right: 0.5rem;
215
+ font-family: 'TreeViewIcons';
216
+ }
217
+
218
+ &.no-file {
219
+ &::before {
220
+ content: '';
221
+ margin-right: 0;
222
+ }
223
+ }
224
+
225
+ &.ext-folder {
226
+ &::before {
227
+ content: '\\ea02';
228
+ }
229
+ }
230
+
231
+ &.ext-img,
232
+ &.ext-png,
233
+ &.ext-jpg,
234
+ &.ext-jpeg,
235
+ &.ext-gif,
236
+ &.ext-svg {
237
+ &::before {
238
+ content: '\\ea03';
239
+ }
240
+ }
241
+
242
+ &.ext-mp2,
243
+ &.ext-mp3,
244
+ &.ext-wav,
245
+ &.ext-ogg,
246
+ &.ext-flac {
247
+ &::before {
248
+ content: '\\ea04';
249
+ margin-right: 0.5rem;
250
+ }
251
+ }
252
+
253
+ &.ext-mp4,
254
+ &.ext-mkv,
255
+ &.ext-avi,
256
+ &.ext-mov,
257
+ &.ext-wmv,
258
+ &.ext-flv {
259
+ &::before {
260
+ content: '\\ea05';
261
+ margin-right: 0.5rem;
262
+ }
263
+ }
264
+
265
+ &.ext-txt,
266
+ &.ext-text,
267
+ &.ext-md,
268
+ &.ext-markdown {
269
+ &::before {
270
+ content: '\\ea06';
271
+ margin-right: 0.5rem;
272
+ }
273
+ }
274
+
275
+ &.ext-js,
276
+ &.ext-ts,
277
+ &.ext-jsx,
278
+ &.ext-tsx,
279
+ &.ext-json,
280
+ &.ext-yaml,
281
+ &.ext-yml,
282
+ &.ext-xml,
283
+ &.ext-html,
284
+ &.ext-css,
285
+ &.ext-scss,
286
+ &.ext-less,
287
+ &.ext-sass,
288
+ &.ext-java,
289
+ &.ext-c,
290
+ &.ext-cpp,
291
+ &.ext-cs,
292
+ &.ext-php,
293
+ &.ext-py,
294
+ &.ext-rb,
295
+ &.ext-go,
296
+ &.ext-swift,
297
+ &.ext-sql,
298
+ &.ext-perl,
299
+ &.ext-lua,
300
+ &.ext-scala,
301
+ &.ext-sh {
302
+ &::before {
303
+ content: '\\ea07';
304
+ margin-right: 0.5rem;
305
+ }
306
+ }
307
+
308
+ &.ext-zip,
309
+ &.ext-rar,
310
+ &.ext-tar,
311
+ &.ext-gz,
312
+ &.ext-iso {
313
+ &::before {
314
+ content: '\\ea08';
315
+ }
316
+ }
317
+
318
+ &.ext-pdf,
319
+ &.ext-PDF {
320
+ &::before {
321
+ content: '\\ea09';
322
+ }
323
+ }
324
+
325
+ &.ext-excel,
326
+ &.ext-xls,
327
+ &.ext-xlsx {
328
+ &::before {
329
+ content: '\\ea0a';
330
+ }
331
+ }
332
+
333
+ &.ext-powerpoint,
334
+ &.ext-ppt,
335
+ &.ext-pptx {
336
+ &::before {
337
+ content: '\\ea0b';
338
+ }
339
+ }
340
+
341
+ &.ext-doc,
342
+ &.ext-docx,
343
+ &.ext-rtf {
344
+ &::before {
345
+ content: '\\ea0c';
346
+ }
347
+ }
348
+ }
349
+
136
350
  ${generateCodeBlockTokens()}
137
351
 
138
352
  ${({ hideCodeColors }) =>
139
353
  hideCodeColors &&
140
354
  css`
141
- .code-line:not(.highlighted),
142
- .code-line:not(.highlighted) > span,
143
- .code-line:not(.highlighted) > span > span {
355
+ .line-number:not(.highlighted),
356
+ .line-number:not(.highlighted) > span,
357
+ .line-number:not(.highlighted) > span > span {
144
358
  color: grey;
145
359
  }
146
360
  `}
@@ -1,6 +1,30 @@
1
1
  import { css } from 'styled-components';
2
2
 
3
3
  export const code = css`
4
+ /**
5
+ * Tree view icons font
6
+ */
7
+ @font-face {
8
+ font-family: 'TreeViewIcons';
9
+ /**
10
+ * Use the following escape sequences to refer to a specific icon:
11
+ *
12
+ * - \\ea01 file
13
+ * - \\ea02 folder
14
+ * - \\ea03 image
15
+ * - \\ea04 audio
16
+ * - \\ea05 video
17
+ * - \\ea06 text
18
+ * - \\ea07 code
19
+ * - \\ea08 archive
20
+ * - \\ea09 pdf
21
+ * - \\ea0a excel
22
+ * - \\ea0b powerpoint
23
+ * - \\ea0c word
24
+ */
25
+ src: url('data:application/font-woff;base64,d09GRgABAAAAAAgYAAsAAAAAEGAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1VCAAABCAAAADsAAABUIIslek9TLzIAAAFEAAAAPwAAAFY1UkH9Y21hcAAAAYQAAAB/AAACCtvO7yxnbHlmAAACBAAAA+MAAAlACm1VqmhlYWQAAAXoAAAAKgAAADZfxj5jaGhlYQAABhQAAAAYAAAAJAFbAMFobXR4AAAGLAAAAA4AAAA0CGQAAGxvY2EAAAY8AAAAHAAAABwM9A9CbWF4cAAABlgAAAAfAAAAIAEgAHZuYW1lAAAGeAAAATcAAAJSfUrk+HBvc3QAAAewAAAAZgAAAIka0DSfeJxjYGRgYOBiMGCwY2BycfMJYeDLSSzJY5BiYGGAAJA8MpsxJzM9kYEDxgPKsYBpDiBmg4gCACY7BUgAeJxjYGRYyjiBgZWBgaGQoRZISkLpUAYOBj0GBiYGVmYGrCAgzTWFweEV4ysehs1ArgDDFgZGIA3CDAB2tQjAAHic7ZHLEcMwCESfLCz/VEoKSEE5parURxMOC4c0Ec283WGFdABgBXrwCAzam4bOK9KWeefM3Hhmjyn3ed+hTRq1pS7Ra/HjYGPniHcXMy4G/zNTP7/KW5HTXArkvdBW3ArN19dCG/NRIN8K5HuB/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igjwPhhYPpGCBCBHhxiKMyFMFT7ziRH7RtfIWdiha+TdW+Rqs7bLHdN2ZJIKl0um0x3op9saYr0REeRdj09pl43pMzz4tjztrY8L4o8bzT+oLY27PR/eFtXs/YY5vtwB5Iqad14eYN0ujveMaGWqkdU3TKbQSC5Uvxaf4fA7SAQ3r2tEfIhd4duld91bwMisjqBw22orthNcroXl7KqO1329HBgAexgoCfGAwiDPoBnriki3lmNojrzvD0tjo6E3vPYP6E2BMIAeJxjYGRgYADiY8t3FsTz23xl4GbYzIAB/v9nWM6wBcjgYGAC8QH+QQhZAAB4nGNgZGBg2MzAACeXMzAyoAJeADPyAh14nGNgAILNpGEA0fgIZQAAAAAAAAA2AHIAvgE+AZgCCAKMAv4DlgPsBEYEoHicY2BkYGDgZchi4GQAASYg5gJCBob/YD4DABTSAZcAeJx9kU1uwjAQhV/4qwpqhdSqi67cTTeVEmBXDgBbhBD7AHYISuLUMSD2PUdP0HNwjp6i676k3qQS9Ujjb968mYUNoI8zPJTHw02Vy9PAFatfbpLuHbfIT47b6MF33KH+6riLF0wc93CHN27wWtdUHvHuuIFbfDhuUv903CKfHbfxgC/HHerfjrtYen3HPTx7ambiIl0YKQ+xPM5ltE9CU9NqxVKaItaZGPqDmj6VmTShlRuxOoniEI2sVUIZnYqJzqxMEi1yo3dybf2ttfk4CJTT/bVOMYNBjAIpFiTJOLCWOGLOHGGPBCE7l32XO0tmw04MjQwCQ7774B//lDmrZkJY3hvOrHBiLuiJMKJqoVgrejQ3CP5Yubt0JwxNJa96Oypr6j621VSOMQKG+uP36eKmHylcb0MAeJxtwdEOgjAMBdBeWEFR/Mdl7bTJtMsygc/nwVfPoYF+QP+tGDAigDFhxgVXLLjhjhUPCtmKTtmLaGN7x6dy/Io5bybqoevRQ3LRObb0sk3HKpn1SFqW6ru26vbpYfcmRCccJhqsAAA=') format('woff');
26
+ }
27
+
4
28
  /**
5
29
  * @tokens Code base styles
6
30
  */
@@ -88,5 +112,11 @@ export const code = css`
88
112
  --code-block-tokens-class-name-color: var(--color-carrot-7); // @presenter Color
89
113
  --code-block-tokens-function-color: var(--color-carrot-7); // @presenter Color
90
114
 
115
+ /**
116
+ * @tokens Code Block tree view
117
+ */
118
+ --code-block-tree-view-icon-font-family: 'TreeViewIcons'; // @presenter FontFamily
119
+ --code-block-tree-view-lines-color: var(--border-color-primary); // @presenter Color
120
+
91
121
  // @tokens End
92
122
  `;
@@ -109,6 +109,13 @@ export type ThemeHooks = {
109
109
  useTelemetry: () => { send(action: TelemetryEvent, data: unknown): void };
110
110
  useUserTeams: () => string[];
111
111
  usePageProps: <T extends Record<string, unknown>>() => PageProps & T;
112
+ useCodeHighlight: () => {
113
+ highlight: (
114
+ code: string,
115
+ language: string | undefined,
116
+ { withLineNumbers, startLineNumber }: { withLineNumbers?: boolean; startLineNumber?: number },
117
+ ) => string;
118
+ };
112
119
  };
113
120
 
114
121
  export type L10nConfig = {
@@ -0,0 +1,17 @@
1
+ const NEW_LINE_EXP = /\n(?!$)/g;
2
+
3
+ /**
4
+ * @param highlightedCode - A string with new line breaks; the line numbers will be added before each line.
5
+ * @param start - The number to start the line numbering from; default is 1
6
+ * @returns A string with line numbers added before each line
7
+ */
8
+ export function addLineNumbers(highlightedCode: string, start = 1): string {
9
+ console.log('from addLineNumbers ---> ', highlightedCode);
10
+ const codeLines = highlightedCode.split(NEW_LINE_EXP);
11
+
12
+ return codeLines
13
+ .map((line, i) => {
14
+ return `<span class='line-number'>${start + i}</span>${line}`;
15
+ })
16
+ .join('\n');
17
+ }
@@ -1,6 +1,6 @@
1
1
  export * from '@redocly/theme/core/utils/clipboard-service';
2
2
  export * from '@redocly/theme/core/utils/css-variables';
3
- export * from '@redocly/theme/core/utils/highlight';
3
+ export * from '@redocly/theme/core/utils/add-line-numbers';
4
4
  export * from '@redocly/theme/core/utils/media-css';
5
5
  export * from '@redocly/theme/core/utils/theme-helpers';
6
6
  export * from '@redocly/theme/core/utils/class-names';
@@ -1,35 +0,0 @@
1
- import 'prismjs/components/prism-bash.js';
2
- import 'prismjs/components/prism-c.js';
3
- import 'prismjs/components/prism-clike.js';
4
- import 'prismjs/components/prism-coffeescript.js';
5
- import 'prismjs/components/prism-cpp.js';
6
- import 'prismjs/components/prism-csharp.js';
7
- import 'prismjs/components/prism-go.js';
8
- import 'prismjs/components/prism-http.js';
9
- import 'prismjs/components/prism-java.js';
10
- import 'prismjs/components/prism-lua.js';
11
- import 'prismjs/components/prism-markdown.js';
12
- import 'prismjs/components/prism-markup-templating.js';
13
- import 'prismjs/components/prism-markup.js';
14
- import 'prismjs/components/prism-objectivec.js';
15
- import 'prismjs/components/prism-perl.js';
16
- import 'prismjs/components/prism-php.js';
17
- import 'prismjs/components/prism-python.js';
18
- import 'prismjs/components/prism-ruby.js';
19
- import 'prismjs/components/prism-scala.js';
20
- import 'prismjs/components/prism-sql.js';
21
- import 'prismjs/components/prism-swift.js';
22
- import 'prismjs/components/prism-graphql.js';
23
- import 'prismjs/plugins/treeview/prism-treeview.js';
24
- /**
25
- * map language names to Prism.js names
26
- */
27
- export declare function mapLang(lang: string): string;
28
- /**
29
- * Highlight source code string using Prism.js
30
- * @param source source code to highlight
31
- * @param lang highlight language
32
- * @return highlighted source code as **html string**
33
- */
34
- export declare function highlight(source: string | number | boolean, lang?: string): string;
35
- export declare function addLineNumbers(highlightedCode: string, start?: number): string;
@@ -1,129 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.mapLang = mapLang;
27
- exports.highlight = highlight;
28
- exports.addLineNumbers = addLineNumbers;
29
- const Prism = __importStar(require("prismjs"));
30
- require("prismjs/components/prism-bash.js");
31
- require("prismjs/components/prism-c.js");
32
- require("prismjs/components/prism-clike.js");
33
- require("prismjs/components/prism-coffeescript.js");
34
- require("prismjs/components/prism-cpp.js");
35
- require("prismjs/components/prism-csharp.js");
36
- require("prismjs/components/prism-go.js");
37
- require("prismjs/components/prism-http.js");
38
- require("prismjs/components/prism-java.js");
39
- require("prismjs/components/prism-lua.js");
40
- require("prismjs/components/prism-markdown.js");
41
- require("prismjs/components/prism-markup-templating.js"); // dep of php
42
- require("prismjs/components/prism-markup.js"); // xml
43
- require("prismjs/components/prism-objectivec.js");
44
- require("prismjs/components/prism-perl.js");
45
- require("prismjs/components/prism-php.js");
46
- require("prismjs/components/prism-python.js");
47
- require("prismjs/components/prism-ruby.js");
48
- require("prismjs/components/prism-scala.js");
49
- require("prismjs/components/prism-sql.js");
50
- require("prismjs/components/prism-swift.js");
51
- require("prismjs/components/prism-graphql.js");
52
- require("prismjs/plugins/treeview/prism-treeview.js");
53
- const DEFAULT_LANG = 'clike';
54
- const NEW_LINE_EXP = /\n(?!$)/g;
55
- Prism.languages.insertBefore('javascript', 'string', {
56
- 'property string': {
57
- pattern: /([{,]\s*)"(?:\\.|[^\\"\r\n])*"(?=\s*:)/i,
58
- lookbehind: true,
59
- },
60
- }, undefined);
61
- Prism.languages.insertBefore('javascript', 'punctuation', {
62
- property: {
63
- pattern: /([{,]\s*)[a-z]\w*(?=\s*:)/i,
64
- lookbehind: true,
65
- },
66
- }, undefined);
67
- Prism.languages.markdoc = Object.assign(Object.assign({}, Prism.languages.markdown), { tag: {
68
- pattern: /{%(.|\n)*?%}/i,
69
- inside: {
70
- keyword: {
71
- pattern: /^({%\s*\/?)(\w*|-)*\b/i,
72
- lookbehind: true,
73
- },
74
- id: /#(\w|-)*\b/,
75
- string: /".*?"/,
76
- equals: /=/,
77
- number: /\b\d+\b/i,
78
- variable: {
79
- pattern: /\$[\w.]+/i,
80
- inside: {
81
- punctuation: /\./i,
82
- },
83
- },
84
- function: /\b\w+(?=\()/,
85
- punctuation: /({%|\/?%})/i,
86
- boolean: /false|true/,
87
- },
88
- }, variable: {
89
- pattern: /\$\w+/i,
90
- }, function: {
91
- pattern: /\b\w+(?=\()/i,
92
- } });
93
- /**
94
- * map language names to Prism.js names
95
- */
96
- function mapLang(lang) {
97
- return ({
98
- json: 'js',
99
- 'c++': 'cpp',
100
- 'c#': 'csharp',
101
- 'c#+newtonsoft': 'csharp',
102
- 'java8+apache': 'java',
103
- 'objective-c': 'objectivec',
104
- shell: 'bash',
105
- viml: 'vim',
106
- curl: 'bash',
107
- 'node.js': 'js',
108
- }[lang] || DEFAULT_LANG);
109
- }
110
- /**
111
- * Highlight source code string using Prism.js
112
- * @param source source code to highlight
113
- * @param lang highlight language
114
- * @return highlighted source code as **html string**
115
- */
116
- function highlight(source, lang = DEFAULT_LANG) {
117
- lang = lang.toLowerCase();
118
- const grammar = Prism.languages[lang] || Prism.languages[mapLang(lang)];
119
- return Prism.highlight(source.toString(), grammar, lang);
120
- }
121
- function addLineNumbers(highlightedCode, start = 1) {
122
- const codeLines = highlightedCode.split(NEW_LINE_EXP);
123
- return codeLines
124
- .map((line, i) => {
125
- return `<span class="code-line" data-line-number=${start + i}>${line}</span>`;
126
- })
127
- .join('\n');
128
- }
129
- //# sourceMappingURL=highlight.js.map
@@ -1,125 +0,0 @@
1
- import * as Prism from 'prismjs';
2
- import 'prismjs/components/prism-bash.js';
3
- import 'prismjs/components/prism-c.js';
4
- import 'prismjs/components/prism-clike.js';
5
- import 'prismjs/components/prism-coffeescript.js';
6
- import 'prismjs/components/prism-cpp.js';
7
- import 'prismjs/components/prism-csharp.js';
8
- import 'prismjs/components/prism-go.js';
9
- import 'prismjs/components/prism-http.js';
10
- import 'prismjs/components/prism-java.js';
11
- import 'prismjs/components/prism-lua.js';
12
- import 'prismjs/components/prism-markdown.js';
13
- import 'prismjs/components/prism-markup-templating.js'; // dep of php
14
- import 'prismjs/components/prism-markup.js'; // xml
15
- import 'prismjs/components/prism-objectivec.js';
16
- import 'prismjs/components/prism-perl.js';
17
- import 'prismjs/components/prism-php.js';
18
- import 'prismjs/components/prism-python.js';
19
- import 'prismjs/components/prism-ruby.js';
20
- import 'prismjs/components/prism-scala.js';
21
- import 'prismjs/components/prism-sql.js';
22
- import 'prismjs/components/prism-swift.js';
23
- import 'prismjs/components/prism-graphql.js';
24
- import 'prismjs/plugins/treeview/prism-treeview.js';
25
-
26
- const DEFAULT_LANG = 'clike';
27
- const NEW_LINE_EXP = /\n(?!$)/g;
28
-
29
- Prism.languages.insertBefore(
30
- 'javascript',
31
- 'string',
32
- {
33
- 'property string': {
34
- pattern: /([{,]\s*)"(?:\\.|[^\\"\r\n])*"(?=\s*:)/i,
35
- lookbehind: true,
36
- },
37
- },
38
- undefined,
39
- );
40
-
41
- Prism.languages.insertBefore(
42
- 'javascript',
43
- 'punctuation',
44
- {
45
- property: {
46
- pattern: /([{,]\s*)[a-z]\w*(?=\s*:)/i,
47
- lookbehind: true,
48
- },
49
- },
50
- undefined,
51
- );
52
-
53
- Prism.languages.markdoc = {
54
- ...Prism.languages.markdown,
55
- tag: {
56
- pattern: /{%(.|\n)*?%}/i,
57
- inside: {
58
- keyword: {
59
- pattern: /^({%\s*\/?)(\w*|-)*\b/i,
60
- lookbehind: true,
61
- },
62
- id: /#(\w|-)*\b/,
63
- string: /".*?"/,
64
- equals: /=/,
65
- number: /\b\d+\b/i,
66
- variable: {
67
- pattern: /\$[\w.]+/i,
68
- inside: {
69
- punctuation: /\./i,
70
- },
71
- },
72
- function: /\b\w+(?=\()/,
73
- punctuation: /({%|\/?%})/i,
74
- boolean: /false|true/,
75
- },
76
- },
77
- variable: {
78
- pattern: /\$\w+/i,
79
- },
80
- function: {
81
- pattern: /\b\w+(?=\()/i,
82
- },
83
- };
84
-
85
- /**
86
- * map language names to Prism.js names
87
- */
88
- export function mapLang(lang: string): string {
89
- return (
90
- {
91
- json: 'js',
92
- 'c++': 'cpp',
93
- 'c#': 'csharp',
94
- 'c#+newtonsoft': 'csharp',
95
- 'java8+apache': 'java',
96
- 'objective-c': 'objectivec',
97
- shell: 'bash',
98
- viml: 'vim',
99
- curl: 'bash',
100
- 'node.js': 'js',
101
- }[lang] || DEFAULT_LANG
102
- );
103
- }
104
-
105
- /**
106
- * Highlight source code string using Prism.js
107
- * @param source source code to highlight
108
- * @param lang highlight language
109
- * @return highlighted source code as **html string**
110
- */
111
- export function highlight(source: string | number | boolean, lang: string = DEFAULT_LANG): string {
112
- lang = lang.toLowerCase();
113
- const grammar = Prism.languages[lang] || Prism.languages[mapLang(lang)];
114
- return Prism.highlight(source.toString(), grammar, lang);
115
- }
116
-
117
- export function addLineNumbers(highlightedCode: string, start = 1): string {
118
- const codeLines = highlightedCode.split(NEW_LINE_EXP);
119
-
120
- return codeLines
121
- .map((line, i) => {
122
- return `<span class="code-line" data-line-number=${start + i}>${line}</span>`;
123
- })
124
- .join('\n');
125
- }