Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[i18nIgnore] Add new subpage handling logic #2758

Merged
merged 6 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/components/LeftSidebar/SidebarContent.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import { getLanguageFromURL, removeSubpageSegment } from '../../util';
import { getLanguageFromURL } from '~/util';
import { isSubPage } from '~/util/isSubPage';

export interface Props {
type: TabType;
Expand Down Expand Up @@ -44,11 +45,7 @@ const lang = getLanguageFromURL(Astro.url.pathname);
<a
href={`${Astro.site?.pathname}${lang}/${slug}/`}
aria-current={`${currentPageMatch.endsWith(slug) ? 'page' : 'false'}`}
data-current-parent={`${
removeSubpageSegment(currentPageMatch).endsWith(removeSubpageSegment(slug))
? 'true'
: 'false'
}`}
data-current-parent={`${isSubPage(currentPageMatch, slug) ? 'true' : 'false'}`}
>
<Fragment set:html={text} /> {isFallback && <sup class="fallback">EN</sup>}
</a>
Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/es/core-concepts/sharing-state.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: Compartiendo Estado
description: Learn how to share state across components — and frameworks! — with Nano Stores.
i18nReady: true
type: recipe
---

import UIFrameworkTabs from '~/components/tabs/UIFrameworkTabs.astro'
Expand Down
1 change: 1 addition & 0 deletions src/content/docs/es/guides/rss.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: RSS
description: Introducción a RSS en Astro
i18nReady: true
type: recipe
---

import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro';
Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/fr/core-concepts/sharing-state.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: Partage d'État
description: Learn how to share state across components — and frameworks! — with Nano Stores.
i18nReady: true
type: recipe
---
import UIFrameworkTabs from '~/components/tabs/UIFrameworkTabs.astro'
import LoopingVideo from '~/components/LoopingVideo.astro'
Expand Down
1 change: 1 addition & 0 deletions src/content/docs/fr/guides/rss.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Flux RSS
description: Une introduction aux flux RSS avec Astro.
type: recipe
---
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'
import Since from '~/components/Since.astro'
Expand Down
1 change: 1 addition & 0 deletions src/content/docs/ja/guides/rss.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: RSS
description: AstroのRSS入門
i18nReady: true
type: recipe
---
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'

Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/pt-br/core-concepts/sharing-state.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: Compartilhamento de Estado
description: Aprenda como compartilhar estado entre componentes — e frameworks! — com Nano Stores.
i18nReady: true
type: recipe
---
import UIFrameworkTabs from '~/components/tabs/UIFrameworkTabs.astro';
import LoopingVideo from '~/components/LoopingVideo.astro';
Expand Down
1 change: 1 addition & 0 deletions src/content/docs/pt-br/guides/rss.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: RSS
description: Uma introdução a RSS em Astro
i18nReady: true
type: recipe
---
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro';
import Since from '~/components/Since.astro';
Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/zh-cn/core-concepts/sharing-state.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: 状态共享
description: Learn how to share state across components — and frameworks! — with Nano Stores.
i18nReady: false
type: recipe
---
import UIFrameworkTabs from '~/components/tabs/UIFrameworkTabs.astro'
import LoopingVideo from '~/components/LoopingVideo.astro'
Expand Down
1 change: 1 addition & 0 deletions src/content/docs/zh-cn/guides/rss.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: RSS
description: Astro RSS 介绍
type: recipe
---

Astro 支持为博客和其他内容网站快速自动生成 RSS 摘要。更多关于 RSS 摘要的信息参见 [aboutfeeds.com](https://aboutfeeds.com/)。
Expand Down
15 changes: 0 additions & 15 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,3 @@ export const stripLangFromSlug = (slug: CollectionEntry<'docs'>['slug']) =>

/** Get a page’s lang tag from its slug (e.g. `'en/migrate'` => `'en'`). */
export const getLangFromSlug = (slug: CollectionEntry<'docs'>['slug']) => slug.split('/')[0];

/** Remove the subpage segment of a URL string */
export function removeSubpageSegment(path: string) {
// Include new pages with subpages as part of this regex.
const regex = /(?:install|deploy|integrations-guide|tutorial|migrate-to-astro|recipes|cms)\//;
const matches = regex.exec(path);

if (matches) {
const matchIndex = matches.index;
// Get the first slash index after the main page path segment.
const slashIndex = path.slice(matchIndex).indexOf('/') + matchIndex;
return path.slice(0, slashIndex);
}
return path;
}
2 changes: 1 addition & 1 deletion src/util/getPageCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const categories = [
* @param url URL for the current page.
* @returns The category for the current page as used by Algolia DocSearch to group search results.
*/
export function getPageCategory(url: URL) {
export function getPageCategory(url: { pathname: string }) {
const langAgnosticPath = url.pathname.replace(/\/\w\w(-\w\w)?\//, '');
for (const [path, label] of categories) {
if (langAgnosticPath.startsWith(path)) return label;
Expand Down
49 changes: 49 additions & 0 deletions src/util/isSubPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { CollectionEntry } from 'astro:content';
import { englishPages } from '~/content';
import { getPageCategory } from './getPageCategory';

/** Remove the sub-page segment of a URL string */
export function removeSubPageSegment(path: string) {
// Include new pages with sub-pages as part of this regex.
const regex = /(?:install|deploy|integrations-guide|tutorial|migrate-to-astro|recipes|cms)\//;
const matches = regex.exec(path);

if (matches) {
const matchIndex = matches.index;
// Get the first slash index after the main page path segment.
const slashIndex = path.slice(matchIndex).indexOf('/') + matchIndex;
return path.slice(0, slashIndex);
}
return path;
}

const typeIndexes: Partial<Record<CollectionEntry<'docs'>['data']['type'], string>> = {
recipe: 'recipes',
};

const categoryIndex: Partial<Record<ReturnType<typeof getPageCategory>, string>> = {
'Error Reference': 'reference/error-reference',
};

/**
* Test if `currentPage` is considered a sub-page of `parentSlug`.
* @param currentPage The full slug for the current page, e.g. `'en/guides/rss'`
* @param parentSlug The language-less slug for the parent to test against e.g. `'guides/content-collections'`
*/
export function isSubPage(currentPage: string, parentSlug: string): boolean {
// Test 1: do the two pages share a base URL segment?
if (removeSubPageSegment(currentPage).endsWith(removeSubPageSegment(parentSlug))) {
return true;
}
// Test 2: is there a known parent page for this page category?
const category = getPageCategory({ pathname: '/' + currentPage + '/' });
if (categoryIndex[category] === parentSlug) {
return true;
}
// Test 3: is there a known parent page for this page type?
const type = englishPages.find(({ slug }) => slug === currentPage)?.data.type;
if (type && typeIndexes[type] === parentSlug) {
return true;
}
return false;
}