Skip to content

Commit

Permalink
Merge pull request #128 from thegalactiks/support-multiple-locales-wi…
Browse files Browse the repository at this point in the history
…th-same-lang

feat(@galactiks/explorer): create multiple pages when locales with th…
  • Loading branch information
emmanuelgautier authored Jan 1, 2024
2 parents f0fa7c1 + c571ccf commit 9e35afe
Show file tree
Hide file tree
Showing 32 changed files with 369 additions and 303 deletions.
2 changes: 2 additions & 0 deletions packages/explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
"@std-uritemplate/std-uritemplate": "0.0.49",
"debug": "4.3.4",
"esbuild": "0.19.11",
"lodash.groupby": "^4.6.0",
"mdx-bundler": "10.0.1",
"react": "18.2.0",
"slugify": "1.6.6",
"zod": "3.22.4"
},
"devDependencies": {
"@types/debug": "4.1.12",
"@types/lodash.groupby": "^4.6.9",
"@types/react": "18.2.46",
"schema-dts": "1.1.2"
}
Expand Down
226 changes: 0 additions & 226 deletions packages/explorer/src/core/content/compute.mts

This file was deleted.

27 changes: 27 additions & 0 deletions packages/explorer/src/core/content/hydrate/common.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { ContentlayerWebPageDocument } from '../../types/index.mjs'
import { createIdentifierFromString } from '../utils.mjs';
import { addBodyRender, emptyRender } from './render.mjs';

export function createPage<T>(
identifier: string,
document: Partial<ContentlayerWebPageDocument>
) {
const id = createIdentifierFromString(identifier);

return {
_id: id,
identifier: id,
slug: id,
description: '',
type: 'Page',
name: identifier,
...document,
body: document.body?.raw
? addBodyRender(document.body)
: {
raw: '',
code: '',
render: emptyRender,
},
} as T;
}
30 changes: 30 additions & 0 deletions packages/explorer/src/core/content/hydrate/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Content, ContentlayerWebPageDocument } from '../../types/index.mjs';
import type { ComputeDTO } from './types.mjs';
import { computeRemainingListingPages } from './listing-pages.mjs';
import { computeDocumentsUrl } from './urls.mjs';
import { computeMissingFields } from './missing-fields.mjs';
import { addBodyRender } from './render.mjs';
import { createSameLanguagePages } from './same-language.mjs';

export type * from './types.mjs';

const hydratePagesWithRender = async (
documents: ContentlayerWebPageDocument[]
) =>
documents.map((d) => ({
...d,
body: addBodyRender(d.body),
}));

export const computeDocuments = async ({
config,
documents,
people,
websites,
}: ComputeDTO<ContentlayerWebPageDocument>): Promise<Content[]> =>
Promise.resolve(documents)
.then(hydratePagesWithRender)
.then(createSameLanguagePages(config))
.then(computeRemainingListingPages())
.then(computeDocumentsUrl(websites))
.then(computeMissingFields(config, people));
82 changes: 82 additions & 0 deletions packages/explorer/src/core/content/hydrate/listing-pages.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type { Id } from '@galactiks/contentlayer';
import { documentByIdentifierSelector, isInLanguage } from '../selectors.mjs';
import type { ContentlayerWebPageDocument, ContentlayerDocumentWithRender, ContentlayerWebPageDocumentWithRender } from '../../types/index.mjs';

import { createPage } from './common.mjs';

const createListingPage = (
identifier: string,
document: Partial<ContentlayerWebPageDocument> = {}
) =>
createPage<ContentlayerDocumentWithRender<ContentlayerWebPageDocument>>(
identifier,
{
listingPage: true,
...document,
}
);

export const computeRemainingListingPages = () => async (
documents: ContentlayerWebPageDocumentWithRender[]
) => {
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 (
_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']
);
if (translationOfWorkDocument?.isPartOf) {
translationOfWork = {
type: 'Id',
'@id': translationOfWorkDocument.isPartOf,
};
}
}

acc = acc.concat(
createListingPage(_d.isPartOf, {
...templateDocument,
translationOfWork,
})
);
}

// 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',
})
)
);
}

return acc;
}, documents);
};
Loading

0 comments on commit 9e35afe

Please sign in to comment.