@redocly/theme 0.45.2-rc.1 → 0.45.2
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 +1 -2
- package/lib/components/CodeBlock/CodeBlock.js +9 -31
- package/lib/components/CodeBlock/CodeBlockContainer.d.ts +1 -1
- package/lib/components/CodeBlock/CodeBlockContainer.js +13 -242
- package/lib/components/CodeBlock/variables.js +0 -30
- package/lib/components/Filter/FilterContent.js +4 -1
- package/lib/components/Footer/FooterColumn.js +1 -1
- package/lib/components/Markdown/variables.js +1 -1
- package/lib/components/Search/SearchFilterField.js +2 -2
- package/lib/components/UserMenu/LogoutMenuItem.js +1 -1
- package/lib/components/UserMenu/UserMenu.js +2 -2
- package/lib/core/types/hooks.d.ts +0 -7
- package/lib/core/utils/highlight.d.ts +35 -0
- package/lib/core/utils/highlight.js +129 -0
- package/lib/core/utils/index.d.ts +1 -1
- package/lib/core/utils/index.js +1 -1
- package/lib/core/utils/type-guards.d.ts +6 -0
- package/lib/core/utils/type-guards.js +9 -1
- package/package.json +3 -1
- package/src/components/CodeBlock/CodeBlock.tsx +7 -19
- package/src/components/CodeBlock/CodeBlockContainer.tsx +14 -243
- package/src/components/CodeBlock/variables.ts +0 -30
- package/src/components/Filter/FilterContent.tsx +6 -1
- package/src/components/Footer/FooterColumn.tsx +2 -2
- package/src/components/Markdown/variables.ts +1 -1
- package/src/components/Search/SearchFilterField.tsx +1 -1
- package/src/components/UserMenu/LogoutMenuItem.tsx +1 -2
- package/src/components/UserMenu/UserMenu.tsx +1 -1
- package/src/core/types/hooks.ts +0 -10
- package/src/core/utils/highlight.ts +125 -0
- package/src/core/utils/index.ts +1 -1
- package/src/core/utils/type-guards.ts +12 -0
- package/lib/core/utils/add-line-numbers.d.ts +0 -6
- package/lib/core/utils/add-line-numbers.js +0 -19
- package/src/core/utils/add-line-numbers.ts +0 -17
package/src/core/types/hooks.ts
CHANGED
|
@@ -109,16 +109,6 @@ 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
|
-
};
|
|
122
112
|
};
|
|
123
113
|
|
|
124
114
|
export type L10nConfig = {
|
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
}
|
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/highlight';
|
|
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,3 +1,5 @@
|
|
|
1
|
+
import { ResolvedNavItem, ResolvedNavLinkItem } from '@redocly/config';
|
|
2
|
+
|
|
1
3
|
export function isUndefined<T>(value: T | undefined): value is undefined {
|
|
2
4
|
return value === undefined;
|
|
3
5
|
}
|
|
@@ -17,3 +19,13 @@ export function isNotNull<T>(value: T): value is NonNullable<T> {
|
|
|
17
19
|
export const isObject = (item: unknown): item is object => {
|
|
18
20
|
return isNotNull(item) && typeof item === 'object';
|
|
19
21
|
};
|
|
22
|
+
|
|
23
|
+
export const isNavLinkItem = (item: ResolvedNavItem): item is ResolvedNavLinkItem => {
|
|
24
|
+
return item.link !== undefined;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const isFromToSelectedOptions = (
|
|
28
|
+
options: unknown,
|
|
29
|
+
): options is { from: string; to: string } => {
|
|
30
|
+
return isObject(options) && 'from' in options && 'to' in options;
|
|
31
|
+
};
|
|
@@ -1,6 +0,0 @@
|
|
|
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;
|
|
@@ -1,19 +0,0 @@
|
|
|
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,17 +0,0 @@
|
|
|
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
|
-
}
|