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): add product and offer types #155

Merged
merged 1 commit into from
Feb 13, 2024
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
Empty file.
7 changes: 7 additions & 0 deletions examples/contentlayer/content/organizations/brand.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
name: Brand name
description: Brand description
identifier: brand
---

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
10 changes: 10 additions & 0 deletions examples/contentlayer/content/products/product.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: Product
description: Product description
identifier: product
dateCreated: 1970-01-01
brand:
'@id': brand
---

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
2 changes: 2 additions & 0 deletions examples/contentlayer/contentlayer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PageDocumentType,
PersonDocumentType,
PlaceDocumentType,
ProductDocumentType,
WebsiteDocumentType,
WebpageElementDocumentType,
} from '@galactiks/contentlayer';
Expand All @@ -17,6 +18,7 @@ const contentLayerConfig = makeSource({
PageDocumentType,
PersonDocumentType,
PlaceDocumentType,
ProductDocumentType,
WebsiteDocumentType,
WebpageElementDocumentType,
],
Expand Down
15 changes: 12 additions & 3 deletions packages/config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const galactiksConfigFileSchema = z.object({
organizations: pagesObjectItemSchema,
pages: pagesObjectItemSchema,
people: pagesObjectItemSchema,
place: pagesObjectItemSchema,
products: pagesObjectItemSchema,
places: pagesObjectItemSchema,
tags: pagesObjectItemSchema,
})
.optional(),
Expand Down Expand Up @@ -65,6 +66,10 @@ const defaultPages: GalactiksConfig['pages'] = {
path: '/{+isPartOf}/{/identifier}/',
},

organizations: {
path: '/organizations/{/identifier}/',
},

pages: {
path: '/{+isPartOf}{/identifier}/',
},
Expand All @@ -73,8 +78,12 @@ const defaultPages: GalactiksConfig['pages'] = {
path: '/authors/{/identifier}/',
},

organizations: {
path: '/organizations/{/identifier}/',
products: {
path: '/products/{+isPartOf}/{/identifier}/',
},

places: {
path: '/places/{+isPartOf}/{/identifier}/',
},

tags: {
Expand Down
5 changes: 4 additions & 1 deletion packages/contentlayer/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ export enum pageDocumentTypes {
Organization = 'organizations',
Page = 'pages',
Person = 'people',
Place = 'place',
Place = 'places',
Product = 'products',
Tag = 'tags',
}

export enum documentTypes {
Article = pageDocumentTypes.Article,
Offer = 'offer',
Organization = pageDocumentTypes.Organization,
Page = pageDocumentTypes.Page,
Person = pageDocumentTypes.Person,
Place = pageDocumentTypes.Place,
Product = pageDocumentTypes.Product,
Tag = pageDocumentTypes.Tag,
WebPageElement = 'webPageElements',
Website = 'websites',
Expand Down
8 changes: 8 additions & 0 deletions packages/contentlayer/src/document-types/product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineDocumentType } from 'contentlayer/source-files';
import { ContentLayerProductFields } from '../fields/product.js';

export const ProductDocumentType = defineDocumentType(() => ({
...ContentLayerProductFields,
filePathPattern: 'products/**/*.md?(x)',
contentType: 'mdx',
}));
63 changes: 63 additions & 0 deletions packages/contentlayer/src/fields/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ export const idDocumentType = defineNestedType(() => ({
fields: idFields,
}));

/*
* Those fields are following schema.org fields specifications:
* - https://schema.org/inLanguage
* - https://schema.org/translationOfWork
* - https://schema.org/workTranslation
*
* Those fields are applied for every document types when a readable text exists (ex: description, name, ...etc).
*/
export const translationFields: FieldDefs = {
inLanguage: { type: 'string', required: false },
translationOfWork: { type: 'nested', of: idDocumentType, required: false },
workTranslation: { type: 'nested', of: idDocumentType, required: false },
};

export const itemListElementFields: NestedType = defineNestedType(() => ({
name: 'ItemListElement',
fields: {
Expand Down Expand Up @@ -49,3 +63,52 @@ export const postalAddress = defineNestedType(() => ({
streetAddress: { type: 'string', required: false },
},
}));

export const quantitativeValue = defineNestedType(() => ({
name: 'QuantitativeValue',
fields: {
maxValue: { type: 'number', required: false },
minValue: { type: 'number', required: false },
value: { type: 'number', required: true },
},
}));

const EUEnergyEfficiencyEnumeration = [
'EUEnergyEfficiencyCategoryA',
'EUEnergyEfficiencyCategoryA1Plus',
'EUEnergyEfficiencyCategoryA2Plus',
'EUEnergyEfficiencyCategoryA3Plus',
'EUEnergyEfficiencyCategoryB',
'EUEnergyEfficiencyCategoryC',
'EUEnergyEfficiencyCategoryD',
'EUEnergyEfficiencyCategoryE',
'EUEnergyEfficiencyCategoryF',
'EUEnergyEfficiencyCategoryG',
];
export const energyConsumptionDetails = defineNestedType(() => ({
name: 'EnergyConsumptionDetails',
fields: {
energyEfficiencyScaleMax: {
type: 'enum',
options: EUEnergyEfficiencyEnumeration,
required: false,
},
energyEfficiencyScaleMin: {
type: 'enum',
options: EUEnergyEfficiencyEnumeration,
required: false,
},
},
}));

export const propertyValue = defineNestedType(() => ({
name: 'PropertyValue',
fields: {
name: { type: 'string', required: true },
value: { type: 'string', required: true },
minValue: { type: 'number', required: false },
maxValue: { type: 'number', required: false },
unitCode: { type: 'string', required: false },
unitText: { type: 'string', required: false },
},
}));
4 changes: 0 additions & 4 deletions packages/contentlayer/src/fields/creative-work.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { FieldDefs } from 'contentlayer/source-files';
import { idDocumentType } from './core.js';
import { thingsFields } from './things.js';

export const creativeWorkFields: FieldDefs = {
Expand All @@ -10,8 +9,5 @@ export const creativeWorkFields: FieldDefs = {
dateModified: { type: 'date', required: false },
datePublished: { type: 'date', required: false },
isPartOf: { type: 'string', required: false },
inLanguage: { type: 'string', required: false },
translationOfWork: { type: 'nested', of: idDocumentType, required: false },
workTranslation: { type: 'nested', of: idDocumentType, required: false },
keywords: { type: 'list', required: false, of: { type: 'string' } },
};
15 changes: 15 additions & 0 deletions packages/contentlayer/src/fields/offer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { DocumentTypeDef } from '../consts.js';
import { galactiksFields, idDocumentType, propertyValue } from './core.js';
import { thingsFields } from './things.js';

export const ContentLayerOfferFields: DocumentTypeDef = {
name: 'Offer',
fields: {
...galactiksFields,
...thingsFields,
additionalProperty: { type: 'nested', of: propertyValue },
price: { type: 'number', required: true },
priceCurrency: { type: 'string', required: true },
seller: { type: 'nested', of: idDocumentType, required: false },
},
};
38 changes: 38 additions & 0 deletions packages/contentlayer/src/fields/product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { DocumentTypeDef } from '../consts.js';
import {
energyConsumptionDetails,
galactiksFields,
idDocumentType,
quantitativeValue,
} from './core.js';
import { thingsFields } from './things.js';
import { ContentLayerOfferFields } from './offer.js';

export const ContentLayerProductFields: DocumentTypeDef = {
name: 'Product',
fields: {
...galactiksFields,
...thingsFields,
brand: { type: 'nested', of: idDocumentType, required: false },
color: { type: 'string', required: false },
depth: { type: 'nested', of: quantitativeValue, required: false },
gtin: { type: 'string', required: false },
hasEnergyConsumptionDetails: {
type: 'nested',
of: energyConsumptionDetails,
required: false,
},
hasMeasurement: { type: 'nested', of: quantitativeValue, required: false },
height: { type: 'nested', of: quantitativeValue, required: false },
keywords: { type: 'list', required: false, of: { type: 'string' } },
manufacturer: { type: 'nested', of: idDocumentType, required: false },
model: { type: 'nested', of: idDocumentType, required: false },
offers: {
type: 'list',
of: { type: 'nested', def: () => ContentLayerOfferFields },
},
sku: { type: 'string', required: false },
weight: { type: 'nested', of: quantitativeValue, required: false },
width: { type: 'nested', of: quantitativeValue, required: false },
},
};
2 changes: 2 additions & 0 deletions packages/contentlayer/src/fields/things.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { FieldDefs } from 'contentlayer/source-files';
import { translationFields } from './core.js';

export const thingsFields: FieldDefs = {
...translationFields,
name: { type: 'string', required: true },
description: { type: 'string', required: true },
url: { type: 'string', required: false },
Expand Down
4 changes: 2 additions & 2 deletions packages/contentlayer/src/fields/webpage-element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DocumentTypeDef } from '../consts.js';
import { itemListFields } from './core.js';
import { itemListFields, translationFields } from './core.js';

export const ContentLayerWebPageElementFields: DocumentTypeDef = {
name: 'WebPageElement',
Expand All @@ -9,7 +9,7 @@ export const ContentLayerWebPageElementFields: DocumentTypeDef = {
options: ['SiteNavigationElement', 'WPFooter', 'WPHeader', 'WPSideBar'],
required: false,
}, // avoid collision with contentlayer type
inLanguage: { type: 'string', required: false },
...translationFields, //
...itemListFields,
},
};
1 change: 1 addition & 0 deletions packages/contentlayer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { OrganizationDocumentType } from './document-types/organization.js';
export { PageDocumentType } from './document-types/page.js';
export { PersonDocumentType } from './document-types/person.js';
export { PlaceDocumentType } from './document-types/place.js';
export { ProductDocumentType } from './document-types/product.js';
export { WebpageElementDocumentType } from './document-types/webpage-element.js';
export { WebsiteDocumentType } from './document-types/website.js';

Expand Down
Loading
Loading