Skip to content

Commit

Permalink
Merge pull request #84 from thegalactiks/add-page-repo-language-filter
Browse files Browse the repository at this point in the history
feat(@galactiks/explorer): add inLanguage filter for getpagebyidentifier
  • Loading branch information
emmanuelgautier authored Oct 21, 2023
2 parents adb52dc + 068f82b commit 51be3c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Content } from '../types/index.mjs';

export type RepositoryFilters = {
type?: Content['type'];
inLanguage?: string;
inLanguages?: string[];
};

Expand Down
32 changes: 18 additions & 14 deletions packages/explorer/src/core/content/repositories/generated.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ let _documents: Content[];

const documentsByLanguagesSelector =
<T extends Pick<ContentlayerWebPageDocument, 'inLanguage'>>(documents: T[]) =>
(inLanguages: string[]) =>
documents.filter(
(_d) => !_d.inLanguage || inLanguages.indexOf(_d.inLanguage) !== -1
);
(inLanguages: string[]) =>
documents.filter(
(_d) => !_d.inLanguage || inLanguages.indexOf(_d.inLanguage) !== -1
);

const getGenerated = async (): Promise<ContentlayerDataExports> => {
if (!_generated) {
Expand Down Expand Up @@ -79,17 +79,21 @@ export const getPages = async (
filters?: RepositoryFilters
): Promise<Content[]> => {
const documents = await getWebPageDocuments();
if (!filters) {
return documents
}

let inLanguages = Array.isArray(filters.inLanguages) ? filters.inLanguages : [];
if (typeof filters.inLanguage === 'string') {
inLanguages = inLanguages.concat(filters.inLanguage);
}

return filters
? documents.filter(
(_d) =>
(!Array.isArray(filters.inLanguages) ||
filters.inLanguages.length === 0 ||
!_d.inLanguage ||
filters.inLanguages.indexOf(_d.inLanguage) !== -1) &&
(!filters.type || _d.type === filters.type)
)
: documents;
return documents.filter(
(_d) =>
(inLanguages.length === 0 || !_d.inLanguage ||
inLanguages.indexOf(_d.inLanguage) !== -1) &&
(!filters.type || _d.type === filters.type)
);
};

export const getWebPageDocumentsByType = async (
Expand Down

0 comments on commit 51be3c3

Please sign in to comment.