@docusaurus/theme-common 2.0.0-beta.16 → 2.0.0-beta.19

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 (279) hide show
  1. package/Details.d.ts +14 -0
  2. package/lib/components/Collapsible/index.d.ts +32 -4
  3. package/lib/components/Collapsible/index.d.ts.map +1 -1
  4. package/lib/components/Collapsible/index.js +18 -10
  5. package/lib/components/Collapsible/index.js.map +1 -1
  6. package/lib/components/Details/index.d.ts +6 -1
  7. package/lib/components/Details/index.d.ts.map +1 -1
  8. package/lib/components/Details/index.js +11 -6
  9. package/lib/components/Details/index.js.map +1 -1
  10. package/lib/components/Details/styles.module.css +4 -0
  11. package/lib/{utils/announcementBarUtils.d.ts → contexts/announcementBar.d.ts} +7 -3
  12. package/lib/contexts/announcementBar.d.ts.map +1 -0
  13. package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +15 -14
  14. package/lib/contexts/announcementBar.js.map +1 -0
  15. package/lib/contexts/colorMode.d.ts +27 -0
  16. package/lib/contexts/colorMode.d.ts.map +1 -0
  17. package/lib/contexts/colorMode.js +132 -0
  18. package/lib/contexts/colorMode.js.map +1 -0
  19. package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
  20. package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
  21. package/lib/{utils → contexts}/docSidebarItemsExpandedState.js +10 -4
  22. package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
  23. package/lib/contexts/docsPreferredVersion.d.ts +31 -0
  24. package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
  25. package/lib/contexts/docsPreferredVersion.js +128 -0
  26. package/lib/contexts/docsPreferredVersion.js.map +1 -0
  27. package/lib/contexts/docsSidebar.d.ts +26 -0
  28. package/lib/contexts/docsSidebar.d.ts.map +1 -0
  29. package/lib/contexts/docsSidebar.js +30 -0
  30. package/lib/contexts/docsSidebar.js.map +1 -0
  31. package/lib/contexts/docsVersion.d.ts +20 -0
  32. package/lib/contexts/docsVersion.d.ts.map +1 -0
  33. package/lib/contexts/docsVersion.js +26 -0
  34. package/lib/contexts/docsVersion.js.map +1 -0
  35. package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
  36. package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
  37. package/lib/contexts/navbarMobileSidebar.js +56 -0
  38. package/lib/contexts/navbarMobileSidebar.js.map +1 -0
  39. package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
  40. package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
  41. package/lib/contexts/navbarSecondaryMenu/content.js +56 -0
  42. package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
  43. package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
  44. package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
  45. package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
  46. package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
  47. package/lib/{utils/tabGroupChoiceUtils.d.ts → contexts/tabGroupChoice.d.ts} +5 -3
  48. package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
  49. package/lib/{utils/tabGroupChoiceUtils.js → contexts/tabGroupChoice.js} +15 -20
  50. package/lib/contexts/tabGroupChoice.js.map +1 -0
  51. package/lib/hooks/useBackToTopButton.d.ts +27 -0
  52. package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
  53. package/lib/hooks/useBackToTopButton.js +50 -0
  54. package/lib/hooks/useBackToTopButton.js.map +1 -0
  55. package/lib/hooks/useCodeWordWrap.d.ts +14 -0
  56. package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
  57. package/lib/hooks/useCodeWordWrap.js +41 -0
  58. package/lib/hooks/useCodeWordWrap.js.map +1 -0
  59. package/lib/hooks/useHideableNavbar.d.ts +7 -3
  60. package/lib/hooks/useHideableNavbar.d.ts.map +1 -1
  61. package/lib/hooks/useHideableNavbar.js +10 -9
  62. package/lib/hooks/useHideableNavbar.js.map +1 -1
  63. package/lib/hooks/useKeyboardNavigation.d.ts +11 -1
  64. package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -1
  65. package/lib/hooks/useKeyboardNavigation.js +11 -3
  66. package/lib/hooks/useKeyboardNavigation.js.map +1 -1
  67. package/lib/hooks/useLockBodyScroll.d.ts +5 -1
  68. package/lib/hooks/useLockBodyScroll.d.ts.map +1 -1
  69. package/lib/hooks/useLockBodyScroll.js +5 -1
  70. package/lib/hooks/useLockBodyScroll.js.map +1 -1
  71. package/lib/hooks/usePrismTheme.d.ts +6 -2
  72. package/lib/hooks/usePrismTheme.d.ts.map +1 -1
  73. package/lib/hooks/usePrismTheme.js +9 -6
  74. package/lib/hooks/usePrismTheme.js.map +1 -1
  75. package/lib/hooks/useSearchPage.d.ts +15 -4
  76. package/lib/hooks/useSearchPage.d.ts.map +1 -1
  77. package/lib/hooks/useSearchPage.js +4 -4
  78. package/lib/hooks/useSearchPage.js.map +1 -1
  79. package/lib/hooks/useSkipToContent.d.ts +25 -0
  80. package/lib/hooks/useSkipToContent.d.ts.map +1 -0
  81. package/lib/hooks/useSkipToContent.js +35 -0
  82. package/lib/hooks/useSkipToContent.js.map +1 -0
  83. package/lib/hooks/useTOCHighlight.d.ts +25 -0
  84. package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
  85. package/lib/{utils → hooks}/useTOCHighlight.js +18 -16
  86. package/lib/hooks/useTOCHighlight.js.map +1 -0
  87. package/lib/hooks/useWindowSize.d.ts +14 -1
  88. package/lib/hooks/useWindowSize.d.ts.map +1 -1
  89. package/lib/hooks/useWindowSize.js +14 -11
  90. package/lib/hooks/useWindowSize.js.map +1 -1
  91. package/lib/index.d.ts +31 -32
  92. package/lib/index.d.ts.map +1 -1
  93. package/lib/index.js +31 -26
  94. package/lib/index.js.map +1 -1
  95. package/lib/utils/ThemeClassNames.d.ts +7 -0
  96. package/lib/utils/ThemeClassNames.d.ts.map +1 -1
  97. package/lib/utils/ThemeClassNames.js +7 -4
  98. package/lib/utils/ThemeClassNames.js.map +1 -1
  99. package/lib/utils/codeBlockUtils.d.ts +57 -4
  100. package/lib/utils/codeBlockUtils.d.ts.map +1 -1
  101. package/lib/utils/codeBlockUtils.js +111 -80
  102. package/lib/utils/codeBlockUtils.js.map +1 -1
  103. package/lib/utils/docsUtils.d.ts +83 -18
  104. package/lib/utils/docsUtils.d.ts.map +1 -1
  105. package/lib/utils/docsUtils.js +143 -62
  106. package/lib/utils/docsUtils.js.map +1 -1
  107. package/lib/utils/footerUtils.d.ts +13 -0
  108. package/lib/utils/footerUtils.d.ts.map +1 -0
  109. package/lib/utils/footerUtils.js +14 -0
  110. package/lib/utils/footerUtils.js.map +1 -0
  111. package/lib/utils/generalUtils.d.ts +4 -1
  112. package/lib/utils/generalUtils.d.ts.map +1 -1
  113. package/lib/utils/generalUtils.js +6 -3
  114. package/lib/utils/generalUtils.js.map +1 -1
  115. package/lib/utils/historyUtils.d.ts +1 -7
  116. package/lib/utils/historyUtils.d.ts.map +1 -1
  117. package/lib/utils/historyUtils.js +10 -13
  118. package/lib/utils/historyUtils.js.map +1 -1
  119. package/lib/utils/jsUtils.d.ts +1 -1
  120. package/lib/utils/jsUtils.js +1 -1
  121. package/lib/utils/metadataUtils.d.ts +38 -0
  122. package/lib/utils/metadataUtils.d.ts.map +1 -0
  123. package/lib/utils/metadataUtils.js +61 -0
  124. package/lib/utils/metadataUtils.js.map +1 -0
  125. package/lib/utils/navbarUtils.d.ts +21 -0
  126. package/lib/utils/navbarUtils.d.ts.map +1 -0
  127. package/lib/utils/navbarUtils.js +32 -0
  128. package/lib/utils/navbarUtils.js.map +1 -0
  129. package/lib/utils/reactUtils.d.ts +21 -7
  130. package/lib/utils/reactUtils.d.ts.map +1 -1
  131. package/lib/utils/reactUtils.js +34 -6
  132. package/lib/utils/reactUtils.js.map +1 -1
  133. package/lib/utils/regexpUtils.d.ts +2 -1
  134. package/lib/utils/regexpUtils.d.ts.map +1 -1
  135. package/lib/utils/regexpUtils.js +2 -1
  136. package/lib/utils/regexpUtils.js.map +1 -1
  137. package/lib/utils/routesUtils.d.ts +19 -4
  138. package/lib/utils/routesUtils.d.ts.map +1 -1
  139. package/lib/utils/routesUtils.js +40 -19
  140. package/lib/utils/routesUtils.js.map +1 -1
  141. package/lib/utils/scrollUtils.d.ts +55 -25
  142. package/lib/utils/scrollUtils.d.ts.map +1 -1
  143. package/lib/utils/scrollUtils.js +85 -21
  144. package/lib/utils/scrollUtils.js.map +1 -1
  145. package/lib/utils/searchUtils.d.ts +12 -0
  146. package/lib/utils/searchUtils.d.ts.map +1 -1
  147. package/lib/utils/searchUtils.js +36 -0
  148. package/lib/utils/searchUtils.js.map +1 -1
  149. package/lib/utils/storageUtils.d.ts +12 -9
  150. package/lib/utils/storageUtils.d.ts.map +1 -1
  151. package/lib/utils/storageUtils.js +21 -13
  152. package/lib/utils/storageUtils.js.map +1 -1
  153. package/lib/utils/tagsUtils.d.ts +7 -8
  154. package/lib/utils/tagsUtils.d.ts.map +1 -1
  155. package/lib/utils/tagsUtils.js +8 -6
  156. package/lib/utils/tagsUtils.js.map +1 -1
  157. package/lib/utils/tocUtils.d.ts +17 -1
  158. package/lib/utils/tocUtils.d.ts.map +1 -1
  159. package/lib/utils/tocUtils.js +20 -9
  160. package/lib/utils/tocUtils.js.map +1 -1
  161. package/lib/utils/useAlternatePageUtils.d.ts +20 -1
  162. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -1
  163. package/lib/utils/useAlternatePageUtils.js +6 -3
  164. package/lib/utils/useAlternatePageUtils.js.map +1 -1
  165. package/lib/utils/useLocalPathname.d.ts +5 -0
  166. package/lib/utils/useLocalPathname.d.ts.map +1 -1
  167. package/lib/utils/useLocalPathname.js +6 -4
  168. package/lib/utils/useLocalPathname.js.map +1 -1
  169. package/lib/utils/useLocationChange.d.ts +7 -5
  170. package/lib/utils/useLocationChange.d.ts.map +1 -1
  171. package/lib/utils/useLocationChange.js +6 -2
  172. package/lib/utils/useLocationChange.js.map +1 -1
  173. package/lib/utils/usePluralForm.d.ts +11 -0
  174. package/lib/utils/usePluralForm.d.ts.map +1 -1
  175. package/lib/utils/usePluralForm.js +19 -24
  176. package/lib/utils/usePluralForm.js.map +1 -1
  177. package/lib/utils/useThemeConfig.d.ts +28 -22
  178. package/lib/utils/useThemeConfig.d.ts.map +1 -1
  179. package/lib/utils/useThemeConfig.js +3 -0
  180. package/lib/utils/useThemeConfig.js.map +1 -1
  181. package/package.json +18 -11
  182. package/src/components/Collapsible/index.tsx +42 -24
  183. package/src/components/Details/index.tsx +15 -8
  184. package/src/components/Details/styles.module.css +4 -0
  185. package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +20 -22
  186. package/src/contexts/colorMode.tsx +199 -0
  187. package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
  188. package/src/contexts/docsPreferredVersion.tsx +253 -0
  189. package/src/contexts/docsSidebar.tsx +50 -0
  190. package/src/contexts/docsVersion.tsx +36 -0
  191. package/src/contexts/navbarMobileSidebar.tsx +99 -0
  192. package/src/contexts/navbarSecondaryMenu/content.tsx +110 -0
  193. package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
  194. package/src/{utils/tabGroupChoiceUtils.tsx → contexts/tabGroupChoice.tsx} +28 -33
  195. package/src/hooks/useBackToTopButton.ts +73 -0
  196. package/src/hooks/useCodeWordWrap.ts +56 -0
  197. package/src/hooks/useHideableNavbar.ts +12 -14
  198. package/src/hooks/useKeyboardNavigation.ts +11 -3
  199. package/src/hooks/useLockBodyScroll.ts +5 -2
  200. package/src/hooks/usePrismTheme.ts +10 -6
  201. package/src/hooks/useSearchPage.ts +18 -5
  202. package/src/hooks/useSkipToContent.ts +58 -0
  203. package/src/{utils → hooks}/useTOCHighlight.ts +24 -15
  204. package/src/hooks/useWindowSize.ts +14 -12
  205. package/src/index.ts +77 -57
  206. package/src/utils/ThemeClassNames.ts +10 -6
  207. package/src/utils/codeBlockUtils.ts +178 -92
  208. package/src/utils/docsUtils.tsx +205 -101
  209. package/src/utils/footerUtils.ts +18 -0
  210. package/src/utils/generalUtils.ts +6 -3
  211. package/src/utils/historyUtils.ts +11 -17
  212. package/src/utils/jsUtils.ts +1 -1
  213. package/src/utils/metadataUtils.tsx +115 -0
  214. package/src/utils/navbarUtils.tsx +45 -0
  215. package/src/utils/reactUtils.tsx +40 -7
  216. package/src/utils/regexpUtils.ts +2 -1
  217. package/src/utils/routesUtils.ts +58 -22
  218. package/src/utils/scrollUtils.tsx +120 -49
  219. package/src/utils/searchUtils.ts +51 -0
  220. package/src/utils/storageUtils.ts +23 -15
  221. package/src/utils/tagsUtils.ts +12 -10
  222. package/src/utils/tocUtils.ts +22 -11
  223. package/src/utils/useAlternatePageUtils.ts +17 -5
  224. package/src/utils/useLocalPathname.ts +6 -4
  225. package/src/utils/useLocationChange.ts +12 -10
  226. package/src/utils/usePluralForm.ts +27 -24
  227. package/src/utils/useThemeConfig.ts +23 -22
  228. package/yarn-error.log +20199 -0
  229. package/lib/utils/announcementBarUtils.d.ts.map +0 -1
  230. package/lib/utils/announcementBarUtils.js.map +0 -1
  231. package/lib/utils/colorModeUtils.d.ts +0 -18
  232. package/lib/utils/colorModeUtils.d.ts.map +0 -1
  233. package/lib/utils/colorModeUtils.js +0 -106
  234. package/lib/utils/colorModeUtils.js.map +0 -1
  235. package/lib/utils/docSidebarItemsExpandedState.d.ts +0 -17
  236. package/lib/utils/docSidebarItemsExpandedState.d.ts.map +0 -1
  237. package/lib/utils/docSidebarItemsExpandedState.js.map +0 -1
  238. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -22
  239. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +0 -1
  240. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -91
  241. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +0 -1
  242. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -14
  243. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +0 -1
  244. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -19
  245. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +0 -1
  246. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -14
  247. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +0 -1
  248. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
  249. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +0 -1
  250. package/lib/utils/mobileSecondaryMenu.d.ts +0 -21
  251. package/lib/utils/mobileSecondaryMenu.d.ts.map +0 -1
  252. package/lib/utils/mobileSecondaryMenu.js +0 -50
  253. package/lib/utils/mobileSecondaryMenu.js.map +0 -1
  254. package/lib/utils/pathUtils.d.ts +0 -8
  255. package/lib/utils/pathUtils.d.ts.map +0 -1
  256. package/lib/utils/pathUtils.js +0 -17
  257. package/lib/utils/pathUtils.js.map +0 -1
  258. package/lib/utils/tabGroupChoiceUtils.d.ts.map +0 -1
  259. package/lib/utils/tabGroupChoiceUtils.js.map +0 -1
  260. package/lib/utils/useContextualSearchFilters.d.ts +0 -12
  261. package/lib/utils/useContextualSearchFilters.d.ts.map +0 -1
  262. package/lib/utils/useContextualSearchFilters.js +0 -37
  263. package/lib/utils/useContextualSearchFilters.js.map +0 -1
  264. package/lib/utils/usePrevious.d.ts +0 -8
  265. package/lib/utils/usePrevious.d.ts.map +0 -1
  266. package/lib/utils/usePrevious.js +0 -16
  267. package/lib/utils/usePrevious.js.map +0 -1
  268. package/lib/utils/useTOCHighlight.d.ts +0 -14
  269. package/lib/utils/useTOCHighlight.d.ts.map +0 -1
  270. package/lib/utils/useTOCHighlight.js.map +0 -1
  271. package/src/utils/colorModeUtils.tsx +0 -156
  272. package/src/utils/docSidebarItemsExpandedState.tsx +0 -41
  273. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -167
  274. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -33
  275. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -70
  276. package/src/utils/mobileSecondaryMenu.tsx +0 -115
  277. package/src/utils/pathUtils.ts +0 -19
  278. package/src/utils/useContextualSearchFilters.ts +0 -53
  279. package/src/utils/usePrevious.ts +0 -19
