Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@prismicio/api-renderer

Package Overview
Dependencies
Maintainers
0
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@prismicio/api-renderer - npm Package Compare versions

Comparing version 5.0.0 to 6.0.0-alpha.0

src/widgets/RepeatableRenderer.ts

2

lib/DocumentRenderer.js

@@ -12,3 +12,3 @@ "use strict";

function isStaticWidgetContent(content) {
return (0, content_1.isGroupContent)(content) || (0, content_1.isNestableContent)(content);
return (0, content_1.isGroupContent)(content) || (0, content_1.isRepeatableContent)(content) || (0, content_1.isNestableContent)(content);
}

@@ -15,0 +15,0 @@ function i18nWriter(doc) {

@@ -142,2 +142,3 @@ import * as t from "io-ts";

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -313,2 +314,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -485,2 +487,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -653,2 +656,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -827,2 +831,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -995,2 +1000,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -997,0 +1003,0 @@ }) | ({

@@ -6,8 +6,9 @@ import type { LinkConfig } from "@prismicio/types-internal/lib/customtypes";

allowText: t.BooleanC;
repeat: t.BooleanC;
}>;
export type AnyLinkDef = {
export type AnyLinkDef = Omit<t.TypeOf<typeof codec>, "select"> & {
select: null;
} & Pick<t.TypeOf<typeof codec>, "allowText">;
};
export declare const AnyLinkDef: t.Type<AnyLinkDef, AnyLinkDef, unknown>;
export declare function defaultAnyLinkDef(config?: Omit<LinkConfig, "select" | "customtypes">): AnyLinkDef;
export {};

@@ -11,2 +11,3 @@ "use strict";

allowText: t.boolean,
repeat: t.boolean,
});

@@ -16,4 +17,4 @@ exports.AnyLinkDef = new t.Type("AnyLinkDef", (u) => u.select === "any", (u) => {

return {
...decoded,
select: null,
allowText: decoded.allowText,
};

@@ -20,0 +21,0 @@ }));

@@ -8,4 +8,5 @@ import type { LinkConfig } from "@prismicio/types-internal/lib/customtypes";

allowText: t.BooleanC;
repeat: t.BooleanC;
}>]>;
export type DocumentLinkDef = t.TypeOf<typeof DocumentLinkDef>;
export declare function defaultDocumentLinkDef(config?: Omit<LinkConfig, "select" | "customtypes">): DocumentLinkDef;

@@ -13,2 +13,3 @@ "use strict";

allowText: t.boolean,
repeat: t.boolean,
}),

@@ -15,0 +16,0 @@ ]);

@@ -9,2 +9,3 @@ import { Link } from "@prismicio/types-internal/lib/customtypes/widgets/nestable";

allowText: t.BooleanC;
repeat: t.BooleanC;
}>]>, t.IntersectionC<[t.ExactC<t.TypeC<{

@@ -15,2 +16,3 @@ select: t.LiteralC<"document">;

allowText: t.BooleanC;
repeat: t.BooleanC;
}>]>, t.IntersectionC<[t.ExactC<t.TypeC<{

@@ -20,2 +22,3 @@ select: t.LiteralC<"web">;

allowText: t.BooleanC;
repeat: t.BooleanC;
}>]>, t.Type<AnyLinkDef, AnyLinkDef, unknown>]>;

@@ -37,2 +40,3 @@ type LinkConfig = t.TypeOf<typeof LinkConfig>;

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -53,2 +57,3 @@ }>, WithKey<{

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -55,0 +60,0 @@ }>, unknown>;

@@ -7,4 +7,5 @@ import type { LinkConfig } from "@prismicio/types-internal/lib/customtypes";

allowText: t.BooleanC;
repeat: t.BooleanC;
}>]>;
export type MediaLinkDef = t.TypeOf<typeof MediaLinkDef>;
export declare function defaultMediaLinkDef(config?: Omit<LinkConfig, "select" | "customtypes">): MediaLinkDef;

@@ -12,2 +12,3 @@ "use strict";

allowText: t.boolean,
repeat: t.boolean,
}),

@@ -14,0 +15,0 @@ ]);

@@ -7,4 +7,5 @@ import type { LinkConfig } from "@prismicio/types-internal/lib/customtypes";

allowText: t.BooleanC;
repeat: t.BooleanC;
}>]>;
export type WebLinkDef = t.TypeOf<typeof WebLinkDef>;
export declare function defaultWebLinkDef(config?: Omit<LinkConfig, "select" | "customtypes">): WebLinkDef;

@@ -12,2 +12,3 @@ "use strict";

allowText: t.boolean,
repeat: t.boolean,
}),

@@ -14,0 +15,0 @@ ]);

@@ -300,2 +300,3 @@ import * as t from "io-ts";

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -316,3 +317,4 @@ }>, import("../..").WithKey<{

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;
}>, unknown>]>;

