Skip to content

Commit

Permalink
Merge pull request #36 from withmoons/navlinks
Browse files Browse the repository at this point in the history
feat(@withmoons/explorer): add webpage element type
  • Loading branch information
emmanuelgautier authored Aug 22, 2023
2 parents 5148728 + 8e11f7f commit 343e9a2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
16 changes: 15 additions & 1 deletion packages/moons/src/core/content/repository.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getConfig } from '@withmoons/config'

import type { Content, ContentlayerDataExports, ContentlayerWebPageDocument, ContentlayerWebsite } from './types/index.mjs'
import type { Content, ContentlayerDataExports, ContentlayerWebPageDocument, ContentlayerWebPageElement, ContentlayerWebsite } from './types/index.mjs'
import { computeDocuments } from './compute.mjs'
import { homeIdentifier } from './consts.mjs'
import { documentByIdentifierSelector, documentsByLanguagesSelector } from './selectors.mjs'
Expand All @@ -23,6 +23,20 @@ const getGenerated = async (): Promise<ContentlayerDataExports> => {

export const getWebsites = async (): Promise<ContentlayerWebsite[]> => (await getGenerated()).allWebsites

export type WebPageElementFilters = {
inLanguage?: string;
}
export const getWebPageElements = async (filters?: WebPageElementFilters): Promise<ContentlayerWebPageElement[]> => {
const elements = (await getGenerated()).allWebPageElements

return filters?.inLanguage
? documentsByLanguagesSelector(elements)([filters.inLanguage])
: elements
}
export const getWebPageElementByType = async (type: ContentlayerWebPageElement['elementType'], filters?: WebPageElementFilters): Promise<ContentlayerWebPageElement | undefined> =>
(await getWebPageElements(filters)).find(({ elementType: _t }) => _t === type)
export const getSiteNavigationElement = (filters?: WebPageElementFilters) => getWebPageElementByType('SiteNavigationElement', filters)

const getWebPageDocuments = async (): Promise<Content[]> => {
if (Array.isArray(_documents)) {
return _documents
Expand Down
31 changes: 29 additions & 2 deletions packages/moons/src/core/content/types/contentlayer.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { isType } from 'contentlayer/client'

export type { Markdown, MDX, ImageFieldData, IsoDateTimeString }


/** Document types */
export type Article = {
/** File path relative to `contentDirPath` */
Expand Down Expand Up @@ -117,6 +118,20 @@ export type Person = {
collection: string
}

export type WebPageElement = {
/** File path relative to `contentDirPath` */
_id: string
_raw: Local.RawDocumentData
type: 'WebPageElement'
elementType?: 'SiteNavigationElement' | undefined
identifier: string
inLanguage?: string | undefined
name?: string | undefined
itemListElement: ItemListElement[]
/** MDX file body */
body: MDX
}

export type Website = {
/** File path relative to `contentDirPath` */
_id: string
Expand Down Expand Up @@ -153,13 +168,24 @@ export type Id = {
"@id": string
}

export type ItemListElement = {
/** File path relative to `contentDirPath` */
_id: string
_raw: Local.RawDocumentData
type: 'ItemListElement'
name: string
path?: string | undefined
url?: string | undefined
itemListElement?: ItemListElement[] | undefined
}

/** Helper types */

export type AllTypes = DocumentTypes | NestedTypes
export type AllTypeNames = DocumentTypeNames | NestedTypeNames

export type DocumentTypes = Article | Organization | Page | Person | Website
export type DocumentTypeNames = 'Article' | 'Organization' | 'Page' | 'Person' | 'Website'
export type DocumentTypes = Article | Organization | Page | Person | WebPageElement | Website
export type DocumentTypeNames = 'Article' | 'Organization' | 'Page' | 'Person' | 'WebPageElement' | 'Website'

export type NestedTypes = never
export type NestedTypeNames = never
Expand All @@ -171,4 +197,5 @@ export type DataExports = {
allPages: Page[]
allPeople: Person[]
allWebsites: Website[]
allWebPageElements: WebPageElement[]
}
1 change: 1 addition & 0 deletions packages/moons/src/core/content/types/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type {
DataExports as ContentlayerDataExports,
Person as ContentlayerPerson,
Website as ContentlayerWebsite,
WebPageElement as ContentlayerWebPageElement,
} from './contentlayer.mjs'
export type * from './_schemas.mjs'
export type ContentlayerWebPageDocument = ContentlayerArticle | ContentlayerPage
37 changes: 31 additions & 6 deletions packages/moons/src/core/contentlayer.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineNestedType, type FieldDefs } from 'contentlayer/source-files'
import { defineNestedType, type DocumentTypeDef, type FieldDefs, type NestedType } from 'contentlayer/source-files'
import { collectionName } from './content/index.mjs'

const moonsFields: FieldDefs = {
Expand All @@ -15,6 +15,22 @@ const idDocumentType = defineNestedType(() => ({
fields: idFields,
}))

const itemListElementFields: NestedType = defineNestedType(() => ({
name: 'ItemListElement',
fields: {
name: { type: 'string', required: true },
path: { type: 'string', required: false },
url: { type: 'string', required: false },
itemListElement: { type: 'list', required: false, of: itemListElementFields }
}
}))

const itemListFields: FieldDefs = {
name: { type: 'string', required: false },
identifier: { type: 'string', required: true },
itemListElement: { type: 'list', required: true, of: itemListElementFields }
}

const thingsFields: FieldDefs = {
name: { type: 'string', required: true },
description: { type: 'string', required: true },
Expand All @@ -38,15 +54,24 @@ const creativeWorkFields: FieldDefs = {
keywords: { type: 'list', required: false, of: { type: 'string' } },
}

export const ContentLayerWebsiteFields = {
export const ContentLayerWebsiteFields: DocumentTypeDef = {
name: 'Website',
fields: {
...creativeWorkFields,
issn: { type: 'string', required: false }
}
}

export const ContentLayerArticleFields = {
export const ContentLayerWebPageElementFields: DocumentTypeDef = {
name: 'WebPageElement',
fields: {
elementType: { type: 'enum', options: ['SiteNavigationElement'], required: false }, // avoid collision with contentlayer type
inLanguage: { type: 'string', required: false },
...itemListFields
}
}

export const ContentLayerArticleFields: DocumentTypeDef = {
name: 'Article',
fields: {
...moonsFields,
Expand All @@ -57,7 +82,7 @@ export const ContentLayerArticleFields = {
}
}

export const ContentLayerPageFields = {
export const ContentLayerPageFields: DocumentTypeDef = {
name: 'Page',
fields: {
...moonsFields,
Expand All @@ -68,7 +93,7 @@ export const ContentLayerPageFields = {
}
}

export const ContentLayerPersonFields = {
export const ContentLayerPersonFields: DocumentTypeDef = {
name: 'Person',
fields: {
...moonsFields,
Expand All @@ -85,7 +110,7 @@ export const ContentLayerPersonFields = {
}
}

export const ContentLayerOrganizationFields = {
export const ContentLayerOrganizationFields: DocumentTypeDef = {
name: 'Organization',
fields: {
...moonsFields,
Expand Down

0 comments on commit 343e9a2

Please sign in to comment.