import { JSX, ReactElement } from "react"; import { Block, BlockInstance } from "../"; /** * Returns the block attributes of a registered block node given its type. * * @param blockTypeOrName - Block type or name. * @param innerHTML - Raw block content. * @param attributes - Known block attributes (from delimiters). * * @returns All block attributes. */ export function getBlockAttributes>( blockTypeOrName: T, innerHTML: string, attributes?: Record, ): T extends Block ? U : never; export function getBlockAttributes( blockTypeOrName: string, innerHTML: string, attributes?: Record, ): Record; export namespace Schema { interface Attribute { source: "attribute"; selector?: string | undefined; attribute: string; type?: "string" | "boolean" | undefined; } interface Children { source: "children"; selector?: string | undefined; } interface HTML { source: "html"; selector?: string | undefined; multiline?: keyof HTMLElementTagNameMap | undefined; } interface Node { source: "node"; selector?: string | undefined; } interface Tag { source: "tag"; selector?: string | undefined; } interface Text { source: "text"; selector?: string | undefined; } interface Query { source: "query"; selector?: string | undefined; query: T; } } export type Source = | Schema.Attribute | Schema.Children | Schema.HTML | Schema.Node | Schema.Query | Schema.Tag | Schema.Text; // prettier-ignore export type SourceReturnValue = T extends Schema.Attribute & { type: "boolean" } ? boolean | undefined : T extends Schema.Children ? ReactElement | number | string[] : T extends Schema.Node ? JSX.Element | null : T extends Schema.Tag ? keyof (HTMLElementTagNameMap & SVGElementTagNameMap) | undefined : T extends Schema.Query ? { [k in keyof U]: U[k] extends Schema.Query ? SourceReturnValue> : SourceReturnValue; } : (string | undefined); /** * Given a block's raw content and an attribute's schema returns the attribute's * value depending on its source. * * @param innerHTML - Block's raw content. * @param attributeSchema - Attribute's schema. * * @returns Attribute value (if located). */ export function parseWithAttributeSchema( innerHTML: string, schema: Schema.Attribute & { type: "boolean" }, ): boolean | undefined; export function parseWithAttributeSchema( innerHTML: string, schema: Schema.Attribute | Schema.HTML | Schema.Text, ): string | undefined; export function parseWithAttributeSchema( innerHTML: string, schema: Schema.Children, ): Array; export function parseWithAttributeSchema(innerHTML: string, schema: Schema.Node): JSX.Element | null; export function parseWithAttributeSchema( innerHTML: string, schema: Schema.Tag, ): keyof (HTMLElementTagNameMap & SVGElementTagNameMap) | undefined; export function parseWithAttributeSchema>>( innerHTML: string, schema: Schema.Query, ): { [k in keyof T]: SourceReturnValue; }; /** * Parses the post content with a PegJS grammar and returns a list of blocks. * * @param content - The post content. */ export function parse(content: string): BlockInstance[];