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

refactor(@thegalactiks/explorer): move locales selectors outsite builder #149

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
56 changes: 39 additions & 17 deletions packages/explorer/src/content/metadata/alternates.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ import type {
MetadataHeaders,
} from '../../types/index.mjs';

export const alternatesHeaderBuilder = (
documents: ContentlayerDocumentWithURL[]
) => {
const selectNonTranslatedPagesByIdentifier = (identifier: string) =>
documents.filter((_d) => !_d.translationOfWork?.['@id'] && _d.identifier === identifier);
const selectDocumentsTranslationOfWorkByIdentifierOrURL = (
identifier: string,
url: string
) =>
const nonTranslatedPagesByIdentifierSelector =
(documents: ContentlayerDocumentWithURL[]) => (identifier: string) =>
documents.filter(
(_d) => !_d.translationOfWork?.['@id'] && _d.identifier === identifier
);

const documentsTranslationOfWorkByIdentifierOrURLSelector =
(documents: ContentlayerDocumentWithURL[]) =>
(identifier: string, url: string) =>
documents.filter(
({ translationOfWork }) =>
translationOfWork?.['@id'] &&
(translationOfWork?.['@id'] === identifier ||
translationOfWork?.['@id'] === url)
);

const getTranslations = (document: Content) => {
const translationsSelector = (documents: ContentlayerDocumentWithURL[]) => {
const selectDocumentsTranslationOfWorkByIdentifierOrURL =
documentsTranslationOfWorkByIdentifierOrURLSelector(documents);

return (document: Content) => {
if (!document.translationOfWork) {
return selectDocumentsTranslationOfWorkByIdentifierOrURL(
document.identifier,
Expand All @@ -34,33 +38,51 @@ export const alternatesHeaderBuilder = (
document.translationOfWork['@id']
);
};
};

type Translations = Array<Content | ContentlayerDocumentWithURL>
const defaultTranslationSelector = (
documents: ContentlayerDocumentWithURL[]
) => {
const selectNonTranslatedPagesByIdentifier =
nonTranslatedPagesByIdentifierSelector(documents);

const getDefaultTranslation = (document: Content, translations: Translations) => {
return (document: Content, translations: Translations) => {
if (document.translationOfWork?.['@id']) {
const defaultPageFromTranslationOfWork = selectNonTranslatedPagesByIdentifier(document.translationOfWork['@id'])
const defaultPageFromTranslationOfWork =
selectNonTranslatedPagesByIdentifier(document.translationOfWork['@id']);
if (defaultPageFromTranslationOfWork.length === 1) {
return defaultPageFromTranslationOfWork[0];
}
}

const defaultPageFromDefaultLanguage = translations.filter((t) => t.inLanguage === getDefaultLanguage());
const defaultPageFromDefaultLanguage = translations.filter(
(t) => t.inLanguage === getDefaultLanguage()
);
if (defaultPageFromDefaultLanguage.length > 0) {
return defaultPageFromDefaultLanguage[0];
}

return document;
}
};
};

type Translations = Array<Content | ContentlayerDocumentWithURL>;

export const alternatesHeaderBuilder = (
documents: ContentlayerDocumentWithURL[]
) => {
const selectTranslations = translationsSelector(documents);
const selectDefaultTranslation = defaultTranslationSelector(documents);

return (document: Content): MetadataHeaders['alternates'] => {
const translations: Translations = getTranslations(document);
const defaultTranslation = getDefaultTranslation(document, translations);
const translations: Translations = selectTranslations(document);
const defaultTranslation = selectDefaultTranslation(document, translations);

return translations
.concat(defaultTranslation)
.filter((_t) => _t.inLanguage)
.map((_t) => ({ href: _t.url, hreflang: _t.inLanguage as string }))
.sort((a, b) => a.hreflang.localeCompare(b.hreflang))
.concat({ href: defaultTranslation.url, hreflang: 'x-default' });
};
};
Loading