@@ -6,92 +6,105 @@
6
6
  */
7
7
 
8
8
  import rangeParser from 'parse-numeric-range';
9
+ import type {PrismTheme} from 'prism-react-renderer';
10
+ import type {CSSProperties} from 'react';
9
11
 
10
12
  const codeBlockTitleRegex = /title=(?<quote>["'])(?<title>.*?)\1/;
11
- const highlightLinesRangeRegex = /{(?<range>[\d,-]+)}/;
12
-
13
- const commentTypes = ['js', 'jsBlock', 'jsx', 'python', 'html'] as const;
14
- type CommentType = typeof commentTypes[number];
15
-
16
- type CommentPattern = {
17
- start: string;
18
- end: string;
19
- };
13
+ const metastringLinesRangeRegex = /\{(?<range>[\d,-]+)\}/;
20
14
 
21
15
  // Supported types of highlight comments
22
- const commentPatterns: Record<CommentType, CommentPattern> = {
23
- js: {
24
- start: '\\/\\/',
25
- end: '',
26
- },
27
- jsBlock: {
28
- start: '\\/\\*',
29
- end: '\\*\\/',
30
- },
31
- jsx: {
32
- start: '\\{\\s*\\/\\*',
33
- end: '\\*\\/\\s*\\}',
34
- },
35
- python: {
36
- start: '#',
37
- end: '',
38
- },
39
- html: {
40
- start: '<!--',
41
- end: '-->',
42
- },
16
+ const commentPatterns = {
17
+ js: {start: '\\/\\/', end: ''},
18
+ jsBlock: {start: '\\/\\*', end: '\\*\\/'},
19
+ jsx: {start: '\\{\\s*\\/\\*', end: '\\*\\/\\s*\\}'},
20
+ bash: {start: '#', end: ''},
21
+ html: {start: '<!--', end: '-->'},
43
22
  };
44
23
 
45
- const magicCommentDirectives = [
46
- 'highlight-next-line',
47
- 'highlight-start',
48
- 'highlight-end',
49
- ];
24
+ type CommentType = keyof typeof commentPatterns;
50
25
 
51
- const getMagicCommentDirectiveRegex = (
52
- languages: readonly CommentType[] = commentTypes,
53
- ) => {
54
- // to be more reliable, the opening and closing comment must match
26
+ export type MagicCommentConfig = {
27
+ className: string;
28
+ line?: string;
29
+ block?: {start: string; end: string};
30
+ };
31
+
32
+ function getCommentPattern(
33
+ languages: CommentType[],
34
+ magicCommentDirectives: MagicCommentConfig[],
35
+ ) {
36
+ // To be more reliable, the opening and closing comment must match
55
37
  const commentPattern = languages
56
38
  .map((lang) => {
57
39
  const {start, end} = commentPatterns[lang];
58
- return `(?:${start}\\s*(${magicCommentDirectives.join('|')})\\s*${end})`;
40
+ return `(?:${start}\\s*(${magicCommentDirectives
41
+ .flatMap((d) => [d.line, d.block?.start, d.block?.end].filter(Boolean))
42
+ .join('|')})\\s*${end})`;
59
43
  })
60
44
  .join('|');
61
- // white space is allowed, but otherwise it should be on it's own line
45
+ // White space is allowed, but otherwise it should be on it's own line
62
46
  return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
63
- };
47
+ }
64
48
 
65
- // select comment styles based on language
66
- const magicCommentDirectiveRegex = (lang: string) => {
49
+ /**
50
+ * Select comment styles based on language
51
+ */
52
+ function getAllMagicCommentDirectiveStyles(
53
+ lang: string,
54
+ magicCommentDirectives: MagicCommentConfig[],
55
+ ) {
67
56
  switch (lang) {
68
57
  case 'js':
69
58
  case 'javascript':
70
59
  case 'ts':
71
60
  case 'typescript':
72
- return getMagicCommentDirectiveRegex(['js', 'jsBlock']);
61
+ return getCommentPattern(['js', 'jsBlock'], magicCommentDirectives);
73
62
 
74
63
  case 'jsx':
75
64
  case 'tsx':
76
- return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'jsx']);
65
+ return getCommentPattern(
66
+ ['js', 'jsBlock', 'jsx'],
67
+ magicCommentDirectives,
68
+ );
77
69
 
78
70
  case 'html':
79
- return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'html']);
71
+ return getCommentPattern(
72
+ ['js', 'jsBlock', 'html'],
73
+ magicCommentDirectives,
74
+ );
80
75
 
81
76
  case 'python':
82
77
  case 'py':
83
- return getMagicCommentDirectiveRegex(['python']);
78
+ case 'bash':
79
+ return getCommentPattern(['bash'], magicCommentDirectives);
80
+
81
+ case 'markdown':
82
+ case 'md':
83
+ // Text uses HTML, front matter uses bash
84
+ return getCommentPattern(['html', 'jsx', 'bash'], magicCommentDirectives);
84
85
 
85
86
  default:
86
- // all comment types
87
- return getMagicCommentDirectiveRegex();
87
+ // All comment types
88
+ return getCommentPattern(
89
+ Object.keys(commentPatterns) as CommentType[],
90
+ magicCommentDirectives,
91
+ );
88
92
  }
89
- };
93
+ }
90
94
 
