@astrojs/starlight 0.41.10 → 0.42.0
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/dist/_virtual/_rolldown/runtime.js +13 -0
- package/dist/components/MobileMenuToggle.astro +65 -0
- package/{components → dist/components}/PageFrame.astro +32 -8
- package/dist/components-internals/Icons.d.ts +415 -0
- package/dist/components-internals/Icons.js +140 -0
- package/dist/components-internals/SidebarPersistState.d.ts +1 -0
- package/dist/components-internals/SidebarPersistState.js +53 -0
- package/dist/components-internals/TableOfContents/starlight-toc.d.ts +18 -0
- package/dist/components-internals/TableOfContents/starlight-toc.js +88 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +4 -0
- package/{expressive-code.d.ts → dist/expressive-code.d.ts} +6 -13
- package/dist/expressive-code.js +31 -0
- package/dist/global.d.ts +62 -0
- package/dist/global.js +2 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +109 -0
- package/dist/integrations/aside-utils.d.ts +9 -0
- package/dist/integrations/aside-utils.js +35 -0
- package/dist/integrations/expressive-code/hast.d.ts +1 -0
- package/dist/integrations/expressive-code/hast.js +2 -0
- package/dist/integrations/expressive-code/index.d.ts +60 -0
- package/dist/integrations/expressive-code/index.js +48 -0
- package/dist/integrations/expressive-code/preprocessor.d.ts +17 -0
- package/dist/integrations/expressive-code/preprocessor.js +76 -0
- package/dist/integrations/expressive-code/themes/night-owl-dark.js +4 -0
- package/dist/integrations/expressive-code/themes/night-owl-light.js +4 -0
- package/dist/integrations/expressive-code/theming.d.ts +16 -0
- package/dist/integrations/expressive-code/theming.js +71 -0
- package/dist/integrations/expressive-code/translations.d.ts +8 -0
- package/dist/integrations/expressive-code/translations.js +26 -0
- package/dist/integrations/markdown-icon.d.ts +8 -0
- package/dist/integrations/markdown-icon.js +14 -0
- package/dist/integrations/markdown-plugins.d.ts +18 -0
- package/dist/integrations/markdown-plugins.js +36 -0
- package/dist/integrations/markdown-processor.d.ts +29 -0
- package/dist/integrations/markdown-processor.js +44 -0
- package/dist/integrations/pagefind.d.ts +7 -0
- package/dist/integrations/pagefind.js +34 -0
- package/dist/integrations/rehype-code-rtl-support.d.ts +21 -0
- package/dist/integrations/rehype-code-rtl-support.js +36 -0
- package/dist/integrations/rehype-heading-links.d.ts +10 -0
- package/dist/integrations/rehype-heading-links.js +57 -0
- package/dist/integrations/remark-asides.d.ts +32 -0
- package/dist/integrations/remark-asides.js +166 -0
- package/dist/integrations/remark-rehype.d.ts +17 -0
- package/dist/integrations/remark-rehype.js +51 -0
- package/dist/integrations/satteri.d.ts +13 -0
- package/dist/integrations/satteri.js +209 -0
- package/dist/integrations/shared/absolutePathToLang.d.ts +10 -0
- package/dist/integrations/shared/absolutePathToLang.js +13 -0
- package/dist/integrations/shared/localeToLang.d.ts +10 -0
- package/dist/integrations/shared/localeToLang.js +13 -0
- package/dist/integrations/shared/slugToLocale.d.ts +12 -0
- package/dist/integrations/shared/slugToLocale.js +15 -0
- package/dist/integrations/sitemap.d.ts +12 -0
- package/dist/integrations/sitemap.js +19 -0
- package/dist/integrations/vite-layer-order.d.ts +15 -0
- package/dist/integrations/vite-layer-order.js +56 -0
- package/dist/integrations/vite-lazy-barrel-optimization.d.ts +12 -0
- package/dist/integrations/vite-lazy-barrel-optimization.js +33 -0
- package/dist/integrations/vite-virtual-modules.d.ts +14 -0
- package/dist/integrations/vite-virtual-modules.js +108 -0
- package/dist/internal.d.ts +3 -0
- package/dist/internal.js +3 -0
- package/dist/loaders.d.ts +20 -0
- package/dist/loaders.js +49 -0
- package/dist/locals.d.ts +15 -0
- package/dist/locals.js +33 -0
- package/dist/package.js +4 -0
- package/{props.ts → dist/props.d.ts} +6 -4
- package/dist/props.js +1 -0
- package/dist/route-data.d.ts +7 -0
- package/dist/route-data.js +6 -0
- package/dist/schema.d.ts +426 -0
- package/dist/schema.js +103 -0
- package/dist/schemas/badge.d.ts +86 -0
- package/dist/schemas/badge.js +39 -0
- package/dist/schemas/components.d.ts +236 -0
- package/dist/schemas/components.js +36 -0
- package/dist/schemas/expressiveCode.d.ts +7 -0
- package/dist/schemas/expressiveCode.js +5 -0
- package/dist/schemas/favicon.d.ts +9 -0
- package/dist/schemas/favicon.js +32 -0
- package/dist/schemas/head.d.ts +33 -0
- package/dist/schemas/head.js +41 -0
- package/dist/schemas/hero.d.ts +319 -0
- package/dist/schemas/hero.js +73 -0
- package/dist/schemas/i18n.d.ts +125 -0
- package/dist/schemas/i18n.js +76 -0
- package/dist/schemas/icon.d.ts +284 -0
- package/dist/schemas/icon.js +7 -0
- package/dist/schemas/locale.d.ts +20 -0
- package/dist/schemas/locale.js +11 -0
- package/dist/schemas/logo.d.ts +32 -0
- package/dist/schemas/logo.js +14 -0
- package/dist/schemas/pagefind.d.ts +166 -0
- package/dist/schemas/pagefind.js +31 -0
- package/dist/schemas/prevNextLink.d.ts +10 -0
- package/dist/schemas/prevNextLink.js +14 -0
- package/dist/schemas/sidebar.d.ts +205 -0
- package/dist/schemas/sidebar.js +71 -0
- package/dist/schemas/site-title.d.ts +7 -0
- package/dist/schemas/site-title.js +17 -0
- package/dist/schemas/social.d.ts +292 -0
- package/dist/schemas/social.js +18 -0
- package/dist/schemas/tableOfContents.d.ts +24 -0
- package/dist/schemas/tableOfContents.js +16 -0
- package/{style → dist/style}/anchor-links.css +13 -1
- package/dist/translations/ar.js +36 -0
- package/dist/translations/ca.js +46 -0
- package/dist/translations/cs.js +46 -0
- package/dist/translations/da.js +33 -0
- package/dist/translations/de.js +33 -0
- package/dist/translations/el.js +33 -0
- package/dist/translations/en.js +33 -0
- package/dist/translations/es.js +46 -0
- package/dist/translations/fa.js +33 -0
- package/dist/translations/fi.js +33 -0
- package/dist/translations/fr.js +36 -0
- package/dist/translations/gl.js +46 -0
- package/dist/translations/he.js +33 -0
- package/dist/translations/hi.js +36 -0
- package/dist/translations/hu.js +46 -0
- package/dist/translations/id.js +33 -0
- package/dist/translations/index.d.ts +48 -0
- package/dist/translations/index.js +75 -0
- package/dist/translations/it.js +36 -0
- package/dist/translations/ja.js +36 -0
- package/dist/translations/ko.js +36 -0
- package/dist/translations/lv.js +33 -0
- package/dist/translations/nb.js +33 -0
- package/dist/translations/nl.js +33 -0
- package/dist/translations/pl.js +36 -0
- package/dist/translations/pt.js +36 -0
- package/dist/translations/ro.js +33 -0
- package/dist/translations/ru.js +36 -0
- package/dist/translations/sk.js +33 -0
- package/dist/translations/sv.js +33 -0
- package/dist/translations/th.js +33 -0
- package/dist/translations/tr.js +33 -0
- package/dist/translations/uk.js +33 -0
- package/dist/translations/vi.js +33 -0
- package/dist/translations/zh-CN.js +36 -0
- package/dist/translations/zh-TW.js +36 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.js +1 -0
- package/{user-components → dist/user-components}/FileTree.astro +1 -1
- package/{user-components → dist/user-components}/Steps.astro +1 -1
- package/{user-components → dist/user-components}/TabItem.astro +1 -1
- package/{user-components → dist/user-components}/Tabs.astro +1 -1
- package/dist/user-components/file-tree-icons.d.ts +178 -0
- package/dist/user-components/file-tree-icons.js +632 -0
- package/dist/user-components/file-tree-processor.d.ts +16 -0
- package/dist/user-components/file-tree-processor.js +132 -0
- package/dist/user-components/steps-processor.d.ts +10 -0
- package/dist/user-components/steps-processor.js +39 -0
- package/dist/user-components/tabs-processor.d.ts +23 -0
- package/dist/user-components/tabs-processor.js +70 -0
- package/dist/utils/base.d.ts +7 -0
- package/dist/utils/base.js +15 -0
- package/dist/utils/canonical.d.ts +10 -0
- package/dist/utils/canonical.js +14 -0
- package/dist/utils/collection-fs.d.ts +13 -0
- package/dist/utils/collection-fs.js +19 -0
- package/{utils/collection.ts → dist/utils/collection.d.ts} +9 -20
- package/dist/utils/collection.js +26 -0
- package/dist/utils/createPathFormatter.d.ts +9 -0
- package/dist/utils/createPathFormatter.js +35 -0
- package/dist/utils/createTranslationSystem.d.ts +23 -0
- package/dist/utils/createTranslationSystem.js +84 -0
- package/dist/utils/error-map.d.ts +23 -0
- package/dist/utils/error-map.js +125 -0
- package/dist/utils/format-path.d.ts +4 -0
- package/dist/utils/format-path.js +9 -0
- package/dist/utils/generateToC.d.ts +14 -0
- package/dist/utils/generateToC.js +25 -0
- package/dist/utils/git.d.ts +12 -0
- package/dist/utils/git.js +71 -0
- package/dist/utils/gitInlined.d.ts +6 -0
- package/dist/utils/gitInlined.js +11 -0
- package/dist/utils/head.d.ts +7 -0
- package/dist/utils/head.js +233 -0
- package/dist/utils/i18n.d.ts +50 -0
- package/dist/utils/i18n.js +141 -0
- package/dist/utils/localizedUrl.d.ts +8 -0
- package/dist/utils/localizedUrl.js +28 -0
- package/dist/utils/navigation.d.ts +19 -0
- package/dist/utils/navigation.js +353 -0
- package/dist/utils/path.d.ts +21 -0
- package/dist/utils/path.js +51 -0
- package/dist/utils/plugins.d.ts +721 -0
- package/dist/utils/plugins.js +174 -0
- package/dist/utils/routing/data.d.ts +20 -0
- package/dist/utils/routing/data.js +127 -0
- package/dist/utils/routing/index.d.ts +21 -0
- package/dist/utils/routing/index.js +106 -0
- package/dist/utils/routing/middleware.d.ts +11 -0
- package/dist/utils/routing/middleware.js +65 -0
- package/dist/utils/routing/types.d.ts +93 -0
- package/dist/utils/routing/types.js +1 -0
- package/dist/utils/slugs.d.ts +45 -0
- package/dist/utils/slugs.js +104 -0
- package/dist/utils/starlight-page.d.ts +36 -0
- package/dist/utils/starlight-page.js +120 -0
- package/dist/utils/translations-fs.d.ts +15 -0
- package/dist/utils/translations-fs.js +39 -0
- package/dist/utils/translations.d.ts +20 -0
- package/dist/utils/translations.js +29 -0
- package/dist/utils/types.d.ts +5 -0
- package/dist/utils/types.js +1 -0
- package/dist/utils/url.d.ts +7 -0
- package/{utils/url.ts → dist/utils/url.js} +5 -4
- package/dist/utils/user-config.d.ts +2055 -0
- package/dist/utils/user-config.js +136 -0
- package/dist/utils/validateLogoImports.d.ts +5 -0
- package/dist/utils/validateLogoImports.js +16 -0
- package/dist/utils/zodDeepMerge.d.ts +16 -0
- package/dist/utils/zodDeepMerge.js +53 -0
- package/package.json +62 -33
- package/CHANGELOG.md +0 -3043
- package/components/MobileMenuToggle.astro +0 -114
- package/components-internals/Icons.ts +0 -248
- package/components-internals/SidebarPersistState.ts +0 -71
- package/components-internals/TableOfContents/starlight-toc.ts +0 -127
- package/constants.ts +0 -4
- package/expressive-code.mjs +0 -23
- package/global.d.ts +0 -11
- package/i18n.d.ts +0 -18
- package/index.ts +0 -200
- package/integrations/aside-utils.ts +0 -41
- package/integrations/expressive-code/hast.d.ts +0 -5
- package/integrations/expressive-code/hast.mjs +0 -9
- package/integrations/expressive-code/index.ts +0 -125
- package/integrations/expressive-code/preprocessor.ts +0 -126
- package/integrations/expressive-code/themes/night-owl-dark.jsonc +0 -1796
- package/integrations/expressive-code/themes/night-owl-light.jsonc +0 -1695
- package/integrations/expressive-code/theming.ts +0 -105
- package/integrations/expressive-code/translations.ts +0 -36
- package/integrations/markdown-icon.ts +0 -13
- package/integrations/markdown-plugins.ts +0 -71
- package/integrations/markdown-processor.ts +0 -64
- package/integrations/pagefind.ts +0 -60
- package/integrations/rehype-code-rtl-support.ts +0 -37
- package/integrations/rehype-heading-links.ts +0 -79
- package/integrations/remark-asides.ts +0 -219
- package/integrations/remark-rehype.ts +0 -87
- package/integrations/satteri.ts +0 -265
- package/integrations/shared/absolutePathToLang.ts +0 -26
- package/integrations/shared/localeToLang.ts +0 -15
- package/integrations/shared/slugToLocale.ts +0 -18
- package/integrations/sitemap.ts +0 -23
- package/integrations/vite-layer-order.ts +0 -69
- package/integrations/vite-lazy-barrel-optimization.ts +0 -40
- package/integrations/vite-virtual-modules.ts +0 -175
- package/internal.ts +0 -6
- package/loaders.ts +0 -63
- package/locals.d.ts +0 -43
- package/locals.ts +0 -43
- package/route-data.ts +0 -11
- package/schema.ts +0 -167
- package/schemas/badge.ts +0 -40
- package/schemas/components.ts +0 -265
- package/schemas/expressiveCode.ts +0 -12
- package/schemas/favicon.ts +0 -40
- package/schemas/head.ts +0 -50
- package/schemas/hero.ts +0 -69
- package/schemas/i18n.ts +0 -244
- package/schemas/icon.ts +0 -7
- package/schemas/logo.ts +0 -28
- package/schemas/pagefind.ts +0 -128
- package/schemas/prevNextLink.ts +0 -18
- package/schemas/sidebar.ts +0 -163
- package/schemas/site-title.ts +0 -20
- package/schemas/social.ts +0 -23
- package/schemas/tableOfContents.ts +0 -21
- package/translations/README.md +0 -14
- package/translations/ar.json +0 -33
- package/translations/ca.json +0 -43
- package/translations/cs.json +0 -43
- package/translations/da.json +0 -30
- package/translations/de.json +0 -30
- package/translations/el.json +0 -30
- package/translations/en.json +0 -30
- package/translations/es.json +0 -43
- package/translations/fa.json +0 -30
- package/translations/fi.json +0 -30
- package/translations/fr.json +0 -33
- package/translations/gl.json +0 -43
- package/translations/he.json +0 -30
- package/translations/hi.json +0 -33
- package/translations/hu.json +0 -43
- package/translations/id.json +0 -30
- package/translations/index.ts +0 -76
- package/translations/it.json +0 -33
- package/translations/ja.json +0 -33
- package/translations/ko.json +0 -33
- package/translations/lv.json +0 -30
- package/translations/nb.json +0 -30
- package/translations/nl.json +0 -30
- package/translations/pl.json +0 -33
- package/translations/pt.json +0 -33
- package/translations/ro.json +0 -30
- package/translations/ru.json +0 -33
- package/translations/sk.json +0 -30
- package/translations/sv.json +0 -30
- package/translations/th.json +0 -30
- package/translations/tr.json +0 -30
- package/translations/uk.json +0 -30
- package/translations/vi.json +0 -30
- package/translations/zh-CN.json +0 -33
- package/translations/zh-TW.json +0 -33
- package/types.ts +0 -7
- package/user-components/file-tree-icons.ts +0 -782
- package/user-components/rehype-file-tree.ts +0 -251
- package/user-components/rehype-steps.ts +0 -85
- package/user-components/rehype-tabs.ts +0 -121
- package/utils/base.ts +0 -15
- package/utils/canonical.ts +0 -19
- package/utils/collection-fs.ts +0 -20
- package/utils/createPathFormatter.ts +0 -60
- package/utils/createTranslationSystem.ts +0 -137
- package/utils/error-map.ts +0 -222
- package/utils/format-path.ts +0 -7
- package/utils/generateToC.ts +0 -33
- package/utils/git.ts +0 -121
- package/utils/gitInlined.ts +0 -20
- package/utils/head.ts +0 -235
- package/utils/i18n.ts +0 -221
- package/utils/localizedUrl.ts +0 -51
- package/utils/navigation.ts +0 -566
- package/utils/path.ts +0 -58
- package/utils/plugins.ts +0 -498
- package/utils/routing/data.ts +0 -154
- package/utils/routing/index.ts +0 -135
- package/utils/routing/middleware.ts +0 -81
- package/utils/routing/types.ts +0 -101
- package/utils/slugs.ts +0 -119
- package/utils/starlight-page.ts +0 -205
- package/utils/translations-fs.ts +0 -54
- package/utils/translations.ts +0 -57
- package/utils/types.ts +0 -15
- package/utils/user-config.ts +0 -348
- package/utils/validateLogoImports.ts +0 -21
- package/utils/zodDeepMerge.ts +0 -144
- package/virtual-internal.d.ts +0 -149
- package/virtual.d.ts +0 -27
- /package/{components → dist/components}/AnchorHeading.astro +0 -0
- /package/{components → dist/components}/Banner.astro +0 -0
- /package/{components → dist/components}/ContentNotice.astro +0 -0
- /package/{components → dist/components}/ContentPanel.astro +0 -0
- /package/{components → dist/components}/DraftContentNotice.astro +0 -0
- /package/{components → dist/components}/EditLink.astro +0 -0
- /package/{components → dist/components}/FallbackContentNotice.astro +0 -0
- /package/{components → dist/components}/Footer.astro +0 -0
- /package/{components → dist/components}/Head.astro +0 -0
- /package/{components → dist/components}/Header.astro +0 -0
- /package/{components → dist/components}/Hero.astro +0 -0
- /package/{components → dist/components}/LanguageSelect.astro +0 -0
- /package/{components → dist/components}/LastUpdated.astro +0 -0
- /package/{components → dist/components}/MarkdownContent.astro +0 -0
- /package/{components → dist/components}/MobileMenuFooter.astro +0 -0
- /package/{components → dist/components}/MobileTableOfContents.astro +0 -0
- /package/{components → dist/components}/Page.astro +0 -0
- /package/{components → dist/components}/PageSidebar.astro +0 -0
- /package/{components → dist/components}/PageTitle.astro +0 -0
- /package/{components → dist/components}/Pagination.astro +0 -0
- /package/{components → dist/components}/Search.astro +0 -0
- /package/{components → dist/components}/Select.astro +0 -0
- /package/{components → dist/components}/Sidebar.astro +0 -0
- /package/{components → dist/components}/SidebarPersister.astro +0 -0
- /package/{components → dist/components}/SidebarRestorePoint.astro +0 -0
- /package/{components → dist/components}/SidebarSublist.astro +0 -0
- /package/{components → dist/components}/SiteTitle.astro +0 -0
- /package/{components → dist/components}/SkipLink.astro +0 -0
- /package/{components → dist/components}/SocialIcons.astro +0 -0
- /package/{components → dist/components}/StarlightPage.astro +0 -0
- /package/{components → dist/components}/TableOfContents.astro +0 -0
- /package/{components → dist/components}/ThemeProvider.astro +0 -0
- /package/{components → dist/components}/ThemeSelect.astro +0 -0
- /package/{components → dist/components}/TwoColumnContent.astro +0 -0
- /package/{components-internals → dist/components-internals}/TableOfContents/TableOfContentsList.astro +0 -0
- /package/{components.ts → dist/components.ts} +0 -0
- /package/{routes → dist/routes}/common.astro +0 -0
- /package/{routes → dist/routes}/ssr/404.astro +0 -0
- /package/{routes → dist/routes}/ssr/index.astro +0 -0
- /package/{routes → dist/routes}/static/404.astro +0 -0
- /package/{routes → dist/routes}/static/index.astro +0 -0
- /package/{style → dist/style}/asides.css +0 -0
- /package/{style → dist/style}/layers.css +0 -0
- /package/{style → dist/style}/markdown.css +0 -0
- /package/{style → dist/style}/print.css +0 -0
- /package/{style → dist/style}/props.css +0 -0
- /package/{style → dist/style}/reset.css +0 -0
- /package/{style → dist/style}/util.css +0 -0
- /package/{user-components → dist/user-components}/Aside.astro +0 -0
- /package/{user-components → dist/user-components}/Badge.astro +0 -0
- /package/{user-components → dist/user-components}/Card.astro +0 -0
- /package/{user-components → dist/user-components}/CardGrid.astro +0 -0
- /package/{user-components → dist/user-components}/Icon.astro +0 -0
- /package/{user-components → dist/user-components}/LinkButton.astro +0 -0
- /package/{user-components → dist/user-components}/LinkCard.astro +0 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { getCollectionPosixPath } from "./collection-fs.js";
|
|
2
|
+
import { absolutePathToLang } from "../integrations/shared/absolutePathToLang.js";
|
|
3
|
+
import { parseWithFriendlyErrors } from "./error-map.js";
|
|
4
|
+
import { StarlightConfigSchema } from "./user-config.js";
|
|
5
|
+
import { createTranslationSystemFromFs } from "./translations-fs.js";
|
|
6
|
+
import { AstroError } from "astro/errors";
|
|
7
|
+
import { z } from "astro/zod";
|
|
8
|
+
//#region src/utils/plugins.ts
|
|
9
|
+
/**
|
|
10
|
+
* Runs Starlight plugins in the order that they are configured after validating the user-provided
|
|
11
|
+
* configuration and returns the final validated user config that may have been updated by the
|
|
12
|
+
* plugins and a list of any integrations added by the plugins.
|
|
13
|
+
*/
|
|
14
|
+
async function runPlugins(starlightUserConfig, pluginsUserConfig, context) {
|
|
15
|
+
let userConfig = starlightUserConfig;
|
|
16
|
+
let starlightConfig = parseWithFriendlyErrors(StarlightConfigSchema, userConfig, "Invalid config passed to starlight integration");
|
|
17
|
+
const pluginsConfig = parseWithFriendlyErrors(StarlightPluginsConfigSchema, pluginsUserConfig, "Invalid plugins config passed to starlight integration");
|
|
18
|
+
const pluginTranslations = {};
|
|
19
|
+
const routeMiddlewareConfigs = [];
|
|
20
|
+
for (const { hooks: { "i18n:setup": i18nSetup } } of pluginsConfig) if (i18nSetup) await i18nSetup({ injectTranslations(translations) {
|
|
21
|
+
for (const [locale, localeTranslations] of Object.entries(translations)) {
|
|
22
|
+
pluginTranslations[locale] ??= {};
|
|
23
|
+
Object.assign(pluginTranslations[locale], localeTranslations);
|
|
24
|
+
}
|
|
25
|
+
} });
|
|
26
|
+
const useTranslations = await createTranslationSystemFromFs(starlightConfig, context.config, pluginTranslations);
|
|
27
|
+
function absolutePathToLang$1(path) {
|
|
28
|
+
return absolutePathToLang(path, {
|
|
29
|
+
docsPath: getCollectionPosixPath("docs", context.config.srcDir),
|
|
30
|
+
starlightConfig
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
const integrations = [];
|
|
34
|
+
for (const { name, hooks: { "config:setup": configSetup, setup: deprecatedSetup } } of pluginsConfig) await (configSetup ?? deprecatedSetup)({
|
|
35
|
+
config: pluginsUserConfig ? {
|
|
36
|
+
...userConfig,
|
|
37
|
+
plugins: pluginsUserConfig
|
|
38
|
+
} : userConfig,
|
|
39
|
+
updateConfig(newConfig) {
|
|
40
|
+
if ("plugins" in newConfig) throw new AstroError(`The \`${name}\` plugin tried to update the \`plugins\` config key which is not supported.`);
|
|
41
|
+
if ("routeMiddleware" in newConfig) throw new AstroError(`The \`${name}\` plugin tried to update the \`routeMiddleware\` config key which is not supported.`, "Use the `addRouteMiddleware()` utility instead.\nSee https://starlight.astro.build/reference/plugins/#addroutemiddleware for more details.");
|
|
42
|
+
const mergedUserConfig = {
|
|
43
|
+
...userConfig,
|
|
44
|
+
...newConfig
|
|
45
|
+
};
|
|
46
|
+
const mergedConfig = parseWithFriendlyErrors(StarlightConfigSchema, mergedUserConfig, `Invalid config update provided by the '${name}' plugin`);
|
|
47
|
+
userConfig = mergedUserConfig;
|
|
48
|
+
starlightConfig = mergedConfig;
|
|
49
|
+
},
|
|
50
|
+
addIntegration(integration) {
|
|
51
|
+
integrations.push(integration);
|
|
52
|
+
},
|
|
53
|
+
addRouteMiddleware(middlewareConfig) {
|
|
54
|
+
routeMiddlewareConfigs.push(middlewareConfig);
|
|
55
|
+
},
|
|
56
|
+
astroConfig: {
|
|
57
|
+
...context.config,
|
|
58
|
+
integrations: [...context.config.integrations, ...integrations]
|
|
59
|
+
},
|
|
60
|
+
command: context.command,
|
|
61
|
+
isRestart: context.isRestart,
|
|
62
|
+
logger: context.logger.fork(name),
|
|
63
|
+
useTranslations,
|
|
64
|
+
absolutePathToLang: absolutePathToLang$1
|
|
65
|
+
});
|
|
66
|
+
applyPluginMiddleware(routeMiddlewareConfigs, starlightConfig);
|
|
67
|
+
return {
|
|
68
|
+
integrations,
|
|
69
|
+
starlightConfig,
|
|
70
|
+
pluginTranslations,
|
|
71
|
+
useTranslations,
|
|
72
|
+
absolutePathToLang: absolutePathToLang$1
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** Updates `routeMiddleware` in the Starlight config to add plugin middlewares in the correct order. */
|
|
76
|
+
function applyPluginMiddleware(routeMiddlewareConfigs, starlightConfig) {
|
|
77
|
+
const middlewareBuckets = routeMiddlewareConfigs.reduce((buckets, { entrypoint, order = "default" }) => {
|
|
78
|
+
buckets[order].push(entrypoint);
|
|
79
|
+
return buckets;
|
|
80
|
+
}, {
|
|
81
|
+
pre: [],
|
|
82
|
+
default: [],
|
|
83
|
+
post: []
|
|
84
|
+
});
|
|
85
|
+
starlightConfig.routeMiddleware.unshift(...middlewareBuckets.pre);
|
|
86
|
+
starlightConfig.routeMiddleware.push(...middlewareBuckets.default, ...middlewareBuckets.post);
|
|
87
|
+
}
|
|
88
|
+
function injectPluginTranslationsTypes(translations, injectTypes) {
|
|
89
|
+
const allKeys = /* @__PURE__ */ new Set();
|
|
90
|
+
for (const localeTranslations of Object.values(translations)) for (const key of Object.keys(localeTranslations)) allKeys.add(key);
|
|
91
|
+
if (allKeys.size === 0) return;
|
|
92
|
+
injectTypes({
|
|
93
|
+
filename: "i18n-plugins.d.ts",
|
|
94
|
+
content: `declare namespace StarlightApp {
|
|
95
|
+
type PluginUIStringKeys = {
|
|
96
|
+
${[...allKeys].map((key) => `'${key}': string;`).join("\n ")}
|
|
97
|
+
};
|
|
98
|
+
interface I18n extends PluginUIStringKeys {}
|
|
99
|
+
}`
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
const astroIntegrationSchema = z.object({
|
|
103
|
+
name: z.string(),
|
|
104
|
+
hooks: z.looseObject({}).default({})
|
|
105
|
+
});
|
|
106
|
+
const routeMiddlewareConfigSchema = z.object({
|
|
107
|
+
entrypoint: z.string(),
|
|
108
|
+
order: z.enum([
|
|
109
|
+
"pre",
|
|
110
|
+
"post",
|
|
111
|
+
"default"
|
|
112
|
+
]).default("default")
|
|
113
|
+
});
|
|
114
|
+
const baseStarlightPluginSchema = z.object({ name: z.string() });
|
|
115
|
+
const configSetupHookSchema = z.function({
|
|
116
|
+
input: [z.object({
|
|
117
|
+
config: z.any(),
|
|
118
|
+
updateConfig: z.function({
|
|
119
|
+
input: [z.record(z.string(), z.any())],
|
|
120
|
+
output: z.void()
|
|
121
|
+
}),
|
|
122
|
+
addIntegration: z.function({
|
|
123
|
+
input: [astroIntegrationSchema],
|
|
124
|
+
output: z.void()
|
|
125
|
+
}),
|
|
126
|
+
addRouteMiddleware: z.function({
|
|
127
|
+
input: [routeMiddlewareConfigSchema],
|
|
128
|
+
output: z.void()
|
|
129
|
+
}),
|
|
130
|
+
astroConfig: z.any(),
|
|
131
|
+
command: z.any(),
|
|
132
|
+
isRestart: z.any(),
|
|
133
|
+
logger: z.any(),
|
|
134
|
+
useTranslations: z.any(),
|
|
135
|
+
absolutePathToLang: z.function({
|
|
136
|
+
input: [z.string()],
|
|
137
|
+
output: z.string()
|
|
138
|
+
})
|
|
139
|
+
})],
|
|
140
|
+
output: z.promise(z.void())
|
|
141
|
+
}).optional();
|
|
142
|
+
/**
|
|
143
|
+
* A plugin `config` and `updateConfig` argument are purposely not validated using the Starlight
|
|
144
|
+
* user config schema but properly typed for user convenience because we do not want to run any of
|
|
145
|
+
* the Zod `transform`s used in the user config schema when running plugins.
|
|
146
|
+
*/
|
|
147
|
+
const starlightPluginSchema = z.object({
|
|
148
|
+
...baseStarlightPluginSchema.shape,
|
|
149
|
+
hooks: z.object({
|
|
150
|
+
"i18n:setup": z.function({
|
|
151
|
+
input: [z.object({ injectTranslations: z.function({
|
|
152
|
+
input: [z.record(z.string(), z.record(z.string(), z.string()))],
|
|
153
|
+
output: z.void()
|
|
154
|
+
}) })],
|
|
155
|
+
output: z.promise(z.void())
|
|
156
|
+
}).optional(),
|
|
157
|
+
"config:setup": configSetupHookSchema,
|
|
158
|
+
setup: configSetupHookSchema
|
|
159
|
+
})
|
|
160
|
+
}).superRefine((plugin, ctx) => {
|
|
161
|
+
if (!plugin.hooks["config:setup"] && !plugin.hooks.setup) ctx.issues.push({
|
|
162
|
+
code: "custom",
|
|
163
|
+
message: "A plugin must define at least a `config:setup` hook.",
|
|
164
|
+
input: plugin
|
|
165
|
+
});
|
|
166
|
+
else if (plugin.hooks["config:setup"] && plugin.hooks.setup) ctx.issues.push({
|
|
167
|
+
code: "custom",
|
|
168
|
+
message: "A plugin cannot define both a `config:setup` and `setup` hook. As `setup` is deprecated and will be removed in a future version, consider using `config:setup` instead.",
|
|
169
|
+
input: plugin
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
const StarlightPluginsConfigSchema = z.array(starlightPluginSchema).default([]);
|
|
173
|
+
//#endregion
|
|
174
|
+
export { StarlightPluginsConfigSchema, injectPluginTranslationsTypes, runPlugins };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Route, StarlightRouteData } from "./types.js";
|
|
2
|
+
import { RenderResult } from "astro:content";
|
|
3
|
+
import { APIContext, MarkdownHeading } from "astro";
|
|
4
|
+
//#region src/utils/routing/data.d.ts
|
|
5
|
+
interface PageProps extends Route {
|
|
6
|
+
headings: MarkdownHeading[];
|
|
7
|
+
}
|
|
8
|
+
type RouteDataContext = Pick<APIContext, 'generator' | 'site' | 'url'>;
|
|
9
|
+
declare function getRoute(context: APIContext): Promise<Route>;
|
|
10
|
+
declare function useRouteData(context: APIContext, route: Route, { Content, headings }: RenderResult): StarlightRouteData;
|
|
11
|
+
declare function generateRouteData({ props, context }: {
|
|
12
|
+
props: PageProps;
|
|
13
|
+
context: RouteDataContext;
|
|
14
|
+
}): StarlightRouteData;
|
|
15
|
+
declare function getToC({ entry, lang, headings }: PageProps): any;
|
|
16
|
+
/** Get the site title for a given language. **/
|
|
17
|
+
declare function getSiteTitle(lang: string): string;
|
|
18
|
+
declare function getSiteTitleHref(locale: string | undefined): string;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { PageProps, RouteDataContext, generateRouteData, getRoute, getSiteTitle, getSiteTitleHref, getToC, useRouteData };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { getCollectionPathFromRoot } from "../collection.js";
|
|
2
|
+
import { ensureTrailingSlash } from "../path.js";
|
|
3
|
+
import { BuiltInDefaultLocale } from "../i18n.js";
|
|
4
|
+
import { useTranslations } from "../translations.js";
|
|
5
|
+
import { formatPath } from "../format-path.js";
|
|
6
|
+
import { generateToC } from "../generateToC.js";
|
|
7
|
+
import { getHead } from "../head.js";
|
|
8
|
+
import { getRouteBySlugParam, normalizeCollectionEntry } from "./index.js";
|
|
9
|
+
import { getPrevNextLinks, getSidebar } from "../navigation.js";
|
|
10
|
+
import { getEntry } from "astro:content";
|
|
11
|
+
import config from "virtual:starlight/user-config";
|
|
12
|
+
import project from "virtual:starlight/project-context";
|
|
13
|
+
import { getNewestCommitDate } from "virtual:starlight/git-info";
|
|
14
|
+
//#region src/utils/routing/data.ts
|
|
15
|
+
async function getRoute(context) {
|
|
16
|
+
return "slug" in context.params && getRouteBySlugParam(context.params.slug) || await get404Route(context.locals);
|
|
17
|
+
}
|
|
18
|
+
function useRouteData(context, route, { Content, headings }) {
|
|
19
|
+
return {
|
|
20
|
+
...generateRouteData({
|
|
21
|
+
props: {
|
|
22
|
+
...route,
|
|
23
|
+
headings
|
|
24
|
+
},
|
|
25
|
+
context
|
|
26
|
+
}),
|
|
27
|
+
Content
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function generateRouteData({ props, context }) {
|
|
31
|
+
const { entry, locale, lang } = props;
|
|
32
|
+
const sidebar = getSidebar(context.url.pathname, locale);
|
|
33
|
+
const siteTitle = getSiteTitle(lang);
|
|
34
|
+
return {
|
|
35
|
+
...props,
|
|
36
|
+
siteTitle,
|
|
37
|
+
siteTitleHref: getSiteTitleHref(locale),
|
|
38
|
+
sidebar,
|
|
39
|
+
hasSidebar: entry.data.template !== "splash",
|
|
40
|
+
pagination: getPrevNextLinks(sidebar, config.pagination, entry.data),
|
|
41
|
+
toc: getToC(props),
|
|
42
|
+
lastUpdated: getLastUpdated(props),
|
|
43
|
+
editUrl: getEditUrl(props),
|
|
44
|
+
head: getHead(props, context, siteTitle)
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function getToC({ entry, lang, headings }) {
|
|
48
|
+
const tocConfig = entry.data.template === "splash" ? false : entry.data.tableOfContents !== void 0 ? entry.data.tableOfContents : config.tableOfContents;
|
|
49
|
+
if (!tocConfig) return;
|
|
50
|
+
const t = useTranslations(lang);
|
|
51
|
+
return {
|
|
52
|
+
...tocConfig,
|
|
53
|
+
items: generateToC(headings, {
|
|
54
|
+
...tocConfig,
|
|
55
|
+
title: t("tableOfContents.overview")
|
|
56
|
+
})
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function getLastUpdated({ entry }) {
|
|
60
|
+
const { lastUpdated: frontmatterLastUpdated } = entry.data;
|
|
61
|
+
const { lastUpdated: configLastUpdated } = config;
|
|
62
|
+
if (frontmatterLastUpdated ?? configLastUpdated) try {
|
|
63
|
+
return frontmatterLastUpdated instanceof Date ? frontmatterLastUpdated : getNewestCommitDate(entry.filePath);
|
|
64
|
+
} catch {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function getEditUrl({ entry }) {
|
|
69
|
+
const { editUrl } = entry.data;
|
|
70
|
+
if (editUrl === false) return;
|
|
71
|
+
let url;
|
|
72
|
+
if (typeof editUrl === "string") url = editUrl;
|
|
73
|
+
else if (config.editLink.baseUrl) url = ensureTrailingSlash(config.editLink.baseUrl) + entry.filePath;
|
|
74
|
+
return url ? new URL(url) : void 0;
|
|
75
|
+
}
|
|
76
|
+
/** Get the site title for a given language. **/
|
|
77
|
+
function getSiteTitle(lang) {
|
|
78
|
+
const defaultLang = config.defaultLocale.lang;
|
|
79
|
+
if (lang && config.title[lang]) return config.title[lang];
|
|
80
|
+
return config.title[defaultLang];
|
|
81
|
+
}
|
|
82
|
+
function getSiteTitleHref(locale) {
|
|
83
|
+
return formatPath(locale || "/");
|
|
84
|
+
}
|
|
85
|
+
/** Generate a route object for Starlight’s 404 page. */
|
|
86
|
+
async function get404Route(locals) {
|
|
87
|
+
const { lang = BuiltInDefaultLocale.lang, dir = BuiltInDefaultLocale.dir } = config.defaultLocale || {};
|
|
88
|
+
let locale = config.defaultLocale?.locale;
|
|
89
|
+
if (locale === "root") locale = void 0;
|
|
90
|
+
const entryMeta = {
|
|
91
|
+
dir,
|
|
92
|
+
lang,
|
|
93
|
+
locale
|
|
94
|
+
};
|
|
95
|
+
const fallbackEntry = {
|
|
96
|
+
id: "404",
|
|
97
|
+
body: "",
|
|
98
|
+
collection: "docs",
|
|
99
|
+
data: {
|
|
100
|
+
title: "404",
|
|
101
|
+
template: "splash",
|
|
102
|
+
editUrl: false,
|
|
103
|
+
head: [],
|
|
104
|
+
hero: {
|
|
105
|
+
tagline: locals.t("404.text"),
|
|
106
|
+
actions: []
|
|
107
|
+
},
|
|
108
|
+
pagefind: false,
|
|
109
|
+
sidebar: {
|
|
110
|
+
hidden: false,
|
|
111
|
+
attrs: {}
|
|
112
|
+
},
|
|
113
|
+
draft: false
|
|
114
|
+
},
|
|
115
|
+
filePath: `${getCollectionPathFromRoot("docs", project)}/404.md`
|
|
116
|
+
};
|
|
117
|
+
const userEntry = await getEntry("docs", "404");
|
|
118
|
+
const entry = userEntry ? normalizeCollectionEntry(userEntry) : fallbackEntry;
|
|
119
|
+
return {
|
|
120
|
+
...entryMeta,
|
|
121
|
+
entryMeta,
|
|
122
|
+
entry,
|
|
123
|
+
id: entry.id
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
export { generateRouteData, getRoute, getSiteTitle, getSiteTitleHref, getToC, useRouteData };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Route, StarlightDocsCollectionEntry, StarlightDocsEntry } from "./types.js";
|
|
2
|
+
import { GetStaticPathsItem } from "astro";
|
|
3
|
+
//#region src/utils/routing/index.d.ts
|
|
4
|
+
interface Path extends GetStaticPathsItem {
|
|
5
|
+
params: {
|
|
6
|
+
slug: string | undefined;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** Normalize a collection entry. */
|
|
10
|
+
declare function normalizeCollectionEntry(entry: StarlightDocsCollectionEntry): StarlightDocsEntry;
|
|
11
|
+
declare const routes: Route[];
|
|
12
|
+
declare function getRouteById(id: string): Route | undefined;
|
|
13
|
+
declare function getRouteBySlugParam(slugParam: string | undefined): Route | undefined;
|
|
14
|
+
declare const paths: Path[];
|
|
15
|
+
/**
|
|
16
|
+
* Get all routes for a specific locale.
|
|
17
|
+
* A locale of `undefined` is treated as the “root” locale, if configured.
|
|
18
|
+
*/
|
|
19
|
+
declare function getLocaleRoutes(locale: string | undefined): Route[];
|
|
20
|
+
//#endregion
|
|
21
|
+
export { getLocaleRoutes, getRouteById, getRouteBySlugParam, normalizeCollectionEntry, paths, routes };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { BuiltInDefaultLocale } from "../i18n.js";
|
|
2
|
+
import { localizedSlug, slugToLocaleData, slugToParam } from "../slugs.js";
|
|
3
|
+
import { validateLogoImports } from "../validateLogoImports.js";
|
|
4
|
+
import { getCollection } from "astro:content";
|
|
5
|
+
import config from "virtual:starlight/user-config";
|
|
6
|
+
//#region src/utils/routing/index.ts
|
|
7
|
+
validateLogoImports();
|
|
8
|
+
/**
|
|
9
|
+
* Astro is inconsistent in its `index.md` slug generation. In most cases,
|
|
10
|
+
* `index` is stripped, but in the root of a collection, we get a slug of `index`.
|
|
11
|
+
* We map that to an empty string for consistent behaviour.
|
|
12
|
+
*/
|
|
13
|
+
const normalizeIndexSlug = (slug) => slug === "index" ? "" : slug;
|
|
14
|
+
/** Normalize a collection entry. */
|
|
15
|
+
function normalizeCollectionEntry(entry) {
|
|
16
|
+
return {
|
|
17
|
+
...entry,
|
|
18
|
+
id: normalizeIndexSlug(entry.id),
|
|
19
|
+
filePath: entry.filePath
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** All entries in the docs content collection. */
|
|
23
|
+
const docs = (await getCollection("docs", ({ data }) => {
|
|
24
|
+
return import.meta.env.MODE !== "production" || data.draft === false;
|
|
25
|
+
}) ?? []).map(normalizeCollectionEntry);
|
|
26
|
+
function getRoutes() {
|
|
27
|
+
const routes = docs.map((entry) => ({
|
|
28
|
+
entry,
|
|
29
|
+
id: entry.id,
|
|
30
|
+
entryMeta: slugToLocaleData(entry.id),
|
|
31
|
+
...slugToLocaleData(entry.id)
|
|
32
|
+
}));
|
|
33
|
+
if (config.isMultilingual) {
|
|
34
|
+
/** Entries in the docs content collection for the default locale. */
|
|
35
|
+
const defaultLocaleDocs = getLocaleDocs(config.defaultLocale?.locale === "root" ? void 0 : config.defaultLocale?.locale);
|
|
36
|
+
for (const key in config.locales) {
|
|
37
|
+
if (key === config.defaultLocale.locale) continue;
|
|
38
|
+
const localeConfig = config.locales[key];
|
|
39
|
+
if (!localeConfig) continue;
|
|
40
|
+
const locale = key === "root" ? void 0 : key;
|
|
41
|
+
const localeDocs = getLocaleDocs(locale);
|
|
42
|
+
const localeDocIds = new Set(localeDocs.map(({ id }) => id));
|
|
43
|
+
for (const fallback of defaultLocaleDocs) {
|
|
44
|
+
const id = localizedSlug(fallback.id, locale);
|
|
45
|
+
if (localeDocIds.has(id)) continue;
|
|
46
|
+
routes.push({
|
|
47
|
+
entry: fallback,
|
|
48
|
+
id,
|
|
49
|
+
isFallback: true,
|
|
50
|
+
lang: localeConfig.lang || BuiltInDefaultLocale.lang,
|
|
51
|
+
locale,
|
|
52
|
+
dir: localeConfig.dir,
|
|
53
|
+
entryMeta: slugToLocaleData(fallback.id)
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return routes;
|
|
59
|
+
}
|
|
60
|
+
const routes = getRoutes();
|
|
61
|
+
const routesById = new Map(routes.map((route) => [route.id, route]));
|
|
62
|
+
function getRouteById(id) {
|
|
63
|
+
return routesById.get(id);
|
|
64
|
+
}
|
|
65
|
+
function getParamRouteMapping() {
|
|
66
|
+
const map = /* @__PURE__ */ new Map();
|
|
67
|
+
for (const route of routes) map.set(slugToParam(route.id), route);
|
|
68
|
+
return map;
|
|
69
|
+
}
|
|
70
|
+
const routesBySlugParam = getParamRouteMapping();
|
|
71
|
+
function getRouteBySlugParam(slugParam) {
|
|
72
|
+
return routesBySlugParam.get(slugParam?.replace(/\/$/, "") || void 0);
|
|
73
|
+
}
|
|
74
|
+
function getPaths() {
|
|
75
|
+
return routes.map((route) => ({ params: { slug: slugToParam(route.id) } }));
|
|
76
|
+
}
|
|
77
|
+
const paths = getPaths();
|
|
78
|
+
/**
|
|
79
|
+
* Get all routes for a specific locale.
|
|
80
|
+
* A locale of `undefined` is treated as the “root” locale, if configured.
|
|
81
|
+
*/
|
|
82
|
+
function getLocaleRoutes(locale) {
|
|
83
|
+
return filterByLocale(routes, locale);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Get all entries in the docs content collection for a specific locale.
|
|
87
|
+
* A locale of `undefined` is treated as the “root” locale, if configured.
|
|
88
|
+
*/
|
|
89
|
+
function getLocaleDocs(locale) {
|
|
90
|
+
return filterByLocale(docs, locale);
|
|
91
|
+
}
|
|
92
|
+
/** Filter an array to find items whose slug matches the passed locale. */
|
|
93
|
+
function filterByLocale(items, locale) {
|
|
94
|
+
if (config.locales) {
|
|
95
|
+
if (locale && locale in config.locales) return items.filter((i) => i.id === locale || i.id.startsWith(locale + "/"));
|
|
96
|
+
else if (config.locales.root) {
|
|
97
|
+
const langKeys = Object.keys(config.locales).filter((k) => k !== "root");
|
|
98
|
+
const isLangIndex = new RegExp(`^(${langKeys.join("|")})$`);
|
|
99
|
+
const isLangDir = new RegExp(`^(${langKeys.join("|")})/`);
|
|
100
|
+
return items.filter((i) => !isLangIndex.test(i.id) && !isLangDir.test(i.id));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return items;
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
export { getLocaleRoutes, getRouteById, getRouteBySlugParam, normalizeCollectionEntry, paths, routes };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { StarlightRouteData } from "./types.js";
|
|
2
|
+
import { APIContext } from "astro";
|
|
3
|
+
//#region src/utils/routing/middleware.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Adds a deep clone of the passed `routeData` object to locals and then runs middleware.
|
|
6
|
+
* @param context Astro context object
|
|
7
|
+
* @param routeData Initial route data object to attach.
|
|
8
|
+
*/
|
|
9
|
+
declare function attachRouteDataAndRunMiddleware(context: APIContext, routeData: StarlightRouteData): Promise<void>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { attachRouteDataAndRunMiddleware };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { klona } from "klona/lite";
|
|
2
|
+
import { routeMiddleware } from "virtual:starlight/route-middleware";
|
|
3
|
+
//#region src/utils/routing/middleware.ts
|
|
4
|
+
/**
|
|
5
|
+
* Adds a deep clone of the passed `routeData` object to locals and then runs middleware.
|
|
6
|
+
* @param context Astro context object
|
|
7
|
+
* @param routeData Initial route data object to attach.
|
|
8
|
+
*/
|
|
9
|
+
async function attachRouteDataAndRunMiddleware(context, routeData) {
|
|
10
|
+
context.locals.starlightRoute = klona(routeData);
|
|
11
|
+
await new MiddlewareRunner(context, routeMiddleware).run();
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A middleware function wrapper that only allows a single execution of the wrapped function.
|
|
15
|
+
* Subsequent calls to `run()` are no-ops.
|
|
16
|
+
*/
|
|
17
|
+
var MiddlewareRunnerStep = class {
|
|
18
|
+
#callback;
|
|
19
|
+
constructor(callback) {
|
|
20
|
+
this.#callback = callback;
|
|
21
|
+
}
|
|
22
|
+
async run(context, next) {
|
|
23
|
+
if (this.#callback) {
|
|
24
|
+
await this.#callback(context, next);
|
|
25
|
+
this.#callback = null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Class that runs a stack of middleware handlers with an initial context object.
|
|
31
|
+
* Middleware functions can mutate properties of the `context` object, but cannot replace it.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* const context = { value: 10 };
|
|
35
|
+
* const timesTwo = async (ctx, next) => {
|
|
36
|
+
* await next();
|
|
37
|
+
* ctx.value *= 2;
|
|
38
|
+
* };
|
|
39
|
+
* const addFive = async (ctx) => {
|
|
40
|
+
* ctx.value += 5;
|
|
41
|
+
* }
|
|
42
|
+
* const runner = new MiddlewareRunner(context, [timesTwo, addFive]);
|
|
43
|
+
* runner.run();
|
|
44
|
+
* console.log(context); // { value: 30 }
|
|
45
|
+
*/
|
|
46
|
+
var MiddlewareRunner = class {
|
|
47
|
+
#context;
|
|
48
|
+
#steps;
|
|
49
|
+
constructor(context, stack = []) {
|
|
50
|
+
this.#context = context;
|
|
51
|
+
this.#steps = stack.map((callback) => new MiddlewareRunnerStep(callback));
|
|
52
|
+
}
|
|
53
|
+
async #stepThrough(steps) {
|
|
54
|
+
let currentStep;
|
|
55
|
+
while (steps.length > 0) {
|
|
56
|
+
[currentStep, ...steps] = steps;
|
|
57
|
+
await currentStep.run(this.#context, async () => this.#stepThrough(steps));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async run() {
|
|
61
|
+
await this.#stepThrough(this.#steps);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
//#endregion
|
|
65
|
+
export { attachRouteDataAndRunMiddleware };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Badge } from "../../schemas/badge.js";
|
|
2
|
+
import { LinkHTMLAttributes } from "../../schemas/sidebar.js";
|
|
3
|
+
import { HeadConfig } from "../../schemas/head.js";
|
|
4
|
+
import { TocItem } from "../generateToC.js";
|
|
5
|
+
import { CollectionEntry, RenderResult } from "astro:content";
|
|
6
|
+
import { MarkdownHeading } from "astro";
|
|
7
|
+
//#region src/utils/routing/types.d.ts
|
|
8
|
+
interface LocaleData {
|
|
9
|
+
/** Writing direction. */
|
|
10
|
+
dir: 'ltr' | 'rtl';
|
|
11
|
+
/** BCP-47 language tag. */
|
|
12
|
+
lang: string;
|
|
13
|
+
/** The base path at which a language is served. `undefined` for root locale slugs. */
|
|
14
|
+
locale: string | undefined;
|
|
15
|
+
}
|
|
16
|
+
interface SidebarAutogenerateRouteData {
|
|
17
|
+
directory: string;
|
|
18
|
+
}
|
|
19
|
+
interface SidebarManualLink {
|
|
20
|
+
type: 'link';
|
|
21
|
+
label: string;
|
|
22
|
+
href: string;
|
|
23
|
+
isCurrent: boolean;
|
|
24
|
+
badge: Badge | undefined;
|
|
25
|
+
attrs: LinkHTMLAttributes;
|
|
26
|
+
}
|
|
27
|
+
interface SidebarAutoLink extends SidebarManualLink {
|
|
28
|
+
autogenerate: SidebarAutogenerateRouteData;
|
|
29
|
+
}
|
|
30
|
+
interface SidebarManualGroup {
|
|
31
|
+
type: 'group';
|
|
32
|
+
label: string;
|
|
33
|
+
entries: (SidebarLink | SidebarGroup)[];
|
|
34
|
+
collapsed: boolean;
|
|
35
|
+
badge: Badge | undefined;
|
|
36
|
+
}
|
|
37
|
+
interface SidebarAutoGroup extends SidebarManualGroup {
|
|
38
|
+
autogenerate: SidebarAutogenerateRouteData;
|
|
39
|
+
}
|
|
40
|
+
type SidebarLink = SidebarManualLink | SidebarAutoLink;
|
|
41
|
+
type SidebarGroup = SidebarManualGroup | SidebarAutoGroup;
|
|
42
|
+
type SidebarEntry = SidebarLink | SidebarGroup;
|
|
43
|
+
interface PaginationLinks {
|
|
44
|
+
/** Link to previous page in the sidebar. */
|
|
45
|
+
prev: SidebarLink | undefined;
|
|
46
|
+
/** Link to next page in the sidebar. */
|
|
47
|
+
next: SidebarLink | undefined;
|
|
48
|
+
}
|
|
49
|
+
type StarlightDocsCollectionEntry = CollectionEntry<'docs'>;
|
|
50
|
+
type StarlightDocsEntry = StarlightDocsCollectionEntry & {
|
|
51
|
+
filePath: string;
|
|
52
|
+
};
|
|
53
|
+
interface Route extends LocaleData {
|
|
54
|
+
/** Content collection entry for the current page. Includes frontmatter at `data`. */
|
|
55
|
+
entry: StarlightDocsEntry;
|
|
56
|
+
/** Locale metadata for the page content. Can be different from top-level locale values when a page is using fallback content. */
|
|
57
|
+
entryMeta: LocaleData;
|
|
58
|
+
/** The slug. */
|
|
59
|
+
id: string;
|
|
60
|
+
/** Whether this page is untranslated in the current language and using fallback content from the default locale. */
|
|
61
|
+
isFallback?: boolean;
|
|
62
|
+
[key: string]: unknown;
|
|
63
|
+
}
|
|
64
|
+
interface StarlightRouteData extends Route {
|
|
65
|
+
/** Title of the site. */
|
|
66
|
+
siteTitle: string;
|
|
67
|
+
/** URL or path used as the link when clicking on the site title. */
|
|
68
|
+
siteTitleHref: string;
|
|
69
|
+
/** Array of Markdown headings extracted from the current page. */
|
|
70
|
+
headings: MarkdownHeading[];
|
|
71
|
+
/** Site navigation sidebar entries for this page. */
|
|
72
|
+
sidebar: SidebarEntry[];
|
|
73
|
+
/** Whether or not the sidebar should be displayed on this page. */
|
|
74
|
+
hasSidebar: boolean;
|
|
75
|
+
/** Links to the previous and next page in the sidebar if enabled. */
|
|
76
|
+
pagination: PaginationLinks;
|
|
77
|
+
/** Table of contents for this page if enabled. */
|
|
78
|
+
toc: {
|
|
79
|
+
minHeadingLevel: number;
|
|
80
|
+
maxHeadingLevel: number;
|
|
81
|
+
items: TocItem[];
|
|
82
|
+
} | undefined;
|
|
83
|
+
/** JS Date object representing when this page was last updated if enabled. */
|
|
84
|
+
lastUpdated: Date | undefined;
|
|
85
|
+
/** URL object for the address where this page can be edited if enabled. */
|
|
86
|
+
editUrl: URL | undefined;
|
|
87
|
+
/** An Astro component to render the current page’s content if this route is a Markdown page. */
|
|
88
|
+
Content?: RenderResult['Content'];
|
|
89
|
+
/** Array of tags to include in the `<head>` of the current page. */
|
|
90
|
+
head: HeadConfig;
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
export { LocaleData, PaginationLinks, Route, SidebarAutoGroup, SidebarAutoLink, SidebarAutogenerateRouteData, SidebarEntry, SidebarGroup, SidebarLink, SidebarManualGroup, SidebarManualLink, StarlightDocsCollectionEntry, StarlightDocsEntry, StarlightRouteData };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { LocaleData } from "./routing/types.js";
|
|
2
|
+
//#region src/utils/slugs.d.ts
|
|
3
|
+
/** Get locale information for a given slug. */
|
|
4
|
+
declare function slugToLocaleData(slug: string): LocaleData;
|
|
5
|
+
/**
|
|
6
|
+
* Get the BCP-47 language tag for the given locale.
|
|
7
|
+
* @param locale Locale string or `undefined` for the root locale.
|
|
8
|
+
*/
|
|
9
|
+
declare function localeToLang(locale: string | undefined): string;
|
|
10
|
+
/**
|
|
11
|
+
* Convert a content collection slug to a param as expected by Astro’s router.
|
|
12
|
+
* This utility handles stripping `index` from file names and matches
|
|
13
|
+
* [Astro’s param sanitization logic](https://github.com/withastro/astro/blob/687d25365a41ff8a9e6da155d3527f841abb70dd/packages/astro/src/core/routing/manifest/generator.ts#L4-L18)
|
|
14
|
+
* by normalizing strings to their canonical representations.
|
|
15
|
+
* @param slug Content collection slug
|
|
16
|
+
* @returns Param compatible with Astro’s router
|
|
17
|
+
*/
|
|
18
|
+
declare function slugToParam(slug: string): string | undefined;
|
|
19
|
+
declare function slugToPathname(slug: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Convert a slug to a different locale.
|
|
22
|
+
* For example, passing a slug of `en/home` and a locale of `fr` results in `fr/home`.
|
|
23
|
+
* An undefined locale is treated as the root locale, resulting in `home`
|
|
24
|
+
* @param slug A collection entry slug
|
|
25
|
+
* @param locale The target locale
|
|
26
|
+
* @example
|
|
27
|
+
* localizedSlug('en/home', 'fr') // => 'fr/home'
|
|
28
|
+
* localizedSlug('en/home', undefined) // => 'home'
|
|
29
|
+
*/
|
|
30
|
+
declare function localizedSlug(slug: string, locale: string | undefined): string;
|
|
31
|
+
/**
|
|
32
|
+
* Convert a file path relative to the collection root to a different locale.
|
|
33
|
+
* For example, passing a file path of `en/home.md` and a locale of `fr` results in `fr/home.md`.
|
|
34
|
+
* An undefined locale is treated as the root locale, resulting in `home.md`.
|
|
35
|
+
* @param filePath A collection entry file path relative to the collection root
|
|
36
|
+
* @param locale The target locale
|
|
37
|
+
* @example
|
|
38
|
+
* localizedFilePath('en/home.md', 'fr') // => 'fr/home.md'
|
|
39
|
+
* localizedFilePath('en/home.md', undefined) // => 'home.md'
|
|
40
|
+
*/
|
|
41
|
+
declare function localizedFilePath(filePath: string, locale: string | undefined): string;
|
|
42
|
+
/** Extract the slug from a URL. */
|
|
43
|
+
declare function urlToSlug(url: URL): string;
|
|
44
|
+
//#endregion
|
|
45
|
+
export { localeToLang, localizedFilePath, localizedSlug, slugToLocaleData, slugToParam, slugToPathname, urlToSlug };
|