-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from thegalactiks/support-multiple-locales-wi…
…th-same-lang feat(@galactiks/explorer): create multiple pages when locales with th…
- Loading branch information
Showing
32 changed files
with
369 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
82
packages/explorer/src/core/content/hydrate/listing-pages.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
Oops, something went wrong.