Skip to content

Commit

Permalink
feature: add poc for preview template support
Browse files Browse the repository at this point in the history
  • Loading branch information
julrich committed Oct 25, 2024
1 parent fe652d5 commit 610de66
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@kickstartds/jsonschema-utils",
"comment": "add preview cms annotation",
"type": "patch"
}
],
"packageName": "@kickstartds/jsonschema-utils"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@kickstartds/jsonschema2storyblok",
"comment": "add poc for preview annotation support",
"type": "minor"
}
],
"packageName": "@kickstartds/jsonschema2storyblok"
}
5 changes: 5 additions & 0 deletions tools/jsonschema-utils/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export function getSchemaRegistry({ support2019 = false }: { support2019?: boole
schemaType: 'boolean',
validate: () => true
});
ajv.addKeyword({
keyword: 'x-cms-preview',
schemaType: 'string',
validate: () => true
});

return ajv;
}
Expand Down
34 changes: 34 additions & 0 deletions tools/jsonschema2storyblok/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ function componentsEqual(componentOne: IStoryblokBlock, componentTwo: IStoryblok
return componentOne.name === componentTwo.name;
}

function isCmsAnnotatedSchema(schema: unknown): schema is JSONSchema.Interface & {
'x-cms-hidden'?: boolean;
'x-cms-preview'?: string;
} {
return (
typeof schema === 'object' &&
schema !== null &&
(('x-cms-hidden' in schema && schema['x-cms-hidden'] === true) ||
('x-cms-preview' in schema && schema['x-cms-preview'] !== undefined))
);
}

function createBlokSchema(fields: IStoryblokSchemaElement[]): Record<string, IStoryblokSchemaElement> {
return fields.reduce<Record<string, IStoryblokSchemaElement>>((schema, field) => {
schema[field.key] = field;
Expand Down Expand Up @@ -252,6 +264,13 @@ function processObject({
icon: icons[blokName] || 'block-wallet'
};

if (isCmsAnnotatedSchema(subSchema) && subSchema['x-cms-preview']) {
if (subSchema['x-cms-preview'].startsWith('field:')) {
blok.preview_field = subSchema['x-cms-preview'].split(':')[1];
} else if (subSchema['x-cms-preview'].startsWith('template:')) {
blok.preview_tmpl = subSchema['x-cms-preview'].split(':')[1];
}
}
if (description) field.description = description;

return { field, components: [blok] };
Expand Down Expand Up @@ -334,6 +353,13 @@ function processObject({
})
};
} else if (classification && classification === 'component') {
if (isCmsAnnotatedSchema(subSchema) && subSchema['x-cms-preview']) {
if (subSchema['x-cms-preview'].startsWith('field:')) {
bloks[0].preview_field = subSchema['x-cms-preview'].split(':')[1];
} else if (subSchema['x-cms-preview'].startsWith('template:')) {
bloks[0].preview_tmpl = subSchema['x-cms-preview'].split(':')[1];
}
}
return { field: dummy, components: bloks };
}
throw new Error(
Expand Down Expand Up @@ -386,6 +412,14 @@ function processRef({
blok.component_group_name = 'Components';
}

if (isCmsAnnotatedSchema(subSchema) && subSchema['x-cms-preview']) {
if (subSchema['x-cms-preview'].startsWith('field:')) {
blok.preview_field = subSchema['x-cms-preview'].split(':')[1];
} else if (subSchema['x-cms-preview'].startsWith('template:')) {
blok.preview_tmpl = subSchema['x-cms-preview'].split(':')[1];
}
}

if (description) field.description = description;

return { field, components: [blok] };
Expand Down

0 comments on commit 610de66

Please sign in to comment.