Skip to content

Commit

Permalink
Merge pull request #87 from thegalactiks/detect-ispartof-translationo…
Browse files Browse the repository at this point in the history
…fwork

feat(@galactiks/explorer): detect ispartof document translation of work
  • Loading branch information
emmanuelgautier authored Oct 25, 2023
2 parents a4f4f72 + 48b26ff commit ae7aa5c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 115 deletions.
3 changes: 0 additions & 3 deletions packages/contentlayer/src/types.mts
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ export type Website = {
/** Nested types */

export type Id = {
/** File path relative to `contentDirPath` */
_id: string;
_raw: Local.RawDocumentData;
type: 'Id';
'@id': string;
};
Expand Down
232 changes: 120 additions & 112 deletions packages/explorer/src/core/content/compute.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GalactiksConfig } from '@galactiks/config';
import type { Id } from '@galactiks/contentlayer';

import {
alternatesHeaderBuilder,
Expand All @@ -19,6 +20,7 @@ import {
type ContentlayerDocumentWithURL,
} from './urls.mjs';
import {
documentByIdentifierSelector,
documentByTypeAndIdentifierAndLanguageSelector,
isInLanguage,
} from './selectors.mjs';
Expand Down Expand Up @@ -55,10 +57,10 @@ function createPage<T>(
body: document.body?.raw
? addBodyRender(document.body)
: {
raw: '',
code: '',
render: emptyRender,
},
raw: '',
code: '',
render: emptyRender,
},
} as T;
}

