@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.
- package/Details.d.ts +14 -0
- package/lib/components/Collapsible/index.d.ts +32 -4
- package/lib/components/Collapsible/index.d.ts.map +1 -1
- package/lib/components/Collapsible/index.js +18 -10
- package/lib/components/Collapsible/index.js.map +1 -1
- package/lib/components/Details/index.d.ts +6 -1
- package/lib/components/Details/index.d.ts.map +1 -1
- package/lib/components/Details/index.js +11 -6
- package/lib/components/Details/index.js.map +1 -1
- package/lib/components/Details/styles.module.css +4 -0
- package/lib/{utils/announcementBarUtils.d.ts → contexts/announcementBar.d.ts} +7 -3
- package/lib/contexts/announcementBar.d.ts.map +1 -0
- package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +15 -14
- package/lib/contexts/announcementBar.js.map +1 -0
- package/lib/contexts/colorMode.d.ts +27 -0
- package/lib/contexts/colorMode.d.ts.map +1 -0
- package/lib/contexts/colorMode.js +132 -0
- package/lib/contexts/colorMode.js.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
- package/lib/{utils → contexts}/docSidebarItemsExpandedState.js +10 -4
- package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
- package/lib/contexts/docsPreferredVersion.d.ts +31 -0
- package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
- package/lib/contexts/docsPreferredVersion.js +128 -0
- package/lib/contexts/docsPreferredVersion.js.map +1 -0
- package/lib/contexts/docsSidebar.d.ts +26 -0
- package/lib/contexts/docsSidebar.d.ts.map +1 -0
- package/lib/contexts/docsSidebar.js +30 -0
- package/lib/contexts/docsSidebar.js.map +1 -0
- package/lib/contexts/docsVersion.d.ts +20 -0
- package/lib/contexts/docsVersion.d.ts.map +1 -0
- package/lib/contexts/docsVersion.js +26 -0
- package/lib/contexts/docsVersion.js.map +1 -0
- package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
- package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
- package/lib/contexts/navbarMobileSidebar.js +56 -0
- package/lib/contexts/navbarMobileSidebar.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.js +56 -0
- package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
- package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
- package/lib/{utils/tabGroupChoiceUtils.d.ts → contexts/tabGroupChoice.d.ts} +5 -3
- package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
- package/lib/{utils/tabGroupChoiceUtils.js → contexts/tabGroupChoice.js} +15 -20
- package/lib/contexts/tabGroupChoice.js.map +1 -0
- package/lib/hooks/useBackToTopButton.d.ts +27 -0
- package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
- package/lib/hooks/useBackToTopButton.js +50 -0
- package/lib/hooks/useBackToTopButton.js.map +1 -0
- package/lib/hooks/useCodeWordWrap.d.ts +14 -0
- package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
- package/lib/hooks/useCodeWordWrap.js +41 -0
- package/lib/hooks/useCodeWordWrap.js.map +1 -0
- package/lib/hooks/useHideableNavbar.d.ts +7 -3
- package/lib/hooks/useHideableNavbar.d.ts.map +1 -1
- package/lib/hooks/useHideableNavbar.js +10 -9
- package/lib/hooks/useHideableNavbar.js.map +1 -1
- package/lib/hooks/useKeyboardNavigation.d.ts +11 -1
- package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -1
- package/lib/hooks/useKeyboardNavigation.js +11 -3
- package/lib/hooks/useKeyboardNavigation.js.map +1 -1
- package/lib/hooks/useLockBodyScroll.d.ts +5 -1
- package/lib/hooks/useLockBodyScroll.d.ts.map +1 -1
- package/lib/hooks/useLockBodyScroll.js +5 -1
- package/lib/hooks/useLockBodyScroll.js.map +1 -1
- package/lib/hooks/usePrismTheme.d.ts +6 -2
- package/lib/hooks/usePrismTheme.d.ts.map +1 -1
- package/lib/hooks/usePrismTheme.js +9 -6
- package/lib/hooks/usePrismTheme.js.map +1 -1
- package/lib/hooks/useSearchPage.d.ts +15 -4
- package/lib/hooks/useSearchPage.d.ts.map +1 -1
- package/lib/hooks/useSearchPage.js +4 -4
- package/lib/hooks/useSearchPage.js.map +1 -1
- package/lib/hooks/useSkipToContent.d.ts +25 -0
- package/lib/hooks/useSkipToContent.d.ts.map +1 -0
- package/lib/hooks/useSkipToContent.js +35 -0
- package/lib/hooks/useSkipToContent.js.map +1 -0
- package/lib/hooks/useTOCHighlight.d.ts +25 -0
- package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
- package/lib/{utils → hooks}/useTOCHighlight.js +18 -16
- package/lib/hooks/useTOCHighlight.js.map +1 -0
- package/lib/hooks/useWindowSize.d.ts +14 -1
- package/lib/hooks/useWindowSize.d.ts.map +1 -1
- package/lib/hooks/useWindowSize.js +14 -11
- package/lib/hooks/useWindowSize.js.map +1 -1
- package/lib/index.d.ts +31 -32
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +31 -26
- package/lib/index.js.map +1 -1
- package/lib/utils/ThemeClassNames.d.ts +7 -0
- package/lib/utils/ThemeClassNames.d.ts.map +1 -1
- package/lib/utils/ThemeClassNames.js +7 -4
- package/lib/utils/ThemeClassNames.js.map +1 -1
- package/lib/utils/codeBlockUtils.d.ts +57 -4
- package/lib/utils/codeBlockUtils.d.ts.map +1 -1
- package/lib/utils/codeBlockUtils.js +111 -80
- package/lib/utils/codeBlockUtils.js.map +1 -1
- package/lib/utils/docsUtils.d.ts +83 -18
- package/lib/utils/docsUtils.d.ts.map +1 -1
- package/lib/utils/docsUtils.js +143 -62
- package/lib/utils/docsUtils.js.map +1 -1
- package/lib/utils/footerUtils.d.ts +13 -0
- package/lib/utils/footerUtils.d.ts.map +1 -0
- package/lib/utils/footerUtils.js +14 -0
- package/lib/utils/footerUtils.js.map +1 -0
- package/lib/utils/generalUtils.d.ts +4 -1
- package/lib/utils/generalUtils.d.ts.map +1 -1
- package/lib/utils/generalUtils.js +6 -3
- package/lib/utils/generalUtils.js.map +1 -1
- package/lib/utils/historyUtils.d.ts +1 -7
- package/lib/utils/historyUtils.d.ts.map +1 -1
- package/lib/utils/historyUtils.js +10 -13
- package/lib/utils/historyUtils.js.map +1 -1
- package/lib/utils/jsUtils.d.ts +1 -1
- package/lib/utils/jsUtils.js +1 -1
- package/lib/utils/metadataUtils.d.ts +38 -0
- package/lib/utils/metadataUtils.d.ts.map +1 -0
- package/lib/utils/metadataUtils.js +61 -0
- package/lib/utils/metadataUtils.js.map +1 -0
- package/lib/utils/navbarUtils.d.ts +21 -0
- package/lib/utils/navbarUtils.d.ts.map +1 -0
- package/lib/utils/navbarUtils.js +32 -0
- package/lib/utils/navbarUtils.js.map +1 -0
- package/lib/utils/reactUtils.d.ts +21 -7
- package/lib/utils/reactUtils.d.ts.map +1 -1
- package/lib/utils/reactUtils.js +34 -6
- package/lib/utils/reactUtils.js.map +1 -1
- package/lib/utils/regexpUtils.d.ts +2 -1
- package/lib/utils/regexpUtils.d.ts.map +1 -1
- package/lib/utils/regexpUtils.js +2 -1
- package/lib/utils/regexpUtils.js.map +1 -1
- package/lib/utils/routesUtils.d.ts +19 -4
- package/lib/utils/routesUtils.d.ts.map +1 -1
- package/lib/utils/routesUtils.js +40 -19
- package/lib/utils/routesUtils.js.map +1 -1
- package/lib/utils/scrollUtils.d.ts +55 -25
- package/lib/utils/scrollUtils.d.ts.map +1 -1
- package/lib/utils/scrollUtils.js +85 -21
- package/lib/utils/scrollUtils.js.map +1 -1
- package/lib/utils/searchUtils.d.ts +12 -0
- package/lib/utils/searchUtils.d.ts.map +1 -1
- package/lib/utils/searchUtils.js +36 -0
- package/lib/utils/searchUtils.js.map +1 -1
- package/lib/utils/storageUtils.d.ts +12 -9
- package/lib/utils/storageUtils.d.ts.map +1 -1
- package/lib/utils/storageUtils.js +21 -13
- package/lib/utils/storageUtils.js.map +1 -1
- package/lib/utils/tagsUtils.d.ts +7 -8
- package/lib/utils/tagsUtils.d.ts.map +1 -1
- package/lib/utils/tagsUtils.js +8 -6
- package/lib/utils/tagsUtils.js.map +1 -1
- package/lib/utils/tocUtils.d.ts +17 -1
- package/lib/utils/tocUtils.d.ts.map +1 -1
- package/lib/utils/tocUtils.js +20 -9
- package/lib/utils/tocUtils.js.map +1 -1
- package/lib/utils/useAlternatePageUtils.d.ts +20 -1
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -1
- package/lib/utils/useAlternatePageUtils.js +6 -3
- package/lib/utils/useAlternatePageUtils.js.map +1 -1
- package/lib/utils/useLocalPathname.d.ts +5 -0
- package/lib/utils/useLocalPathname.d.ts.map +1 -1
- package/lib/utils/useLocalPathname.js +6 -4
- package/lib/utils/useLocalPathname.js.map +1 -1
- package/lib/utils/useLocationChange.d.ts +7 -5
- package/lib/utils/useLocationChange.d.ts.map +1 -1
- package/lib/utils/useLocationChange.js +6 -2
- package/lib/utils/useLocationChange.js.map +1 -1
- package/lib/utils/usePluralForm.d.ts +11 -0
- package/lib/utils/usePluralForm.d.ts.map +1 -1
- package/lib/utils/usePluralForm.js +19 -24
- package/lib/utils/usePluralForm.js.map +1 -1
- package/lib/utils/useThemeConfig.d.ts +28 -22
- package/lib/utils/useThemeConfig.d.ts.map +1 -1
- package/lib/utils/useThemeConfig.js +3 -0
- package/lib/utils/useThemeConfig.js.map +1 -1
- package/package.json +18 -11
- package/src/components/Collapsible/index.tsx +42 -24
- package/src/components/Details/index.tsx +15 -8
- package/src/components/Details/styles.module.css +4 -0
- package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +20 -22
- package/src/contexts/colorMode.tsx +199 -0
- package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
- package/src/contexts/docsPreferredVersion.tsx +253 -0
- package/src/contexts/docsSidebar.tsx +50 -0
- package/src/contexts/docsVersion.tsx +36 -0
- package/src/contexts/navbarMobileSidebar.tsx +99 -0
- package/src/contexts/navbarSecondaryMenu/content.tsx +110 -0
- package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
- package/src/{utils/tabGroupChoiceUtils.tsx → contexts/tabGroupChoice.tsx} +28 -33
- package/src/hooks/useBackToTopButton.ts +73 -0
- package/src/hooks/useCodeWordWrap.ts +56 -0
- package/src/hooks/useHideableNavbar.ts +12 -14
- package/src/hooks/useKeyboardNavigation.ts +11 -3
- package/src/hooks/useLockBodyScroll.ts +5 -2
- package/src/hooks/usePrismTheme.ts +10 -6
- package/src/hooks/useSearchPage.ts +18 -5
- package/src/hooks/useSkipToContent.ts +58 -0
- package/src/{utils → hooks}/useTOCHighlight.ts +24 -15
- package/src/hooks/useWindowSize.ts +14 -12
- package/src/index.ts +77 -57
- package/src/utils/ThemeClassNames.ts +10 -6
- package/src/utils/codeBlockUtils.ts +178 -92
- package/src/utils/docsUtils.tsx +205 -101
- package/src/utils/footerUtils.ts +18 -0
- package/src/utils/generalUtils.ts +6 -3
- package/src/utils/historyUtils.ts +11 -17
- package/src/utils/jsUtils.ts +1 -1
- package/src/utils/metadataUtils.tsx +115 -0
- package/src/utils/navbarUtils.tsx +45 -0
- package/src/utils/reactUtils.tsx +40 -7
- package/src/utils/regexpUtils.ts +2 -1
- package/src/utils/routesUtils.ts +58 -22
- package/src/utils/scrollUtils.tsx +120 -49
- package/src/utils/searchUtils.ts +51 -0
- package/src/utils/storageUtils.ts +23 -15
- package/src/utils/tagsUtils.ts +12 -10
- package/src/utils/tocUtils.ts +22 -11
- package/src/utils/useAlternatePageUtils.ts +17 -5
- package/src/utils/useLocalPathname.ts +6 -4
- package/src/utils/useLocationChange.ts +12 -10
- package/src/utils/usePluralForm.ts +27 -24
- package/src/utils/useThemeConfig.ts +23 -22
- package/yarn-error.log +20199 -0
- package/lib/utils/announcementBarUtils.d.ts.map +0 -1
- package/lib/utils/announcementBarUtils.js.map +0 -1
- package/lib/utils/colorModeUtils.d.ts +0 -18
- package/lib/utils/colorModeUtils.d.ts.map +0 -1
- package/lib/utils/colorModeUtils.js +0 -106
- package/lib/utils/colorModeUtils.js.map +0 -1
- package/lib/utils/docSidebarItemsExpandedState.d.ts +0 -17
- package/lib/utils/docSidebarItemsExpandedState.d.ts.map +0 -1
- package/lib/utils/docSidebarItemsExpandedState.js.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -22
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -91
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -14
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -19
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +0 -1
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -14
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +0 -1
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +0 -1
- package/lib/utils/mobileSecondaryMenu.d.ts +0 -21
- package/lib/utils/mobileSecondaryMenu.d.ts.map +0 -1
- package/lib/utils/mobileSecondaryMenu.js +0 -50
- package/lib/utils/mobileSecondaryMenu.js.map +0 -1
- package/lib/utils/pathUtils.d.ts +0 -8
- package/lib/utils/pathUtils.d.ts.map +0 -1
- package/lib/utils/pathUtils.js +0 -17
- package/lib/utils/pathUtils.js.map +0 -1
- package/lib/utils/tabGroupChoiceUtils.d.ts.map +0 -1
- package/lib/utils/tabGroupChoiceUtils.js.map +0 -1
- package/lib/utils/useContextualSearchFilters.d.ts +0 -12
- package/lib/utils/useContextualSearchFilters.d.ts.map +0 -1
- package/lib/utils/useContextualSearchFilters.js +0 -37
- package/lib/utils/useContextualSearchFilters.js.map +0 -1
- package/lib/utils/usePrevious.d.ts +0 -8
- package/lib/utils/usePrevious.d.ts.map +0 -1
- package/lib/utils/usePrevious.js +0 -16
- package/lib/utils/usePrevious.js.map +0 -1
- package/lib/utils/useTOCHighlight.d.ts +0 -14
- package/lib/utils/useTOCHighlight.d.ts.map +0 -1
- package/lib/utils/useTOCHighlight.js.map +0 -1
- package/src/utils/colorModeUtils.tsx +0 -156
- package/src/utils/docSidebarItemsExpandedState.tsx +0 -41
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -167
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -33
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -70
- package/src/utils/mobileSecondaryMenu.tsx +0 -115
- package/src/utils/pathUtils.ts +0 -19
- package/src/utils/useContextualSearchFilters.ts +0 -53
- 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
|
|
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
|
|
23
|
-
js: {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
},
|
|
27
|
-
|
|
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
|
-
|
|
46
|
-
'highlight-next-line',
|
|
47
|
-
'highlight-start',
|
|
48
|
-
'highlight-end',
|
|
49
|
-
];
|
|
24
|
+
type CommentType = keyof typeof commentPatterns;
|
|
50
25
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
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
|
-
//
|
|
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
|
-
|
|
66
|
-
|
|
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
|
|
61
|
+
return getCommentPattern(['js', 'jsBlock'], magicCommentDirectives);
|
|
73
62
|
|
|
74
63
|
case 'jsx':
|
|
75
64
|
case 'tsx':
|
|
76
|
-
return
|
|
65
|
+
return getCommentPattern(
|
|
66
|
+
['js', 'jsBlock', 'jsx'],
|
|
67
|
+
magicCommentDirectives,
|
|
68
|
+
);
|
|
77
69
|
|
|
78
70
|
case 'html':
|
|
79
|
-
return
|
|
71
|
+
return getCommentPattern(
|
|
72
|
+
['js', 'jsBlock', 'html'],
|
|
73
|
+
magicCommentDirectives,
|
|
74
|
+
);
|
|
80
75
|
|
|
81
76
|
case 'python':
|
|
82
77
|
case 'py':
|
|
83
|
-
|
|
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
|
-
//
|
|
87
|
-
return
|
|
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
|
-
*
|
|
104
|
-
*
|
|
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
|
-
|
|
109
|
-
|
|
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
|
-
|
|
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 &&
|
|
117
|
-
const
|
|
118
|
-
.
|
|
119
|
-
|
|
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 {
|
|
174
|
+
.map((n) => [n - 1, [metastringRangeClassName]]);
|
|
175
|
+
return {lineClassNames: Object.fromEntries(lines), code};
|
|
123
176
|
}
|
|
124
177
|
if (language === undefined) {
|
|
125
|
-
return {
|
|
178
|
+
return {lineClassNames: {}, code};
|
|
126
179
|
}
|
|
127
|
-
const directiveRegex =
|
|
128
|
-
|
|
180
|
+
const directiveRegex = getAllMagicCommentDirectiveStyles(
|
|
181
|
+
language,
|
|
182
|
+
magicComments,
|
|
183
|
+
);
|
|
184
|
+
// Go through line by line
|
|
129
185
|
const lines = code.split('\n');
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
|
137
|
-
|
|
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
|
-
|
|
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
|
}
|