@redocly/theme 0.45.1 → 0.45.2-rc.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.
- package/lib/components/CodeBlock/CodeBlock.d.ts +2 -1
- package/lib/components/CodeBlock/CodeBlock.js +31 -9
- package/lib/components/CodeBlock/CodeBlockContainer.d.ts +1 -1
- package/lib/components/CodeBlock/CodeBlockContainer.js +242 -13
- package/lib/components/CodeBlock/variables.js +30 -0
- package/lib/core/types/hooks.d.ts +7 -0
- package/lib/core/utils/add-line-numbers.d.ts +6 -0
- package/lib/core/utils/add-line-numbers.js +19 -0
- package/lib/core/utils/index.d.ts +1 -1
- package/lib/core/utils/index.js +1 -1
- package/package.json +1 -3
- package/src/components/CodeBlock/CodeBlock.tsx +19 -7
- package/src/components/CodeBlock/CodeBlockContainer.tsx +243 -14
- package/src/components/CodeBlock/variables.ts +30 -0
- package/src/core/types/hooks.ts +10 -0
- package/src/core/utils/add-line-numbers.ts +17 -0
- package/src/core/utils/index.ts +1 -1
- package/lib/core/utils/highlight.d.ts +0 -35
- package/lib/core/utils/highlight.js +0 -129
- package/src/core/utils/highlight.ts +0 -125
|
@@ -16,6 +16,7 @@ export type CodeBlockProps = {
|
|
|
16
16
|
codeBlockMaxHeight?: string;
|
|
17
17
|
hideCodeColors?: boolean;
|
|
18
18
|
wrapContents?: boolean;
|
|
19
|
+
[key: string]: unknown;
|
|
19
20
|
};
|
|
20
21
|
type UnstableExternalCodeSample = {
|
|
21
22
|
lang: string;
|
|
@@ -36,5 +37,5 @@ type ExternalSource = {
|
|
|
36
37
|
properties?: any;
|
|
37
38
|
operation?: any;
|
|
38
39
|
};
|
|
39
|
-
export declare function CodeBlock({ lang, source, externalSource, header, dataTestId, codeBlockRef, highlightedHtml, withLineNumbers, startLineNumber, className, codeBlockMaxHeight, tabs, hideCodeColors, wrapContents, children, }: React.PropsWithChildren<CodeBlockProps>): JSX.Element;
|
|
40
|
+
export declare function CodeBlock({ lang, source, externalSource, header, dataTestId, codeBlockRef, highlightedHtml, withLineNumbers, startLineNumber, className, codeBlockMaxHeight, tabs, hideCodeColors, wrapContents, children, ...rest }: React.PropsWithChildren<CodeBlockProps>): JSX.Element;
|
|
40
41
|
export {};
|
|
@@ -22,6 +22,17 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
26
|
+
var t = {};
|
|
27
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
28
|
+
t[p] = s[p];
|
|
29
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
30
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
31
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
32
|
+
t[p[i]] = s[p[i]];
|
|
33
|
+
}
|
|
34
|
+
return t;
|
|
35
|
+
};
|
|
25
36
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
37
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
38
|
};
|
|
@@ -34,10 +45,23 @@ const hooks_1 = require("../../core/hooks");
|
|
|
34
45
|
const ReportDialog_1 = require("../../components/Feedback/ReportDialog");
|
|
35
46
|
const CodeBlockContainer_1 = require("../../components/CodeBlock/CodeBlockContainer");
|
|
36
47
|
const CodeBlockControls_1 = require("../../components/CodeBlock/CodeBlockControls");
|
|
37
|
-
function CodeBlock(
|
|
38
|
-
var
|
|
39
|
-
|
|
40
|
-
const
|
|
48
|
+
function CodeBlock(_a) {
|
|
49
|
+
var _b, _c, _d, _e, _f, _g;
|
|
50
|
+
var { lang, source, externalSource, header, dataTestId = 'source-code', codeBlockRef, highlightedHtml, withLineNumbers, startLineNumber, className, codeBlockMaxHeight, tabs, hideCodeColors, wrapContents = false, children } = _a, rest = __rest(_a, ["lang", "source", "externalSource", "header", "dataTestId", "codeBlockRef", "highlightedHtml", "withLineNumbers", "startLineNumber", "className", "codeBlockMaxHeight", "tabs", "hideCodeColors", "wrapContents", "children"]);
|
|
51
|
+
const [sourceCode, setSourceCode] = (0, react_1.useState)((_d = (source || ((_c = (_b = externalSource === null || externalSource === void 0 ? void 0 : externalSource.sample) === null || _b === void 0 ? void 0 : _b.get) === null || _c === void 0 ? void 0 : _c.call(_b, externalSource)))) !== null && _d !== void 0 ? _d : '');
|
|
52
|
+
const { useCodeHighlight } = (0, hooks_1.useThemeHooks)();
|
|
53
|
+
const { highlight } = useCodeHighlight() || {};
|
|
54
|
+
const highlightedCode = highlightedHtml
|
|
55
|
+
? withLineNumbers
|
|
56
|
+
? (0, utils_1.addLineNumbers)(highlightedHtml, startLineNumber)
|
|
57
|
+
: highlightedHtml
|
|
58
|
+
: children
|
|
59
|
+
? null
|
|
60
|
+
: highlight === null || highlight === void 0 ? void 0 : highlight(sourceCode, lang, {
|
|
61
|
+
withLineNumbers,
|
|
62
|
+
startLineNumber,
|
|
63
|
+
highlight: rest['data-highlight'],
|
|
64
|
+
});
|
|
41
65
|
// The same initial value should be returned for ssr and frontend to avoid issues
|
|
42
66
|
// Because we don't have session storage in ssr and can't get the security details there
|
|
43
67
|
// Issue for more details https://github.com/Redocly/reference-docs/issues/888
|
|
@@ -50,15 +74,13 @@ function CodeBlock({ lang, source, externalSource, header, dataTestId = 'source-
|
|
|
50
74
|
}, [source, externalSource]);
|
|
51
75
|
const { reportDialog, reportButton } = (0, hooks_1.useReportDialog)();
|
|
52
76
|
(0, hooks_1.useModalScrollLock)(Boolean(reportDialog.visible));
|
|
53
|
-
const controls = (header === null || header === void 0 ? void 0 : header.controls) && Object.assign(Object.assign({}, header === null || header === void 0 ? void 0 : header.controls), { report: Object.assign(Object.assign({}, (
|
|
77
|
+
const controls = (header === null || header === void 0 ? void 0 : header.controls) && Object.assign(Object.assign({}, header === null || header === void 0 ? void 0 : header.controls), { report: Object.assign(Object.assign({}, (_e = header === null || header === void 0 ? void 0 : header.controls) === null || _e === void 0 ? void 0 : _e.report), { props: reportButton.props }), copy: ((_f = header === null || header === void 0 ? void 0 : header.controls) === null || _f === void 0 ? void 0 : _f.copy) ? Object.assign(Object.assign({}, (_g = header === null || header === void 0 ? void 0 : header.controls) === null || _g === void 0 ? void 0 : _g.copy), { data: sourceCode }) : undefined });
|
|
54
78
|
return (react_1.default.createElement(CodeBlockWrapper, { "data-component-name": "CodeBlock/CodeBlock", className: className },
|
|
55
79
|
react_1.default.createElement(ContainerWrapper, null,
|
|
56
80
|
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,
|
|
81
|
+
react_1.default.createElement(CodeBlockContainer_1.CodeBlockContainer, { ref: codeBlockRef, withLineNumbers: withLineNumbers, dangerouslySetInnerHTML: highlightedCode
|
|
58
82
|
? {
|
|
59
|
-
__html:
|
|
60
|
-
? (0, utils_1.addLineNumbers)(highlightedCode, startLineNumber)
|
|
61
|
-
: highlightedCode,
|
|
83
|
+
__html: highlightedCode,
|
|
62
84
|
}
|
|
63
85
|
: undefined, suppressHydrationWarning // TODO: investigate issue
|
|
64
86
|
: true, "data-testid": dataTestId, hideCodeColors: hideCodeColors, maxHeight: codeBlockMaxHeight, wrapContents: wrapContents, tabIndex: 0 }, children),
|
|
@@ -127,29 +127,258 @@ const CodeBlockContainerComponent = styled_components_1.default.pre `
|
|
|
127
127
|
cursor: help;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
.
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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);
|
|
141
|
+
margin-left: calc(var(--spacing-sm) * -1);
|
|
142
|
+
padding-left: var(--spacing-sm);
|
|
143
|
+
|
|
144
|
+
&.error {
|
|
145
|
+
background: var(--color-raspberry-2);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
&.warning {
|
|
149
|
+
background: var(--color-carrot-2);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/*
|
|
154
|
+
* Shiki styles
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
.line {
|
|
158
|
+
display: block;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
.has-diff {
|
|
162
|
+
> .line {
|
|
163
|
+
&:before {
|
|
164
|
+
content: ' ';
|
|
165
|
+
display: inline-block;
|
|
166
|
+
min-width: 1em;
|
|
167
|
+
padding-right: 0.4em;
|
|
168
|
+
text-align: right;
|
|
169
|
+
pointer-events: none;
|
|
170
|
+
user-select: none;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
.diff {
|
|
176
|
+
margin-left: calc(var(--spacing-sm) * -1);
|
|
177
|
+
padding-left: var(--spacing-sm);
|
|
178
|
+
|
|
179
|
+
&.add {
|
|
180
|
+
background: var(--color-grass-2);
|
|
181
|
+
&:before {
|
|
182
|
+
content: '+';
|
|
183
|
+
color: var(--color-grass-7);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
&.remove {
|
|
187
|
+
background: var(--color-raspberry-2);
|
|
188
|
+
|
|
189
|
+
&:before {
|
|
190
|
+
content: '-';
|
|
191
|
+
color: var(--color-raspberry-7);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
.highlighted-word {
|
|
197
|
+
background: var(--color-carrot-2);
|
|
198
|
+
border: 1px solid var(--color-carrot-5);
|
|
199
|
+
border-radius: 4px;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
.has-focused {
|
|
203
|
+
.line {
|
|
204
|
+
opacity: 0.7;
|
|
205
|
+
filter: blur(0.095rem);
|
|
206
|
+
transition:
|
|
207
|
+
filter 0.35s,
|
|
208
|
+
opacity 0.35s;
|
|
209
|
+
&.focused {
|
|
210
|
+
opacity: 1;
|
|
211
|
+
filter: blur(0);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
&:hover {
|
|
216
|
+
.line {
|
|
217
|
+
opacity: 1;
|
|
218
|
+
filter: blur(0);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/*
|
|
224
|
+
* Tree view styles
|
|
225
|
+
*/
|
|
226
|
+
|
|
227
|
+
.tree-view-line {
|
|
228
|
+
display: flex;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
.tree-view-branch {
|
|
232
|
+
color: var(--code-block-tree-view-lines-color);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
.tree-view-file {
|
|
236
|
+
display: inline-flex;
|
|
237
|
+
&::before {
|
|
238
|
+
content: '\\ea01';
|
|
239
|
+
margin-right: 0.5rem;
|
|
240
|
+
font-family: 'TreeViewIcons';
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
&.no-file {
|
|
244
|
+
&::before {
|
|
245
|
+
content: '';
|
|
246
|
+
margin-right: 0;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
&.ext-folder {
|
|
251
|
+
&::before {
|
|
252
|
+
content: '\\ea02';
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
&.ext-img,
|
|
257
|
+
&.ext-png,
|
|
258
|
+
&.ext-jpg,
|
|
259
|
+
&.ext-jpeg,
|
|
260
|
+
&.ext-gif,
|
|
261
|
+
&.ext-svg {
|
|
262
|
+
&::before {
|
|
263
|
+
content: '\\ea03';
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
&.ext-mp2,
|
|
268
|
+
&.ext-mp3,
|
|
269
|
+
&.ext-wav,
|
|
270
|
+
&.ext-ogg,
|
|
271
|
+
&.ext-flac {
|
|
272
|
+
&::before {
|
|
273
|
+
content: '\\ea04';
|
|
274
|
+
margin-right: 0.5rem;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
&.ext-mp4,
|
|
279
|
+
&.ext-mkv,
|
|
280
|
+
&.ext-avi,
|
|
281
|
+
&.ext-mov,
|
|
282
|
+
&.ext-wmv,
|
|
283
|
+
&.ext-flv {
|
|
284
|
+
&::before {
|
|
285
|
+
content: '\\ea05';
|
|
286
|
+
margin-right: 0.5rem;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
&.ext-txt,
|
|
291
|
+
&.ext-text,
|
|
292
|
+
&.ext-md,
|
|
293
|
+
&.ext-markdown {
|
|
294
|
+
&::before {
|
|
295
|
+
content: '\\ea06';
|
|
296
|
+
margin-right: 0.5rem;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
&.ext-js,
|
|
301
|
+
&.ext-ts,
|
|
302
|
+
&.ext-jsx,
|
|
303
|
+
&.ext-tsx,
|
|
304
|
+
&.ext-json,
|
|
305
|
+
&.ext-yaml,
|
|
306
|
+
&.ext-yml,
|
|
307
|
+
&.ext-xml,
|
|
308
|
+
&.ext-html,
|
|
309
|
+
&.ext-css,
|
|
310
|
+
&.ext-scss,
|
|
311
|
+
&.ext-less,
|
|
312
|
+
&.ext-sass,
|
|
313
|
+
&.ext-java,
|
|
314
|
+
&.ext-c,
|
|
315
|
+
&.ext-cpp,
|
|
316
|
+
&.ext-cs,
|
|
317
|
+
&.ext-php,
|
|
318
|
+
&.ext-py,
|
|
319
|
+
&.ext-rb,
|
|
320
|
+
&.ext-go,
|
|
321
|
+
&.ext-swift,
|
|
322
|
+
&.ext-sql,
|
|
323
|
+
&.ext-perl,
|
|
324
|
+
&.ext-lua,
|
|
325
|
+
&.ext-scala,
|
|
326
|
+
&.ext-sh {
|
|
327
|
+
&::before {
|
|
328
|
+
content: '\\ea07';
|
|
329
|
+
margin-right: 0.5rem;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
&.ext-zip,
|
|
334
|
+
&.ext-rar,
|
|
335
|
+
&.ext-tar,
|
|
336
|
+
&.ext-gz,
|
|
337
|
+
&.ext-iso {
|
|
338
|
+
&::before {
|
|
339
|
+
content: '\\ea08';
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
&.ext-pdf,
|
|
344
|
+
&.ext-PDF {
|
|
345
|
+
&::before {
|
|
346
|
+
content: '\\ea09';
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
&.ext-excel,
|
|
351
|
+
&.ext-xls,
|
|
352
|
+
&.ext-xlsx {
|
|
353
|
+
&::before {
|
|
354
|
+
content: '\\ea0a';
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
&.ext-powerpoint,
|
|
359
|
+
&.ext-ppt,
|
|
360
|
+
&.ext-pptx {
|
|
361
|
+
&::before {
|
|
362
|
+
content: '\\ea0b';
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
&.ext-doc,
|
|
367
|
+
&.ext-docx,
|
|
368
|
+
&.ext-rtf {
|
|
369
|
+
&::before {
|
|
370
|
+
content: '\\ea0c';
|
|
371
|
+
}
|
|
372
|
+
}
|
|
144
373
|
}
|
|
145
374
|
|
|
146
375
|
${(0, utils_1.generateCodeBlockTokens)()}
|
|
147
376
|
|
|
148
377
|
${({ hideCodeColors }) => hideCodeColors &&
|
|
149
378
|
(0, styled_components_1.css) `
|
|
150
|
-
.
|
|
151
|
-
.
|
|
152
|
-
.
|
|
379
|
+
.line-number:not(.highlighted),
|
|
380
|
+
.line-number:not(.highlighted) > span,
|
|
381
|
+
.line-number:not(.highlighted) > span > span {
|
|
153
382
|
color: grey;
|
|
154
383
|
}
|
|
155
384
|
`}
|
|
@@ -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+MAAAlACm1VqmhlYWQAAAXoAAAAKgAAADZfxj5jaGhlYQAABhQAAAAYAAAAJAFbAMFobXR4AAAGLAAAAA4AAAA0CGQAAGxvY2EAAAY8AAAAHAAAABwM9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RuxOoniEI2sVUIZnYqJzqxMEi1yo3dybf2ttfk4CJTT/bVOMYNBjAIpFiTJOLCWOGLOHGGPBCE7l32XO0tmw04MjQwCQ7774B//lDmrZkJY3hvOrHBiLuiJMKJqoVgrejQ3CP5Yubt0JwxNJa96Oypr6j621VSOMQKG+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,13 @@ 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
|
+
highlight?: string;
|
|
103
|
+
}) => string;
|
|
104
|
+
};
|
|
98
105
|
};
|
|
99
106
|
export type L10nConfig = {
|
|
100
107
|
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/
|
|
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';
|
package/lib/core/utils/index.js
CHANGED
|
@@ -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/
|
|
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.1",
|
|
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 {
|
|
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';
|
|
@@ -26,6 +26,7 @@ export type CodeBlockProps = {
|
|
|
26
26
|
codeBlockMaxHeight?: string;
|
|
27
27
|
hideCodeColors?: boolean;
|
|
28
28
|
wrapContents?: boolean;
|
|
29
|
+
[key: string]: unknown;
|
|
29
30
|
};
|
|
30
31
|
|
|
31
32
|
type UnstableExternalCodeSample = {
|
|
@@ -64,12 +65,25 @@ export function CodeBlock({
|
|
|
64
65
|
hideCodeColors,
|
|
65
66
|
wrapContents = false,
|
|
66
67
|
children,
|
|
68
|
+
...rest
|
|
67
69
|
}: React.PropsWithChildren<CodeBlockProps>): JSX.Element {
|
|
68
70
|
const [sourceCode, setSourceCode] = useState<string>(
|
|
69
71
|
(source || externalSource?.sample?.get?.(externalSource)) ?? '',
|
|
70
72
|
);
|
|
73
|
+
const { useCodeHighlight } = useThemeHooks();
|
|
74
|
+
const { highlight } = useCodeHighlight() || {};
|
|
71
75
|
|
|
72
|
-
const highlightedCode = highlightedHtml
|
|
76
|
+
const highlightedCode = highlightedHtml
|
|
77
|
+
? withLineNumbers
|
|
78
|
+
? addLineNumbers(highlightedHtml, startLineNumber)
|
|
79
|
+
: highlightedHtml
|
|
80
|
+
: children
|
|
81
|
+
? null
|
|
82
|
+
: highlight?.(sourceCode, lang, {
|
|
83
|
+
withLineNumbers,
|
|
84
|
+
startLineNumber,
|
|
85
|
+
highlight: rest['data-highlight'] as string | undefined,
|
|
86
|
+
});
|
|
73
87
|
|
|
74
88
|
// The same initial value should be returned for ssr and frontend to avoid issues
|
|
75
89
|
// Because we don't have session storage in ssr and can't get the security details there
|
|
@@ -102,13 +116,11 @@ export function CodeBlock({
|
|
|
102
116
|
/>
|
|
103
117
|
<CodeBlockContainer
|
|
104
118
|
ref={codeBlockRef}
|
|
105
|
-
|
|
119
|
+
withLineNumbers={withLineNumbers}
|
|
106
120
|
dangerouslySetInnerHTML={
|
|
107
121
|
highlightedCode
|
|
108
122
|
? {
|
|
109
|
-
__html:
|
|
110
|
-
? addLineNumbers(highlightedCode, startLineNumber)
|
|
111
|
-
: highlightedCode,
|
|
123
|
+
__html: highlightedCode,
|
|
112
124
|
}
|
|
113
125
|
: undefined
|
|
114
126
|
}
|
|
@@ -8,7 +8,7 @@ export type CodeBlockContainerProps = PropsWithChildren<{
|
|
|
8
8
|
maxHeight?: string;
|
|
9
9
|
wrapContents?: boolean;
|
|
10
10
|
ref?: (instance: HTMLPreElement | null) => void;
|
|
11
|
-
|
|
11
|
+
withLineNumbers?: boolean;
|
|
12
12
|
dangerouslySetInnerHTML?: {
|
|
13
13
|
__html: string;
|
|
14
14
|
};
|
|
@@ -117,20 +117,249 @@ const CodeBlockContainerComponent = styled.pre<CodeBlockContainerProps>`
|
|
|
117
117
|
cursor: help;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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);
|
|
131
|
+
margin-left: calc(var(--spacing-sm) * -1);
|
|
132
|
+
padding-left: var(--spacing-sm);
|
|
133
|
+
|
|
134
|
+
&.error {
|
|
135
|
+
background: var(--color-raspberry-2);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
&.warning {
|
|
139
|
+
background: var(--color-carrot-2);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
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
|
+
margin-left: calc(var(--spacing-sm) * -1);
|
|
167
|
+
padding-left: var(--spacing-sm);
|
|
168
|
+
|
|
169
|
+
&.add {
|
|
170
|
+
background: var(--color-grass-2);
|
|
171
|
+
&:before {
|
|
172
|
+
content: '+';
|
|
173
|
+
color: var(--color-grass-7);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
&.remove {
|
|
177
|
+
background: var(--color-raspberry-2);
|
|
178
|
+
|
|
179
|
+
&:before {
|
|
180
|
+
content: '-';
|
|
181
|
+
color: var(--color-raspberry-7);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
.highlighted-word {
|
|
187
|
+
background: var(--color-carrot-2);
|
|
188
|
+
border: 1px solid var(--color-carrot-5);
|
|
189
|
+
border-radius: 4px;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
.has-focused {
|
|
193
|
+
.line {
|
|
194
|
+
opacity: 0.7;
|
|
195
|
+
filter: blur(0.095rem);
|
|
196
|
+
transition:
|
|
197
|
+
filter 0.35s,
|
|
198
|
+
opacity 0.35s;
|
|
199
|
+
&.focused {
|
|
200
|
+
opacity: 1;
|
|
201
|
+
filter: blur(0);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
&:hover {
|
|
206
|
+
.line {
|
|
207
|
+
opacity: 1;
|
|
208
|
+
filter: blur(0);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/*
|
|
214
|
+
* Tree view styles
|
|
215
|
+
*/
|
|
216
|
+
|
|
217
|
+
.tree-view-line {
|
|
218
|
+
display: flex;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
.tree-view-branch {
|
|
222
|
+
color: var(--code-block-tree-view-lines-color);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
.tree-view-file {
|
|
226
|
+
display: inline-flex;
|
|
227
|
+
&::before {
|
|
228
|
+
content: '\\ea01';
|
|
229
|
+
margin-right: 0.5rem;
|
|
230
|
+
font-family: 'TreeViewIcons';
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
&.no-file {
|
|
234
|
+
&::before {
|
|
235
|
+
content: '';
|
|
236
|
+
margin-right: 0;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
&.ext-folder {
|
|
241
|
+
&::before {
|
|
242
|
+
content: '\\ea02';
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
&.ext-img,
|
|
247
|
+
&.ext-png,
|
|
248
|
+
&.ext-jpg,
|
|
249
|
+
&.ext-jpeg,
|
|
250
|
+
&.ext-gif,
|
|
251
|
+
&.ext-svg {
|
|
252
|
+
&::before {
|
|
253
|
+
content: '\\ea03';
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
&.ext-mp2,
|
|
258
|
+
&.ext-mp3,
|
|
259
|
+
&.ext-wav,
|
|
260
|
+
&.ext-ogg,
|
|
261
|
+
&.ext-flac {
|
|
262
|
+
&::before {
|
|
263
|
+
content: '\\ea04';
|
|
264
|
+
margin-right: 0.5rem;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
&.ext-mp4,
|
|
269
|
+
&.ext-mkv,
|
|
270
|
+
&.ext-avi,
|
|
271
|
+
&.ext-mov,
|
|
272
|
+
&.ext-wmv,
|
|
273
|
+
&.ext-flv {
|
|
274
|
+
&::before {
|
|
275
|
+
content: '\\ea05';
|
|
276
|
+
margin-right: 0.5rem;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
&.ext-txt,
|
|
281
|
+
&.ext-text,
|
|
282
|
+
&.ext-md,
|
|
283
|
+
&.ext-markdown {
|
|
284
|
+
&::before {
|
|
285
|
+
content: '\\ea06';
|
|
286
|
+
margin-right: 0.5rem;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
&.ext-js,
|
|
291
|
+
&.ext-ts,
|
|
292
|
+
&.ext-jsx,
|
|
293
|
+
&.ext-tsx,
|
|
294
|
+
&.ext-json,
|
|
295
|
+
&.ext-yaml,
|
|
296
|
+
&.ext-yml,
|
|
297
|
+
&.ext-xml,
|
|
298
|
+
&.ext-html,
|
|
299
|
+
&.ext-css,
|
|
300
|
+
&.ext-scss,
|
|
301
|
+
&.ext-less,
|
|
302
|
+
&.ext-sass,
|
|
303
|
+
&.ext-java,
|
|
304
|
+
&.ext-c,
|
|
305
|
+
&.ext-cpp,
|
|
306
|
+
&.ext-cs,
|
|
307
|
+
&.ext-php,
|
|
308
|
+
&.ext-py,
|
|
309
|
+
&.ext-rb,
|
|
310
|
+
&.ext-go,
|
|
311
|
+
&.ext-swift,
|
|
312
|
+
&.ext-sql,
|
|
313
|
+
&.ext-perl,
|
|
314
|
+
&.ext-lua,
|
|
315
|
+
&.ext-scala,
|
|
316
|
+
&.ext-sh {
|
|
317
|
+
&::before {
|
|
318
|
+
content: '\\ea07';
|
|
319
|
+
margin-right: 0.5rem;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
&.ext-zip,
|
|
324
|
+
&.ext-rar,
|
|
325
|
+
&.ext-tar,
|
|
326
|
+
&.ext-gz,
|
|
327
|
+
&.ext-iso {
|
|
328
|
+
&::before {
|
|
329
|
+
content: '\\ea08';
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
&.ext-pdf,
|
|
334
|
+
&.ext-PDF {
|
|
335
|
+
&::before {
|
|
336
|
+
content: '\\ea09';
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
&.ext-excel,
|
|
341
|
+
&.ext-xls,
|
|
342
|
+
&.ext-xlsx {
|
|
343
|
+
&::before {
|
|
344
|
+
content: '\\ea0a';
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
&.ext-powerpoint,
|
|
349
|
+
&.ext-ppt,
|
|
350
|
+
&.ext-pptx {
|
|
351
|
+
&::before {
|
|
352
|
+
content: '\\ea0b';
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
&.ext-doc,
|
|
357
|
+
&.ext-docx,
|
|
358
|
+
&.ext-rtf {
|
|
359
|
+
&::before {
|
|
360
|
+
content: '\\ea0c';
|
|
361
|
+
}
|
|
362
|
+
}
|
|
134
363
|
}
|
|
135
364
|
|
|
136
365
|
${generateCodeBlockTokens()}
|
|
@@ -138,9 +367,9 @@ const CodeBlockContainerComponent = styled.pre<CodeBlockContainerProps>`
|
|
|
138
367
|
${({ hideCodeColors }) =>
|
|
139
368
|
hideCodeColors &&
|
|
140
369
|
css`
|
|
141
|
-
.
|
|
142
|
-
.
|
|
143
|
-
.
|
|
370
|
+
.line-number:not(.highlighted),
|
|
371
|
+
.line-number:not(.highlighted) > span,
|
|
372
|
+
.line-number:not(.highlighted) > span > span {
|
|
144
373
|
color: grey;
|
|
145
374
|
}
|
|
146
375
|
`}
|
|
@@ -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+HBvc3QAAAewAAAAZgAAAIka0DSfeJxjYGRgYOBiMGCwY2BycfMJYeDLSSzJY5BiYGGAAJA8MpsxJzM9kYEDxgPKsYBpDiBmg4gCACY7BUgAeJxjYGRYyjiBgZWBgaGQoRZISkLpUAYOBj0GBiYGVmYGrCAgzTWFweEV4ysehs1ArgDDFgZGIA3CDAB2tQjAAHic7ZHLEc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6dy/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
|
`;
|
package/src/core/types/hooks.ts
CHANGED
|
@@ -109,6 +109,16 @@ 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
|
+
{
|
|
117
|
+
withLineNumbers,
|
|
118
|
+
startLineNumber,
|
|
119
|
+
}: { withLineNumbers?: boolean; startLineNumber?: number; highlight?: string },
|
|
120
|
+
) => string;
|
|
121
|
+
};
|
|
112
122
|
};
|
|
113
123
|
|
|
114
124
|
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
|
+
}
|
package/src/core/utils/index.ts
CHANGED
|
@@ -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/
|
|
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
|
-
}
|