Skip to content

Commit

Permalink
Merge pull request #67 from thegalactiks/support-homepage-template
Browse files Browse the repository at this point in the history
Support homepage template for multilang websites
  • Loading branch information
emmanuelgautier authored Sep 24, 2023
2 parents 21d69c0 + 4d06818 commit 22c4a7c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
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 });
};

0 comments on commit 22c4a7c

Please sign in to comment.