datocms-structured-text-to-html-string
Version:
Convert DatoCMS Structured Text field to HTML string
67 lines (66 loc) • 4.44 kB
TypeScript
import { RenderMarkRule, renderMarkRule, renderNodeRule, TransformedMeta, TransformMetaFn } from 'datocms-structured-text-generic-html-renderer';
import { Adapter, CdaStructuredTextRecord, CdaStructuredTextValue, Document as StructuredTextDocument, Node, Record as StructuredTextGraphQlResponseRecord, RenderError, RenderResult, RenderRule, StructuredText as StructuredTextGraphQlResponse, TypesafeCdaStructuredTextValue, TypesafeStructuredText as TypesafeStructuredTextGraphQlResponse } from 'datocms-structured-text-utils';
export { renderNodeRule, renderMarkRule, RenderError };
export type { StructuredTextDocument, CdaStructuredTextValue, TypesafeCdaStructuredTextValue, CdaStructuredTextRecord, };
declare type AdapterReturn = string | null;
export declare const defaultAdapter: {
renderNode: (tagName: string | null, attrs?: Record<string, string> | null | undefined, ...children: any[]) => AdapterReturn;
renderFragment: (children: AdapterReturn[]) => AdapterReturn;
renderText: (text: string) => AdapterReturn;
};
declare type H = typeof defaultAdapter.renderNode;
declare type T = typeof defaultAdapter.renderText;
declare type F = typeof defaultAdapter.renderFragment;
declare type RenderInlineRecordContext<R extends StructuredTextGraphQlResponseRecord> = {
record: R;
adapter: Adapter<H, T, F>;
};
declare type RenderRecordLinkContext<R extends StructuredTextGraphQlResponseRecord> = {
record: R;
adapter: Adapter<H, T, F>;
children: RenderResult<H, T, F>;
transformedMeta: TransformedMeta;
};
declare type RenderBlockContext<R extends StructuredTextGraphQlResponseRecord> = {
record: R;
adapter: Adapter<H, T, F>;
};
export declare type RenderSettings<BlockRecord extends StructuredTextGraphQlResponseRecord = StructuredTextGraphQlResponseRecord, LinkRecord extends StructuredTextGraphQlResponseRecord = StructuredTextGraphQlResponseRecord, InlineBlockRecord extends StructuredTextGraphQlResponseRecord = StructuredTextGraphQlResponseRecord> = {
/** A set of additional rules to convert the document to HTML **/
customNodeRules?: RenderRule<H, T, F>[];
/** A set of additional rules to convert the document to HTML **/
customMarkRules?: RenderMarkRule<H, T, F>[];
/** Function that converts 'link' and 'itemLink' `meta` into HTML attributes */
metaTransformer?: TransformMetaFn;
/** Fuction that converts an 'inlineItem' node into an HTML string **/
renderInlineRecord?: (context: RenderInlineRecordContext<LinkRecord>) => string | null;
/** Fuction that converts an 'itemLink' node into an HTML string **/
renderLinkToRecord?: (context: RenderRecordLinkContext<LinkRecord>) => string | null;
/** Fuction that converts a 'block' node into an HTML string **/
renderBlock?: (context: RenderBlockContext<BlockRecord>) => string | null;
/** Fuction that converts an 'inlineBlock' node into an HTML string **/
renderInlineBlock?: (context: RenderBlockContext<InlineBlockRecord>) => string | null;
/** Fuction that converts a simple string text into an HTML string **/
renderText?: T;
/** React.createElement-like function to use to convert a node into an HTML string **/
renderNode?: H;
/** Function to use to generate a React.Fragment **/
renderFragment?: F;
/** @deprecated use `customNodeRules` instead **/
customRules?: RenderRule<H, T, F>[];
};
export declare function render<BlockRecord extends StructuredTextGraphQlResponseRecord = StructuredTextGraphQlResponseRecord, LinkRecord extends StructuredTextGraphQlResponseRecord = StructuredTextGraphQlResponseRecord, InlineBlockRecord extends StructuredTextGraphQlResponseRecord = StructuredTextGraphQlResponseRecord>(
/** The actual field value you get from DatoCMS **/
structuredTextOrNode: StructuredTextGraphQlResponse<BlockRecord, LinkRecord, InlineBlockRecord> | StructuredTextDocument | Node | null | undefined,
/** Additional render settings **/
settings?: RenderSettings<BlockRecord, LinkRecord, InlineBlockRecord>): ReturnType<F> | null;
/**
* @deprecated Use renderNodeRule instead
*/
export { renderNodeRule as renderRule };
/** @deprecated Use CdaStructuredTextValue */
export type { StructuredTextGraphQlResponse };
/** @deprecated Use TypesafeCdaStructuredTextValue */
export type { TypesafeStructuredTextGraphQlResponse };
/** @deprecated Use CdaStructuredTextRecord */
export type { StructuredTextGraphQlResponseRecord };