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

feat(@galactiks/explorer): detect ispartof document translation of work #87

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
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
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