Skip to content

Commit

Permalink
Merge pull request #149 from thegalactiks/fix-alternates-locales
Browse files Browse the repository at this point in the history
refactor(@thegalactiks/explorer): move locales selectors outsite builder
  • Loading branch information
emmanuelgautier authored Feb 13, 2024
2 parents 459513a + b261465 commit e5726de
Showing 1 changed file with 39 additions and 17 deletions.
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' });
};
};

0 comments on commit e5726de

Please sign in to comment.