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

Support homepage template for multilang websites #67

Merged
merged 3 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/adapters/astro/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { fileURLToPath } from 'node:url';
import { join } from 'path';

export { getStaticPaths, getHomePage } from './pages.mjs';
export { getStaticPaths, getIndexPage } from './pages.mjs';
export { integrationsPreset } from './preset.mjs';

type GalactiksOptions = {
Expand Down
18 changes: 9 additions & 9 deletions packages/adapters/astro/src/pages.mts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { getDefaultLanguage, getLanguages } from '@galactiks/config';
import { getLanguages } from '@galactiks/config';
import {
getAllPagesExceptHome,
getHomePage as explorerGetHomePage,
getPages,
getIndexPage as explorerGetIndexPage,
} from '@galactiks/explorer';

export async function getStaticPaths() {
return (await getAllPagesExceptHome({ inLanguages: getLanguages() })).map(
(page) => ({
return (await getPages({ inLanguages: getLanguages() }))
.filter((_p) => _p.slug !== '')
.map((page) => ({
params: { path: page.path.slice(1) },
props: { page },
})
);
}));
}

export function getHomePage() {
return explorerGetHomePage({ inLanguage: getDefaultLanguage() });
export function getIndexPage() {
return explorerGetIndexPage();
}
8 changes: 6 additions & 2 deletions packages/config/src/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export function getDefaultLanguage(): string | undefined {
return getConfig().locales?.default;
}

export function getLanguages(): string[] | undefined {
return getConfig().locales?.available;
export function getLanguages(): string[] {
const locales = getConfig().locales;

return typeof locales === 'object'
? locales.available || [locales.default]
: [];
}
24 changes: 11 additions & 13 deletions packages/explorer/src/core/content/repositories/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createIdentifierFromString } from '../utils.mjs';

import type { RepositoryFilters } from './filters.mjs';
import { getOrganizations, getPages } from './generated.mjs';
import { getDefaultLanguage, getLanguages } from '@galactiks/config';

export * from './generated.mjs';

Expand Down Expand Up @@ -50,26 +51,23 @@ export const getPageBySlug = async (
export const getOrganizationByIdentifier = async (identifier: string) =>
documentByIdentifierSelector(await getOrganizations())(identifier);

export const getAllPagesExceptHome = async (
filters?: RepositoryFilters
): Promise<Content[]> =>
(await getPages(filters)).filter(
({ identifier }) => identifier !== homeIdentifier
);

export type RepositoryHomeFilters = {
inLanguage?: string;
};
export const getHomePage = async (
filters?: RepositoryHomeFilters
): Promise<Content> => {
const homepageContent = await getPageByIdentifier(
): Promise<Content | undefined> =>
getPageByIdentifier(
homeIdentifier,
filters?.inLanguage ? { inLanguages: [filters.inLanguage] } : undefined
);
if (!homepageContent) {
throw new Error('no content for homepage');
}

return homepageContent;
export const getIndexPage = async (): Promise<Content | undefined> => {
const defaultLanguage = getDefaultLanguage();
const languages = getLanguages();

const inLanguage =
defaultLanguage || (languages.length === 1 && languages[0]) || undefined;

return getHomePage({ inLanguage });
};