Expand All @@ -84,124 +86,130 @@ const hydratePagesWithRender = async (

const computeRemainingListingPages = async (
documents: ContentlayerWebPageDocumentWithRender[]
) =>
documents.reduce(
(
acc,
{
keywords,
dateCreated,
datePublished,
dateModified,
isPartOf,
inLanguage,
}
) => {
const templateDocument = {
dateCreated,
datePublished,
dateModified,
inLanguage,
};
) => {
const getDocumentByIdentifier = documentByIdentifierSelector(documents);

return documents.reduce((acc, _d) => {
const templateDocument: Partial<ContentlayerWebPageDocument> = {
dateCreated: _d.dateCreated,
datePublished: _d.datePublished,
dateModified: _d.dateModified,
inLanguage: _d.inLanguage,
};

// If parent page does not exist, create it
if (
isPartOf &&
acc.some(
(_a) => _a.identifier === isPartOf && isInLanguage(_a, inLanguage)
) === false
) {
acc = acc.concat(createListingPage(isPartOf, templateDocument));
// If parent page does not exist, create it
if (
_d.isPartOf &&
acc.some(
(_a) => _a.identifier === _d.isPartOf && isInLanguage(_a, _d.inLanguage)
) === false
) {
let translationOfWork: Id | undefined = undefined;
if (_d.translationOfWork && _d.translationOfWork['@id']) {
const translationOfWorkDocument = getDocumentByIdentifier(_d.translationOfWork['@id']);
console.log(_d.translationOfWork, translationOfWorkDocument)

if (translationOfWorkDocument?.isPartOf) {
translationOfWork = {
"type": "Id",
"@id": translationOfWorkDocument.isPartOf,
};
}
}

// Create all keywords pages not existing yet
if (Array.isArray(keywords)) {
acc = acc.concat(
keywords
.filter(
(_k) =>
_k &&
acc.some(
(_a) => _a.identifier === _k && isInLanguage(_a, inLanguage)
) === false
)
.map((_k) =>
createListingPage(_k, {
...templateDocument,
type: 'Tag',
})
)
);
}
acc = acc.concat(createListingPage(_d.isPartOf, {
...templateDocument,
translationOfWork,
}));
}

return acc;
},
documents
);
// Create all keywords pages not existing yet
if (Array.isArray(_d.keywords)) {
acc = acc.concat(
_d.keywords
.filter(
(_k) =>
_k &&
acc.some(
(_a) => _a.identifier === _k && isInLanguage(_a, _d.inLanguage)
) === false
)
.map((_k) =>
createListingPage(_k, {
...templateDocument,
type: 'Tag',
})
)
);
}

const computeMissingFields =
(config: GalactiksConfig, people: ContentlayerPerson[]) =>
async (
documents: Array<
ContentlayerDocumentWithURL & ContentlayerWebPageDocumentWithRender
>
): Promise<Content[]> => {
const buildBreadcrumb = breadcrumbBuilder(documents);
const buildAlternates = alternatesHeaderBuilder(documents);
const selectPersonByIdentifierAndLanguage =
documentByTypeAndIdentifierAndLanguageSelector('Person', documents);

const getAuthor = (
identifier?: string,
inLanguage?: string
): Person | undefined => {
let author;
if (identifier) {
author = selectPersonByIdentifierAndLanguage(identifier, inLanguage);
} else if (people.length === 1) {
author = people[0];
}
return acc;
}, documents);
}

return (
author && {
identifier: author.identifier,
name: author.name,
description: author.description,
url: author.url,
image: author.image,
const computeMissingFields =
(_: GalactiksConfig, people: ContentlayerPerson[]) =>
async (
documents: Array<
ContentlayerDocumentWithURL & ContentlayerWebPageDocumentWithRender
>
): Promise<Content[]> => {
const buildBreadcrumb = breadcrumbBuilder(documents);
const buildAlternates = alternatesHeaderBuilder(documents);
const selectPersonByIdentifierAndLanguage =
documentByTypeAndIdentifierAndLanguageSelector('Person', documents);

const getAuthor = (
identifier?: string,
inLanguage?: string
): Person | undefined => {
let author;
if (identifier) {
author = selectPersonByIdentifierAndLanguage(identifier, inLanguage);
} else if (people.length === 1) {
author = people[0];
}
);
};

return documents.map((document) => {
const dateCreated = new Date(document.dateCreated);
const contentWithoutHeaders: Omit<Content, 'headers'> = {
...document,
author: getAuthor(document.author, document.inLanguage),
breadcrumb: buildBreadcrumb(document),
dateCreated,
dateModified: document.dateModified
? new Date(document.dateModified)
: dateCreated,
datePublished: document.datePublished
? new Date(document.datePublished)
: dateCreated,
return (
author && {
identifier: author.identifier,
name: author.name,
description: author.description,
url: author.url,
image: author.image,
}
);
};

return {
...contentWithoutHeaders,
headers: {
...getBasicHeaders(contentWithoutHeaders),
alternates: buildAlternates(contentWithoutHeaders),
structuredDataSchemas: getStructuredDataSchemas(
contentWithoutHeaders
),
openGraph: getOpenGraphObjects(contentWithoutHeaders),
twitterCard: getTwitterCard(contentWithoutHeaders),
},
};
});
};
return documents.map((document) => {
const dateCreated = new Date(document.dateCreated);
const contentWithoutHeaders: Omit<Content, 'headers'> = {
...document,
author: getAuthor(document.author, document.inLanguage),
breadcrumb: buildBreadcrumb(document),
dateCreated,
dateModified: document.dateModified
? new Date(document.dateModified)
: dateCreated,
datePublished: document.datePublished
? new Date(document.datePublished)
: dateCreated,
};

return {
...contentWithoutHeaders,
headers: {
...getBasicHeaders(contentWithoutHeaders),
alternates: buildAlternates(contentWithoutHeaders),
structuredDataSchemas: getStructuredDataSchemas(
contentWithoutHeaders
),
openGraph: getOpenGraphObjects(contentWithoutHeaders),
twitterCard: getTwitterCard(contentWithoutHeaders),
},
};
});
};

export const computeDocuments = async ({
config,
Expand Down

0 comments on commit ae7aa5c

Please sign in to comment.