@docusaurus/theme-classic 0.0.0-4185 → 0.0.0-4193

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.
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ declare module 'prism-react-renderer/prism' {
9
+ import type * as PrismNamespace from 'prismjs';
10
+
11
+ const Prism: typeof PrismNamespace;
12
+ export default Prism;
13
+ }
@@ -4,84 +4,19 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import React, {useEffect, useState, useRef} from 'react';
7
+ import React, {useEffect, useState} from 'react';
8
8
  import clsx from 'clsx';
9
9
  import Highlight, {defaultProps} from 'prism-react-renderer';
10
10
  import copy from 'copy-text-to-clipboard';
11
- import rangeParser from 'parse-numeric-range';
12
- import usePrismTheme from '@theme/hooks/usePrismTheme';
13
11
  import Translate, {translate} from '@docusaurus/Translate';
12
+ import {
13
+ useThemeConfig,
14
+ parseCodeBlockTitle,
15
+ parseLanguage,
16
+ parseLines,
17
+ } from '@docusaurus/theme-common';
18
+ import usePrismTheme from '@theme/hooks/usePrismTheme';
14
19
  import styles from './styles.module.css';
15
- import {useThemeConfig, parseCodeBlockTitle} from '@docusaurus/theme-common';
16
- const HighlightLinesRangeRegex = /{([\d,-]+)}/;
17
- const HighlightLanguages = ['js', 'jsBlock', 'jsx', 'python', 'html'];
18
- // Supported types of highlight comments
19
- const HighlightComments = {
20
- js: {
21
- start: '\\/\\/',
22
- end: '',
23
- },
24
- jsBlock: {
25
- start: '\\/\\*',
26
- end: '\\*\\/',
27
- },
28
- jsx: {
29
- start: '\\{\\s*\\/\\*',
30
- end: '\\*\\/\\s*\\}',
31
- },
32
- python: {
33
- start: '#',
34
- end: '',
35
- },
36
- html: {
37
- start: '<!--',
38
- end: '-->',
39
- },
40
- }; // Supported highlight directives
41
-
42
- const HighlightDirectives = [
43
- 'highlight-next-line',
44
- 'highlight-start',
45
- 'highlight-end',
46
- ];
47
-
48
- const getHighlightDirectiveRegex = (languages = HighlightLanguages) => {
49
- // to be more reliable, the opening and closing comment must match
50
- const commentPattern = languages
51
- .map((lang) => {
52
- const {start, end} = HighlightComments[lang];
53
- return `(?:${start}\\s*(${HighlightDirectives.join('|')})\\s*${end})`;
54
- })
55
- .join('|'); // white space is allowed, but otherwise it should be on it's own line
56
-
57
- return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
58
- }; // select comment styles based on language
59
-
60
- const highlightDirectiveRegex = (lang) => {
61
- switch (lang) {
62
- case 'js':
63
- case 'javascript':
64
- case 'ts':
65
- case 'typescript':
66
- return getHighlightDirectiveRegex(['js', 'jsBlock']);
67
-
68
- case 'jsx':
69
- case 'tsx':
70
- return getHighlightDirectiveRegex(['js', 'jsBlock', 'jsx']);
71
-
72
- case 'html':
73
- return getHighlightDirectiveRegex(['js', 'jsBlock', 'html']);
74
-
75
- case 'python':
76
- case 'py':
77
- return getHighlightDirectiveRegex(['python']);
78
-
79
- default:
80
- // all comment types
81
- return getHighlightDirectiveRegex();
82
- }
83
- };
84
-
85
20
  export default function CodeBlock({
86
21
  children,
87
22
  className: blockClassName,
@@ -105,74 +40,11 @@ export default function CodeBlock({
105
40
  // (note: title="xyz" => title prop still has the quotes)
106
41
 
107
42
  const codeBlockTitle = parseCodeBlockTitle(metastring) || title;
108
- const button = useRef(null);
109
- let highlightLines = [];
110
43
  const prismTheme = usePrismTheme(); // In case interleaved Markdown (e.g. when using CodeBlock as standalone component).
111
44
 
112
45
  const content = Array.isArray(children) ? children.join('') : children;
113
-
114
- if (metastring && HighlightLinesRangeRegex.test(metastring)) {
115
- // Tested above
116
- const highlightLinesRange = metastring.match(HighlightLinesRangeRegex)[1];
117
- highlightLines = rangeParser(highlightLinesRange).filter((n) => n > 0);
118
- }
119
-
120
- const languageClassName = blockClassName
121
- ?.split(' ')
122
- .find((str) => str.startsWith('language-'));
123
- let language = languageClassName?.replace(/language-/, '');
124
-
125
- if (!language && prism.defaultLanguage) {
126
- language = prism.defaultLanguage;
127
- } // only declaration OR directive highlight can be used for a block
128
-
129
- let code = content.replace(/\n$/, '');
130
-
131
- if (highlightLines.length === 0 && language !== undefined) {
132
- let range = '';
133
- const directiveRegex = highlightDirectiveRegex(language); // go through line by line
134
-
135
- const lines = content.replace(/\n$/, '').split('\n');
136
- let blockStart; // loop through lines
137
-
138
- for (let index = 0; index < lines.length; ) {
139
- const line = lines[index]; // adjust for 0-index
140
-
141
- const lineNumber = index + 1;
142
- const match = line.match(directiveRegex);
143
-
144
- if (match !== null) {
145
- const directive = match
146
- .slice(1)
147
- .reduce((final, item) => final || item, undefined);
148
-
149
- switch (directive) {
150
- case 'highlight-next-line':
151
- range += `${lineNumber},`;
152
- break;
153
-
154
- case 'highlight-start':
155
- blockStart = lineNumber;
156
- break;
157
-
158
- case 'highlight-end':
159
- range += `${blockStart}-${lineNumber - 1},`;
160
- break;
161
-
162
- default:
163
- break;
164
- }
165
-
166
- lines.splice(index, 1);
167
- } else {
168
- // lines without directives are unchanged
169
- index += 1;
170
- }
171
- }
172
-
173
- highlightLines = rangeParser(range);
174
- code = lines.join('\n');
175
- }
46
+ const language = parseLanguage(blockClassName) ?? prism.defaultLanguage;
47
+ const {highlightLines, code} = parseLines(content, metastring, language);
176
48
 
177
49
  const handleCopyCode = () => {
178
50
  copy(code);
@@ -188,11 +60,7 @@ export default function CodeBlock({
188
60
  code={code}
189
61
  language={language}>
190
62
  {({className, style, tokens, getLineProps, getTokenProps}) => (
191
- <div
192
- className={clsx(
193
- styles.codeBlockContainer,
194
- blockClassName?.replace(/language-[^ ]+/, ''),
195
- )}>
63
+ <div className={clsx(styles.codeBlockContainer, blockClassName)}>
196
64
  {codeBlockTitle && (
197
65
  <div style={style} className={styles.codeBlockTitle}>
198
66
  {codeBlockTitle}
@@ -215,7 +83,7 @@ export default function CodeBlock({
215
83
  key: i,
216
84
  });
217
85
 
218
- if (highlightLines.includes(i + 1)) {
86
+ if (highlightLines.includes(i)) {
219
87
  lineProps.className += ' docusaurus-highlight-code-line';
220
88
  }
221
89
 
@@ -238,7 +106,6 @@ export default function CodeBlock({
238
106
  </pre>
239
107
 
240
108
  <button
241
- ref={button}
242
109
  type="button"
243
110
  aria-label={translate({
244
111
  id: 'theme.CodeBlock.copyButtonAriaLabel',
@@ -9,8 +9,7 @@ import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
9
9
  import {useLatestVersion, useActiveDocContext} from '@theme/hooks/useDocs';
10
10
  import clsx from 'clsx';
11
11
  import {getInfimaActiveClassName} from './index';
12
- import {useDocsPreferredVersion} from '@docusaurus/theme-common';
13
- import {uniq} from '@docusaurus/utils-common';
12
+ import {useDocsPreferredVersion, uniq} from '@docusaurus/theme-common';
14
13
 
15
14
  function getDocInVersions(versions, docId) {
16
15
  const allDocs = versions.flatMap((version) => version.docs);