@@ -140,2 +140,3 @@ import * as t from "io-ts";

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -301,2 +302,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -472,2 +474,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -633,2 +636,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -635,0 +639,0 @@ }) | ({

@@ -132,2 +132,3 @@ import * as t from "io-ts";

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -300,2 +301,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -461,2 +463,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -629,2 +632,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -631,0 +635,0 @@ }) | ({

@@ -8,3 +8,3 @@ export * from "./ApiDocument";

export type { default as RenderContext } from "./RenderContext";
export type { LinkRenderer, Renderer } from "./Renderer";
export type { LinkRenderer, LinkRendererOptions, Renderer } from "./Renderer";
export type { default as UrlRewriter } from "./URLRewriter";

@@ -1,15 +0,18 @@

import type { AnyLinkContent, DocumentLinkContent, ExternalLinkContent, FileLinkContent, ImageLinkContent, MediaLinkContent, SliceContent, WidgetContent } from "@prismicio/types-internal/lib/content";
import type { LinkContent, SliceContent, WidgetContent } from "@prismicio/types-internal/lib/content";
import type { StaticSlice, StaticWidget } from "@prismicio/types-internal/lib/customtypes";
import type { AnyLinkDef, DocumentLinkDef, MediaLinkDef, WebLinkDef } from "./customtypes";
import type { Field, GroupOrField, Slice } from "./fetch";
export interface Renderer<D extends StaticWidget, C extends WidgetContent, F extends Field | undefined = undefined> {
renderV1(content: C, fetch?: F | undefined): unknown;
renderV2(definition: D, content: C, fetch?: F | undefined): unknown;
export interface Renderer<D extends StaticWidget, C extends WidgetContent, F extends Field | undefined = undefined, O = unknown> {
renderV1(content: C, fetch?: F | undefined, options?: O): unknown;
renderV2(definition: D, content: C, fetch?: F | undefined, options?: O): unknown;
renderDefault(definition: D): unknown;
renderMocks(definition: D, content: C): unknown;
renderMocks(definition: D, content: C, options?: O): unknown;
}
export interface LinkRenderer<D extends MediaLinkDef | DocumentLinkDef | WebLinkDef | MediaLinkDef | AnyLinkDef, C extends DocumentLinkContent | FileLinkContent | ExternalLinkContent | ImageLinkContent | MediaLinkContent | AnyLinkContent, F extends Field | undefined = undefined> {
renderV1(content: C, fetch?: F | undefined): unknown;
renderV2(definition: D | AnyLinkDef, content: C, fetch?: F | undefined): unknown;
renderMocks(definition: D | AnyLinkDef, content: C): unknown;
export type LinkRendererOptions = {
omitKey: boolean;
};
export interface LinkRenderer<D extends MediaLinkDef | DocumentLinkDef | WebLinkDef | MediaLinkDef | AnyLinkDef, C extends LinkContent, F extends Field | undefined = undefined, O = LinkRendererOptions> {
renderV1(content: C, fetch?: F | undefined, options?: O): unknown;
renderV2(definition: D | AnyLinkDef, content: C, fetch?: F | undefined, options?: O): unknown;
renderMocks(definition: D | AnyLinkDef, content: C, options?: O): unknown;
}

@@ -16,0 +19,0 @@ export interface SliceRenderer<D extends StaticSlice, C extends SliceContent, F extends Slice | GroupOrField> {

@@ -1,4 +0,6 @@

import type { AnyLinkContent } from "@prismicio/types-internal/lib/content";
import type { AnyLinkDef, LinkRenderer } from "../../../models";
declare const AnyLinkRenderer: LinkRenderer<AnyLinkDef, AnyLinkContent>;
import type { AnyLinkContent, LinkContent } from "@prismicio/types-internal/lib/content";
import type { AnyLinkDef, LinkRenderer, LinkRendererOptions } from "../../../models";
declare const AnyLinkRenderer: LinkRenderer<AnyLinkDef, LinkContent & {
value: AnyLinkContent;
}, undefined, LinkRendererOptions>;
export default AnyLinkRenderer;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AnyLinkRenderer = {
renderV1(link) {
renderV1(link, _fetch, options) {
return {
type: "Link.any",
value: {
...(link.text && { text: link.text }),
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(link.value.text && { text: link.value.text }),
},
};
},
renderV2(def, link) {
renderV2(def, link, _fetch, options) {
return {
link_type: "Any",
...(def.allowText && link.text && { text: link.text }),
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(def.allowText && link.value.text && { text: link.value.text }),
};
},
renderMocks(def, link) {
return this.renderV2(def, link);
renderMocks(def, link, options) {
return this.renderV2(def, link, undefined, options);
},
};
exports.default = AnyLinkRenderer;

@@ -1,7 +0,9 @@

import { type DocumentLinkContent } from "@prismicio/types-internal/lib/content";
import type { RenderContext } from "../../../models";
import { type DocumentLinkContent, LinkContent } from "@prismicio/types-internal/lib/content";
import type { LinkRendererOptions, RenderContext } from "../../../models";
import { DocumentLinkDef, LinkRenderer } from "../../../models";
import type { DocRelation } from "../../../models/fetch";
export declare const BROKEN_CUSTOM_TYPE = "broken_type";
declare const DocumentLinkRenderer: (ctx: RenderContext) => LinkRenderer<DocumentLinkDef, DocumentLinkContent, DocRelation>;
declare const DocumentLinkRenderer: (ctx: RenderContext) => LinkRenderer<DocumentLinkDef, LinkContent & {
value: DocumentLinkContent;
}, DocRelation, LinkRendererOptions>;
export default DocumentLinkRenderer;

@@ -7,19 +7,21 @@ "use strict";

exports.BROKEN_CUSTOM_TYPE = "broken_type";
function brokenLinkV1(link) {
function brokenLinkV1(link, options) {
return {
type: "Link.document",
value: {
...(content_1.FilledDocumentLinkContent.is(link) && { id: link.id, tags: [] }),
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(content_1.FilledDocumentLinkContent.is(link.value) && { id: link.value.id, tags: [] }),
type: exports.BROKEN_CUSTOM_TYPE,
isBroken: true,
...(link.text && { text: link.text }),
...(link.value.text && { text: link.value.text }),
},
};
}
function brokenLinkV2(link, def) {
function brokenLinkV2(link, def, options) {
return {
...(content_1.FilledDocumentLinkContent.is(link) && { id: link.id, tags: [] }),
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(content_1.FilledDocumentLinkContent.is(link.value) && { id: link.value.id, tags: [] }),
type: exports.BROKEN_CUSTOM_TYPE,
isBroken: true,
...(def.allowText && link.text && { text: link.text }),
...(def.allowText && link.value.text && { text: link.value.text }),
};

@@ -30,9 +32,10 @@ }

return {
renderV1(link, fetch) {
renderV1(link, fetch, options) {
if (!extension)
return brokenLinkV1(link);
return brokenLinkV1(link, options);
//todo check fetch type
try {
if (content_1.FilledDocumentLinkContent.is(link)) {
const id = extension.DocEncoder.encodeDocId(link.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [link.text]);
if (content_1.FilledDocumentLinkContent.is(link.value)) {
const linkKey = (options === null || options === void 0 ? void 0 : options.omitKey) ? undefined : link.key;
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [linkKey, link.value.text]);
return {

@@ -46,3 +49,4 @@ type: "Link.document",

value: {
...(link.text && { text: link.text }),
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(link.value.text && { text: link.value.text }),
},

@@ -52,13 +56,14 @@ };

catch (e) {
return brokenLinkV1(link);
return brokenLinkV1(link, options);
}
},
renderV2(def, link, fetch) {
renderV2(def, link, fetch, options) {
if (!extension)
return brokenLinkV2(link, def);
return brokenLinkV2(link, def, options);
//todo check fetch type
try {
const linkText = def.allowText ? link.text : undefined;
if (content_1.FilledDocumentLinkContent.is(link)) {
const id = extension.DocEncoder.encodeDocId(link.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [linkText]);
const linkText = def.allowText ? link.value.text : undefined;
if (content_1.FilledDocumentLinkContent.is(link.value)) {
const linkKey = (options === null || options === void 0 ? void 0 : options.omitKey) ? undefined : link.key;
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [linkKey, linkText]);
return extension.encoders.documentLinks.encode(id);

@@ -68,2 +73,3 @@ }

link_type: "Document",
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(linkText && { text: linkText }),

@@ -73,12 +79,13 @@ };

catch (e) {
return brokenLinkV2(link, def);
return brokenLinkV2(link, def, options);
}
},
renderMocks(def, link) {
if (content_1.FilledDocumentLinkContent.is(link)) {
renderMocks(def, link, options) {
if (content_1.FilledDocumentLinkContent.is(link.value)) {
const type = (models_1.DocumentLinkDef.is(def) && def.customtypes[0]) || "mock";
return {
id: link.id,
...(def.allowText && link.text && { text: link.text }),
id: link.value.id,
...(def.allowText && link.value.text && { text: link.value.text }),
link_type: "Document",
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
type,

@@ -92,3 +99,3 @@ tags: [],

}
return brokenLinkV2(link, def);
return brokenLinkV2(link, def, options);
},

@@ -95,0 +102,0 @@ };

@@ -1,4 +0,6 @@

import { type ExternalLinkContent } from "@prismicio/types-internal/lib/content";
import type { LinkRenderer, WebLinkDef } from "../../../models";
declare const ExternalLinkRenderer: LinkRenderer<WebLinkDef, ExternalLinkContent>;
import { type ExternalLinkContent, LinkContent } from "@prismicio/types-internal/lib/content";
import type { LinkRenderer, LinkRendererOptions, WebLinkDef } from "../../../models";
declare const ExternalLinkRenderer: LinkRenderer<WebLinkDef, LinkContent & {
value: ExternalLinkContent;
}, undefined, LinkRendererOptions>;
export default ExternalLinkRenderer;

@@ -5,28 +5,30 @@ "use strict";

const ExternalLinkRenderer = {
renderV1(link) {
renderV1(link, _fetch, options) {
return {
type: "Link.web",
value: {
...(content_1.FilledExternalLinkContent.is(link) && {
url: link.url,
...(typeof link.target === "string" && { target: link.target }),
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(content_1.FilledExternalLinkContent.is(link.value) && {
url: link.value.url,
...(typeof link.value.target === "string" && { target: link.value.target }),
}),
...(link.text && { text: link.text }),
...(link.value.text && { text: link.value.text }),
},
};
},
renderV2(def, link) {
renderV2(def, link, _fetch, options) {
return {
link_type: "Web",
...(content_1.FilledExternalLinkContent.is(link) && {
url: link.url,
...(typeof link.target === "string" && { target: link.target }),
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(content_1.FilledExternalLinkContent.is(link.value) && {
url: link.value.url,
...(typeof link.value.target === "string" && { target: link.value.target }),
}),
...(def.allowText && link.text && { text: link.text }),
...(def.allowText && link.value.text && { text: link.value.text }),
};
},
renderMocks(def, link) {
return this.renderV2(def, link);
renderMocks(def, link, options) {
return this.renderV2(def, link, undefined, options);
},
};
exports.default = ExternalLinkRenderer;

@@ -1,5 +0,7 @@

import { type FileLinkContent } from "@prismicio/types-internal/lib/content";
import type { RenderContext } from "../../../models";
import { type FileLinkContent, LinkContent } from "@prismicio/types-internal/lib/content";
import type { LinkRendererOptions, RenderContext } from "../../../models";
import type { LinkRenderer, MediaLinkDef } from "../../../models";
declare const FileLinkRenderer: (ctx: RenderContext) => LinkRenderer<MediaLinkDef, FileLinkContent>;
declare const FileLinkRenderer: (ctx: RenderContext) => LinkRenderer<MediaLinkDef, LinkContent & {
value: FileLinkContent;
}, undefined, LinkRendererOptions>;
export default FileLinkRenderer;

@@ -5,14 +5,15 @@ "use strict";

const FileLinkRenderer = (ctx) => ({
renderV1(link) {
renderV1(link, _fetch, options) {
return {
type: "Link.file",
value: {
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
file: {
kind: link.kind,
...(content_1.FilledFileLinkContent.is(link) && {
url: ctx.urlRewriter.rewriteFileUrl(link.url),
name: link.name,
size: link.size,
kind: link.value.kind,
...(content_1.FilledFileLinkContent.is(link.value) && {
url: ctx.urlRewriter.rewriteFileUrl(link.value.url),
name: link.value.name,
size: link.value.size,
}),
...(link.text && { text: link.text }),
...(link.value.text && { text: link.value.text }),
},

@@ -22,19 +23,20 @@ },

},
renderV2(def, link) {
renderV2(def, link, _fetch, options) {
return {
link_type: "Media",
kind: link.kind,
...(content_1.FilledFileLinkContent.is(link) && {
id: link.id,
url: ctx.urlRewriter.rewriteFileUrl(link.url),
name: link.name,
size: link.size,
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
kind: link.value.kind,
...(content_1.FilledFileLinkContent.is(link.value) && {
id: link.value.id,
url: ctx.urlRewriter.rewriteFileUrl(link.value.url),
name: link.value.name,
size: link.value.size,
}),
...(def.allowText && link.text && { text: link.text }),
...(def.allowText && link.value.text && { text: link.value.text }),
};
},
renderMocks(def, link) {
return this.renderV2(def, link);
renderMocks(def, link, options) {
return this.renderV2(def, link, undefined, options);
},
});
exports.default = FileLinkRenderer;

@@ -1,5 +0,7 @@

import { type ImageLinkContent } from "@prismicio/types-internal/lib/content";
import type { RenderContext } from "../../../models";
import { type ImageLinkContent, LinkContent } from "@prismicio/types-internal/lib/content";
import type { LinkRendererOptions, RenderContext } from "../../../models";
import type { LinkRenderer, MediaLinkDef } from "../../../models";
declare const ImageLinkRenderer: (ctx: RenderContext) => LinkRenderer<MediaLinkDef, ImageLinkContent>;
declare const ImageLinkRenderer: (ctx: RenderContext) => LinkRenderer<MediaLinkDef, LinkContent & {
value: ImageLinkContent;
}, undefined, LinkRendererOptions>;
export default ImageLinkRenderer;

@@ -5,17 +5,18 @@ "use strict";

const ImageLinkRenderer = (ctx) => ({
renderV1(link) {
renderV1(link, _fetch, options) {
return {
type: "Link.image",
value: {
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
image: {
kind: link.kind,
...(content_1.FilledImageLinkContent.is(link) && {
id: link.id,
url: ctx.urlRewriter.optimizeImageUrl(link.url),
name: link.name,
size: link.size,
width: link.width,
height: link.height,
kind: link.value.kind,
...(content_1.FilledImageLinkContent.is(link.value) && {
id: link.value.id,
url: ctx.urlRewriter.optimizeImageUrl(link.value.url),
name: link.value.name,
size: link.value.size,
width: link.value.width,
height: link.value.height,
}),
...(link.text && { text: link.text }),
...(link.value.text && { text: link.value.text }),
},

@@ -25,21 +26,22 @@ },

},
renderV2(def, link) {
renderV2(def, link, _fetch, options) {
return {
link_type: "Media",
kind: link.kind,
...(content_1.FilledImageLinkContent.is(link) && {
id: link.id,
url: ctx.urlRewriter.optimizeImageUrl(link.url),
name: link.name,
size: link.size,
width: link.width,
height: link.height,
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
kind: link.value.kind,
...(content_1.FilledImageLinkContent.is(link.value) && {
id: link.value.id,
url: ctx.urlRewriter.optimizeImageUrl(link.value.url),
name: link.value.name,
size: link.value.size,
width: link.value.width,
height: link.value.height,
}),
...(def.allowText && link.text && { text: link.text }),
...(def.allowText && link.value.text && { text: link.value.text }),
};
},
renderMocks(def, link) {
return this.renderV2(def, link);
renderMocks(def, link, options) {
return this.renderV2(def, link, undefined, options);
},
});
exports.default = ImageLinkRenderer;

@@ -17,20 +17,20 @@ "use strict";

const LinkRenderer = (ctx) => ({
renderV1(link, fetch) {
renderV1(link, fetch, options) {
if (link.value.__TYPE__ === "ImageLink") {
return (0, ImageLinkRenderer_1.default)(ctx).renderV1(link.value);
return (0, ImageLinkRenderer_1.default)(ctx).renderV1({ ...link, value: link.value }, undefined, options);
}
else if (link.value.__TYPE__ === "DocumentLink") {
return (0, DocumentLinkRenderer_1.default)(ctx).renderV1(link.value, fetch);
return (0, DocumentLinkRenderer_1.default)(ctx).renderV1({ ...link, value: link.value }, fetch, options);
}
else if (link.value.__TYPE__ === "FileLink") {
return (0, FileLinkRenderer_1.default)(ctx).renderV1(link.value);
return (0, FileLinkRenderer_1.default)(ctx).renderV1({ ...link, value: link.value }, undefined, options);
}
else if (link.value.__TYPE__ === "ExternalLink") {
return ExternalLinkRenderer_1.default.renderV1(link.value);
return ExternalLinkRenderer_1.default.renderV1({ ...link, value: link.value }, undefined, options);
}
else if (link.value.__TYPE__ === "MediaLink") {
return MediaLinkRenderer_1.default.renderV1(link.value);
return MediaLinkRenderer_1.default.renderV1({ ...link, value: link.value }, undefined, options);
}
else if (link.value.__TYPE__ === "AnyLink") {
return AnyLinkRenderer_1.default.renderV1(link.value);
return AnyLinkRenderer_1.default.renderV1({ ...link, value: link.value }, undefined, options);
}

@@ -41,20 +41,26 @@ else {

},
renderV2(def, content, fetch) {
renderV2(def, content, fetch, options) {
if (((0, models_1.isMediaLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "ImageLink") {
return (0, ImageLinkRenderer_1.default)(ctx).renderV2(def.config, content.value);
return (0, ImageLinkRenderer_1.default)(ctx).renderV2(def.config, {
...content,
value: content.value,
}, undefined, options);
}
else if (((0, models_1.isDocumentLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "DocumentLink") {
return (0, DocumentLinkRenderer_1.default)(ctx).renderV2(def.config, content.value, fetch);
return (0, DocumentLinkRenderer_1.default)(ctx).renderV2(def.config, { ...content, value: content.value }, fetch, options);
}
else if (((0, models_1.isMediaLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "FileLink") {
return (0, FileLinkRenderer_1.default)(ctx).renderV2(def.config, content.value);
return (0, FileLinkRenderer_1.default)(ctx).renderV2(def.config, {
...content,
value: content.value,
}, undefined, options);
}
else if (((0, models_1.isWebLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "ExternalLink") {
return ExternalLinkRenderer_1.default.renderV2(def.config, content.value);
return ExternalLinkRenderer_1.default.renderV2(def.config, { ...content, value: content.value }, undefined, options);
}
else if (((0, models_1.isMediaLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "MediaLink") {
return MediaLinkRenderer_1.default.renderV2(def.config, content.value);
return MediaLinkRenderer_1.default.renderV2(def.config, { ...content, value: content.value }, undefined, options);
}
else if ((0, models_1.isAnyLinkDef)(def) && content.value.__TYPE__ === "AnyLink") {
return AnyLinkRenderer_1.default.renderV2(def.config, content.value);
return AnyLinkRenderer_1.default.renderV2(def.config, { ...content, value: content.value }, undefined, options);
}

@@ -86,20 +92,32 @@ else {

},
renderMocks(def, content) {
renderMocks(def, content, options) {
if (((0, models_1.isMediaLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "ImageLink") {
return (0, ImageLinkRenderer_1.default)(ctx).renderMocks(def.config, content.value);
return (0, ImageLinkRenderer_1.default)(ctx).renderMocks(def.config, {
...content,
value: content.value,
}, options);
}
else if (((0, models_1.isDocumentLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "DocumentLink") {
return (0, DocumentLinkRenderer_1.default)(ctx).renderMocks(def.config, content.value);
return (0, DocumentLinkRenderer_1.default)(ctx).renderMocks(def.config, {
...content,
value: content.value,
}, options);
}
else if (((0, models_1.isMediaLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "FileLink") {
return (0, FileLinkRenderer_1.default)(ctx).renderMocks(def.config, content.value);
return (0, FileLinkRenderer_1.default)(ctx).renderMocks(def.config, {
...content,
value: content.value,
}, options);
}
else if (((0, models_1.isWebLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "ExternalLink") {
return ExternalLinkRenderer_1.default.renderMocks(def.config, content.value);
return ExternalLinkRenderer_1.default.renderMocks(def.config, {
...content,
value: content.value,
}, options);
}
else if (((0, models_1.isMediaLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "MediaLink") {
return MediaLinkRenderer_1.default.renderMocks(def.config, content.value);
return MediaLinkRenderer_1.default.renderMocks(def.config, { ...content, value: content.value }, options);
}
else if ((0, models_1.isAnyLinkDef)(def) && content.value.__TYPE__ === "AnyLink") {
return AnyLinkRenderer_1.default.renderMocks(def.config, content.value);
return AnyLinkRenderer_1.default.renderMocks(def.config, { ...content, value: content.value }, options);
}

@@ -106,0 +124,0 @@ else {

@@ -1,4 +0,6 @@

import type { MediaLinkContent } from "@prismicio/types-internal/lib/content";
import type { LinkRenderer, MediaLinkDef } from "../../../models";
declare const MediaLinkRenderer: LinkRenderer<MediaLinkDef, MediaLinkContent>;
import type { LinkContent, MediaLinkContent } from "@prismicio/types-internal/lib/content";
import type { LinkRenderer, LinkRendererOptions, MediaLinkDef } from "../../../models";
declare const MediaLinkRenderer: LinkRenderer<MediaLinkDef, LinkContent & {
value: MediaLinkContent;
}, undefined, LinkRendererOptions>;
export default MediaLinkRenderer;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const MediaLinkRenderer = {
renderV1(link) {
renderV1(link, _fetch, options) {
return {
type: "Link.media",
value: {
...(link.text && { text: link.text }),
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(link.value.text && { text: link.value.text }),
},
};
},
renderV2(def, link) {
renderV2(def, link, _fetch, options) {
return {
link_type: "Media",
...(def.allowText && link.text && { text: link.text }),
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }),
...(def.allowText && link.value.text && { text: link.value.text }),
};
},
renderMocks(def, link) {
return this.renderV2(def, link);
renderMocks(def, link, options) {
return this.renderV2(def, link, undefined, options);
},
};
exports.default = MediaLinkRenderer;

@@ -29,9 +29,9 @@ "use strict";

if (apiVersion === models_1.ApiVersion.v1) {
return (0, Link_1.default)(ctx).renderV1(link, undefined);
return (0, Link_1.default)(ctx).renderV1(link, undefined, { omitKey: true });
}
else if (apiVersion === models_1.ApiVersion.v2) {
return (0, Link_1.default)(ctx).renderV2((0, models_2.defaultLink)(linkType(link)), link, undefined);
return (0, Link_1.default)(ctx).renderV2((0, models_2.defaultLink)(linkType(link)), link, undefined, { omitKey: true });
}
else if (apiVersion === models_1.ApiVersion.mocks) {
return (0, Link_1.default)(ctx).renderMocks((0, models_2.defaultLink)(linkType(link)), link);
return (0, Link_1.default)(ctx).renderMocks((0, models_2.defaultLink)(linkType(link)), link, { omitKey: true });
}

@@ -47,2 +47,6 @@ }

__TYPE__: "LinkContent",
// When a Link is used within a StructuredText, the key is not present,
// we need to specify one to respect LinkContent type
// but the Renderer will not use it thanks to `omitKey` value.
key: "",
value: block.data.linkTo,

@@ -49,0 +53,0 @@ };

@@ -1,2 +0,2 @@

import type { RepeatableContent } from "@prismicio/types-internal/lib/content/fields/RepeatableContent";
import type { RepeatableContent } from "@prismicio/types-internal/lib/content";
import type { NestableWidget } from "@prismicio/types-internal/lib/customtypes";

@@ -3,0 +3,0 @@ import type { RenderContext, Renderer } from "../models";

@@ -15,5 +15,4 @@ "use strict";

return (0, StaticWidgetRenderer_1.default)(ctx).renderV2(
// We need to override the repeat value to false in order to not
// propagate the fact the item is this repeatable.
// Each item inside a repeatable array is not repeatable anymore.
// Override the repeat value to false to prevent propagating the repeat configuration.
// Items inside a repeatable array should not be repeatable.
{

@@ -34,5 +33,4 @@ ...def,

return (0, StaticWidgetRenderer_1.default)(ctx).renderMocks(
// We need to override the repeat value to false in order to not
// propagate the fact the item is this repeatable.
// Each item inside a repeatable array is not repeatable anymore.
// Override the repeat value to false to prevent propagating the repeat configuration.
// Items inside a repeatable array should not be repeatable.
{

@@ -39,0 +37,0 @@ ...def,

@@ -132,2 +132,3 @@ import type { RenderContext } from "../models";

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -273,7 +274,9 @@ }) | ({

value: string;
} | {
} | ({
key: string;
} & {
__TYPE__: "LinkContent";
value: ({
__TYPE__: "ImageLink";
} & (({
} & ((({
kind: "image";

@@ -288,10 +291,9 @@ id: string;

date?: string | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "image";
text: string;
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "FileLink";
} & (({
} & ((({
kind: "file";

@@ -304,7 +306,6 @@ id: string;

date?: string | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "file";
text: string;
}) & {
text?: string | undefined;
})) | ({

@@ -314,10 +315,9 @@ __TYPE__: "DocumentLink";

id: string;
} & {
} | {
kind: "document";
}) & {
text?: string | undefined;
}) | {
kind: "document";
text: string;
})) | ({
__TYPE__: "ExternalLink";
} & (({
} & ((({
url: string;

@@ -330,7 +330,6 @@ } & {

} | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "web";
text: string;
}) & {
text?: string | undefined;
})) | ({

@@ -340,9 +339,12 @@ __TYPE__: "MediaLink";

kind: "media";
text: string;
} & {
text?: string | undefined;
}) | ({
__TYPE__: "AnyLink";
} & {
text: string;
kind: "any";
} & {
text?: string | undefined;
});
} | {
}) | {
__TYPE__: "StructuredTextContent";

@@ -502,3 +504,76 @@ value: (({

__TYPE__: "SeparatorContent";
}, Field>, ({
} | {
__TYPE__: "RepeatableContent";
type: "Link";
value: ({
key: string;
} & {
__TYPE__: "LinkContent";
value: ({
__TYPE__: "ImageLink";
} & ((({
kind: "image";
id: string;
url: string;
height: string;
width: string;
size: string;
name: string;
} & {
date?: string | null | undefined;
}) | {
kind: "image";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "FileLink";
} & ((({
kind: "file";
id: string;
url: string;
name: string;
size: string;
} & {
date?: string | null | undefined;
}) | {
kind: "file";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "DocumentLink";
} & (({
id: string;
} | {
kind: "document";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "ExternalLink";
} & ((({
url: string;
} & {
kind?: "web" | undefined;
target?: string | null | undefined;
preview?: {
title?: string | undefined;
} | null | undefined;
}) | {
kind: "web";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "MediaLink";
} & {
kind: "media";
} & {
text?: string | undefined;
}) | ({
__TYPE__: "AnyLink";
} & {
kind: "any";
} & {
text?: string | undefined;
});
})[];
}, Field, unknown>, ({
type: "Color";

@@ -632,2 +707,3 @@ } & {

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -773,7 +849,9 @@ }) | ({

value: string;
} | {
} | ({
key: string;
} & {
__TYPE__: "LinkContent";
value: ({
__TYPE__: "ImageLink";
} & (({
} & ((({
kind: "image";

@@ -788,10 +866,9 @@ id: string;

date?: string | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "image";
text: string;
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "FileLink";
} & (({
} & ((({
kind: "file";

@@ -804,7 +881,6 @@ id: string;

date?: string | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "file";
text: string;
}) & {
text?: string | undefined;
})) | ({

@@ -814,10 +890,9 @@ __TYPE__: "DocumentLink";

id: string;
} & {
} | {
kind: "document";
}) & {
text?: string | undefined;
}) | {
kind: "document";
text: string;
})) | ({
__TYPE__: "ExternalLink";
} & (({
} & ((({
url: string;

@@ -830,7 +905,6 @@ } & {

} | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "web";
text: string;
}) & {
text?: string | undefined;
})) | ({

@@ -840,9 +914,12 @@ __TYPE__: "MediaLink";

kind: "media";
text: string;
} & {
text?: string | undefined;
}) | ({
__TYPE__: "AnyLink";
} & {
text: string;
kind: "any";
} & {
text?: string | undefined;
});
} | {
}) | {
__TYPE__: "StructuredTextContent";

@@ -1002,3 +1079,76 @@ value: (({

__TYPE__: "SeparatorContent";
} | {
__TYPE__: "RepeatableContent";
type: "Link";
value: ({
key: string;
} & {
__TYPE__: "LinkContent";
value: ({
__TYPE__: "ImageLink";
} & ((({
kind: "image";
id: string;
url: string;
height: string;
width: string;
size: string;
name: string;
} & {
date?: string | null | undefined;
}) | {
kind: "image";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "FileLink";
} & ((({
kind: "file";
id: string;
url: string;
name: string;
size: string;
} & {
date?: string | null | undefined;
}) | {
kind: "file";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "DocumentLink";
} & (({
id: string;
} | {
kind: "document";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "ExternalLink";
} & ((({
url: string;
} & {
kind?: "web" | undefined;
target?: string | null | undefined;
preview?: {
title?: string | undefined;
} | null | undefined;
}) | {
kind: "web";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "MediaLink";
} & {
kind: "media";
} & {
text?: string | undefined;
}) | ({
__TYPE__: "AnyLink";
} & {
kind: "any";
} & {
text?: string | undefined;
});
})[];
}, Field>;
export default SimpleWidgetRenderer;

@@ -143,2 +143,3 @@ import { GroupContent } from "@prismicio/types-internal/lib/content";

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -311,2 +312,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -475,2 +477,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -616,7 +619,9 @@ }) | ({

value: string;
} | {
} | ({
key: string;
} & {
__TYPE__: "LinkContent";
value: ({
__TYPE__: "ImageLink";
} & (({
} & ((({
kind: "image";

@@ -631,10 +636,9 @@ id: string;

date?: string | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "image";
text: string;
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "FileLink";
} & (({
} & ((({
kind: "file";

@@ -647,7 +651,6 @@ id: string;

date?: string | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "file";
text: string;
}) & {
text?: string | undefined;
})) | ({

@@ -657,10 +660,9 @@ __TYPE__: "DocumentLink";

id: string;
} & {
} | {
kind: "document";
}) & {
text?: string | undefined;
}) | {
kind: "document";
text: string;
})) | ({
__TYPE__: "ExternalLink";
} & (({
} & ((({
url: string;

@@ -673,7 +675,6 @@ } & {

} | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "web";
text: string;
}) & {
text?: string | undefined;
})) | ({

@@ -683,9 +684,12 @@ __TYPE__: "MediaLink";

kind: "media";
text: string;
} & {
text?: string | undefined;
}) | ({
__TYPE__: "AnyLink";
} & {
text: string;
kind: "any";
} & {
text?: string | undefined;
});
} | {
}) | {
__TYPE__: "StructuredTextContent";

@@ -845,3 +849,76 @@ value: (({

__TYPE__: "SeparatorContent";
}, GroupOrField>, ({
} | {
__TYPE__: "RepeatableContent";
type: "Link";
value: ({
key: string;
} & {
__TYPE__: "LinkContent";
value: ({
__TYPE__: "ImageLink";
} & ((({
kind: "image";
id: string;
url: string;
height: string;
width: string;
size: string;
name: string;
} & {
date?: string | null | undefined;
}) | {
kind: "image";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "FileLink";
} & ((({
kind: "file";
id: string;
url: string;
name: string;
size: string;
} & {
date?: string | null | undefined;
}) | {
kind: "file";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "DocumentLink";
} & (({
id: string;
} | {
kind: "document";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "ExternalLink";
} & ((({
url: string;
} & {
kind?: "web" | undefined;
target?: string | null | undefined;
preview?: {
title?: string | undefined;
} | null | undefined;
}) | {
kind: "web";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "MediaLink";
} & {
kind: "media";
} & {
text?: string | undefined;
}) | ({
__TYPE__: "AnyLink";
} & {
kind: "any";
} & {
text?: string | undefined;
});
})[];
}, GroupOrField, unknown>, ({
type: "Group";

@@ -985,2 +1062,3 @@ } & {

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -1153,2 +1231,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -1317,2 +1396,3 @@ }) | ({

allowText?: boolean | undefined;
repeat?: boolean | undefined;
} | undefined;

@@ -1458,7 +1538,9 @@ }) | ({

value: string;
} | {
} | ({
key: string;
} & {
__TYPE__: "LinkContent";
value: ({
__TYPE__: "ImageLink";
} & (({
} & ((({
kind: "image";

@@ -1473,10 +1555,9 @@ id: string;

date?: string | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "image";
text: string;
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "FileLink";
} & (({
} & ((({
kind: "file";

@@ -1489,7 +1570,6 @@ id: string;

date?: string | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "file";
text: string;
}) & {
text?: string | undefined;
})) | ({

@@ -1499,10 +1579,9 @@ __TYPE__: "DocumentLink";

id: string;
} & {
} | {
kind: "document";
}) & {
text?: string | undefined;
}) | {
kind: "document";
text: string;
})) | ({
__TYPE__: "ExternalLink";
} & (({
} & ((({
url: string;

@@ -1515,7 +1594,6 @@ } & {

} | null | undefined;
} & {
text?: string | undefined;
}) | {
kind: "web";
text: string;
}) & {
text?: string | undefined;
})) | ({

@@ -1525,9 +1603,12 @@ __TYPE__: "MediaLink";

kind: "media";
text: string;
} & {
text?: string | undefined;
}) | ({
__TYPE__: "AnyLink";
} & {
text: string;
kind: "any";
} & {
text?: string | undefined;
});
} | {
}) | {
__TYPE__: "StructuredTextContent";

@@ -1687,3 +1768,76 @@ value: (({

__TYPE__: "SeparatorContent";
} | {
__TYPE__: "RepeatableContent";
type: "Link";
value: ({
key: string;
} & {
__TYPE__: "LinkContent";
value: ({
__TYPE__: "ImageLink";
} & ((({
kind: "image";
id: string;
url: string;
height: string;
width: string;
size: string;
name: string;
} & {
date?: string | null | undefined;
}) | {
kind: "image";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "FileLink";
} & ((({
kind: "file";
id: string;
url: string;
name: string;
size: string;
} & {
date?: string | null | undefined;
}) | {
kind: "file";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "DocumentLink";
} & (({
id: string;
} | {
kind: "document";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "ExternalLink";
} & ((({
url: string;
} & {
kind?: "web" | undefined;
target?: string | null | undefined;
preview?: {
title?: string | undefined;
} | null | undefined;
}) | {
kind: "web";
}) & {
text?: string | undefined;
})) | ({
__TYPE__: "MediaLink";
} & {
kind: "media";
} & {
text?: string | undefined;
}) | ({
__TYPE__: "AnyLink";
} & {
kind: "any";
} & {
text?: string | undefined;
});
})[];
}, GroupOrField>;
export default StaticWidgetRenderer;

@@ -8,2 +8,3 @@ "use strict";

const GroupRenderer_1 = tslib_1.__importDefault(require("./GroupRenderer"));
const RepeatableRenderer_1 = tslib_1.__importDefault(require("./RepeatableRenderer"));
const SimpleWidgetRenderer_1 = tslib_1.__importDefault(require("./SimpleWidgetRenderer"));

@@ -26,2 +27,47 @@ function extractFetchGroupIfAny(fetch) {

}
function extractSimpleFieldIfAny(field) {
if (field !== undefined && (field.TYPE === "SimpleField" || field.TYPE === "DocRelation")) {
return field;
}
else {
return undefined;
}
}
function renderContent(def, content, fetch, renderer) {
var _a, _b, _c, _d;
// When def is a Link, there is a special management with the fact it can be repeated or not.
// The custom type is always the one deciding the return type of the content.
// If repeat is true, it will always return an array, and if repeat is false, it will always return an object.
if (def.type === "Link") {
// When repeat is true and existing content is repeatable, render the content as is
if (((_a = def.config) === null || _a === void 0 ? void 0 : _a.repeat) && (0, content_1.isRepeatableContent)(content)) {
return renderer.repeatable(def, content, extractSimpleFieldIfAny(fetch));
}
// When repeat is true and existing content is not repeatable, render as repeatable
if (((_b = def.config) === null || _b === void 0 ? void 0 : _b.repeat) && (0, content_1.isLinkContent)(content)) {
return renderer.repeatable(def, {
__TYPE__: "RepeatableContent",
type: def.type,
value: [content],
}, extractSimpleFieldIfAny(fetch));
}
// When repeat is false and existing content is repeatable with data, render the first item
if (!((_c = def.config) === null || _c === void 0 ? void 0 : _c.repeat) && (0, content_1.isRepeatableContent)(content) && content.value[0]) {
return renderer.simple(def, content.value[0], extractFetchFieldIfAny(fetch));
}
// When repeat is false and existing content is repeatable but empty, render the default widget
if (!((_d = def.config) === null || _d === void 0 ? void 0 : _d.repeat) && (0, content_1.isRepeatableContent)(content)) {
return renderer.default(def);
}
// When repeat is false and existing content is not repeatable, render the content as is,
// using the normal nestable content renderer condition bellow
}
if (def.type !== "Group" && (0, content_1.isNestableContent)(content)) {
return renderer.simple(def, content, extractFetchFieldIfAny(fetch));
}
if (def.type === "Group" && (0, content_1.isGroupContent)(content)) {
return renderer.group(def, content, extractFetchGroupIfAny(fetch));
}
return renderer.default(def);
}
const StaticWidgetRenderer = (ctx) => (0, object_1.withObjectRenderers)({

@@ -32,2 +78,5 @@ renderV1(content, fetch) {

}
else if ((0, content_1.isRepeatableContent)(content)) {
return (0, RepeatableRenderer_1.default)(ctx).renderV1(content, extractSimpleFieldIfAny(fetch));
}
else {

@@ -38,15 +87,15 @@ return (0, SimpleWidgetRenderer_1.default)(ctx).renderV1(content, extractFetchFieldIfAny(fetch));

renderV2(def, content, fetch) {
if (def.type !== "Group" && (0, content_1.isNestableContent)(content)) {
// todo maybe find an other condition
return (0, SimpleWidgetRenderer_1.default)(ctx).renderV2(def, content, extractFetchFieldIfAny(fetch));
}
else if (def.type === "Group" && (0, content_1.isGroupContent)(content)) {
return (0, GroupRenderer_1.default)(ctx).renderV2(def, content, extractFetchGroupIfAny(fetch));
}
else {
return this.renderDefault(def);
}
return renderContent(def, content, fetch, {
group: (def, content, fetch) => (0, GroupRenderer_1.default)(ctx).renderV2(def, content, fetch),
repeatable: (def, content, fetch) => (0, RepeatableRenderer_1.default)(ctx).renderV2(def, content, fetch),
simple: (def, content, fetch) => (0, SimpleWidgetRenderer_1.default)(ctx).renderV2(def, content, fetch),
default: (def) => this.renderDefault(def),
});
},
renderDefault(def) {
if (def.type === "Group") {
var _a;
if (def.type === "Link" && ((_a = def.config) === null || _a === void 0 ? void 0 : _a.repeat)) {
return (0, RepeatableRenderer_1.default)(ctx).renderDefault(def);
}
else if (def.type === "Group") {
return (0, GroupRenderer_1.default)(ctx).renderDefault(def);

@@ -59,13 +108,10 @@ }

renderMocks(def, content) {
if ((0, content_1.isNestableContent)(content) && def.type !== "Group") {
return (0, SimpleWidgetRenderer_1.default)(ctx).renderMocks(def, content);
}
else if ((0, content_1.isGroupContent)(content) && def.type === "Group") {
return (0, GroupRenderer_1.default)(ctx).renderMocks(def, content);
}
else {
return this.renderDefault(def);
}
return renderContent(def, content, undefined, {
group: (def, content) => (0, GroupRenderer_1.default)(ctx).renderMocks(def, content),
repeatable: (def, content) => (0, RepeatableRenderer_1.default)(ctx).renderMocks(def, content),
simple: (def, content) => (0, SimpleWidgetRenderer_1.default)(ctx).renderMocks(def, content),
default: (def) => this.renderDefault(def),
});
},
});
exports.default = StaticWidgetRenderer;
{
"name": "@prismicio/api-renderer",
"version": "5.0.0",
"version": "6.0.0-alpha.0",
"description": "Prismic renderers for the content API",

@@ -38,3 +38,3 @@ "keywords": [

"dependencies": {
"@prismicio/types-internal": "3.0.0",
"@prismicio/types-internal": "3.1.0",
"tslib": "^2.5.0",

@@ -41,0 +41,0 @@ "uuid": "^9.0.0"

@@ -7,5 +7,7 @@ import {

isNestableContent,
isRepeatableContent,
isSlicesContent,
isUIDContent,
NestableContent,
RepeatableContent,
UIDContent,

@@ -30,4 +32,4 @@ WidgetContent,

function isStaticWidgetContent(content: WidgetContent): content is GroupContent | NestableContent {
return isGroupContent(content) || isNestableContent(content)
function isStaticWidgetContent(content: WidgetContent): content is GroupContent | RepeatableContent | NestableContent {
return isGroupContent(content) || isRepeatableContent(content) || isNestableContent(content)
}

@@ -34,0 +36,0 @@

@@ -9,7 +9,8 @@ import type { LinkConfig } from "@prismicio/types-internal/lib/customtypes"

allowText: t.boolean,
repeat: t.boolean,
})
export type AnyLinkDef = {
export type AnyLinkDef = Omit<t.TypeOf<typeof codec>, "select"> & {
select: null
} & Pick<t.TypeOf<typeof codec>, "allowText">
}
export const AnyLinkDef = new t.Type<AnyLinkDef>(

@@ -23,4 +24,4 @@ "AnyLinkDef",

return {
...decoded,
select: null,
allowText: decoded.allowText,
}

@@ -27,0 +28,0 @@ }),

@@ -11,2 +11,3 @@ import type { LinkConfig } from "@prismicio/types-internal/lib/customtypes"

allowText: t.boolean,
repeat: t.boolean,
}),

@@ -13,0 +14,0 @@ ])

@@ -10,2 +10,3 @@ import type { LinkConfig } from "@prismicio/types-internal/lib/customtypes"

allowText: t.boolean,
repeat: t.boolean,
}),

@@ -12,0 +13,0 @@ ])

@@ -10,2 +10,3 @@ import type { LinkConfig } from "@prismicio/types-internal/lib/customtypes"

allowText: t.boolean,
repeat: t.boolean,
}),

@@ -12,0 +13,0 @@ ])

@@ -8,3 +8,3 @@ export * from "./ApiDocument"

export type { default as RenderContext } from "./RenderContext"
export type { LinkRenderer, Renderer } from "./Renderer"
export type { LinkRenderer, LinkRendererOptions, Renderer } from "./Renderer"
export type { default as UrlRewriter } from "./URLRewriter"

@@ -1,11 +0,2 @@

import type {
AnyLinkContent,
DocumentLinkContent,
ExternalLinkContent,
FileLinkContent,
ImageLinkContent,
MediaLinkContent,
SliceContent,
WidgetContent,
} from "@prismicio/types-internal/lib/content"
import type { LinkContent, SliceContent, WidgetContent } from "@prismicio/types-internal/lib/content"
import type { StaticSlice, StaticWidget } from "@prismicio/types-internal/lib/customtypes"

@@ -16,23 +7,25 @@

export interface Renderer<D extends StaticWidget, C extends WidgetContent, F extends Field | undefined = undefined> {
renderV1(content: C, fetch?: F | undefined): unknown
renderV2(definition: D, content: C, fetch?: F | undefined): unknown
export interface Renderer<
D extends StaticWidget,
C extends WidgetContent,
F extends Field | undefined = undefined,
O = unknown,
> {
renderV1(content: C, fetch?: F | undefined, options?: O): unknown
renderV2(definition: D, content: C, fetch?: F | undefined, options?: O): unknown
renderDefault(definition: D): unknown
renderMocks(definition: D, content: C): unknown
renderMocks(definition: D, content: C, options?: O): unknown
}
export type LinkRendererOptions = { omitKey: boolean }
export interface LinkRenderer<
D extends MediaLinkDef | DocumentLinkDef | WebLinkDef | MediaLinkDef | AnyLinkDef,
C extends
| DocumentLinkContent
| FileLinkContent
| ExternalLinkContent
| ImageLinkContent
| MediaLinkContent
| AnyLinkContent,
C extends LinkContent,
F extends Field | undefined = undefined,
O = LinkRendererOptions,
> {
renderV1(content: C, fetch?: F | undefined): unknown
renderV2(definition: D | AnyLinkDef, content: C, fetch?: F | undefined): unknown
renderMocks(definition: D | AnyLinkDef, content: C): unknown
renderV1(content: C, fetch?: F | undefined, options?: O): unknown
renderV2(definition: D | AnyLinkDef, content: C, fetch?: F | undefined, options?: O): unknown
renderMocks(definition: D | AnyLinkDef, content: C, options?: O): unknown
}

@@ -39,0 +32,0 @@

@@ -1,11 +0,17 @@

import type { AnyLinkContent } from "@prismicio/types-internal/lib/content"
import type { AnyLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"
import type { AnyLinkDef, LinkRenderer } from "../../../models"
import type { AnyLinkDef, LinkRenderer, LinkRendererOptions } from "../../../models"
const AnyLinkRenderer: LinkRenderer<AnyLinkDef, AnyLinkContent> = {
renderV1(link: AnyLinkContent): unknown {
const AnyLinkRenderer: LinkRenderer<
AnyLinkDef,
LinkContent & { value: AnyLinkContent },
undefined,
LinkRendererOptions
> = {
renderV1(link: LinkContent & { value: AnyLinkContent }, _fetch: undefined, options?: LinkRendererOptions): unknown {
return {
type: "Link.any",
value: {
...(link.text && { text: link.text }),
...(!options?.omitKey && { key: link.key }),
...(link.value.text && { text: link.value.text }),
},

@@ -15,11 +21,17 @@ }

renderV2(def: AnyLinkDef, link: AnyLinkContent): unknown {
renderV2(
def: AnyLinkDef,
link: LinkContent & { value: AnyLinkContent },
_fetch: undefined,
options?: LinkRendererOptions,
): unknown {
return {
link_type: "Any",
...(def.allowText && link.text && { text: link.text }),
...(!options?.omitKey && { key: link.key }),
...(def.allowText && link.value.text && { text: link.value.text }),
}
},
renderMocks(def: AnyLinkDef, link: AnyLinkContent): unknown {
return this.renderV2(def, link)
renderMocks(def: AnyLinkDef, link: LinkContent & { value: AnyLinkContent }, options?: LinkRendererOptions): unknown {
return this.renderV2(def, link, undefined, options)
},

@@ -26,0 +38,0 @@ }

@@ -1,4 +0,4 @@

import { type DocumentLinkContent, FilledDocumentLinkContent } from "@prismicio/types-internal/lib/content"
import { type DocumentLinkContent, FilledDocumentLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"
import type { RenderContext } from "../../../models"
import type { LinkRendererOptions, RenderContext } from "../../../models"
import { AnyLinkDef, DocumentLinkDef, LinkRenderer } from "../../../models"

@@ -9,10 +9,14 @@ import type { DocRelation } from "../../../models/fetch"

function brokenLinkV1(link: DocumentLinkContent): Record<string, unknown> {
function brokenLinkV1(
link: LinkContent & { value: DocumentLinkContent },
options?: LinkRendererOptions,
): Record<string, unknown> {
return {
type: "Link.document",
value: {
...(FilledDocumentLinkContent.is(link) && { id: link.id, tags: [] }),
...(!options?.omitKey && { key: link.key }),
...(FilledDocumentLinkContent.is(link.value) && { id: link.value.id, tags: [] }),
type: BROKEN_CUSTOM_TYPE,
isBroken: true,
...(link.text && { text: link.text }),
...(link.value.text && { text: link.value.text }),
},

@@ -22,12 +26,19 @@ }

function brokenLinkV2(link: DocumentLinkContent, def: DocumentLinkDef | AnyLinkDef): Record<string, unknown> {
function brokenLinkV2(
link: LinkContent & { value: DocumentLinkContent },
def: DocumentLinkDef | AnyLinkDef,
options?: LinkRendererOptions,
): Record<string, unknown> {
return {
...(FilledDocumentLinkContent.is(link) && { id: link.id, tags: [] }),
...(!options?.omitKey && { key: link.key }),
...(FilledDocumentLinkContent.is(link.value) && { id: link.value.id, tags: [] }),
type: BROKEN_CUSTOM_TYPE,
isBroken: true,
...(def.allowText && link.text && { text: link.text }),
...(def.allowText && link.value.text && { text: link.value.text }),
}
}
const DocumentLinkRenderer: (ctx: RenderContext) => LinkRenderer<DocumentLinkDef, DocumentLinkContent, DocRelation> = (
const DocumentLinkRenderer: (
ctx: RenderContext,
) => LinkRenderer<DocumentLinkDef, LinkContent & { value: DocumentLinkContent }, DocRelation, LinkRendererOptions> = (
ctx,

@@ -37,9 +48,14 @@ ) => {

return {
renderV1(link: DocumentLinkContent, fetch: DocRelation | undefined): unknown {
if (!extension) return brokenLinkV1(link)
renderV1(
link: LinkContent & { value: DocumentLinkContent },
fetch: DocRelation | undefined,
options?: LinkRendererOptions,
): unknown {
if (!extension) return brokenLinkV1(link, options)
//todo check fetch type
try {
if (FilledDocumentLinkContent.is(link)) {
const id = extension.DocEncoder.encodeDocId(link.id, fetch?.uuid, [link.text])
if (FilledDocumentLinkContent.is(link.value)) {
const linkKey = options?.omitKey ? undefined : link.key
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch?.uuid, [linkKey, link.value.text])

@@ -55,19 +71,26 @@ return {

value: {
...(link.text && { text: link.text }),
...(!options?.omitKey && { key: link.key }),
...(link.value.text && { text: link.value.text }),
},
}
} catch (e) {
return brokenLinkV1(link)
return brokenLinkV1(link, options)
}
},
renderV2(def: DocumentLinkDef | AnyLinkDef, link: DocumentLinkContent, fetch: DocRelation | undefined): unknown {
if (!extension) return brokenLinkV2(link, def)
renderV2(
def: DocumentLinkDef | AnyLinkDef,
link: LinkContent & { value: DocumentLinkContent },
fetch: DocRelation | undefined,
options?: LinkRendererOptions,
): unknown {
if (!extension) return brokenLinkV2(link, def, options)
//todo check fetch type
try {
const linkText = def.allowText ? link.text : undefined
const linkText = def.allowText ? link.value.text : undefined
if (FilledDocumentLinkContent.is(link)) {
const id = extension.DocEncoder.encodeDocId(link.id, fetch?.uuid, [linkText])
if (FilledDocumentLinkContent.is(link.value)) {
const linkKey = options?.omitKey ? undefined : link.key
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch?.uuid, [linkKey, linkText])

@@ -79,17 +102,23 @@ return extension.encoders.documentLinks.encode(id)

link_type: "Document",
...(!options?.omitKey && { key: link.key }),
...(linkText && { text: linkText }),
}
} catch (e) {
return brokenLinkV2(link, def)
return brokenLinkV2(link, def, options)
}
},
renderMocks(def: DocumentLinkDef | AnyLinkDef, link: DocumentLinkContent): unknown {
if (FilledDocumentLinkContent.is(link)) {
renderMocks(
def: DocumentLinkDef | AnyLinkDef,
link: LinkContent & { value: DocumentLinkContent },
options?: LinkRendererOptions,
): unknown {
if (FilledDocumentLinkContent.is(link.value)) {
const type = (DocumentLinkDef.is(def) && def.customtypes[0]) || "mock"
return {
id: link.id,
...(def.allowText && link.text && { text: link.text }),
id: link.value.id,
...(def.allowText && link.value.text && { text: link.value.text }),
link_type: "Document",
...(!options?.omitKey && { key: link.key }),
type,

@@ -104,3 +133,3 @@ tags: [],

return brokenLinkV2(link, def)
return brokenLinkV2(link, def, options)
},

@@ -107,0 +136,0 @@ }

@@ -1,15 +0,25 @@

import { type ExternalLinkContent, FilledExternalLinkContent } from "@prismicio/types-internal/lib/content"
import { type ExternalLinkContent, FilledExternalLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"
import type { AnyLinkDef, LinkRenderer, WebLinkDef } from "../../../models"
import type { AnyLinkDef, LinkRenderer, LinkRendererOptions, WebLinkDef } from "../../../models"
const ExternalLinkRenderer: LinkRenderer<WebLinkDef, ExternalLinkContent> = {
renderV1(link: ExternalLinkContent): unknown {
const ExternalLinkRenderer: LinkRenderer<
WebLinkDef,
LinkContent & { value: ExternalLinkContent },
undefined,
LinkRendererOptions
> = {
renderV1(
link: LinkContent & { value: ExternalLinkContent },
_fetch: undefined,
options?: LinkRendererOptions,
): unknown {
return {
type: "Link.web",
value: {
...(FilledExternalLinkContent.is(link) && {
url: link.url,
...(typeof link.target === "string" && { target: link.target }),
...(!options?.omitKey && { key: link.key }),
...(FilledExternalLinkContent.is(link.value) && {
url: link.value.url,
...(typeof link.value.target === "string" && { target: link.value.target }),
}),
...(link.text && { text: link.text }),
...(link.value.text && { text: link.value.text }),
},

@@ -19,15 +29,25 @@ }

renderV2(def: WebLinkDef | AnyLinkDef, link: ExternalLinkContent): unknown {
renderV2(
def: WebLinkDef | AnyLinkDef,
link: LinkContent & { value: ExternalLinkContent },
_fetch: undefined,
options?: LinkRendererOptions,
): unknown {
return {
link_type: "Web",
...(FilledExternalLinkContent.is(link) && {
url: link.url,
...(typeof link.target === "string" && { target: link.target }),
...(!options?.omitKey && { key: link.key }),
...(FilledExternalLinkContent.is(link.value) && {
url: link.value.url,
...(typeof link.value.target === "string" && { target: link.value.target }),
}),
...(def.allowText && link.text && { text: link.text }),
...(def.allowText && link.value.text && { text: link.value.text }),
}
},
renderMocks(def: WebLinkDef | AnyLinkDef, link: ExternalLinkContent): unknown {
return this.renderV2(def, link)
renderMocks(
def: WebLinkDef | AnyLinkDef,
link: LinkContent & { value: ExternalLinkContent },
options?: LinkRendererOptions,
): unknown {
return this.renderV2(def, link, undefined, options)
},

@@ -34,0 +54,0 @@ }

@@ -1,19 +0,22 @@

import { type FileLinkContent, FilledFileLinkContent } from "@prismicio/types-internal/lib/content"
import { type FileLinkContent, FilledFileLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"
import type { RenderContext } from "../../../models"
import type { LinkRendererOptions, RenderContext } from "../../../models"
import type { AnyLinkDef, LinkRenderer, MediaLinkDef } from "../../../models"
const FileLinkRenderer: (ctx: RenderContext) => LinkRenderer<MediaLinkDef, FileLinkContent> = (ctx) => ({
renderV1(link: FileLinkContent): unknown {
const FileLinkRenderer: (
ctx: RenderContext,
) => LinkRenderer<MediaLinkDef, LinkContent & { value: FileLinkContent }, undefined, LinkRendererOptions> = (ctx) => ({
renderV1(link: LinkContent & { value: FileLinkContent }, _fetch: undefined, options?: LinkRendererOptions): unknown {
return {
type: "Link.file",
value: {
...(!options?.omitKey && { key: link.key }),
file: {
kind: link.kind,
...(FilledFileLinkContent.is(link) && {
url: ctx.urlRewriter.rewriteFileUrl(link.url),
name: link.name,
size: link.size,
kind: link.value.kind,
...(FilledFileLinkContent.is(link.value) && {
url: ctx.urlRewriter.rewriteFileUrl(link.value.url),
name: link.value.name,
size: link.value.size,
}),
...(link.text && { text: link.text }),
...(link.value.text && { text: link.value.text }),
},

@@ -24,18 +27,28 @@ },

renderV2(def: MediaLinkDef | AnyLinkDef, link: FileLinkContent): unknown {
renderV2(
def: MediaLinkDef | AnyLinkDef,
link: LinkContent & { value: FileLinkContent },
_fetch: undefined,
options?: LinkRendererOptions,
): unknown {
return {
link_type: "Media",
kind: link.kind,
...(FilledFileLinkContent.is(link) && {
id: link.id,
url: ctx.urlRewriter.rewriteFileUrl(link.url),
name: link.name,
size: link.size,
...(!options?.omitKey && { key: link.key }),
kind: link.value.kind,
...(FilledFileLinkContent.is(link.value) && {
id: link.value.id,
url: ctx.urlRewriter.rewriteFileUrl(link.value.url),
name: link.value.name,
size: link.value.size,
}),
...(def.allowText && link.text && { text: link.text }),
...(def.allowText && link.value.text && { text: link.value.text }),
}
},
renderMocks(def: MediaLinkDef | AnyLinkDef, link: FileLinkContent): unknown {
return this.renderV2(def, link)
renderMocks(
def: MediaLinkDef | AnyLinkDef,
link: LinkContent & { value: FileLinkContent },
options?: LinkRendererOptions,
): unknown {
return this.renderV2(def, link, undefined, options)
},

@@ -42,0 +55,0 @@ })

@@ -1,22 +0,25 @@

import { type ImageLinkContent, FilledImageLinkContent } from "@prismicio/types-internal/lib/content"
import { type ImageLinkContent, FilledImageLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"
import type { RenderContext } from "../../../models"
import type { LinkRendererOptions, RenderContext } from "../../../models"
import type { AnyLinkDef, LinkRenderer, MediaLinkDef } from "../../../models"
const ImageLinkRenderer: (ctx: RenderContext) => LinkRenderer<MediaLinkDef, ImageLinkContent> = (ctx) => ({
renderV1(link: ImageLinkContent): unknown {
const ImageLinkRenderer: (
ctx: RenderContext,
) => LinkRenderer<MediaLinkDef, LinkContent & { value: ImageLinkContent }, undefined, LinkRendererOptions> = (ctx) => ({
renderV1(link: LinkContent & { value: ImageLinkContent }, _fetch: undefined, options?: LinkRendererOptions): unknown {
return {
type: "Link.image",
value: {
...(!options?.omitKey && { key: link.key }),
image: {
kind: link.kind,
...(FilledImageLinkContent.is(link) && {
id: link.id,
url: ctx.urlRewriter.optimizeImageUrl(link.url),
name: link.name,
size: link.size,
width: link.width,
height: link.height,
kind: link.value.kind,
...(FilledImageLinkContent.is(link.value) && {
id: link.value.id,
url: ctx.urlRewriter.optimizeImageUrl(link.value.url),
name: link.value.name,
size: link.value.size,
width: link.value.width,
height: link.value.height,
}),
...(link.text && { text: link.text }),
...(link.value.text && { text: link.value.text }),
},

@@ -27,20 +30,30 @@ },

renderV2(def: MediaLinkDef | AnyLinkDef, link: ImageLinkContent): unknown {
renderV2(
def: MediaLinkDef | AnyLinkDef,
link: LinkContent & { value: ImageLinkContent },
_fetch: undefined,
options?: LinkRendererOptions,
): unknown {
return {
link_type: "Media",
kind: link.kind,
...(FilledImageLinkContent.is(link) && {
id: link.id,
url: ctx.urlRewriter.optimizeImageUrl(link.url),
name: link.name,
size: link.size,
width: link.width,
height: link.height,
...(!options?.omitKey && { key: link.key }),
kind: link.value.kind,
...(FilledImageLinkContent.is(link.value) && {
id: link.value.id,
url: ctx.urlRewriter.optimizeImageUrl(link.value.url),
name: link.value.name,
size: link.value.size,
width: link.value.width,
height: link.value.height,
}),
...(def.allowText && link.text && { text: link.text }),
...(def.allowText && link.value.text && { text: link.value.text }),
}
},
renderMocks(def: MediaLinkDef | AnyLinkDef, link: ImageLinkContent): unknown {
return this.renderV2(def, link)
renderMocks(
def: MediaLinkDef | AnyLinkDef,
link: LinkContent & { value: ImageLinkContent },
options?: LinkRendererOptions,
): unknown {
return this.renderV2(def, link, undefined, options)
},

@@ -47,0 +60,0 @@ })

@@ -5,3 +5,11 @@ import type { LinkContent } from "@prismicio/types-internal/lib/content"

import type { AnyLinkDef, DocumentLinkDef, MediaLinkDef, WebLinkDef } from "../../../models"
import { isAnyLinkDef, isDocumentLinkDef, isMediaLinkDef, isWebLinkDef, RenderContext, Renderer } from "../../../models"
import {
isAnyLinkDef,
isDocumentLinkDef,
isMediaLinkDef,
isWebLinkDef,
LinkRendererOptions,
RenderContext,
Renderer,
} from "../../../models"
import type { DocRelation } from "../../../models/fetch"

@@ -16,15 +24,15 @@ import AnyLinkRenderer from "./AnyLinkRenderer"

const LinkRenderer: (ctx: RenderContext) => Renderer<Link, LinkContent, DocRelation> = (ctx) => ({
renderV1(link: LinkContent, fetch: DocRelation | undefined): unknown {
renderV1(link: LinkContent, fetch: DocRelation | undefined, options?: LinkRendererOptions): unknown {
if (link.value.__TYPE__ === "ImageLink") {
return ImageLinkRenderer(ctx).renderV1(link.value)
return ImageLinkRenderer(ctx).renderV1({ ...link, value: link.value }, undefined, options)
} else if (link.value.__TYPE__ === "DocumentLink") {
return DocumentLinkRenderer(ctx).renderV1(link.value, fetch)
return DocumentLinkRenderer(ctx).renderV1({ ...link, value: link.value }, fetch, options)
} else if (link.value.__TYPE__ === "FileLink") {
return FileLinkRenderer(ctx).renderV1(link.value)
return FileLinkRenderer(ctx).renderV1({ ...link, value: link.value }, undefined, options)
} else if (link.value.__TYPE__ === "ExternalLink") {
return ExternalLinkRenderer.renderV1(link.value)
return ExternalLinkRenderer.renderV1({ ...link, value: link.value }, undefined, options)
} else if (link.value.__TYPE__ === "MediaLink") {
return MediaLinkRenderer.renderV1(link.value)
return MediaLinkRenderer.renderV1({ ...link, value: link.value }, undefined, options)
} else if (link.value.__TYPE__ === "AnyLink") {
return AnyLinkRenderer.renderV1(link.value)
return AnyLinkRenderer.renderV1({ ...link, value: link.value }, undefined, options)
} else {

@@ -35,15 +43,51 @@ throw new Error(`No rendered found for link ${JSON.stringify(link)}`) // normally unreachable

renderV2(def: Link, content: LinkContent, fetch: DocRelation | undefined): unknown {
renderV2(def: Link, content: LinkContent, fetch: DocRelation | undefined, options?: LinkRendererOptions): unknown {
if ((isMediaLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "ImageLink") {
return ImageLinkRenderer(ctx).renderV2(def.config as MediaLinkDef | AnyLinkDef, content.value)
return ImageLinkRenderer(ctx).renderV2(
def.config as MediaLinkDef | AnyLinkDef,
{
...content,
value: content.value,
},
undefined,
options,
)
} else if ((isDocumentLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "DocumentLink") {
return DocumentLinkRenderer(ctx).renderV2(def.config as DocumentLinkDef | AnyLinkDef, content.value, fetch)
return DocumentLinkRenderer(ctx).renderV2(
def.config as DocumentLinkDef | AnyLinkDef,
{ ...content, value: content.value },
fetch,
options,
)
} else if ((isMediaLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "FileLink") {
return FileLinkRenderer(ctx).renderV2(def.config as MediaLinkDef | AnyLinkDef, content.value)
return FileLinkRenderer(ctx).renderV2(
def.config as MediaLinkDef | AnyLinkDef,
{
...content,
value: content.value,
},
undefined,
options,
)
} else if ((isWebLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "ExternalLink") {
return ExternalLinkRenderer.renderV2(def.config as WebLinkDef | AnyLinkDef, content.value)
return ExternalLinkRenderer.renderV2(
def.config as WebLinkDef | AnyLinkDef,
{ ...content, value: content.value },
undefined,
options,
)
} else if ((isMediaLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "MediaLink") {
return MediaLinkRenderer.renderV2(def.config as MediaLinkDef, content.value)
return MediaLinkRenderer.renderV2(
def.config as MediaLinkDef,
{ ...content, value: content.value },
undefined,
options,
)
} else if (isAnyLinkDef(def) && content.value.__TYPE__ === "AnyLink") {
return AnyLinkRenderer.renderV2(def.config as AnyLinkDef, content.value)
return AnyLinkRenderer.renderV2(
def.config as AnyLinkDef,
{ ...content, value: content.value },
undefined,
options,
)
} else {

@@ -75,15 +119,43 @@ return this.renderDefault(def)

renderMocks(def: Link, content: LinkContent): unknown {
renderMocks(def: Link, content: LinkContent, options?: LinkRendererOptions): unknown {
if ((isMediaLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "ImageLink") {
return ImageLinkRenderer(ctx).renderMocks(def.config as MediaLinkDef | AnyLinkDef, content.value)
return ImageLinkRenderer(ctx).renderMocks(
def.config as MediaLinkDef | AnyLinkDef,
{
...content,
value: content.value,
},
options,
)
} else if ((isDocumentLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "DocumentLink") {
return DocumentLinkRenderer(ctx).renderMocks(def.config as DocumentLinkDef | AnyLinkDef, content.value)
return DocumentLinkRenderer(ctx).renderMocks(
def.config as DocumentLinkDef | AnyLinkDef,
{
...content,
value: content.value,
},
options,
)
} else if ((isMediaLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "FileLink") {
return FileLinkRenderer(ctx).renderMocks(def.config as MediaLinkDef | AnyLinkDef, content.value)
return FileLinkRenderer(ctx).renderMocks(
def.config as MediaLinkDef | AnyLinkDef,
{
...content,
value: content.value,
},
options,
)
} else if ((isWebLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "ExternalLink") {
return ExternalLinkRenderer.renderMocks(def.config as WebLinkDef | AnyLinkDef, content.value)
return ExternalLinkRenderer.renderMocks(
def.config as WebLinkDef | AnyLinkDef,
{
...content,
value: content.value,
},
options,
)
} else if ((isMediaLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "MediaLink") {
return MediaLinkRenderer.renderMocks(def.config as MediaLinkDef, content.value)
return MediaLinkRenderer.renderMocks(def.config as MediaLinkDef, { ...content, value: content.value }, options)
} else if (isAnyLinkDef(def) && content.value.__TYPE__ === "AnyLink") {
return AnyLinkRenderer.renderMocks(def.config as AnyLinkDef, content.value)
return AnyLinkRenderer.renderMocks(def.config as AnyLinkDef, { ...content, value: content.value }, options)
} else {

@@ -90,0 +162,0 @@ return this.renderDefault(def)

@@ -1,11 +0,17 @@

import type { MediaLinkContent } from "@prismicio/types-internal/lib/content"
import type { LinkContent, MediaLinkContent } from "@prismicio/types-internal/lib/content"
import type { LinkRenderer, MediaLinkDef } from "../../../models"
import type { LinkRenderer, LinkRendererOptions, MediaLinkDef } from "../../../models"
const MediaLinkRenderer: LinkRenderer<MediaLinkDef, MediaLinkContent> = {
renderV1(link: MediaLinkContent): unknown {
const MediaLinkRenderer: LinkRenderer<
MediaLinkDef,
LinkContent & { value: MediaLinkContent },
undefined,
LinkRendererOptions
> = {
renderV1(link: LinkContent & { value: MediaLinkContent }, _fetch: undefined, options?: LinkRendererOptions): unknown {
return {
type: "Link.media",
value: {
...(link.text && { text: link.text }),
...(!options?.omitKey && { key: link.key }),
...(link.value.text && { text: link.value.text }),
},

@@ -15,11 +21,21 @@ }

renderV2(def: MediaLinkDef, link: MediaLinkContent): unknown {
renderV2(
def: MediaLinkDef,
link: LinkContent & { value: MediaLinkContent },
_fetch: undefined,
options?: LinkRendererOptions,
): unknown {
return {
link_type: "Media",
...(def.allowText && link.text && { text: link.text }),
...(!options?.omitKey && { key: link.key }),
...(def.allowText && link.value.text && { text: link.value.text }),
}
},
renderMocks(def: MediaLinkDef, link: MediaLinkContent): unknown {
return this.renderV2(def, link)
renderMocks(
def: MediaLinkDef,
link: LinkContent & { value: MediaLinkContent },
options?: LinkRendererOptions,
): unknown {
return this.renderV2(def, link, undefined, options)
},

@@ -26,0 +42,0 @@ }

@@ -36,7 +36,7 @@ import {

if (apiVersion === ApiVersion.v1) {
return LinkRenderer(ctx).renderV1(link, undefined)
return LinkRenderer(ctx).renderV1(link, undefined, { omitKey: true })
} else if (apiVersion === ApiVersion.v2) {
return LinkRenderer(ctx).renderV2(defaultLink(linkType(link)), link, undefined)
return LinkRenderer(ctx).renderV2(defaultLink(linkType(link)), link, undefined, { omitKey: true })
} else if (apiVersion === ApiVersion.mocks) {
return LinkRenderer(ctx).renderMocks(defaultLink(linkType(link)), link)
return LinkRenderer(ctx).renderMocks(defaultLink(linkType(link)), link, { omitKey: true })
}

@@ -51,6 +51,10 @@ }

if (block.data.linkTo) {
const content = {
const content: LinkContent = {
__TYPE__: "LinkContent",
// When a Link is used within a StructuredText, the key is not present,
// we need to specify one to respect LinkContent type
// but the Renderer will not use it thanks to `omitKey` value.
key: "",
value: block.data.linkTo,
} as LinkContent
}
return {

@@ -57,0 +61,0 @@ linkTo: renderStructuredTextLink(content, apiVersion, ctx),

@@ -1,8 +0,17 @@

import { GroupContent, isGroupContent, isNestableContent, NestableContent } from "@prismicio/types-internal/lib/content"
import type { Group as GroupField, NestableWidget } from "@prismicio/types-internal/lib/customtypes"
import {
GroupContent,
isGroupContent,
isLinkContent,
isNestableContent,
isRepeatableContent,
NestableContent,
RepeatableContent,
} from "@prismicio/types-internal/lib/content"
import type { Group as GroupField, Link, NestableWidget } from "@prismicio/types-internal/lib/customtypes"
import type { RenderContext } from "../models"
import { Field, Group, GroupOrField, GroupType } from "../models/fetch"
import { Field, Group, GroupOrField, GroupType, Simple } from "../models/fetch"
import { withObjectRenderers } from "../object"
import GroupRenderer from "./GroupRenderer"
import RepeatableRenderer from "./RepeatableRenderer"
import SimpleWidgetRenderer from "./SimpleWidgetRenderer"

@@ -26,2 +35,68 @@

function extractSimpleFieldIfAny(field: Field | undefined): Simple | undefined {
if (field !== undefined && (field.TYPE === "SimpleField" || field.TYPE === "DocRelation")) {
return field
} else {
return undefined
}
}
function renderContent(
def: NestableWidget | GroupField,
content: NestableContent | GroupContent,
fetch: GroupOrField | Simple | undefined,
renderer: {
group: (def: GroupField, content: GroupContent, fetch?: Group) => unknown
repeatable: (def: Link, content: RepeatableContent, fetch?: Simple) => unknown
simple: (def: NestableWidget, content: NestableContent, fetch?: Field) => unknown
default: (def: NestableWidget | GroupField) => unknown
},
) {
// When def is a Link, there is a special management with the fact it can be repeated or not.
// The custom type is always the one deciding the return type of the content.
// If repeat is true, it will always return an array, and if repeat is false, it will always return an object.
if (def.type === "Link") {
// When repeat is true and existing content is repeatable, render the content as is
if (def.config?.repeat && isRepeatableContent(content)) {
return renderer.repeatable(def, content, extractSimpleFieldIfAny(fetch))
}
// When repeat is true and existing content is not repeatable, render as repeatable
if (def.config?.repeat && isLinkContent(content)) {
return renderer.repeatable(
def,
{
__TYPE__: "RepeatableContent",
type: def.type,
value: [content],
},
extractSimpleFieldIfAny(fetch),
)
}
// When repeat is false and existing content is repeatable with data, render the first item
if (!def.config?.repeat && isRepeatableContent(content) && content.value[0]) {
return renderer.simple(def, content.value[0], extractFetchFieldIfAny(fetch))
}
// When repeat is false and existing content is repeatable but empty, render the default widget
if (!def.config?.repeat && isRepeatableContent(content)) {
return renderer.default(def)
}
// When repeat is false and existing content is not repeatable, render the content as is,
// using the normal nestable content renderer condition bellow
}
if (def.type !== "Group" && isNestableContent(content)) {
return renderer.simple(def, content, extractFetchFieldIfAny(fetch))
}
if (def.type === "Group" && isGroupContent(content)) {
return renderer.group(def, content, extractFetchGroupIfAny(fetch))
}
return renderer.default(def)
}
const StaticWidgetRenderer = (ctx: RenderContext) =>

@@ -32,2 +107,4 @@ withObjectRenderers<NestableWidget | GroupField, NestableContent | GroupContent, GroupOrField>({

return GroupRenderer(ctx).renderV1(content, extractFetchGroupIfAny(fetch))
} else if (isRepeatableContent(content)) {
return RepeatableRenderer(ctx).renderV1(content, extractSimpleFieldIfAny(fetch))
} else {

@@ -39,14 +116,17 @@ return SimpleWidgetRenderer(ctx).renderV1(content, extractFetchFieldIfAny(fetch))

renderV2(def, content, fetch) {
if (def.type !== "Group" && isNestableContent(content)) {
// todo maybe find an other condition
return SimpleWidgetRenderer(ctx).renderV2(def, content, extractFetchFieldIfAny(fetch))
} else if (def.type === "Group" && isGroupContent(content)) {
return GroupRenderer(ctx).renderV2(def, content, extractFetchGroupIfAny(fetch))
} else {
return this.renderDefault(def)
}
return renderContent(def, content, fetch, {
group: (def: GroupField, content: GroupContent, fetch?: Group) =>
GroupRenderer(ctx).renderV2(def, content, fetch),
repeatable: (def: Link, content: RepeatableContent, fetch?: Simple) =>
RepeatableRenderer(ctx).renderV2(def, content, fetch),
simple: (def: NestableWidget, content: NestableContent, fetch?: Field) =>
SimpleWidgetRenderer(ctx).renderV2(def, content, fetch),
default: (def: NestableWidget | GroupField) => this.renderDefault(def),
})
},
renderDefault(def) {
if (def.type === "Group") {
if (def.type === "Link" && def.config?.repeat) {
return RepeatableRenderer(ctx).renderDefault(def)
} else if (def.type === "Group") {
return GroupRenderer(ctx).renderDefault(def)

@@ -59,9 +139,8 @@ } else {

renderMocks(def, content) {
if (isNestableContent(content) && def.type !== "Group") {
return SimpleWidgetRenderer(ctx).renderMocks(def, content)
} else if (isGroupContent(content) && def.type === "Group") {
return GroupRenderer(ctx).renderMocks(def, content)
} else {
return this.renderDefault(def)
}
return renderContent(def, content, undefined, {
group: (def: GroupField, content: GroupContent) => GroupRenderer(ctx).renderMocks(def, content),
repeatable: (def: Link, content: RepeatableContent) => RepeatableRenderer(ctx).renderMocks(def, content),
simple: (def: NestableWidget, content: NestableContent) => SimpleWidgetRenderer(ctx).renderMocks(def, content),
default: (def: NestableWidget | GroupField) => this.renderDefault(def),
})
},

@@ -68,0 +147,0 @@ })

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc