@docusaurus/theme-common 0.0.0-4523 → 0.0.0-4524
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/lib/components/Collapsible/index.d.ts.map +1 -1
- package/lib/components/Collapsible/index.js +9 -5
- package/lib/components/Collapsible/index.js.map +1 -1
- package/lib/components/Details/index.js +1 -1
- package/lib/components/Details/index.js.map +1 -1
- package/lib/hooks/useWindowSize.d.ts.map +1 -1
- package/lib/hooks/useWindowSize.js +4 -2
- package/lib/hooks/useWindowSize.js.map +1 -1
- package/lib/utils/ThemeClassNames.d.ts.map +1 -1
- package/lib/utils/ThemeClassNames.js +2 -1
- package/lib/utils/ThemeClassNames.js.map +1 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +1 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +3 -7
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +1 -1
- package/lib/utils/docsUtils.d.ts.map +1 -1
- package/lib/utils/docsUtils.js +4 -6
- package/lib/utils/docsUtils.js.map +1 -1
- package/lib/utils/historyUtils.d.ts +11 -0
- package/lib/utils/historyUtils.d.ts.map +1 -1
- package/lib/utils/historyUtils.js +9 -8
- package/lib/utils/historyUtils.js.map +1 -1
- package/lib/utils/jsUtils.d.ts +5 -2
- package/lib/utils/jsUtils.d.ts.map +1 -1
- package/lib/utils/jsUtils.js +5 -2
- package/lib/utils/jsUtils.js.map +1 -1
- package/lib/utils/mobileSecondaryMenu.d.ts.map +1 -1
- package/lib/utils/mobileSecondaryMenu.js.map +1 -1
- package/lib/utils/reactUtils.d.ts +18 -0
- package/lib/utils/reactUtils.d.ts.map +1 -1
- package/lib/utils/reactUtils.js +20 -11
- package/lib/utils/reactUtils.js.map +1 -1
- package/lib/utils/regexpUtils.d.ts +1 -1
- package/lib/utils/regexpUtils.js +1 -1
- package/lib/utils/storageUtils.d.ts +4 -4
- package/lib/utils/storageUtils.d.ts.map +1 -1
- package/lib/utils/storageUtils.js +12 -14
- package/lib/utils/storageUtils.js.map +1 -1
- package/lib/utils/tocUtils.d.ts.map +1 -1
- package/lib/utils/tocUtils.js +1 -3
- package/lib/utils/tocUtils.js.map +1 -1
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -1
- package/lib/utils/useAlternatePageUtils.js +2 -1
- package/lib/utils/useAlternatePageUtils.js.map +1 -1
- package/lib/utils/useContextualSearchFilters.js +2 -2
- package/lib/utils/useContextualSearchFilters.js.map +1 -1
- package/lib/utils/usePluralForm.d.ts.map +1 -1
- package/lib/utils/usePluralForm.js +25 -20
- package/lib/utils/usePluralForm.js.map +1 -1
- package/lib/utils/useTOCHighlight.d.ts.map +1 -1
- package/lib/utils/useTOCHighlight.js +20 -17
- package/lib/utils/useTOCHighlight.js.map +1 -1
- package/package.json +8 -8
- package/src/components/Collapsible/index.tsx +14 -9
- package/src/components/Details/index.tsx +1 -1
- package/src/hooks/useWindowSize.ts +4 -2
- package/src/utils/ThemeClassNames.ts +2 -1
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +3 -5
- package/src/utils/docsUtils.tsx +5 -6
- package/src/utils/historyUtils.ts +9 -8
- package/src/utils/jsUtils.ts +5 -2
- package/src/utils/mobileSecondaryMenu.tsx +6 -5
- package/src/utils/reactUtils.tsx +20 -11
- package/src/utils/regexpUtils.ts +1 -1
- package/src/utils/storageUtils.ts +12 -13
- package/src/utils/tocUtils.ts +1 -2
- package/src/utils/useAlternatePageUtils.ts +4 -3
- package/src/utils/useContextualSearchFilters.ts +2 -2
- package/src/utils/usePluralForm.ts +28 -22
- package/src/utils/useTOCHighlight.ts +20 -17
package/src/utils/tocUtils.ts
CHANGED
|
@@ -39,14 +39,15 @@ export function useAlternatePageUtils(): {
|
|
|
39
39
|
: `${baseUrlUnlocalized}${locale}/`;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
// TODO support correct alternate url when localized site is deployed on
|
|
42
|
+
// TODO support correct alternate url when localized site is deployed on
|
|
43
|
+
// another domain
|
|
43
44
|
function createUrl({
|
|
44
45
|
locale,
|
|
45
46
|
fullyQualified,
|
|
46
47
|
}: {
|
|
47
48
|
locale: string;
|
|
48
|
-
// For hreflang SEO headers, we need it to be fully qualified (full
|
|
49
|
-
//
|
|
49
|
+
// For hreflang SEO headers, we need it to be fully qualified (full
|
|
50
|
+
// protocol/domain/path...) or locale dropdown, using a path is good enough
|
|
50
51
|
fullyQualified: boolean;
|
|
51
52
|
}) {
|
|
52
53
|
return `${fullyQualified ? url : ''}${getLocalizedBaseUrl(
|
|
@@ -18,8 +18,8 @@ export type useContextualSearchFiltersReturns = {
|
|
|
18
18
|
tags: string[];
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
// We may want to support multiple search engines, don't couple that to
|
|
22
|
-
// Maybe users
|
|
21
|
+
// We may want to support multiple search engines, don't couple that to
|
|
22
|
+
// Algolia/DocSearch. Maybe users want to use their own search engine solution
|
|
23
23
|
export function useContextualSearchFilters(): useContextualSearchFiltersReturns {
|
|
24
24
|
const {i18n} = useDocusaurusContext();
|
|
25
25
|
const allDocsData = useAllDocsData();
|
|
@@ -49,23 +49,29 @@ function createLocalePluralForms(locale: string): LocalePluralForms {
|
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Poor man's PluralSelector implementation, using an english fallback. We want
|
|
54
|
+
* a lightweight, future-proof and good-enough solution. We don't want a perfect
|
|
55
|
+
* and heavy solution.
|
|
56
|
+
*
|
|
57
|
+
* Docusaurus classic theme has only 2 deeply nested labels requiring complex
|
|
58
|
+
* plural rules. We don't want to use Intl + PluralRules polyfills + full ICU
|
|
59
|
+
* syntax (react-intl) just for that.
|
|
60
|
+
*
|
|
61
|
+
* Notes:
|
|
62
|
+
* - 2021: 92+% Browsers support Intl.PluralRules, and support will increase in
|
|
63
|
+
* the future
|
|
64
|
+
* - NodeJS >= 13 has full ICU support by default
|
|
65
|
+
* - In case of "mismatch" between SSR and Browser ICU support, React keeps
|
|
66
|
+
* working!
|
|
67
|
+
*/
|
|
63
68
|
function useLocalePluralForms(): LocalePluralForms {
|
|
64
69
|
const {
|
|
65
70
|
i18n: {currentLocale},
|
|
66
71
|
} = useDocusaurusContext();
|
|
67
72
|
return useMemo(() => {
|
|
68
|
-
// @ts-expect-error checking Intl.PluralRules in case browser doesn't
|
|
73
|
+
// @ts-expect-error checking Intl.PluralRules in case browser doesn't
|
|
74
|
+
// have it (e.g Safari 12-)
|
|
69
75
|
if (Intl.PluralRules) {
|
|
70
76
|
try {
|
|
71
77
|
return createLocalePluralForms(currentLocale);
|
|
@@ -94,17 +100,17 @@ function selectPluralMessage(
|
|
|
94
100
|
|
|
95
101
|
if (parts.length === 1) {
|
|
96
102
|
return parts[0];
|
|
97
|
-
} else {
|
|
98
|
-
if (parts.length > localePluralForms.pluralForms.length) {
|
|
99
|
-
console.error(
|
|
100
|
-
`For locale=${localePluralForms.locale}, a maximum of ${localePluralForms.pluralForms.length} plural forms are expected (${localePluralForms.pluralForms}), but the message contains ${parts.length} plural forms: ${pluralMessages} `,
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
const pluralForm = localePluralForms.select(count);
|
|
104
|
-
const pluralFormIndex = localePluralForms.pluralForms.indexOf(pluralForm);
|
|
105
|
-
// In case of not enough plural form messages, we take the last one (other) instead of returning undefined
|
|
106
|
-
return parts[Math.min(pluralFormIndex, parts.length - 1)];
|
|
107
103
|
}
|
|
104
|
+
if (parts.length > localePluralForms.pluralForms.length) {
|
|
105
|
+
console.error(
|
|
106
|
+
`For locale=${localePluralForms.locale}, a maximum of ${localePluralForms.pluralForms.length} plural forms are expected (${localePluralForms.pluralForms}), but the message contains ${parts.length} plural forms: ${pluralMessages} `,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
const pluralForm = localePluralForms.select(count);
|
|
110
|
+
const pluralFormIndex = localePluralForms.pluralForms.indexOf(pluralForm);
|
|
111
|
+
// In case of not enough plural form messages, we take the last one (other)
|
|
112
|
+
// instead of returning undefined
|
|
113
|
+
return parts[Math.min(pluralFormIndex, parts.length - 1)];
|
|
108
114
|
}
|
|
109
115
|
|
|
110
116
|
export function usePluralForm(): {
|
|
@@ -13,7 +13,8 @@ TODO make the hardcoded theme-classic classnames configurable
|
|
|
13
13
|
(or add them to ThemeClassNames?)
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
// If the anchor has no height and is just a "marker" in the dom; we'll use the
|
|
16
|
+
// If the anchor has no height and is just a "marker" in the dom; we'll use the
|
|
17
|
+
// parent (normally the link text) rect boundaries instead
|
|
17
18
|
function getVisibleBoundingClientRect(element: HTMLElement): DOMRect {
|
|
18
19
|
const rect = element.getBoundingClientRect();
|
|
19
20
|
const hasNoHeight = rect.top === rect.bottom;
|
|
@@ -23,8 +24,10 @@ function getVisibleBoundingClientRect(element: HTMLElement): DOMRect {
|
|
|
23
24
|
return rect;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Considering we divide viewport into 2 zones of each 50vh, this returns true
|
|
29
|
+
* if an element is in the first zone (ie, appear in viewport, near the top)
|
|
30
|
+
*/
|
|
28
31
|
function isInViewportTopHalf(boundingRect: DOMRect) {
|
|
29
32
|
return boundingRect.top > 0 && boundingRect.bottom < window.innerHeight / 2;
|
|
30
33
|
}
|
|
@@ -54,9 +57,10 @@ function getActiveAnchor(
|
|
|
54
57
|
anchorTopOffset: number;
|
|
55
58
|
},
|
|
56
59
|
): Element | null {
|
|
57
|
-
// Naming is hard
|
|
58
|
-
//
|
|
59
|
-
//
|
|
60
|
+
// Naming is hard: The "nextVisibleAnchor" is the first anchor that appear
|
|
61
|
+
// under the viewport top boundary. It does not mean this anchor is visible
|
|
62
|
+
// yet, but if user continues scrolling down, it will be the first to become
|
|
63
|
+
// visible
|
|
60
64
|
const nextVisibleAnchor = anchors.find((anchor) => {
|
|
61
65
|
const boundingRect = getVisibleBoundingClientRect(anchor);
|
|
62
66
|
return boundingRect.top >= anchorTopOffset;
|
|
@@ -64,23 +68,22 @@ function getActiveAnchor(
|
|
|
64
68
|
|
|
65
69
|
if (nextVisibleAnchor) {
|
|
66
70
|
const boundingRect = getVisibleBoundingClientRect(nextVisibleAnchor);
|
|
67
|
-
// If anchor is in the top half of the viewport: it is the one we consider
|
|
68
|
-
// (unless it's too close to the top and and soon to be scrolled
|
|
71
|
+
// If anchor is in the top half of the viewport: it is the one we consider
|
|
72
|
+
// "active" (unless it's too close to the top and and soon to be scrolled
|
|
73
|
+
// outside viewport)
|
|
69
74
|
if (isInViewportTopHalf(boundingRect)) {
|
|
70
75
|
return nextVisibleAnchor;
|
|
71
76
|
}
|
|
72
|
-
// If anchor is in the bottom half of the viewport, or under the viewport,
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
// If anchor is in the bottom half of the viewport, or under the viewport,
|
|
78
|
+
// we consider the active anchor is the previous one. This is because the
|
|
79
|
+
// main text appearing in the user screen mostly belong to the previous
|
|
80
|
+
// anchor. Returns null for the first anchor, see
|
|
81
|
+
// https://github.com/facebook/docusaurus/issues/5318
|
|
82
|
+
return anchors[anchors.indexOf(nextVisibleAnchor) - 1] ?? null;
|
|
78
83
|
}
|
|
79
84
|
// no anchor under viewport top? (ie we are at the bottom of the page)
|
|
80
85
|
// => highlight the last anchor found
|
|
81
|
-
|
|
82
|
-
return anchors[anchors.length - 1];
|
|
83
|
-
}
|
|
86
|
+
return anchors[anchors.length - 1];
|
|
84
87
|
}
|
|
85
88
|
|
|
86
89
|
function getLinkAnchorValue(link: HTMLAnchorElement): string {
|