91
95
  export function parseCodeBlockTitle(metastring?: string): string {
92
96
  return metastring?.match(codeBlockTitleRegex)?.groups!.title ?? '';
93
97
  }
94
98
 
99
+ export function containsLineNumbers(metastring?: string): boolean {
100
+ return metastring?.includes('showLineNumbers') || false;
101
+ }
102
+
103
+ /**
104
+ * Gets the language name from the class name (set by MDX).
105
+ * e.g. `"language-javascript"` => `"javascript"`.
106
+ * Returns undefined if there is no language class name.
107
+ */
95
108
  export function parseLanguage(className: string): string | undefined {
96
109
  const languageClassName = className
97
110
  .split(' ')
@@ -100,64 +113,137 @@ export function parseLanguage(className: string): string | undefined {
100
113
  }
101
114
 
102
115
  /**
103
- * @param metastring The highlight range declared here starts at 1
104
- * @returns Note: all line numbers start at 0, not 1
116
+ * Parses the code content, strips away any magic comments, and returns the
117
+ * clean content and the highlighted lines marked by the comments or metastring.
118
+ *
119
+ * If the metastring contains a range, the `content` will be returned as-is
120
+ * without any parsing. The returned `lineClassNames` will be a map from that
121
+ * number range to the first magic comment config entry (which _should_ be for
122
+ * line highlight directives.)
123
+ *
124
+ * @param content The raw code with magic comments. Trailing newline will be
125
+ * trimmed upfront.
126
+ * @param options Options for parsing behavior.
105
127
  */
106
128
  export function parseLines(
107
129
  content: string,
108
- metastring?: string,
109
- language?: string,
130
+ options: {
131
+ /**
132
+ * The full metastring, as received from MDX. Line ranges declared here
133
+ * start at 1.
134
+ */
135
+ metastring: string | undefined;
136
+ /**
137
+ * Language of the code block, used to determine which kinds of magic
138
+ * comment styles to enable.
139
+ */
140
+ language: string | undefined;
141
+ /**
142
+ * Magic comment types that we should try to parse. Each entry would
143
+ * correspond to one class name to apply to each line.
144
+ */
145
+ magicComments: MagicCommentConfig[];
146
+ },
110
147
  ): {
111
- highlightLines: number[];
148
+ /**
149
+ * The highlighted lines, 0-indexed. e.g. `{ 0: ["highlight", "sample"] }`
150
+ * means the 1st line should have `highlight` and `sample` as class names.
151
+ */
152
+ lineClassNames: {[lineIndex: number]: string[]};
153
+ /**
154
+ * If there's number range declared in the metastring, the code block is
155
+ * returned as-is (no parsing); otherwise, this is the clean code with all
156
+ * magic comments stripped away.
157
+ */
112
158
  code: string;
113
159
  } {
114
160
  let code = content.replace(/\n$/, '');
161
+ const {language, magicComments, metastring} = options;
115
162
  // Highlighted lines specified in props: don't parse the content
116
- if (metastring && highlightLinesRangeRegex.test(metastring)) {
117
- const highlightLinesRange = metastring.match(highlightLinesRangeRegex)!
118
- .groups!.range;
119
- const highlightLines = rangeParser(highlightLinesRange)
163
+ if (metastring && metastringLinesRangeRegex.test(metastring)) {
164
+ const linesRange = metastring.match(metastringLinesRangeRegex)!.groups!
165
+ .range!;
166
+ if (magicComments.length === 0) {
167
+ throw new Error(
168
+ `A highlight range has been given in code block's metastring (\`\`\` ${metastring}), but no magic comment config is available. Docusaurus applies the first magic comment entry's className for metastring ranges.`,
169
+ );
170
+ }
171
+ const metastringRangeClassName = magicComments[0]!.className;
172
+ const lines = rangeParser(linesRange)
120
173
  .filter((n) => n > 0)
121
- .map((n) => n - 1);
122
- return {highlightLines, code};
174
+ .map((n) => [n - 1, [metastringRangeClassName]]);
175
+ return {lineClassNames: Object.fromEntries(lines), code};
123
176
  }
124
177
  if (language === undefined) {
125
- return {highlightLines: [], code};
178
+ return {lineClassNames: {}, code};
126
179
  }
127
- const directiveRegex = magicCommentDirectiveRegex(language);
128
- // go through line by line
180
+ const directiveRegex = getAllMagicCommentDirectiveStyles(
181
+ language,
182
+ magicComments,
183
+ );
184
+ // Go through line by line
129
185
  const lines = code.split('\n');
130
- let highlightBlockStart: number;
131
- let highlightRange = '';
132
- // loop through lines
186
+ const blocks = Object.fromEntries(
187
+ magicComments.map((d) => [d.className, {start: 0, range: ''}]),
188
+ );
189
+ const lineToClassName: {[comment: string]: string} = Object.fromEntries(
190
+ magicComments
191
+ .filter((d) => d.line)
192
+ .map(({className, line}) => [line, className]),
193
+ );
194
+ const blockStartToClassName: {[comment: string]: string} = Object.fromEntries(
195
+ magicComments
196
+ .filter((d) => d.block)
197
+ .map(({className, block}) => [block!.start, className]),
198
+ );
199
+ const blockEndToClassName: {[comment: string]: string} = Object.fromEntries(
200
+ magicComments
201
+ .filter((d) => d.block)
202
+ .map(({className, block}) => [block!.end, className]),
203
+ );
133
204
  for (let lineNumber = 0; lineNumber < lines.length; ) {
134
- const line = lines[lineNumber];
205
+ const line = lines[lineNumber]!;
135
206
  const match = line.match(directiveRegex);
136
- if (match !== null) {
137
- const directive = match.slice(1).find((item) => item !== undefined);
138
- switch (directive) {
139
- case 'highlight-next-line':
140
- highlightRange += `${lineNumber},`;
141
- break;
142
-
143
- case 'highlight-start':
144
- highlightBlockStart = lineNumber;
145
- break;
146
-
147
- case 'highlight-end':
148
- highlightRange += `${highlightBlockStart!}-${lineNumber - 1},`;
149
- break;
150
-
151
- default:
152
- break;
153
- }
154
- lines.splice(lineNumber, 1);
155
- } else {
156
- // lines without directives are unchanged
207
+ if (!match) {
208
+ // Lines without directives are unchanged
157
209
  lineNumber += 1;
210
+ continue;
211
+ }
212
+ const directive = match.slice(1).find((item) => item !== undefined)!;
213
+ if (lineToClassName[directive]) {
214
+ blocks[lineToClassName[directive]!]!.range += `${lineNumber},`;
215
+ } else if (blockStartToClassName[directive]) {
216
+ blocks[blockStartToClassName[directive]!]!.start = lineNumber;
217
+ } else if (blockEndToClassName[directive]) {
218
+ blocks[blockEndToClassName[directive]!]!.range += `${
219
+ blocks[blockEndToClassName[directive]!]!.start
220
+ }-${lineNumber - 1},`;
158
221
  }
222
+ lines.splice(lineNumber, 1);
159
223
  }
160
- const highlightLines = rangeParser(highlightRange);
161
224
  code = lines.join('\n');
162
- return {highlightLines, code};
225
+ const lineClassNames: {[lineIndex: number]: string[]} = {};
226
+ Object.entries(blocks).forEach(([className, {range}]) => {
227
+ rangeParser(range).forEach((l) => {
228
+ lineClassNames[l] ??= [];
229
+ lineClassNames[l]!.push(className);
230
+ });
231
+ });
232
+ return {lineClassNames, code};
233
+ }
234
+
235
+ export function getPrismCssVariables(prismTheme: PrismTheme): CSSProperties {
236
+ const mapping: {[name: keyof PrismTheme['plain']]: string} = {
237
+ color: '--prism-color',
238
+ backgroundColor: '--prism-background-color',
239
+ };
240
+
241
+ const properties: {[key: string]: string} = {};
242
+ Object.entries(prismTheme.plain).forEach(([key, value]) => {
243
+ const varName = mapping[key];
244
+ if (varName && typeof value === 'string') {
245
+ properties[varName] = value;
246
+ }
247
+ });
248
+ return properties;
163
249
  }