@mui/internal-docs-infra 0.1.1-canary.9 → 0.2.0-alpha.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.
Files changed (217) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +3 -11
  3. package/esm/CodeControllerContext/CodeControllerContext.d.ts +62 -0
  4. package/esm/CodeControllerContext/CodeControllerContext.js +33 -0
  5. package/esm/CodeControllerContext/index.d.ts +1 -0
  6. package/esm/CodeControllerContext/index.js +1 -0
  7. package/esm/CodeExternalsContext/CodeExternalsContext.d.ts +8 -0
  8. package/esm/CodeExternalsContext/CodeExternalsContext.js +8 -0
  9. package/esm/CodeExternalsContext/index.d.ts +1 -0
  10. package/esm/CodeExternalsContext/index.js +1 -0
  11. package/esm/CodeHighlighter/CodeHighlighter.d.ts +2 -0
  12. package/esm/CodeHighlighter/CodeHighlighter.js +441 -0
  13. package/esm/CodeHighlighter/CodeHighlighterClient.d.ts +2 -0
  14. package/esm/CodeHighlighter/CodeHighlighterClient.js +984 -0
  15. package/esm/CodeHighlighter/CodeHighlighterContext.d.ts +16 -0
  16. package/esm/CodeHighlighter/CodeHighlighterContext.js +15 -0
  17. package/esm/CodeHighlighter/CodeHighlighterFallbackContext.d.ts +7 -0
  18. package/esm/CodeHighlighter/CodeHighlighterFallbackContext.js +14 -0
  19. package/esm/CodeHighlighter/addPathsToVariant.d.ts +14 -0
  20. package/esm/CodeHighlighter/addPathsToVariant.js +68 -0
  21. package/esm/CodeHighlighter/applyTransform.d.ts +19 -0
  22. package/esm/CodeHighlighter/applyTransform.js +75 -0
  23. package/esm/CodeHighlighter/calculateMainFilePath.d.ts +1 -0
  24. package/esm/CodeHighlighter/calculateMainFilePath.js +108 -0
  25. package/esm/CodeHighlighter/codeToFallbackProps.d.ts +2 -0
  26. package/esm/CodeHighlighter/codeToFallbackProps.js +73 -0
  27. package/esm/CodeHighlighter/errors.d.ts +141 -0
  28. package/esm/CodeHighlighter/errors.js +441 -0
  29. package/esm/CodeHighlighter/examineVariant.d.ts +25 -0
  30. package/esm/CodeHighlighter/examineVariant.js +73 -0
  31. package/esm/CodeHighlighter/hasAllVariants.d.ts +27 -0
  32. package/esm/CodeHighlighter/hasAllVariants.js +63 -0
  33. package/esm/CodeHighlighter/index.d.ts +1 -0
  34. package/esm/CodeHighlighter/index.js +1 -0
  35. package/esm/CodeHighlighter/loadFallbackCode.d.ts +10 -0
  36. package/esm/CodeHighlighter/loadFallbackCode.js +679 -0
  37. package/esm/CodeHighlighter/loadVariant.d.ts +12 -0
  38. package/esm/CodeHighlighter/loadVariant.js +1044 -0
  39. package/esm/CodeHighlighter/maybeInitialData.d.ts +108 -0
  40. package/esm/CodeHighlighter/maybeInitialData.js +192 -0
  41. package/esm/CodeHighlighter/mergeMetadata.d.ts +40 -0
  42. package/esm/CodeHighlighter/mergeMetadata.js +165 -0
  43. package/esm/CodeHighlighter/parseCode.d.ts +6 -0
  44. package/esm/CodeHighlighter/parseCode.js +134 -0
  45. package/esm/CodeHighlighter/parseControlledCode.d.ts +6 -0
  46. package/esm/CodeHighlighter/parseControlledCode.js +87 -0
  47. package/esm/CodeHighlighter/pathUtils.d.ts +120 -0
  48. package/esm/CodeHighlighter/pathUtils.js +258 -0
  49. package/esm/CodeHighlighter/transformCode.d.ts +21 -0
  50. package/esm/CodeHighlighter/transformCode.js +251 -0
  51. package/esm/CodeHighlighter/transformParsedSource.d.ts +3 -0
  52. package/esm/CodeHighlighter/transformParsedSource.js +60 -0
  53. package/esm/CodeHighlighter/transformSource.d.ts +2 -0
  54. package/esm/CodeHighlighter/transformSource.js +103 -0
  55. package/esm/CodeHighlighter/types.d.ts +276 -0
  56. package/esm/CodeHighlighter/types.js +1 -0
  57. package/esm/CodeProvider/CodeContext.d.ts +31 -0
  58. package/esm/CodeProvider/CodeContext.js +12 -0
  59. package/esm/CodeProvider/CodeProvider.d.ts +13 -0
  60. package/esm/CodeProvider/CodeProvider.js +83 -0
  61. package/esm/CodeProvider/index.d.ts +1 -0
  62. package/esm/CodeProvider/index.js +1 -0
  63. package/esm/abstractCreateDemo/abstractCreateDemo.d.ts +45 -0
  64. package/esm/abstractCreateDemo/abstractCreateDemo.js +100 -0
  65. package/esm/abstractCreateDemo/index.d.ts +1 -0
  66. package/esm/abstractCreateDemo/index.js +1 -0
  67. package/esm/abstractCreateDemoClient/abstractCreateDemoClient.d.ts +32 -0
  68. package/esm/abstractCreateDemoClient/abstractCreateDemoClient.js +52 -0
  69. package/esm/abstractCreateDemoClient/index.d.ts +1 -0
  70. package/esm/abstractCreateDemoClient/index.js +1 -0
  71. package/esm/createDemoData/createDemoData.d.ts +44 -0
  72. package/esm/createDemoData/createDemoData.js +74 -0
  73. package/esm/createDemoData/index.d.ts +1 -0
  74. package/esm/createDemoData/index.js +1 -0
  75. package/esm/createDemoData/types.d.ts +25 -0
  76. package/esm/createDemoData/types.js +1 -0
  77. package/esm/pipeline/hastUtils/hastUtils.d.ts +11 -0
  78. package/esm/pipeline/hastUtils/hastUtils.js +67 -0
  79. package/esm/pipeline/hastUtils/index.d.ts +1 -0
  80. package/esm/pipeline/hastUtils/index.js +1 -0
  81. package/esm/pipeline/loadPrecomputedCodeHighlighter/index.d.ts +2 -0
  82. package/esm/pipeline/loadPrecomputedCodeHighlighter/index.js +4 -0
  83. package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.d.ts +14 -0
  84. package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.js +178 -0
  85. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.d.ts +47 -0
  86. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.js +849 -0
  87. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.d.ts +85 -0
  88. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.js +715 -0
  89. package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.d.ts +14 -0
  90. package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.js +73 -0
  91. package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.d.ts +11 -0
  92. package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.js +203 -0
  93. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.d.ts +6 -0
  94. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.js +23 -0
  95. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.d.ts +6 -0
  96. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.js +148 -0
  97. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.d.ts +9 -0
  98. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.js +198 -0
  99. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/index.d.ts +2 -0
  100. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/index.js +4 -0
  101. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.d.ts +5 -0
  102. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.js +21 -0
  103. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.d.ts +15 -0
  104. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.js +233 -0
  105. package/esm/pipeline/loadServerCodeMeta/index.d.ts +1 -0
  106. package/esm/pipeline/loadServerCodeMeta/index.js +1 -0
  107. package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.d.ts +24 -0
  108. package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.js +90 -0
  109. package/esm/pipeline/loadServerSource/index.d.ts +1 -0
  110. package/esm/pipeline/loadServerSource/index.js +1 -0
  111. package/esm/pipeline/loadServerSource/loadServerSource.d.ts +25 -0
  112. package/esm/pipeline/loadServerSource/loadServerSource.js +134 -0
  113. package/esm/pipeline/loaderUtils/externalsToPackages.d.ts +1 -0
  114. package/esm/pipeline/loaderUtils/externalsToPackages.js +46 -0
  115. package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.d.ts +34 -0
  116. package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.js +161 -0
  117. package/esm/pipeline/loaderUtils/getFileNameFromUrl.d.ts +12 -0
  118. package/esm/pipeline/loaderUtils/getFileNameFromUrl.js +66 -0
  119. package/esm/pipeline/loaderUtils/index.d.ts +7 -0
  120. package/esm/pipeline/loaderUtils/index.js +7 -0
  121. package/esm/pipeline/loaderUtils/mergeExternals.d.ts +32 -0
  122. package/esm/pipeline/loaderUtils/mergeExternals.js +72 -0
  123. package/esm/pipeline/loaderUtils/parseImports.d.ts +19 -0
  124. package/esm/pipeline/loaderUtils/parseImports.js +306 -0
  125. package/esm/pipeline/loaderUtils/processRelativeImports.d.ts +19 -0
  126. package/esm/pipeline/loaderUtils/processRelativeImports.js +352 -0
  127. package/esm/pipeline/loaderUtils/resolveModulePath.d.ts +87 -0
  128. package/esm/pipeline/loaderUtils/resolveModulePath.js +1435 -0
  129. package/esm/pipeline/loaderUtils/resolveModulePathWithFs.d.ts +47 -0
  130. package/esm/pipeline/loaderUtils/resolveModulePathWithFs.js +150 -0
  131. package/esm/pipeline/loaderUtils/rewriteImports.d.ts +9 -0
  132. package/esm/pipeline/loaderUtils/rewriteImports.js +35 -0
  133. package/esm/pipeline/parseSource/addLineGutters.d.ts +9 -0
  134. package/esm/pipeline/parseSource/addLineGutters.js +181 -0
  135. package/esm/pipeline/parseSource/grammars.d.ts +2 -0
  136. package/esm/pipeline/parseSource/grammars.js +27 -0
  137. package/esm/pipeline/parseSource/index.d.ts +1 -0
  138. package/esm/pipeline/parseSource/index.js +1 -0
  139. package/esm/pipeline/parseSource/parseSource.d.ts +3 -0
  140. package/esm/pipeline/parseSource/parseSource.js +51 -0
  141. package/esm/pipeline/transformHtmlCode/index.d.ts +2 -0
  142. package/esm/pipeline/transformHtmlCode/index.js +4 -0
  143. package/esm/pipeline/transformHtmlCode/transformHtmlCode.d.ts +13 -0
  144. package/esm/pipeline/transformHtmlCode/transformHtmlCode.js +300 -0
  145. package/esm/pipeline/transformMarkdownCode/index.d.ts +2 -0
  146. package/esm/pipeline/transformMarkdownCode/index.js +4 -0
  147. package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.d.ts +2 -0
  148. package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.js +514 -0
  149. package/esm/pipeline/transformTypescriptToJavascript/index.d.ts +1 -0
  150. package/esm/pipeline/transformTypescriptToJavascript/index.js +1 -0
  151. package/esm/pipeline/transformTypescriptToJavascript/removeTypes.d.ts +13 -0
  152. package/esm/pipeline/transformTypescriptToJavascript/removeTypes.js +131 -0
  153. package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.d.ts +3 -0
  154. package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.js +31 -0
  155. package/esm/useCode/Pre.d.ts +15 -0
  156. package/esm/useCode/Pre.js +164 -0
  157. package/esm/useCode/index.d.ts +1 -0
  158. package/esm/useCode/index.js +1 -0
  159. package/esm/useCode/useCode.d.ts +41 -0
  160. package/esm/useCode/useCode.js +128 -0
  161. package/esm/useCode/useCodeUtils.d.ts +44 -0
  162. package/esm/useCode/useCodeUtils.js +245 -0
  163. package/esm/useCode/useCopyFunctionality.d.ts +18 -0
  164. package/esm/useCode/useCopyFunctionality.js +28 -0
  165. package/esm/useCode/useFileNavigation.d.ts +41 -0
  166. package/esm/useCode/useFileNavigation.js +453 -0
  167. package/esm/useCode/useSourceEditing.d.ts +21 -0
  168. package/esm/useCode/useSourceEditing.js +32 -0
  169. package/esm/useCode/useTransformManagement.d.ts +30 -0
  170. package/esm/useCode/useTransformManagement.js +72 -0
  171. package/esm/useCode/useUIState.d.ts +16 -0
  172. package/esm/useCode/useUIState.js +21 -0
  173. package/esm/useCode/useVariantSelection.d.ts +23 -0
  174. package/esm/useCode/useVariantSelection.js +75 -0
  175. package/esm/useCopier/index.d.ts +1 -1
  176. package/esm/useCopier/index.js +5 -5
  177. package/esm/useDemo/createCodeSandbox.d.ts +15 -0
  178. package/esm/useDemo/createCodeSandbox.js +42 -0
  179. package/esm/useDemo/createStackBlitz.d.ts +22 -0
  180. package/esm/useDemo/createStackBlitz.js +38 -0
  181. package/esm/useDemo/exportVariant.d.ts +184 -0
  182. package/esm/useDemo/exportVariant.js +422 -0
  183. package/esm/useDemo/exportVariantAsCra.d.ts +15 -0
  184. package/esm/useDemo/exportVariantAsCra.js +57 -0
  185. package/esm/useDemo/flattenVariant.d.ts +19 -0
  186. package/esm/useDemo/flattenVariant.js +49 -0
  187. package/esm/useDemo/index.d.ts +6 -51
  188. package/esm/useDemo/index.js +6 -104
  189. package/esm/useDemo/useDemo.d.ts +81 -0
  190. package/esm/useDemo/useDemo.js +193 -0
  191. package/esm/useErrors/ErrorsContext.d.ts +6 -0
  192. package/esm/useErrors/ErrorsContext.js +8 -0
  193. package/esm/useErrors/index.d.ts +1 -0
  194. package/esm/useErrors/index.js +1 -0
  195. package/esm/useErrors/useErrors.d.ts +5 -0
  196. package/esm/useErrors/useErrors.js +7 -0
  197. package/esm/useLocalStorageState/index.d.ts +2 -0
  198. package/esm/useLocalStorageState/index.js +2 -0
  199. package/esm/useLocalStorageState/useLocalStorageState.d.ts +14 -0
  200. package/esm/useLocalStorageState/useLocalStorageState.js +128 -0
  201. package/esm/usePreference/PreferencesProvider.d.ts +6 -0
  202. package/esm/usePreference/PreferencesProvider.js +8 -0
  203. package/esm/usePreference/index.d.ts +2 -0
  204. package/esm/usePreference/index.js +2 -0
  205. package/esm/usePreference/usePreference.d.ts +2 -0
  206. package/esm/usePreference/usePreference.js +25 -0
  207. package/esm/useUrlHashState/index.d.ts +1 -0
  208. package/esm/useUrlHashState/index.js +1 -0
  209. package/esm/useUrlHashState/useUrlHashState.d.ts +5 -0
  210. package/esm/useUrlHashState/useUrlHashState.js +68 -0
  211. package/esm/withDocsInfra/index.d.ts +1 -0
  212. package/esm/withDocsInfra/index.js +1 -0
  213. package/esm/withDocsInfra/withDocsInfra.d.ts +82 -0
  214. package/esm/withDocsInfra/withDocsInfra.js +147 -0
  215. package/package.json +178 -11
  216. package/esm/useCopier/index.d.ts.map +0 -1
  217. package/esm/useDemo/index.d.ts.map +0 -1
