@mui/internal-docs-infra 0.3.1-canary.2 → 0.3.1-canary.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/esm/CodeControllerContext/CodeControllerContext.js +2 -2
  2. package/esm/CodeExternalsContext/CodeExternalsContext.js +1 -1
  3. package/esm/CodeHighlighter/CodeHighlighter.js +252 -327
  4. package/esm/CodeHighlighter/CodeHighlighterClient.js +447 -653
  5. package/esm/CodeHighlighter/CodeHighlighterContext.js +2 -2
  6. package/esm/CodeHighlighter/CodeHighlighterFallbackContext.js +2 -2
  7. package/esm/CodeHighlighter/codeToFallbackProps.js +21 -37
  8. package/esm/CodeHighlighter/errors.js +248 -400
  9. package/esm/CodeHighlighter/parseControlledCode.js +12 -20
  10. package/esm/CodeHighlighter/types.d.ts +7 -1
  11. package/esm/CodeProvider/CodeContext.js +3 -3
  12. package/esm/CodeProvider/CodeProvider.js +31 -40
  13. package/esm/abstractCreateDemo/abstractCreateDemo.js +13 -17
  14. package/esm/abstractCreateDemoClient/abstractCreateDemoClient.js +12 -12
  15. package/esm/cli/index.js +1 -1
  16. package/esm/cli/runValidate.js +160 -264
  17. package/esm/createDemoData/createDemoData.js +11 -12
  18. package/esm/createSitemap/createSitemap.js +2 -2
  19. package/esm/pipeline/getFileConventions/fileConventions.js +1 -1
  20. package/esm/pipeline/getFileConventions/getFileConventions.js +2 -15
  21. package/esm/pipeline/hastUtils/hastUtils.js +16 -17
  22. package/esm/pipeline/loadCodeVariant/addCodeVariantPaths.js +24 -24
  23. package/esm/pipeline/loadCodeVariant/applyCodeTransform.js +12 -22
  24. package/esm/pipeline/loadCodeVariant/calculateMainFilePath.js +30 -37
  25. package/esm/pipeline/loadCodeVariant/computeHastDeltas.js +107 -185
  26. package/esm/pipeline/loadCodeVariant/diffHast.js +18 -53
  27. package/esm/pipeline/loadCodeVariant/examineCodeVariant.js +24 -27
  28. package/esm/pipeline/loadCodeVariant/flattenCodeVariant.js +9 -10
  29. package/esm/pipeline/loadCodeVariant/hasAllCodeVariants.js +5 -5
  30. package/esm/pipeline/loadCodeVariant/loadCodeFallback.js +517 -727
  31. package/esm/pipeline/loadCodeVariant/loadCodeVariant.js +683 -1032
  32. package/esm/pipeline/loadCodeVariant/maybeCodeInitialData.js +14 -20
  33. package/esm/pipeline/loadCodeVariant/mergeCodeMetadata.js +53 -63
  34. package/esm/pipeline/loadCodeVariant/parseCode.js +40 -48
  35. package/esm/pipeline/loadCodeVariant/pathUtils.js +43 -64
  36. package/esm/pipeline/loadCodeVariant/transformSource.js +55 -125
  37. package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.js +160 -221
  38. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.js +377 -479
  39. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.js +171 -173
  40. package/esm/pipeline/loadPrecomputedCodeHighlighter/performanceLogger.js +14 -30
  41. package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.js +19 -21
  42. package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.js +37 -71
  43. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.js +3 -9
  44. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.js +54 -80
  45. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.js +71 -98
  46. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.js +5 -5
  47. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.js +161 -211
  48. package/esm/pipeline/loadPrecomputedSitemap/loadPrecomputedSitemap.js +159 -207
  49. package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.js +42 -64
  50. package/esm/pipeline/loadServerCodeMeta/resolveModulePathWithFs.js +20 -96
  51. package/esm/pipeline/loadServerPageIndex/loadServerPageIndex.js +66 -85
  52. package/esm/pipeline/loadServerSitemap/loadServerSitemap.js +71 -118
  53. package/esm/pipeline/loadServerSource/loadServerSource.js +121 -148
  54. package/esm/pipeline/loaderUtils/externalsToPackages.js +7 -7
  55. package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.js +8 -12
  56. package/esm/pipeline/loaderUtils/fileUrlToPortablePath.js +5 -5
  57. package/esm/pipeline/loaderUtils/getFileNameFromUrl.js +19 -29
  58. package/esm/pipeline/loaderUtils/getLanguageFromExtension.d.ts +24 -0
  59. package/esm/pipeline/loaderUtils/getLanguageFromExtension.js +62 -0
  60. package/esm/pipeline/loaderUtils/index.d.ts +2 -1
  61. package/esm/pipeline/loaderUtils/index.js +2 -1
  62. package/esm/pipeline/loaderUtils/mergeExternals.js +15 -35
  63. package/esm/pipeline/loaderUtils/parseImportsAndComments.js +413 -433
  64. package/esm/pipeline/loaderUtils/processRelativeImports.js +153 -239
  65. package/esm/pipeline/loaderUtils/resolveModulePath.js +544 -1303
  66. package/esm/pipeline/loaderUtils/rewriteImports.js +73 -111
  67. package/esm/pipeline/parseSource/addLineGutters.js +33 -45
  68. package/esm/pipeline/parseSource/grammars.d.ts +12 -1
  69. package/esm/pipeline/parseSource/grammars.js +36 -4
  70. package/esm/pipeline/parseSource/index.d.ts +2 -1
  71. package/esm/pipeline/parseSource/index.js +2 -1
  72. package/esm/pipeline/parseSource/parseSource.js +23 -32
  73. package/esm/pipeline/syncPageIndex/createMarkdownNodes.js +32 -55
  74. package/esm/pipeline/syncPageIndex/mergeMetadataMarkdown.js +107 -160
  75. package/esm/pipeline/syncPageIndex/metadataToMarkdown.js +846 -1033
  76. package/esm/pipeline/syncPageIndex/syncPageIndex.js +291 -438
  77. package/esm/pipeline/transformHtmlCodePrecomputed/transformHtmlCodePrecomputed.js +266 -320
  78. package/esm/pipeline/transformMarkdownBlockquoteCallouts/transformMarkdownBlockquoteCallouts.js +10 -10
  79. package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.js +183 -267
  80. package/esm/pipeline/transformMarkdownDemoLinks/transformMarkdownDemoLinks.js +25 -27
  81. package/esm/pipeline/transformMarkdownMetadata/transformMarkdownMetadata.js +572 -717
  82. package/esm/pipeline/transformMarkdownRelativePaths/transformMarkdownRelativePaths.js +8 -8
  83. package/esm/pipeline/transformTypescriptToJavascript/removeTypes.js +84 -113
  84. package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.js +10 -26
  85. package/esm/useCode/Pre.d.ts +2 -0
  86. package/esm/useCode/Pre.js +58 -60
  87. package/esm/useCode/useCode.js +59 -61
  88. package/esm/useCode/useCodeUtils.js +54 -63
  89. package/esm/useCode/useCopyFunctionality.js +10 -9
  90. package/esm/useCode/useFileNavigation.js +171 -209
  91. package/esm/useCode/useSourceEditing.js +17 -14
  92. package/esm/useCode/useTransformManagement.js +23 -26
  93. package/esm/useCode/useUIState.js +12 -20
  94. package/esm/useCode/useVariantSelection.js +62 -79
  95. package/esm/useCopier/index.js +29 -56
  96. package/esm/useDemo/createCodeSandbox.js +12 -15
  97. package/esm/useDemo/createStackBlitz.js +14 -20
  98. package/esm/useDemo/exportVariant.js +200 -180
  99. package/esm/useDemo/exportVariantAsCra.js +22 -25
  100. package/esm/useDemo/useDemo.js +80 -84
  101. package/esm/useErrors/ErrorsContext.js +1 -1
  102. package/esm/useErrors/useErrors.js +3 -3
  103. package/esm/useLocalStorageState/useLocalStorageState.js +23 -39
  104. package/esm/usePreference/PreferencesProvider.js +1 -1
  105. package/esm/usePreference/usePreference.js +9 -11
  106. package/esm/useSearch/useSearch.js +290 -387
  107. package/esm/useUrlHashState/useUrlHashState.js +11 -14
  108. package/esm/withDocsInfra/withDeploymentConfig.js +26 -21
  109. package/esm/withDocsInfra/withDocsInfra.js +99 -101
  110. package/package.json +7 -4
