@prismicio/api-renderer
Advanced tools
Comparing version 5.0.0-alpha.6 to 5.0.0-alpha.7
@@ -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"; |
@@ -5,12 +5,15 @@ import type { LinkContent, SliceContent, WidgetContent } from "@prismicio/types-internal/lib/content"; | ||
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 LinkContent, 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; | ||
} | ||
@@ -17,0 +20,0 @@ export interface SliceRenderer<D extends StaticSlice, C extends SliceContent, F extends Slice | GroupOrField> { |
import type { AnyLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"; | ||
import type { AnyLinkDef, LinkRenderer } from "../../../models"; | ||
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.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
...(link.value.text && { text: link.value.text }), | ||
@@ -13,13 +13,13 @@ }, | ||
}, | ||
renderV2(def, link) { | ||
renderV2(def, link, _fetch, options) { | ||
return { | ||
link_type: "Any", | ||
...(link.key && { key: link.key }), | ||
...(!(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; |
import { type DocumentLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"; | ||
import type { RenderContext } from "../../../models"; | ||
import type { LinkRendererOptions, RenderContext } from "../../../models"; | ||
import { DocumentLinkDef, LinkRenderer } from "../../../models"; | ||
@@ -8,3 +8,3 @@ import type { DocRelation } from "../../../models/fetch"; | ||
value: DocumentLinkContent; | ||
}, DocRelation>; | ||
}, DocRelation, LinkRendererOptions>; | ||
export default DocumentLinkRenderer; |
@@ -7,7 +7,7 @@ "use strict"; | ||
exports.BROKEN_CUSTOM_TYPE = "broken_type"; | ||
function brokenLinkV1(link) { | ||
function brokenLinkV1(link, options) { | ||
return { | ||
type: "Link.document", | ||
value: { | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
...(content_1.FilledDocumentLinkContent.is(link.value) && { id: link.value.id, tags: [] }), | ||
@@ -20,5 +20,5 @@ type: exports.BROKEN_CUSTOM_TYPE, | ||
} | ||
function brokenLinkV2(link, def) { | ||
function brokenLinkV2(link, def, options) { | ||
return { | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
...(content_1.FilledDocumentLinkContent.is(link.value) && { id: link.value.id, tags: [] }), | ||
@@ -33,9 +33,10 @@ type: exports.BROKEN_CUSTOM_TYPE, | ||
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.value)) { | ||
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [link.key, link.value.text]); | ||
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 { | ||
@@ -49,3 +50,3 @@ type: "Link.document", | ||
value: { | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
...(link.value.text && { text: link.value.text }), | ||
@@ -56,8 +57,8 @@ }, | ||
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 | ||
@@ -67,3 +68,4 @@ try { | ||
if (content_1.FilledDocumentLinkContent.is(link.value)) { | ||
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [link.key, linkText]); | ||
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); | ||
@@ -73,3 +75,3 @@ } | ||
link_type: "Document", | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
...(linkText && { text: linkText }), | ||
@@ -79,6 +81,6 @@ }; | ||
catch (e) { | ||
return brokenLinkV2(link, def); | ||
return brokenLinkV2(link, def, options); | ||
} | ||
}, | ||
renderMocks(def, link) { | ||
renderMocks(def, link, options) { | ||
if (content_1.FilledDocumentLinkContent.is(link.value)) { | ||
@@ -90,3 +92,3 @@ const type = (models_1.DocumentLinkDef.is(def) && def.customtypes[0]) || "mock"; | ||
link_type: "Document", | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
type, | ||
@@ -100,3 +102,3 @@ tags: [], | ||
} | ||
return brokenLinkV2(link, def); | ||
return brokenLinkV2(link, def, options); | ||
}, | ||
@@ -103,0 +105,0 @@ }; |
import { type ExternalLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"; | ||
import type { LinkRenderer, WebLinkDef } from "../../../models"; | ||
import type { LinkRenderer, LinkRendererOptions, WebLinkDef } from "../../../models"; | ||
declare const ExternalLinkRenderer: LinkRenderer<WebLinkDef, LinkContent & { | ||
value: ExternalLinkContent; | ||
}>; | ||
}, undefined, LinkRendererOptions>; | ||
export default ExternalLinkRenderer; |
@@ -5,7 +5,7 @@ "use strict"; | ||
const ExternalLinkRenderer = { | ||
renderV1(link) { | ||
renderV1(link, _fetch, options) { | ||
return { | ||
type: "Link.web", | ||
value: { | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
...(content_1.FilledExternalLinkContent.is(link.value) && { | ||
@@ -19,6 +19,6 @@ url: link.value.url, | ||
}, | ||
renderV2(def, link) { | ||
renderV2(def, link, _fetch, options) { | ||
return { | ||
link_type: "Web", | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
...(content_1.FilledExternalLinkContent.is(link.value) && { | ||
@@ -31,6 +31,6 @@ url: link.value.url, | ||
}, | ||
renderMocks(def, link) { | ||
return this.renderV2(def, link); | ||
renderMocks(def, link, options) { | ||
return this.renderV2(def, link, undefined, options); | ||
}, | ||
}; | ||
exports.default = ExternalLinkRenderer; |
import { type FileLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"; | ||
import type { RenderContext } from "../../../models"; | ||
import type { LinkRendererOptions, RenderContext } from "../../../models"; | ||
import type { LinkRenderer, MediaLinkDef } from "../../../models"; | ||
declare const FileLinkRenderer: (ctx: RenderContext) => LinkRenderer<MediaLinkDef, LinkContent & { | ||
value: FileLinkContent; | ||
}>; | ||
}, undefined, LinkRendererOptions>; | ||
export default FileLinkRenderer; |
@@ -5,7 +5,7 @@ "use strict"; | ||
const FileLinkRenderer = (ctx) => ({ | ||
renderV1(link) { | ||
renderV1(link, _fetch, options) { | ||
return { | ||
type: "Link.file", | ||
value: { | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
file: { | ||
@@ -23,6 +23,6 @@ kind: link.value.kind, | ||
}, | ||
renderV2(def, link) { | ||
renderV2(def, link, _fetch, options) { | ||
return { | ||
link_type: "Media", | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
kind: link.value.kind, | ||
@@ -38,6 +38,6 @@ ...(content_1.FilledFileLinkContent.is(link.value) && { | ||
}, | ||
renderMocks(def, link) { | ||
return this.renderV2(def, link); | ||
renderMocks(def, link, options) { | ||
return this.renderV2(def, link, undefined, options); | ||
}, | ||
}); | ||
exports.default = FileLinkRenderer; |
import { type ImageLinkContent, LinkContent } from "@prismicio/types-internal/lib/content"; | ||
import type { RenderContext } from "../../../models"; | ||
import type { LinkRendererOptions, RenderContext } from "../../../models"; | ||
import type { LinkRenderer, MediaLinkDef } from "../../../models"; | ||
declare const ImageLinkRenderer: (ctx: RenderContext) => LinkRenderer<MediaLinkDef, LinkContent & { | ||
value: ImageLinkContent; | ||
}>; | ||
}, undefined, LinkRendererOptions>; | ||
export default ImageLinkRenderer; |
@@ -5,7 +5,7 @@ "use strict"; | ||
const ImageLinkRenderer = (ctx) => ({ | ||
renderV1(link) { | ||
renderV1(link, _fetch, options) { | ||
return { | ||
type: "Link.image", | ||
value: { | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
image: { | ||
@@ -26,6 +26,6 @@ kind: link.value.kind, | ||
}, | ||
renderV2(def, link) { | ||
renderV2(def, link, _fetch, options) { | ||
return { | ||
link_type: "Media", | ||
...(link.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
kind: link.value.kind, | ||
@@ -43,6 +43,6 @@ ...(content_1.FilledImageLinkContent.is(link.value) && { | ||
}, | ||
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: 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: 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: 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: 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: 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: link.value }); | ||
return AnyLinkRenderer_1.default.renderV1({ ...link, value: link.value }, undefined, options); | ||
} | ||
@@ -41,3 +41,3 @@ 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") { | ||
@@ -47,6 +47,6 @@ return (0, ImageLinkRenderer_1.default)(ctx).renderV2(def.config, { | ||
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: content.value }, fetch); | ||
return (0, DocumentLinkRenderer_1.default)(ctx).renderV2(def.config, { ...content, value: content.value }, fetch, options); | ||
} | ||
@@ -57,12 +57,12 @@ else if (((0, models_1.isMediaLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "FileLink") { | ||
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: 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: 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: content.value }); | ||
return AnyLinkRenderer_1.default.renderV2(def.config, { ...content, value: content.value }, undefined, options); | ||
} | ||
@@ -94,3 +94,3 @@ else { | ||
}, | ||
renderMocks(def, content) { | ||
renderMocks(def, content, options) { | ||
if (((0, models_1.isMediaLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "ImageLink") { | ||
@@ -100,3 +100,3 @@ return (0, ImageLinkRenderer_1.default)(ctx).renderMocks(def.config, { | ||
value: content.value, | ||
}); | ||
}, options); | ||
} | ||
@@ -107,3 +107,3 @@ else if (((0, models_1.isDocumentLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "DocumentLink") { | ||
value: content.value, | ||
}); | ||
}, options); | ||
} | ||
@@ -114,3 +114,3 @@ else if (((0, models_1.isMediaLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "FileLink") { | ||
value: content.value, | ||
}); | ||
}, options); | ||
} | ||
@@ -121,9 +121,9 @@ else if (((0, models_1.isWebLinkDef)(def) || (0, models_1.isAnyLinkDef)(def)) && content.value.__TYPE__ === "ExternalLink") { | ||
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: 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: content.value }); | ||
return AnyLinkRenderer_1.default.renderMocks(def.config, { ...content, value: content.value }, options); | ||
} | ||
@@ -130,0 +130,0 @@ else { |
import type { LinkContent, MediaLinkContent } from "@prismicio/types-internal/lib/content"; | ||
import type { LinkRenderer, MediaLinkDef } from "../../../models"; | ||
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.key && { key: link.key }), | ||
...(!(options === null || options === void 0 ? void 0 : options.omitKey) && { key: link.key }), | ||
...(link.value.text && { text: link.value.text }), | ||
@@ -13,13 +13,13 @@ }, | ||
}, | ||
renderV2(def, link) { | ||
renderV2(def, link, _fetch, options) { | ||
return { | ||
link_type: "Media", | ||
...(link.key && { key: link.key }), | ||
...(!(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,5 @@ } | ||
__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: "", | ||
@@ -49,0 +52,0 @@ value: block.data.linkTo, |
@@ -570,3 +570,3 @@ import type { RenderContext } from "../models"; | ||
})[]; | ||
}, Field>, ({ | ||
}, Field, unknown>, ({ | ||
type: "Color"; | ||
@@ -573,0 +573,0 @@ } & { |
@@ -913,3 +913,3 @@ import { GroupContent } from "@prismicio/types-internal/lib/content"; | ||
})[]; | ||
}, GroupOrField>, ({ | ||
}, GroupOrField, unknown>, ({ | ||
type: "Group"; | ||
@@ -916,0 +916,0 @@ } & { |
{ | ||
"name": "@prismicio/api-renderer", | ||
"version": "5.0.0-alpha.6", | ||
"version": "5.0.0-alpha.7", | ||
"description": "Prismic renderers for the content API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -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" |
@@ -7,9 +7,16 @@ import type { LinkContent, SliceContent, WidgetContent } from "@prismicio/types-internal/lib/content" | ||
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< | ||
@@ -19,6 +26,7 @@ D extends MediaLinkDef | DocumentLinkDef | WebLinkDef | MediaLinkDef | AnyLinkDef, | ||
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 | ||
} | ||
@@ -25,0 +33,0 @@ |
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, LinkContent & { value: AnyLinkContent }> = { | ||
renderV1(link: LinkContent & { value: 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.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(link.value.text && { text: link.value.text }), | ||
@@ -16,6 +21,11 @@ }, | ||
renderV2(def: AnyLinkDef, link: LinkContent & { value: AnyLinkContent }): unknown { | ||
renderV2( | ||
def: AnyLinkDef, | ||
link: LinkContent & { value: AnyLinkContent }, | ||
_fetch: undefined, | ||
options?: LinkRendererOptions, | ||
): unknown { | ||
return { | ||
link_type: "Any", | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(def.allowText && link.value.text && { text: link.value.text }), | ||
@@ -25,4 +35,4 @@ } | ||
renderMocks(def: AnyLinkDef, link: LinkContent & { value: AnyLinkContent }): unknown { | ||
return this.renderV2(def, link) | ||
renderMocks(def: AnyLinkDef, link: LinkContent & { value: AnyLinkContent }, options?: LinkRendererOptions): unknown { | ||
return this.renderV2(def, link, undefined, options) | ||
}, | ||
@@ -29,0 +39,0 @@ } |
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,7 +9,10 @@ import type { DocRelation } from "../../../models/fetch" | ||
function brokenLinkV1(link: LinkContent & { value: DocumentLinkContent }): Record<string, unknown> { | ||
function brokenLinkV1( | ||
link: LinkContent & { value: DocumentLinkContent }, | ||
options?: LinkRendererOptions, | ||
): Record<string, unknown> { | ||
return { | ||
type: "Link.document", | ||
value: { | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(FilledDocumentLinkContent.is(link.value) && { id: link.value.id, tags: [] }), | ||
@@ -26,5 +29,6 @@ type: BROKEN_CUSTOM_TYPE, | ||
def: DocumentLinkDef | AnyLinkDef, | ||
options?: LinkRendererOptions, | ||
): Record<string, unknown> { | ||
return { | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(FilledDocumentLinkContent.is(link.value) && { id: link.value.id, tags: [] }), | ||
@@ -39,7 +43,13 @@ type: BROKEN_CUSTOM_TYPE, | ||
ctx: RenderContext, | ||
) => LinkRenderer<DocumentLinkDef, LinkContent & { value: DocumentLinkContent }, DocRelation> = (ctx) => { | ||
) => LinkRenderer<DocumentLinkDef, LinkContent & { value: DocumentLinkContent }, DocRelation, LinkRendererOptions> = ( | ||
ctx, | ||
) => { | ||
const extension = ctx.Extension | ||
return { | ||
renderV1(link: LinkContent & { value: 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) | ||
@@ -49,3 +59,4 @@ //todo check fetch type | ||
if (FilledDocumentLinkContent.is(link.value)) { | ||
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch?.uuid, [link.key, link.value.text]) | ||
const linkKey = options?.omitKey ? undefined : link.key | ||
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch?.uuid, [linkKey, link.value.text]) | ||
@@ -61,3 +72,3 @@ return { | ||
value: { | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(link.value.text && { text: link.value.text }), | ||
@@ -67,3 +78,3 @@ }, | ||
} catch (e) { | ||
return brokenLinkV1(link) | ||
return brokenLinkV1(link, options) | ||
} | ||
@@ -76,4 +87,5 @@ }, | ||
fetch: DocRelation | undefined, | ||
options?: LinkRendererOptions, | ||
): unknown { | ||
if (!extension) return brokenLinkV2(link, def) | ||
if (!extension) return brokenLinkV2(link, def, options) | ||
@@ -85,3 +97,4 @@ //todo check fetch type | ||
if (FilledDocumentLinkContent.is(link.value)) { | ||
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch?.uuid, [link.key, linkText]) | ||
const linkKey = options?.omitKey ? undefined : link.key | ||
const id = extension.DocEncoder.encodeDocId(link.value.id, fetch?.uuid, [linkKey, linkText]) | ||
@@ -93,11 +106,15 @@ return extension.encoders.documentLinks.encode(id) | ||
link_type: "Document", | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(linkText && { text: linkText }), | ||
} | ||
} catch (e) { | ||
return brokenLinkV2(link, def) | ||
return brokenLinkV2(link, def, options) | ||
} | ||
}, | ||
renderMocks(def: DocumentLinkDef | AnyLinkDef, link: LinkContent & { value: DocumentLinkContent }): unknown { | ||
renderMocks( | ||
def: DocumentLinkDef | AnyLinkDef, | ||
link: LinkContent & { value: DocumentLinkContent }, | ||
options?: LinkRendererOptions, | ||
): unknown { | ||
if (FilledDocumentLinkContent.is(link.value)) { | ||
@@ -110,3 +127,3 @@ const type = (DocumentLinkDef.is(def) && def.customtypes[0]) || "mock" | ||
link_type: "Document", | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
type, | ||
@@ -121,3 +138,3 @@ tags: [], | ||
return brokenLinkV2(link, def) | ||
return brokenLinkV2(link, def, options) | ||
}, | ||
@@ -124,0 +141,0 @@ } |
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, LinkContent & { value: ExternalLinkContent }> = { | ||
renderV1(link: LinkContent & { value: 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: { | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(FilledExternalLinkContent.is(link.value) && { | ||
@@ -20,6 +29,11 @@ url: link.value.url, | ||
renderV2(def: WebLinkDef | AnyLinkDef, link: LinkContent & { value: ExternalLinkContent }): unknown { | ||
renderV2( | ||
def: WebLinkDef | AnyLinkDef, | ||
link: LinkContent & { value: ExternalLinkContent }, | ||
_fetch: undefined, | ||
options?: LinkRendererOptions, | ||
): unknown { | ||
return { | ||
link_type: "Web", | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(FilledExternalLinkContent.is(link.value) && { | ||
@@ -33,4 +47,8 @@ url: link.value.url, | ||
renderMocks(def: WebLinkDef | AnyLinkDef, link: LinkContent & { value: 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) | ||
}, | ||
@@ -37,0 +55,0 @@ } |
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, LinkContent & { value: FileLinkContent }> = ( | ||
ctx, | ||
) => ({ | ||
renderV1(link: LinkContent & { value: 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: { | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
file: { | ||
@@ -27,6 +27,11 @@ kind: link.value.kind, | ||
renderV2(def: MediaLinkDef | AnyLinkDef, link: LinkContent & { value: FileLinkContent }): unknown { | ||
renderV2( | ||
def: MediaLinkDef | AnyLinkDef, | ||
link: LinkContent & { value: FileLinkContent }, | ||
_fetch: undefined, | ||
options?: LinkRendererOptions, | ||
): unknown { | ||
return { | ||
link_type: "Media", | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
kind: link.value.kind, | ||
@@ -43,4 +48,8 @@ ...(FilledFileLinkContent.is(link.value) && { | ||
renderMocks(def: MediaLinkDef | AnyLinkDef, link: LinkContent & { value: 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) | ||
}, | ||
@@ -47,0 +56,0 @@ }) |
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" | ||
@@ -8,8 +8,8 @@ | ||
ctx: RenderContext, | ||
) => LinkRenderer<MediaLinkDef, LinkContent & { value: ImageLinkContent }> = (ctx) => ({ | ||
renderV1(link: LinkContent & { value: ImageLinkContent }): unknown { | ||
) => LinkRenderer<MediaLinkDef, LinkContent & { value: ImageLinkContent }, undefined, LinkRendererOptions> = (ctx) => ({ | ||
renderV1(link: LinkContent & { value: ImageLinkContent }, _fetch: undefined, options?: LinkRendererOptions): unknown { | ||
return { | ||
type: "Link.image", | ||
value: { | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
image: { | ||
@@ -31,6 +31,11 @@ kind: link.value.kind, | ||
renderV2(def: MediaLinkDef | AnyLinkDef, link: LinkContent & { value: ImageLinkContent }): unknown { | ||
renderV2( | ||
def: MediaLinkDef | AnyLinkDef, | ||
link: LinkContent & { value: ImageLinkContent }, | ||
_fetch: undefined, | ||
options?: LinkRendererOptions, | ||
): unknown { | ||
return { | ||
link_type: "Media", | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
kind: link.value.kind, | ||
@@ -49,4 +54,8 @@ ...(FilledImageLinkContent.is(link.value) && { | ||
renderMocks(def: MediaLinkDef | AnyLinkDef, link: LinkContent & { value: 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) | ||
}, | ||
@@ -53,0 +62,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: link.value }) | ||
return ImageLinkRenderer(ctx).renderV1({ ...link, value: link.value }, undefined, options) | ||
} else if (link.value.__TYPE__ === "DocumentLink") { | ||
return DocumentLinkRenderer(ctx).renderV1({ ...link, value: 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: link.value }) | ||
return FileLinkRenderer(ctx).renderV1({ ...link, value: link.value }, undefined, options) | ||
} else if (link.value.__TYPE__ === "ExternalLink") { | ||
return ExternalLinkRenderer.renderV1({ ...link, value: link.value }) | ||
return ExternalLinkRenderer.renderV1({ ...link, value: link.value }, undefined, options) | ||
} else if (link.value.__TYPE__ === "MediaLink") { | ||
return MediaLinkRenderer.renderV1({ ...link, value: link.value }) | ||
return MediaLinkRenderer.renderV1({ ...link, value: link.value }, undefined, options) | ||
} else if (link.value.__TYPE__ === "AnyLink") { | ||
return AnyLinkRenderer.renderV1({ ...link, value: link.value }) | ||
return AnyLinkRenderer.renderV1({ ...link, value: link.value }, undefined, options) | ||
} else { | ||
@@ -35,8 +43,13 @@ 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: 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") { | ||
@@ -47,14 +60,35 @@ return DocumentLinkRenderer(ctx).renderV2( | ||
fetch, | ||
options, | ||
) | ||
} else if ((isMediaLinkDef(def) || isAnyLinkDef(def)) && content.value.__TYPE__ === "FileLink") { | ||
return FileLinkRenderer(ctx).renderV2(def.config as MediaLinkDef | AnyLinkDef, { | ||
...content, | ||
value: 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: 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: 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: content.value }) | ||
return AnyLinkRenderer.renderV2( | ||
def.config as AnyLinkDef, | ||
{ ...content, value: content.value }, | ||
undefined, | ||
options, | ||
) | ||
} else { | ||
@@ -86,27 +120,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: 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: 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: 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: 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: 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: content.value }) | ||
return AnyLinkRenderer.renderMocks(def.config as AnyLinkDef, { ...content, value: content.value }, options) | ||
} else { | ||
@@ -113,0 +163,0 @@ return this.renderDefault(def) |
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, LinkContent & { value: MediaLinkContent }> = { | ||
renderV1(link: LinkContent & { value: 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.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(link.value.text && { text: link.value.text }), | ||
@@ -16,6 +21,11 @@ }, | ||
renderV2(def: MediaLinkDef, link: LinkContent & { value: MediaLinkContent }): unknown { | ||
renderV2( | ||
def: MediaLinkDef, | ||
link: LinkContent & { value: MediaLinkContent }, | ||
_fetch: undefined, | ||
options?: LinkRendererOptions, | ||
): unknown { | ||
return { | ||
link_type: "Media", | ||
...(link.key && { key: link.key }), | ||
...(!options?.omitKey && { key: link.key }), | ||
...(def.allowText && link.value.text && { text: link.value.text }), | ||
@@ -25,4 +35,8 @@ } | ||
renderMocks(def: MediaLinkDef, link: LinkContent & { value: MediaLinkContent }): unknown { | ||
return this.renderV2(def, link) | ||
renderMocks( | ||
def: MediaLinkDef, | ||
link: LinkContent & { value: MediaLinkContent }, | ||
options?: LinkRendererOptions, | ||
): unknown { | ||
return this.renderV2(def, link, undefined, options) | ||
}, | ||
@@ -29,0 +43,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 }) | ||
} | ||
@@ -53,3 +53,6 @@ } | ||
__TYPE__: "LinkContent", | ||
key: "", // We define the key as empty so it can be removed as it is not needed for StructuredText | ||
// 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, | ||
@@ -56,0 +59,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1085128
25793