@@ -0,0 +1,514 @@
1
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
+ import { visit } from 'unist-util-visit';
4
+ /**
5
+ * Remark plugin that transforms code blocks with variants into HTML pre/code structures.
6
+ *
7
+ * Transforms consecutive code blocks with variant metadata like:
8
+ *
9
+ * ```javascript variant=npm
10
+ * npm install @mui/internal-docs-infra
11
+ * ```
12
+ * ```javascript variant=pnpm
13
+ * pnpm install @mui/internal-docs-infra
14
+ * ```
15
+ * ```javascript variant=yarn
16
+ * yarn add @mui/internal-docs-infra
17
+ * ```
18
+ *
19
+ * OR with variant-type (with labels between):
20
+ *
21
+ * npm
22
+ * ```bash variant-type=install
23
+ * npm install @mui/internal-docs-infra
24
+ * ```
25
+ * pnpm
26
+ * ```bash variant-type=install
27
+ * pnpm install @mui/internal-docs-infra
28
+ * ```
29
+ * yarn
30
+ * ```bash variant-type=install
31
+ * yarn add @mui/internal-docs-infra
32
+ * ```
33
+ *
34
+ * OR individual code blocks with options:
35
+ *
36
+ * ```ts transform
37
+ * console.log('test' as const)
38
+ * ```
39
+ *
40
+ * Into HTML that the existing rehype plugin can process:
41
+ * <pre>
42
+ * <code data-variant="npm">npm install @mui/internal-docs-infra</code>
43
+ * <code data-variant="pnpm">pnpm install @mui/internal-docs-infra</code>
44
+ * <code data-variant="yarn">yarn add @mui/internal-docs-infra</code>
45
+ * </pre>
46
+ *
47
+ * Or for individual blocks:
48
+ * <pre>
49
+ * <code data-transform="true">console.log('test' as const)</code>
50
+ * </pre>
51
+ */
52
+
53
+ /**
54
+ * Parse meta string to extract variant and other properties
55
+ */
56
+ function parseMeta(meta) {
57
+ var result = {
58
+ props: {}
59
+ };
60
+
61
+ // Parse key=value pairs first, handling quoted values
62
+ var keyValueRegex = /([\w-]+)=("(?:[^"\\]|\\.)*"|[^\s]+)/g;
63
+ var match = keyValueRegex.exec(meta);
64
+ var processedPositions = [];
65
+ while (match !== null) {
66
+ var _match = match,
67
+ _match2 = _slicedToArray(_match, 3),
68
+ fullMatch = _match2[0],
69
+ key = _match2[1],
70
+ rawValue = _match2[2];
71
+ var startPos = match.index;
72
+ var endPos = match.index + fullMatch.length;
73
+ processedPositions.push([startPos, endPos]);
74
+
75
+ // Remove quotes if present
76
+ var value = rawValue.startsWith('"') && rawValue.endsWith('"') ? rawValue.slice(1, -1) : rawValue;
77
+ if (key === 'variant') {
78
+ result.variant = value;
79
+ } else if (key === 'variant-type') {
80
+ result.variantType = value;
81
+ } else {
82
+ result.props[key] = value;
83
+ }
84
+ match = keyValueRegex.exec(meta);
85
+ }
86
+
87
+ // Extract remaining parts as standalone flags
88
+ var remainingMeta = meta;
89
+ // Remove processed key=value pairs from the string (in reverse order to maintain positions)
90
+ processedPositions.sort(function (a, b) {
91
+ return b[0] - a[0];
92
+ }).forEach(function (_ref) {
93
+ var _ref2 = _slicedToArray(_ref, 2),
94
+ start = _ref2[0],
95
+ end = _ref2[1];
96
+ remainingMeta = remainingMeta.slice(0, start) + remainingMeta.slice(end);
97
+ });
98
+
99
+ // Process remaining standalone flags
100
+ var remainingParts = remainingMeta.trim().split(/\s+/).filter(Boolean);
101
+ var _iterator = _createForOfIteratorHelper(remainingParts),
102
+ _step;
103
+ try {
104
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
105
+ var part = _step.value;
106
+ if (part === 'variant') {
107
+ // This shouldn't happen, but just in case
108
+ result.variant = 'true';
109
+ } else if (part === 'variant-type') {
110
+ // This shouldn't happen, but just in case
111
+ result.variantType = 'true';
112
+ } else {
113
+ // Handle standalone flags (e.g., "transform" becomes "transform": "true")
114
+ result.props[part] = 'true';
115
+ }
116
+ }
117
+ } catch (err) {
118
+ _iterator.e(err);
119
+ } finally {
120
+ _iterator.f();
121
+ }
122
+ return result;
123
+ }
124
+ export var transformMarkdownCode = function transformMarkdownCode() {
125
+ return function (tree) {
126
+ var processedIndices = new Set();
127
+ visit(tree, function (node, index, parent) {
128
+ if (!parent || !Array.isArray(parent.children) || typeof index !== 'number') {
129
+ return;
130
+ }
131
+
132
+ // Skip if already processed
133
+ if (processedIndices.has(index)) {
134
+ return;
135
+ }
136
+ var parentNode = parent;
137
+
138
+ // Look for code blocks with variant metadata or options
139
+ if (node.type === 'code') {
140
+ var codeNode = node;
141
+
142
+ // Check if variant metadata is in meta field or lang field (when no language is specified)
143
+ var metaString = codeNode.meta;
144
+ var langFromMeta = codeNode.lang || null;
145
+
146
+ // If meta is empty but lang contains '=', it means variant info is in lang
147
+ if (!metaString && codeNode.lang && codeNode.lang.includes('=')) {
148
+ metaString = codeNode.lang;
149
+ langFromMeta = null;
150
+ }
151
+
152
+ // Check if we have variants/variant-types or individual options
153
+ var metaData = {
154
+ props: {}
155
+ };
156
+ if (metaString) {
157
+ metaData = parseMeta(metaString);
158
+ }
159
+
160
+ // Use props from meta as the options for individual blocks
161
+ var allProps = metaData.props;
162
+
163
+ // Handle individual code blocks with options (but no variants)
164
+ if (!metaData.variant && !metaData.variantType && Object.keys(allProps).length > 0) {
165
+ // Create a simple pre/code element for individual blocks with options
166
+ var hProperties = {};
167
+
168
+ // Add language class if available
169
+ if (langFromMeta) {
170
+ hProperties.className = "language-".concat(langFromMeta);
171
+ }
172
+
173
+ // Add all props as data attributes (in camelCase)
174
+ Object.entries(allProps).forEach(function (_ref3) {
175
+ var _ref4 = _slicedToArray(_ref3, 2),
176
+ key = _ref4[0],
177
+ value = _ref4[1];
178
+ // Convert kebab-case to camelCase for data attributes
179
+ var camelKey = key.includes('-') ? "data".concat(key.split('-').map(function (part) {
180
+ return part.charAt(0).toUpperCase() + part.slice(1);
181
+ }).join('')) : "data".concat(key.charAt(0).toUpperCase() + key.slice(1));
182
+ hProperties[camelKey] = value;
183
+ });
184
+ var preElement = {
185
+ type: 'element',
186
+ tagName: 'pre',
187
+ data: {
188
+ hName: 'pre',
189
+ hProperties: {}
190
+ },
191
+ children: [{
192
+ type: 'element',
193
+ tagName: 'code',
194
+ data: {
195
+ hName: 'code',
196
+ hProperties: hProperties
197
+ },
198
+ children: [{
199
+ type: 'text',
200
+ value: codeNode.value
201
+ }]
202
+ }]
203
+ };
204
+
205
+ // Replace this individual code block immediately
206
+ parentNode.children[index] = preElement;
207
+ processedIndices.add(index);
208
+ return;
209
+ }
210
+
211
+ // Handle variant/variant-type logic (existing code)
212
+ if (!metaString) {
213
+ return;
214
+ }
215
+ if (metaData.variant || metaData.variantType) {
216
+ // Collect consecutive code blocks that belong together
217
+ var codeBlocks = [];
218
+ var currentIndex = index;
219
+
220
+ // For variant-type, look for pattern: [label] -> code block
221
+ // For variant, look for adjacent code blocks only
222
+
223
+ if (metaData.variantType) {
224
+ // Add the current code block as the first one for variant-type
225
+ var currentLabelFromPrevious;
226
+ if (index > 0) {
227
+ var prevNode = parentNode.children[index - 1];
228
+ if (prevNode.type === 'paragraph' && prevNode.children.length === 1 && prevNode.children[0].type === 'text') {
229
+ currentLabelFromPrevious = prevNode.children[0].value.trim();
230
+ }
231
+ }
232
+ codeBlocks.push({
233
+ node: codeNode,
234
+ index: index,
235
+ variant: currentLabelFromPrevious || metaData.variantType || 'default',
236
+ props: allProps,
237
+ actualLang: langFromMeta,
238
+ labelFromPrevious: currentLabelFromPrevious
239
+ });
240
+ processedIndices.add(index);
241
+
242
+ // Start looking from the next element
243
+ currentIndex = index + 1;
244
+
245
+ // Collect all blocks with the same variant-type
246
+ while (currentIndex < parentNode.children.length) {
247
+ var currentNode = parentNode.children[currentIndex];
248
+
249
+ // Check if this is a potential label paragraph
250
+ if (currentNode.type === 'paragraph' && currentNode.children.length === 1 && currentNode.children[0].type === 'text') {
251
+ // Look for a code block after this paragraph
252
+ if (currentIndex + 1 < parentNode.children.length) {
253
+ var nextNode = parentNode.children[currentIndex + 1];
254
+ if (nextNode.type === 'code') {
255
+ // Check if this code block has the same variant-type
256
+ var nextMetaString = nextNode.meta;
257
+ var nextActualLang = nextNode.lang;
258
+ if (!nextMetaString && nextActualLang && nextActualLang.includes('=')) {
259
+ nextMetaString = nextActualLang;
260
+ nextActualLang = null;
261
+ }
262
+ if (nextMetaString) {
263
+ var nextMetaData = parseMeta(nextMetaString);
264
+ if (nextMetaData.variantType === metaData.variantType) {
265
+ var labelFromPrevious = currentNode.children[0].value.trim();
266
+ codeBlocks.push({
267
+ node: nextNode,
268
+ index: currentIndex + 1,
269
+ variant: labelFromPrevious || nextMetaData.variantType || 'default',
270
+ props: nextMetaData.props,
271
+ actualLang: nextActualLang,
272
+ labelFromPrevious: labelFromPrevious
273
+ });
274
+ processedIndices.add(currentIndex + 1);
275
+
276
+ // Skip the code block and move to next potential label
277
+ currentIndex += 2;
278
+ continue;
279
+ }
280
+ }
281
+ }
282
+ }
283
+ // If we didn't find a matching code block, break
284
+ break;
285
+ }
286
+ if (currentNode.type === 'code') {
287
+ // Parse language and meta for current node
288
+ var currentMetaString = currentNode.meta;
289
+ var currentActualLang = currentNode.lang;
290
+ if (!currentMetaString && currentActualLang && currentActualLang.includes('=')) {
291
+ currentMetaString = currentActualLang;
292
+ currentActualLang = null;
293
+ }
294
+ if (currentMetaString) {
295
+ var currentMetaData = parseMeta(currentMetaString);
296
+ if (currentMetaData.variantType === metaData.variantType) {
297
+ // Look for label before this code block
298
+ var _labelFromPrevious = void 0;
299
+ if (currentIndex > 0) {
300
+ var _prevNode = parentNode.children[currentIndex - 1];
301
+ if (_prevNode.type === 'paragraph' && _prevNode.children.length === 1 && _prevNode.children[0].type === 'text') {
302
+ _labelFromPrevious = _prevNode.children[0].value.trim();
303
+ }
304
+ }
305
+ codeBlocks.push({
306
+ node: currentNode,
307
+ index: currentIndex,
308
+ variant: _labelFromPrevious || currentMetaData.variantType || 'default',
309
+ props: currentMetaData.props,
310
+ actualLang: currentActualLang,
311
+ labelFromPrevious: _labelFromPrevious
312
+ });
313
+ processedIndices.add(currentIndex);
314
+
315
+ // Skip the label and move to next potential code block
316
+ currentIndex += 1;
317
+
318
+ // Skip ahead past any paragraph that could be a label
319
+ if (currentIndex < parentNode.children.length) {
320
+ var _nextNode = parentNode.children[currentIndex];
321
+ if (_nextNode.type === 'paragraph' && _nextNode.children.length === 1 && _nextNode.children[0].type === 'text') {
322
+ currentIndex += 1; // Skip the potential label
323
+ }
324
+ }
325
+ } else {
326
+ break; // Different group, stop collecting
327
+ }
328
+ } else {
329
+ break; // No meta, stop collecting
330
+ }
331
+ } else {
332
+ break; // Not a code block, stop collecting
333
+ }
334
+ }
335
+ } else if (metaData.variant) {
336
+ // Add the current code block as the first one for variant
337
+ codeBlocks.push({
338
+ node: codeNode,
339
+ index: index,
340
+ variant: metaData.variant,
341
+ props: allProps,
342
+ actualLang: langFromMeta
343
+ });
344
+ processedIndices.add(index);
345
+
346
+ // Start looking from the next element
347
+ currentIndex = index + 1;
348
+
349
+ // Collect adjacent code blocks with variants
350
+ while (currentIndex < parentNode.children.length) {
351
+ var _currentNode = parentNode.children[currentIndex];
352
+ if (_currentNode.type === 'code') {
353
+ // Parse language and meta for current node
354
+ var _currentMetaString = _currentNode.meta;
355
+ var _currentActualLang = _currentNode.lang;
356
+ if (!_currentMetaString && _currentActualLang && _currentActualLang.includes('=')) {
357
+ _currentMetaString = _currentActualLang;
358
+ _currentActualLang = null;
359
+ }
360
+ if (_currentMetaString) {
361
+ var _currentMetaData = parseMeta(_currentMetaString);
362
+ if (_currentMetaData.variant) {
363
+ codeBlocks.push({
364
+ node: _currentNode,
365
+ index: currentIndex,
366
+ variant: _currentMetaData.variant,
367
+ props: _currentMetaData.props,
368
+ actualLang: _currentActualLang
369
+ });
370
+ processedIndices.add(currentIndex);
371
+ currentIndex += 1;
372
+ } else {
373
+ break; // No variant, stop collecting
374
+ }
375
+ } else {
376
+ break; // No meta, stop collecting
377
+ }
378
+ } else {
379
+ break; // Not a code block, stop collecting
380
+ }
381
+ }
382
+ }
383
+
384
+ // Only process if we have multiple blocks
385
+ if (codeBlocks.length > 1) {
386
+ // Create proper HTML elements with hProperties for remark-rehype compatibility
387
+ var _preElement = {
388
+ type: 'element',
389
+ tagName: 'pre',
390
+ data: {
391
+ hName: 'pre',
392
+ hProperties: {}
393
+ },
394
+ children: codeBlocks.map(function (block) {
395
+ // Build hProperties for HTML attributes
396
+ var hProperties = {
397
+ dataVariant: block.variant
398
+ };
399
+
400
+ // Add language class if available
401
+ if (block.actualLang) {
402
+ hProperties.className = "language-".concat(block.actualLang);
403
+ }
404
+
405
+ // Add additional props as data attributes (in camelCase)
406
+ Object.entries(block.props).forEach(function (_ref5) {
407
+ var _ref6 = _slicedToArray(_ref5, 2),
408
+ key = _ref6[0],
409
+ value = _ref6[1];
410
+ // Convert kebab-case to camelCase for data attributes
411
+ var camelKey = key.includes('-') ? "data".concat(key.split('-').map(function (part) {
412
+ return part.charAt(0).toUpperCase() + part.slice(1);
413
+ }).join('')) : "data".concat(key.charAt(0).toUpperCase() + key.slice(1));
414
+ hProperties[camelKey] = value;
415
+ });
416
+ return {
417
+ type: 'element',
418
+ tagName: 'code',
419
+ data: {
420
+ hName: 'code',
421
+ hProperties: hProperties,
422
+ meta: "variant=".concat(block.variant).concat(Object.entries(block.props).map(function (_ref7) {
423
+ var _ref8 = _slicedToArray(_ref7, 2),
424
+ key = _ref8[0],
425
+ value = _ref8[1];
426
+ return " ".concat(key, "=").concat(value);
427
+ }).join(''))
428
+ },
429
+ children: [{
430
+ type: 'text',
431
+ value: block.node.value
432
+ }]
433
+ };
434
+ })
435
+ };
436
+
437
+ // Replace the first block with the group and mark others for removal
438
+ parentNode.children[codeBlocks[0].index] = _preElement;
439
+
440
+ // Remove all other code blocks and their labels (in reverse order to maintain indices)
441
+ var indicesToRemove = codeBlocks.slice(1).map(function (block) {
442
+ var indices = [block.index];
443
+ // Also include label paragraph if it exists
444
+ if (block.labelFromPrevious && block.index > 0) {
445
+ var _prevNode2 = parentNode.children[block.index - 1];
446
+ if (_prevNode2.type === 'paragraph') {
447
+ indices.push(block.index - 1);
448
+ }
449
+ }
450
+ return indices;
451
+ }).flat().sort(function (a, b) {
452
+ return b - a;
453
+ }); // Sort in descending order
454
+
455
+ // Remove the marked indices
456
+ var _iterator2 = _createForOfIteratorHelper(indicesToRemove),
457
+ _step2;
458
+ try {
459
+ var _loop = function _loop() {
460
+ var removeIdx = _step2.value;
461
+ if (removeIdx < parentNode.children.length) {
462
+ parentNode.children.splice(removeIdx, 1);
463
+ // Update processed indices to account for removed elements
464
+ var _updatedProcessedIndices = new Set();
465
+ processedIndices.forEach(function (processedIdx) {
466
+ if (processedIdx > removeIdx) {
467
+ _updatedProcessedIndices.add(processedIdx - 1);
468
+ } else if (processedIdx < removeIdx) {
469
+ _updatedProcessedIndices.add(processedIdx);
470
+ }
471
+ // Don't add the removed index
472
+ });
473
+ processedIndices.clear();
474
+ _updatedProcessedIndices.forEach(function (processedIdx) {
475
+ processedIndices.add(processedIdx);
476
+ });
477
+ }
478
+ };
479
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
480
+ _loop();
481
+ }
482
+
483
+ // Also remove the label of the first block if it exists
484
+ } catch (err) {
485
+ _iterator2.e(err);
486
+ } finally {
487
+ _iterator2.f();
488
+ }
489
+ if (codeBlocks[0].labelFromPrevious && codeBlocks[0].index > 0) {
490
+ var labelIndex = codeBlocks[0].index - 1;
491
+ var _prevNode3 = parentNode.children[labelIndex];
492
+ if (_prevNode3 && _prevNode3.type === 'paragraph') {
493
+ parentNode.children.splice(labelIndex, 1);
494
+ // Update processed indices
495
+ var updatedProcessedIndices = new Set();
496
+ processedIndices.forEach(function (processedIdx) {
497
+ if (processedIdx > labelIndex) {
498
+ updatedProcessedIndices.add(processedIdx - 1);
499
+ } else if (processedIdx < labelIndex) {
500
+ updatedProcessedIndices.add(processedIdx);
501
+ }
502
+ });
503
+ processedIndices.clear();
504
+ updatedProcessedIndices.forEach(function (processedIdx) {
505
+ processedIndices.add(processedIdx);
506
+ });
507
+ }
508
+ }
509
+ }
510
+ }
511
+ }
512
+ });
513
+ };
514
+ };
@@ -0,0 +1 @@
1
+ export * from "./transformTypescriptToJavascript.js";
@@ -0,0 +1 @@
1
+ export * from "./transformTypescriptToJavascript.js";
@@ -0,0 +1,13 @@
1
+ import type { Options as PrettierOptions } from 'prettier';
2
+ /**
3
+ * Strips TypeScript types and decorators from code (including React in TSX),
4
+ * preserving blank lines and optionally formatting with Prettier.
5
+ *
6
+ * @param code - The source code string to transform.
7
+ * @param filename - The name of the file (e.g. "foo.ts" or "Foo.tsx").
8
+ * Determines whether TSX parsing is enabled.
9
+ * @param prettierConfig - `true` for default formatting, `false` to skip,
10
+ * or a Prettier options object to customize.
11
+ * @returns The transformed (and optionally formatted) code.
12
+ */
13
+ export declare function removeTypes(code: string, filename?: string, prettierConfig?: PrettierOptions | boolean): Promise<string>;
@@ -0,0 +1,131 @@
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
+ // Based on https://github.com/ember-cli/babel-remove-types/blob/fc3be010e99c4f4926fd70d00242d6777ab1b8d7/src/index.ts
5
+ // Converted to use Babel standalone, with added TSX support
6
+
7
+ 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';
11
+ /**
12
+ * Strips TypeScript types and decorators from code (including React in TSX),
13
+ * preserving blank lines and optionally formatting with Prettier.
14
+ *
15
+ * @param code - The source code string to transform.
16
+ * @param filename - The name of the file (e.g. "foo.ts" or "Foo.tsx").
17
+ * Determines whether TSX parsing is enabled.
18
+ * @param prettierConfig - `true` for default formatting, `false` to skip,
19
+ * or a Prettier options object to customize.
20
+ * @returns The transformed (and optionally formatted) code.
21
+ */
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');
47
+
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));
127
+ }
128
+ }, _callee);
129
+ }));
130
+ return _removeTypes.apply(this, arguments);
131
+ }
@@ -0,0 +1,3 @@
1
+ import type { SourceTransformer, TransformSource } from "../../CodeHighlighter/types.js";
2
+ export declare const transformTypescriptToJavascript: TransformSource;
3
+ export declare const TypescriptToJavascriptTransformer: SourceTransformer;