@@ -16,21 +16,21 @@ import { fileUrlToPortablePath } from "../loaderUtils/fileUrlToPortablePath.js";
16
16
  * - /(public)/components/page.tsx -> /components
17
17
  * This allows URLs to resolve when reading in VSCode and Github
18
18
  */
19
- export var transformMarkdownRelativePaths = function transformMarkdownRelativePaths() {
20
- return function (tree, file) {
21
- visit(tree, 'link', function (node) {
19
+ export const transformMarkdownRelativePaths = () => {
20
+ return (tree, file) => {
21
+ visit(tree, 'link', node => {
22
22
  if (node.url) {
23
23
  node.url = node.url.replace(/\/page\.(tsx|jsx|js|mdx|md)$/g, '');
24
24
  node.url = node.url.replace(/\/page\.(tsx|jsx|js|mdx|md)(\?[^#]*)?(#.*)?$/g, '$2$3');
25
25
  if ((node.url.startsWith('./') || node.url.startsWith('../')) && file.path) {
26
26
  // Convert filesystem path to portable path for cross-platform compatibility
27
- var portablePath = fileUrlToPortablePath(pathToFileURL(file.path).href);
28
- var currentDir = path.dirname(portablePath);
29
- var appIndex = currentDir.indexOf('/app/');
30
- var baseDir = appIndex !== -1 ? currentDir.substring(appIndex + 4) : '/';
27
+ const portablePath = fileUrlToPortablePath(pathToFileURL(file.path).href);
28
+ const currentDir = path.dirname(portablePath);
29
+ const appIndex = currentDir.indexOf('/app/');
30
+ const baseDir = appIndex !== -1 ? currentDir.substring(appIndex + 4) : '/';
31
31
 
32
32
  // Resolve the relative path from the current directory
33
- var resolvedPath = path.resolve('/', baseDir, node.url);
33
+ const resolvedPath = path.resolve('/', baseDir, node.url);
34
34
  node.url = resolvedPath;
35
35
  }
36
36
 
@@ -1,13 +1,10 @@
1
- import _regenerator from "@babel/runtime/helpers/esm/regenerator";
2
- import _extends from "@babel/runtime/helpers/esm/extends";
3
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
4
1
  // Based on https://github.com/ember-cli/babel-remove-types/blob/fc3be010e99c4f4926fd70d00242d6777ab1b8d7/src/index.ts
5
2
  // Converted to use Babel standalone, with added TSX support
6
3
 
7
4
  import * as Babel from '@babel/standalone';
8
- import prettier from 'prettier/standalone';
9
- import prettierPluginEstree from 'prettier/plugins/estree';
10
- import parserBabel from 'prettier/parser-babel';
5
+ import { format } from 'prettier/standalone';
6
+ import pluginBabel from 'prettier/plugins/babel';
7
+ import pluginEstree from 'prettier/plugins/estree';
11
8
  /**
12
9
  * Strips TypeScript types and decorators from code (including React in TSX),
13
10
  * preserving blank lines and optionally formatting with Prettier.
@@ -19,113 +16,87 @@ import parserBabel from 'prettier/parser-babel';
19
16
  * or a Prettier options object to customize.
20
17
  * @returns The transformed (and optionally formatted) code.
21
18
  */
22
- export function removeTypes(_x) {
23
- return _removeTypes.apply(this, arguments);
24
- }
25
- function _removeTypes() {
26
- _removeTypes = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(code) {
27
- var filename,
28
- prettierConfig,
29
- removeComments,
30
- isTSX,
31
- transformed,
32
- fixed,
33
- standardPrettierOptions,
34
- mergedPrettierOptions,
35
- _args = arguments;
36
- return _regenerator().w(function (_context) {
37
- while (1) switch (_context.n) {
38
- case 0:
39
- filename = _args.length > 1 && _args[1] !== undefined ? _args[1] : 'file.ts';
40
- prettierConfig = _args.length > 2 && _args[2] !== undefined ? _args[2] : true;
41
- // Babel collapses newlines all over the place, which messes with the formatting of almost any
42
- // code you pass to it. To preserve the formatting, we go through and mark all the empty lines
43
- // in the code string *before* transforming it. This allows us to go back through after the
44
- // transformation re-insert the empty lines in the correct place relative to the new code that
45
- // has been generated.
46
- code = code.replace(/\n\n+/g, '/* ___NEWLINE___ */\n');
19
+ export async function removeTypes(code, filename = 'file.ts', prettierConfig = true) {
20
+ // Babel collapses newlines all over the place, which messes with the formatting of almost any
21
+ // code you pass to it. To preserve the formatting, we go through and mark all the empty lines
22
+ // in the code string *before* transforming it. This allows us to go back through after the
23
+ // transformation re-insert the empty lines in the correct place relative to the new code that
24
+ // has been generated.
25
+ code = code.replace(/\n\n+/g, '/* ___NEWLINE___ */\n');
47
26
 
48
- // When removing TS-specific constructs (e.g. interfaces), we want to make sure we also remove
49
- // any comments that are associated with those constructs, since otherwise we'll be left with
50
- // comments that refer to something that isn't actually there.
51
- // Credit to https://github.com/cyco130/detype for figuring out this very useful pattern
52
- removeComments = {
53
- enter: function enter(nodePath) {
54
- if (!nodePath.node.leadingComments) {
55
- return;
56
- }
57
- for (var i = nodePath.node.leadingComments.length - 1; i >= 0; i -= 1) {
58
- var comment = nodePath.node.leadingComments[i];
59
- if (code.slice(comment.end).match(/^\s*\n\s*\n/) || comment.value.includes('___NEWLINE___')) {
60
- // There is at least one empty line between the comment and the TypeScript specific construct
61
- // We should keep this comment and those before it
62
- break;
63
- }
64
- comment.value = '___REMOVE_ME___';
65
- }
66
- }
67
- };
68
- isTSX = /\.tsx$/i.test(filename);
69
- transformed = Babel.transform(code, {
70
- filename: filename,
71
- plugins: [{
72
- name: 'comment-remover',
73
- visitor: {
74
- TSTypeAliasDeclaration: removeComments,
75
- TSInterfaceDeclaration: removeComments,
76
- TSDeclareFunction: removeComments,
77
- TSDeclareMethod: removeComments,
78
- TSImportType: removeComments,
79
- TSModuleDeclaration: removeComments
80
- }
81
- }, ['transform-typescript', {
82
- onlyRemoveTypeImports: true,
83
- isTSX: isTSX,
84
- allExtensions: true
85
- }], ['proposal-decorators', {
86
- legacy: true
87
- }]],
88
- generatorOpts: {
89
- retainLines: true,
90
- shouldPrintComment: function shouldPrintComment(c) {
91
- return c !== '___REMOVE_ME___';
92
- }
93
- }
94
- });
95
- if (!(!transformed || !transformed.code)) {
96
- _context.n = 1;
97
- break;
98
- }
99
- throw new Error('There was an issue with the Babel transform.');
100
- case 1:
101
- fixed = transformed.code.replace(/\/\* ___NEWLINE___ \*\//g, '\n'); // If the user has *explicitly* passed `false` here, it means they do not want us to run Prettier
102
- // at all, so we bail here.
103
- if (!(prettierConfig === false)) {
104
- _context.n = 2;
105
- break;
106
- }
107
- return _context.a(2, fixed);
108
- case 2:
109
- standardPrettierOptions = {
110
- parser: 'babel',
111
- singleQuote: true,
112
- plugins: [prettierPluginEstree, parserBabel]
113
- }; // If `prettierConfig` is *explicitly* true (as opposed to truthy), it means the user has opted in
114
- // to default behavior either explicitly or implicitly. Either way, we run basic Prettier on it.
115
- if (!(prettierConfig === true)) {
116
- _context.n = 3;
117
- break;
118
- }
119
- return _context.a(2, prettier.format(fixed, standardPrettierOptions));
120
- case 3:
121
- // If we've made it here, the user has passed their own Prettier options so we merge it with ours
122
- // and let theirs overwrite any of the default settings.
123
- mergedPrettierOptions = _extends(_extends(_extends({}, standardPrettierOptions), prettierConfig), {}, {
124
- plugins: standardPrettierOptions.plugins
125
- });
126
- return _context.a(2, prettier.format(fixed, mergedPrettierOptions));
27
+ // When removing TS-specific constructs (e.g. interfaces), we want to make sure we also remove
28
+ // any comments that are associated with those constructs, since otherwise we'll be left with
29
+ // comments that refer to something that isn't actually there.
30
+ // Credit to https://github.com/cyco130/detype for figuring out this very useful pattern
31
+ const removeComments = {
32
+ enter(nodePath) {
33
+ if (!nodePath.node.leadingComments) {
34
+ return;
35
+ }
36
+ for (let i = nodePath.node.leadingComments.length - 1; i >= 0; i -= 1) {
37
+ const comment = nodePath.node.leadingComments[i];
38
+ if (code.slice(comment.end).match(/^\s*\n\s*\n/) || comment.value.includes('___NEWLINE___')) {
39
+ // There is at least one empty line between the comment and the TypeScript specific construct
40
+ // We should keep this comment and those before it
41
+ break;
42
+ }
43
+ comment.value = '___REMOVE_ME___';
127
44
  }
128
- }, _callee);
129
- }));
130
- return _removeTypes.apply(this, arguments);
45
+ }
46
+ };
47
+ const isTSX = /\.tsx$/i.test(filename);
48
+ const transformed = Babel.transform(code, {
49
+ filename,
50
+ plugins: [{
51
+ name: 'comment-remover',
52
+ visitor: {
53
+ TSTypeAliasDeclaration: removeComments,
54
+ TSInterfaceDeclaration: removeComments,
55
+ TSDeclareFunction: removeComments,
56
+ TSDeclareMethod: removeComments,
57
+ TSImportType: removeComments,
58
+ TSModuleDeclaration: removeComments
59
+ }
60
+ }, ['transform-typescript', {
61
+ onlyRemoveTypeImports: true,
62
+ isTSX,
63
+ allExtensions: true
64
+ }], ['proposal-decorators', {
65
+ legacy: true
66
+ }]],
67
+ generatorOpts: {
68
+ retainLines: true,
69
+ shouldPrintComment: c => c !== '___REMOVE_ME___'
70
+ }
71
+ });
72
+ if (!transformed || !transformed.code) {
73
+ throw new Error('There was an issue with the Babel transform.');
74
+ }
75
+ const fixed = transformed.code.replace(/\/\* ___NEWLINE___ \*\//g, '\n');
76
+
77
+ // If the user has *explicitly* passed `false` here, it means they do not want us to run Prettier
78
+ // at all, so we bail here.
79
+ if (prettierConfig === false) {
80
+ return fixed;
81
+ }
82
+ const standardPrettierOptions = {
83
+ parser: 'babel',
84
+ singleQuote: true,
85
+ plugins: [pluginBabel, pluginEstree]
86
+ };
87
+
88
+ // If `prettierConfig` is *explicitly* true (as opposed to truthy), it means the user has opted in
89
+ // to default behavior either explicitly or implicitly. Either way, we run basic Prettier on it.
90
+ if (prettierConfig === true) {
91
+ return format(fixed, standardPrettierOptions);
92
+ }
93
+
94
+ // If we've made it here, the user has passed their own Prettier options so we merge it with ours
95
+ // and let theirs overwrite any of the default settings.
96
+ const mergedPrettierOptions = {
97
+ ...standardPrettierOptions,
98
+ ...prettierConfig,
99
+ plugins: standardPrettierOptions.plugins
100
+ };
101
+ return format(fixed, mergedPrettierOptions);
131
102
  }
@@ -1,31 +1,15 @@
1
- import _regenerator from "@babel/runtime/helpers/esm/regenerator";
2
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
1
  import { removeTypes } from "./removeTypes.js";
4
- export var transformTypescriptToJavascript = /*#__PURE__*/function () {
5
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(source, fileName) {
6
- var transformed, transformedFileName;
7
- return _regenerator().w(function (_context) {
8
- while (1) switch (_context.n) {
9
- case 0:
10
- _context.n = 1;
11
- return removeTypes(source, fileName);
12
- case 1:
13
- transformed = _context.v;
14
- transformedFileName = fileName.replace(/\.ts$/, '.js').replace(/\.tsx$/, '.jsx');
15
- return _context.a(2, {
16
- js: {
17
- source: transformed,
18
- fileName: transformedFileName
19
- }
20
- });
21
- }
22
- }, _callee);
23
- }));
24
- return function transformTypescriptToJavascript(_x, _x2) {
25
- return _ref.apply(this, arguments);
2
+ export const transformTypescriptToJavascript = async (source, fileName) => {
3
+ const transformed = await removeTypes(source, fileName);
4
+ const transformedFileName = fileName.replace(/\.ts$/, '.js').replace(/\.tsx$/, '.jsx');
5
+ return {
6
+ js: {
7
+ source: transformed,
8
+ fileName: transformedFileName
9
+ }
26
10
  };
27
- }();
28
- export var TypescriptToJavascriptTransformer = {
11
+ };
12
+ export const TypescriptToJavascriptTransformer = {
29
13
  extensions: ['ts', 'tsx'],
30
14
  transformer: transformTypescriptToJavascript
31
15
  };
@@ -3,12 +3,14 @@ import type { VariantSource } from "../CodeHighlighter/types.js";
3
3
  export declare function Pre({
4
4
  children,
5
5
  className,
6
+ language,
6
7
  ref,
7
8
  shouldHighlight,
8
9
  hydrateMargin
9
10
  }: {
10
11
  children: VariantSource;
11
12
  className?: string;
13
+ language?: string;
12
14
  ref?: React.Ref<HTMLPreElement>;
13
15
  shouldHighlight?: boolean;
14
16
  hydrateMargin?: string;
@@ -1,19 +1,16 @@
1
1
  'use client';
2
2
 
3
- import _extends from "@babel/runtime/helpers/esm/extends";
4
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
6
3
  import * as React from 'react';
7
4
  import { toText } from 'hast-util-to-text';
8
5
  import { decompressSync, strFromU8 } from 'fflate';
9
6
  import { decode } from 'uint8-to-base64';
10
7
  import { hastToJsx } from "../pipeline/hastUtils/index.js";
11
8
  import { jsx as _jsx } from "react/jsx-runtime";
12
- var hastChildrenCache = new WeakMap();
13
- var textChildrenCache = new WeakMap();
9
+ const hastChildrenCache = new WeakMap();
10
+ const textChildrenCache = new WeakMap();
14
11
  function renderCode(hastChildren, renderHast, text) {
15
12
  if (renderHast) {
16
- var jsx = hastChildrenCache.get(hastChildren);
13
+ let jsx = hastChildrenCache.get(hastChildren);
17
14
  if (!jsx) {
18
15
  jsx = hastToJsx({
19
16
  type: 'root',
@@ -26,7 +23,7 @@ function renderCode(hastChildren, renderHast, text) {
26
23
  if (text !== undefined) {
27
24
  return text;
28
25
  }
29
- var txt = textChildrenCache.get(hastChildren);
26
+ let txt = textChildrenCache.get(hastChildren);
30
27
  if (!txt) {
31
28
  txt = toText({
32
29
  type: 'root',
@@ -38,14 +35,15 @@ function renderCode(hastChildren, renderHast, text) {
38
35
  }
39
36
  return txt;
40
37
  }
41
- export function Pre(_ref) {
42
- var children = _ref.children,
43
- className = _ref.className,
44
- ref = _ref.ref,
45
- shouldHighlight = _ref.shouldHighlight,
46
- _ref$hydrateMargin = _ref.hydrateMargin,
47
- hydrateMargin = _ref$hydrateMargin === void 0 ? '200px 0px 200px 0px' : _ref$hydrateMargin;
48
- var hast = React.useMemo(function () {
38
+ export function Pre({
39
+ children,
40
+ className,
41
+ language,
42
+ ref,
43
+ shouldHighlight,
44
+ hydrateMargin = '200px 0px 200px 0px'
45
+ }) {
46
+ const hast = React.useMemo(() => {
49
47
  if (typeof children === 'string') {
50
48
  return null;
51
49
  }
@@ -57,12 +55,11 @@ export function Pre(_ref) {
57
55
  }
58
56
  return children;
59
57
  }, [children]);
60
- var _React$useState = React.useState(_defineProperty({}, 0, true)),
61
- _React$useState2 = _slicedToArray(_React$useState, 2),
62
- visibleFrames = _React$useState2[0],
63
- setVisibleFrames = _React$useState2[1];
64
- var observer = React.useRef(null);
65
- var bindIntersectionObserver = React.useCallback(function (root) {
58
+ const [visibleFrames, setVisibleFrames] = React.useState({
59
+ [0]: true
60
+ });
61
+ const observer = React.useRef(null);
62
+ const bindIntersectionObserver = React.useCallback(root => {
66
63
  if (!root) {
67
64
  if (observer.current) {
68
65
  observer.current.disconnect();
@@ -70,52 +67,53 @@ export function Pre(_ref) {
70
67
  observer.current = null;
71
68
  return;
72
69
  }
73
- observer.current = new IntersectionObserver(function (entries) {
74
- return setVisibleFrames(function (prev) {
75
- var visible = [];
76
- var invisible = [];
77
- entries.forEach(function (entry) {
78
- if (entry.isIntersecting) {
79
- visible.push(Number(entry.target.getAttribute('data-frame')));
80
- } else {
81
- invisible.push(Number(entry.target.getAttribute('data-frame')));
82
- }
83
- });
70
+ observer.current = new IntersectionObserver(entries => setVisibleFrames(prev => {
71
+ const visible = [];
72
+ const invisible = [];
73
+ entries.forEach(entry => {
74
+ if (entry.isIntersecting) {
75
+ visible.push(Number(entry.target.getAttribute('data-frame')));
76
+ } else {
77
+ invisible.push(Number(entry.target.getAttribute('data-frame')));
78
+ }
79
+ });
84
80
 
85
- // avoid mutating the object if nothing changed
86
- var frames;
87
- visible.forEach(function (frame) {
88
- if (prev[frame] !== true) {
89
- if (!frames) {
90
- frames = _extends({}, prev);
91
- }
92
- frames[frame] = true;
81
+ // avoid mutating the object if nothing changed
82
+ let frames;
83
+ visible.forEach(frame => {
84
+ if (prev[frame] !== true) {
85
+ if (!frames) {
86
+ frames = {
87
+ ...prev
88
+ };
93
89
  }
94
- });
95
- invisible.forEach(function (frame) {
96
- if (prev[frame]) {
97
- if (!frames) {
98
- frames = _extends({}, prev);
99
- }
100
- delete frames[frame];
90
+ frames[frame] = true;
91
+ }
92
+ });
93
+ invisible.forEach(frame => {
94
+ if (prev[frame]) {
95
+ if (!frames) {
96
+ frames = {
97
+ ...prev
98
+ };
101
99
  }
102
- });
103
- return frames || prev;
100
+ delete frames[frame];
101
+ }
104
102
  });
105
- }, {
103
+ return frames || prev;
104
+ }), {
106
105
  rootMargin: hydrateMargin
107
106
  });
108
107
 
109
108
  // <pre><code><span class="frame" data-frame="0">...</span><span class="frame" data-frame="1">...</span>...</code></pre>
110
- root.childNodes[0].childNodes.forEach(function (node) {
109
+ root.childNodes[0].childNodes.forEach(node => {
111
110
  if (node.nodeType === Node.ELEMENT_NODE) {
112
- var _observer$current;
113
- var element = node;
111
+ const element = node;
114
112
  if (!element.hasAttribute('data-frame')) {
115
113
  console.warn('Expected frame element in useCode <Pre>', element);
116
114
  return;
117
115
  }
118
- (_observer$current = observer.current) == null || _observer$current.observe(element);
116
+ observer.current?.observe(element);
119
117
  }
120
118
  });
121
119
  if (ref) {
@@ -126,13 +124,13 @@ export function Pre(_ref) {
126
124
  }
127
125
  }
128
126
  }, [ref, hydrateMargin]);
129
- var observeFrame = React.useCallback(function (node) {
127
+ const observeFrame = React.useCallback(node => {
130
128
  if (observer.current && node) {
131
129
  observer.current.observe(node);
132
130
  }
133
131
  }, []);
134
- var frames = React.useMemo(function () {
135
- return hast == null ? void 0 : hast.children.map(function (child, index) {
132
+ const frames = React.useMemo(() => {
133
+ return hast?.children.map((child, index) => {
136
134
  if (child.type !== 'element') {
137
135
  if (child.type === 'text') {
138
136
  return /*#__PURE__*/_jsx(React.Fragment, {
@@ -142,13 +140,12 @@ export function Pre(_ref) {
142
140
  return null;
143
141
  }
144
142
  if (child.properties.className === 'frame') {
145
- var _child$properties, _child$properties2;
146
- var isVisible = Boolean(visibleFrames[index]);
143
+ const isVisible = Boolean(visibleFrames[index]);
147
144
  return /*#__PURE__*/_jsx("span", {
148
145
  className: "frame",
149
146
  "data-frame": index,
150
147
  ref: observeFrame,
151
- children: renderCode(child.children, shouldHighlight && isVisible, (_child$properties = child.properties) != null && _child$properties.dataAsString ? String((_child$properties2 = child.properties) == null ? void 0 : _child$properties2.dataAsString) : undefined)
148
+ children: renderCode(child.children, shouldHighlight && isVisible, child.properties?.dataAsString ? String(child.properties?.dataAsString) : undefined)
152
149
  }, index);
153
150
  }
154
151
  return /*#__PURE__*/_jsx(React.Fragment, {
@@ -162,6 +159,7 @@ export function Pre(_ref) {
162
159
  ref: bindIntersectionObserver,
163
160
  className: className,
164
161
  children: /*#__PURE__*/_jsx("code", {
162
+ className: language ? `language-${language}` : undefined,
165
163
  children: typeof children === 'string' ? children : frames
166
164
  })
167
165
  });