@sanity/agent-directives 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"react.d.ts","names":["CallableInstance","Parameters","Result","property","parameters","unified5","trough0","vfile1","___index_js2","CallableInstance","Processor","Key","Parameters_1","Input","Output","ParseTree","HeadTree","TailTree","CompileTree","CompileResult","unist26","Node","CompileResults","Compiler","Parser","Plugin","Array","Data","Pipeline","Compatible","VFileWithOutput","ProcessCallback","Promise","RunCallback","Value","Preset","PluggableList","UsePlugin","constructor","attachers","plugin","parameters","compiler","freezeIndex","frozen","namespace","parser","transformers","copy","data","dataset","key","value","freeze","parse","file","process","done","processSync","run","tree","runSync","stringify","use","preset","list","unified","CompileResultMap","Settings","Tree","Result","VFile","document","Pluggable","PluginTuple","PluginParameters","Transformer","this","TupleParameters","plugins","settings","File","Error","error","TransformCallback","next","result","Value","CompileResults","Compiler","Parser","Pluggable","PluggableList","Plugin","PluginTuple","Preset","ProcessCallback","Processor","RunCallback","TransformCallback","Transformer","unified","emptyObjectSymbol","CompileResultMap","Uint8Array","string","Data","Settings","settings"],"sources":["../../../../node_modules/.pnpm/unified@11.0.5/node_modules/unified/lib/callable-instance.d.ts","../../../../node_modules/.pnpm/unified@11.0.5/node_modules/unified/lib/index.d.ts","../../../../node_modules/.pnpm/unified@11.0.5/node_modules/unified/index.d.ts","../../src/lib/react.tsx"],"x_google_ignoreList":[0,1,2],"mappings":";;;;;;;cAAaA,gBAAAA,6CAA6DG,QAAAA,0BAAkCC,UAAAA,EAAYH,UAAAA,KAAeC,MAAAA;;;;;;;;AAAvI;;;;;;cCcaQ,SAAAA,mBAASU,OAAAA,CAAmCC,IAAAA,2CAAID,OAAAA,CAA2DC,IAAAA,2CAAID,OAAAA,CAA2DC,IAAAA,8CAAID,OAAAA,CAA8DC,IAAAA,gDAAoDC,cAAAA,kCAAgDb,gBAAAA,KAAqBC,SAA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F,MAAA;AAAA,IACE,SAAA;AApBJ;;;;;;;;;AAIA;;;;AAJA,iBAmMgB,qBAAA,CAAsB,IAAA,EAAM,SAAA,IAAS,IAAA,EAnIrC,IAAA;AAAA,UAqJC,mBAAA;EACf,cAAA,IAAkB,MAAA,yBAA+B,YAAA;AAAA;;;;;;;;;;;;;;;;;iBAmBnC,kBAAA,0BAAA,CACd,OAAA,GAAS,mBAAA,CAAoB,YAAA;kCAKb,aAAA,+CACsB,IAAA,EAE9B,KAAA,EAAK,MAAA,GACF,KAAA,EAAO,oBAAA,CAAqB,KAAA,EAAO,YAAA,EAAc,oBAAA,MAA0B,SAAA,EAAS,gBAAA,GAC3E,sBAAA,KACjB,aAAA,CAAc,uBAAA,CAAwB,KAAA;AAAA"}
package/dist/lib/react.js CHANGED
@@ -2,8 +2,8 @@ import { pascalCase } from "es-toolkit";
2
2
  import { directiveFromMarkdown, directiveToMarkdown } from "mdast-util-directive";
3
3
  import { directive } from "micromark-extension-directive";
4
4
  import { visit } from "unist-util-visit";
5
- import { DirectiveSchemas } from "../index.js";
6
- const DIRECTIVE_TYPES = ["textDirective", "leafDirective", "containerDirective"], isDirectiveNode = (node) => "name" in node && typeof node.name == "string" && /^[a-zA-Z-]+$/.test(node.name) && DIRECTIVE_TYPES.includes(node.type), isListItemWithOnlyDirective = (node) => {
5
+ import { DIRECTIVE_TYPES, DirectiveSchemas } from "../_chunks-es/formatters.js";
6
+ const isDirectiveNode = (node) => "name" in node && typeof node.name == "string" && /^[a-zA-Z-]+$/.test(node.name) && DIRECTIVE_TYPES.includes(node.type), isListItemWithOnlyDirective = (node) => {
7
7
  if (node.type !== "listItem") return !1;
8
8
  const children = node.children || [];
9
9
  return children.length === 1 && children[0].type === "leafDirective";
@@ -85,7 +85,8 @@ function createDirectiveKit(options = {}) {
85
85
  return console.error(`Directive "${name}" validation error:`, result.error.format()), null;
86
86
  const renderProps = {
87
87
  ...result.data,
88
- ...requiresContext && useApplication ? { application } : {}
88
+ ...requiresContext && useApplication ? { application } : {},
89
+ isInline: merged.isInline === !0
89
90
  };
90
91
  return render(renderProps);
91
92
  }
@@ -1 +1 @@
1
- {"version":3,"file":"react.js","sources":["../../src/lib/react.tsx"],"sourcesContent":["import { pascalCase } from 'es-toolkit'\nimport { directiveFromMarkdown, directiveToMarkdown } from 'mdast-util-directive'\nimport { directive } from 'micromark-extension-directive'\nimport type { ComponentType, ReactNode } from 'react'\nimport type { Processor } from 'unified'\nimport type { Node } from 'unist'\nimport { visit } from 'unist-util-visit'\nimport {\n type DefineDirectiveOptions,\n type DirectiveName,\n type DirectivePropsMap,\n DirectiveSchemas,\n} from '../_internal'\n\n// ============================================================================\n// Types\n// ============================================================================\n\ntype DirectiveProps<T extends DirectiveName> = DirectivePropsMap[T]\n\nexport interface NodeProps {\n node?: { properties?: Record<string, unknown> }\n}\n\nexport type DirectiveRenderProps<\n TName extends DirectiveName,\n TApplication = undefined,\n TRequiresApplication extends boolean = true,\n> = DirectiveProps<TName> &\n (TRequiresApplication extends true\n ? TApplication extends undefined\n ? {}\n : { application: TApplication }\n : {})\n\nexport type { DefineDirectiveOptions }\n\nexport type DirectiveComponentProps<TName extends DirectiveName> = DirectiveProps<TName> & {\n source?: string\n} & NodeProps\n\n// ============================================================================\n// Remark Plugin Types\n// ============================================================================\n\ninterface DirectiveNode extends Node {\n type: 'textDirective' | 'leafDirective' | 'containerDirective'\n name: string\n attributes?: Record<string, string>\n children?: DirectiveNode[]\n value?: string\n data?: {\n hName?: string\n hProperties?: Record<string, unknown>\n }\n}\n\ntype DirectiveType = 'textDirective' | 'leafDirective' | 'containerDirective'\n\nconst DIRECTIVE_TYPES: DirectiveType[] = ['textDirective', 'leafDirective', 'containerDirective']\n\nconst isDirectiveNode = (node: Node): node is DirectiveNode =>\n 'name' in node &&\n typeof (node as DirectiveNode).name === 'string' &&\n /^[a-zA-Z-]+$/.test((node as DirectiveNode).name) &&\n DIRECTIVE_TYPES.includes(node.type as DirectiveType)\n\nconst isListItemWithOnlyDirective = (node: Node): boolean => {\n if (node.type !== 'listItem') return false\n const children = (node as DirectiveNode).children || []\n return children.length === 1 && children[0].type === 'leafDirective'\n}\n\n// ============================================================================\n// Remark Plugin\n// ============================================================================\n\n/**\n * Remark plugin that transforms directive syntax into React components.\n *\n * - Validates directive names (letters and hyphens only)\n * - Extracts directives from list items\n * - Groups consecutive leaf directives into DirectivesStack wrappers\n * - Filters incomplete streaming directives\n * - Converts directive names to PascalCase component names\n */\nfunction remarkDirectivesTransform() {\n return (tree: Node) => {\n // Remove invalid directive nodes (e.g., \":02\" in \"14:02\")\n visit(\n tree,\n DIRECTIVE_TYPES,\n (node: Node, index: number | undefined, parent: Node | undefined) => {\n if (!isDirectiveNode(node)) {\n const parentNode = parent as DirectiveNode | undefined\n if (parentNode?.children && typeof index === 'number') {\n const nodeName = 'name' in node ? String((node as DirectiveNode).name) : ''\n parentNode.children[index] = {\n type: 'text' as DirectiveType,\n name: '',\n value: `:${nodeName}`,\n } as DirectiveNode\n }\n }\n },\n )\n\n // Extract directives from list items\n visit(\n tree,\n 'list',\n (node: Node, index: number | undefined, parent: Node | undefined): number | undefined => {\n const listNode = node as DirectiveNode\n if (!listNode.children) return undefined\n\n const transformed: DirectiveNode[] = []\n let hasTransformed = false\n\n for (const child of listNode.children) {\n if (isListItemWithOnlyDirective(child) && child.children?.[0]) {\n transformed.push(child.children[0])\n hasTransformed = true\n } else {\n transformed.push(child)\n }\n }\n\n const parentNode = parent as DirectiveNode | undefined\n if (hasTransformed && transformed.every((n) => isDirectiveNode(n))) {\n if (parentNode?.children && typeof index === 'number') {\n parentNode.children.splice(index, 1, ...transformed)\n return index + transformed.length\n }\n }\n return undefined\n },\n )\n\n // Group consecutive leaf directives into DirectivesStack\n visit(tree, (node: Node) => {\n const dirNode = node as DirectiveNode\n if (!dirNode.children?.length || dirNode.data?.hName === 'DirectivesStack') return\n\n const newChildren: DirectiveNode[] = []\n let group: DirectiveNode[] = []\n\n for (const child of dirNode.children) {\n if (isDirectiveNode(child) && child.type === 'leafDirective') {\n group.push(child)\n } else {\n if (group.length > 0) {\n newChildren.push({\n type: 'leafDirective',\n name: 'DirectivesStack',\n data: { hName: 'DirectivesStack' },\n children: group,\n })\n group = []\n }\n newChildren.push(child)\n }\n }\n\n if (group.length > 0) {\n newChildren.push({\n type: 'leafDirective',\n name: 'DirectivesStack',\n data: { hName: 'DirectivesStack' },\n children: group,\n })\n }\n\n dirNode.children = newChildren\n })\n\n // Filter incomplete streaming directives\n visit(tree, 'text', (node: Node) => {\n const textNode = node as DirectiveNode\n if (typeof textNode.value === 'string') {\n if (textNode.value.match(/::?\\w+[{[]/) && !textNode.value.match(/::?\\w+[{[].+[}\\]]/)) {\n textNode.value = ''\n }\n }\n })\n\n // Transform directives to React components\n visit(tree, DIRECTIVE_TYPES, (node: Node) => {\n if (!isDirectiveNode(node)) return\n\n node.data = node.data || {}\n node.data.hName = pascalCase(node.name)\n node.data.hProperties = {\n ...node.attributes,\n isInline: node.type === 'textDirective',\n }\n })\n }\n}\n\ninterface RemarkData {\n micromarkExtensions?: unknown[]\n fromMarkdownExtensions?: unknown[]\n toMarkdownExtensions?: unknown[]\n}\n\n/**\n * Remark plugin for agent directive support.\n * Parses and transforms directive syntax into React components.\n *\n * @example\n * ```tsx\n * import { remarkAgentDirectives } from '@sanity/agent-directives/react'\n *\n * <ReactMarkdown remarkPlugins={[remarkAgentDirectives]}>\n * {content}\n * </ReactMarkdown>\n * ```\n */\nexport function remarkAgentDirectives(this: Processor) {\n const data = this.data() as RemarkData\n\n if (!data.micromarkExtensions) data.micromarkExtensions = []\n if (!data.fromMarkdownExtensions) data.fromMarkdownExtensions = []\n if (!data.toMarkdownExtensions) data.toMarkdownExtensions = []\n\n data.micromarkExtensions.push(directive())\n data.fromMarkdownExtensions.push(directiveFromMarkdown())\n data.toMarkdownExtensions.push(directiveToMarkdown())\n\n return remarkDirectivesTransform()\n}\n\n// ============================================================================\n// Directive Kit\n// ============================================================================\n\nexport interface DirectiveKitOptions<TApplication> {\n useApplication?: (source: string | undefined) => TApplication | undefined\n}\n\n/**\n * Creates a directive kit with application injection.\n *\n * @example\n * ```tsx\n * // kit.ts\n * export const { defineDirective } = createDirectiveKit<Application>({\n * useApplication: (source) => useThreadApplication(source),\n * })\n *\n * // Document.tsx\n * export const DocumentDirective = defineDirective('document', ({ id, application }) => {\n * return <DocumentCard id={id} app={application} />\n * })\n * ```\n */\nexport function createDirectiveKit<TApplication = undefined>(\n options: DirectiveKitOptions<TApplication> = {},\n) {\n const { useApplication } = options\n\n function defineDirective<\n TName extends DirectiveName,\n TRequiresApplication extends boolean = true,\n >(\n name: TName,\n render: (props: DirectiveRenderProps<TName, TApplication, TRequiresApplication>) => ReactNode,\n directiveOptions: DefineDirectiveOptions = {},\n ): ComponentType<DirectiveComponentProps<TName>> {\n const requiresContext =\n directiveOptions.requiresContext ?? directiveOptions.requiresApplication ?? true\n\n function Directive(props: DirectiveComponentProps<TName>): ReactNode {\n const { node, source, ...rest } = props\n\n const application = useApplication?.(source)\n if (requiresContext && useApplication && !application) return null\n\n const merged = { ...(node?.properties ?? {}), ...rest }\n\n const schema = DirectiveSchemas[name]\n const result = schema.safeParse(merged)\n if (!result.success) {\n console.error(`Directive \"${name}\" validation error:`, result.error.format())\n return null\n }\n\n const renderProps = {\n ...(result.data as DirectiveProps<TName>),\n ...(requiresContext && useApplication ? { application } : {}),\n } as DirectiveRenderProps<TName, TApplication, TRequiresApplication>\n\n return render(renderProps)\n }\n\n Directive.displayName = `Directive(${name})`\n return Directive\n }\n\n return { defineDirective }\n}\n"],"names":[],"mappings":";;;;;AA2DA,MAAM,kBAAmC,CAAC,iBAAiB,iBAAiB,oBAAoB,GAE1F,kBAAkB,CAAC,SACvB,UAAU,QACV,OAAQ,KAAuB,QAAS,YACxC,eAAe,KAAM,KAAuB,IAAI,KAChD,gBAAgB,SAAS,KAAK,IAAqB,GAE/C,8BAA8B,CAAC,SAAwB;AAC3D,MAAI,KAAK,SAAS,WAAY,QAAO;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AC7C;AACA,QAAM,EAAE,mBAAmB;AAE3B,WAAS,gBAIP,MACA,QACA,mBAA2C,CAAA,GACI;AAC/C,UAAM,kBACJ,iBAAiB,mBAAmB,iBAAiB,uBAAuB;AAE9E,aAAS,UAAU,OAAkD;AACnE,YAAM,EAAE,MAAM,QAAQ,GAAG,SAAS,OAE5B,cAAc,iBAAiB,MAAM;AAC3C,UAAI,mBAAmB,kBAAkB,CAAC,YAAa,QAAO;AAE9D,YAAM,SAAS,EAAE,GAAI,MAAM,cAAc,CAAA,GAAK,GAAG,KAAA,GAG3C,SADS,iBAAiB,IAAI,EACd,UAAU,MAAM;AACtC,UAAI,CAAC,OAAO;AACV,eAAA,QAAQ,MAAM,cAAc,IAAI,uBAAuB,OAAO,MAAM,OAAA,CAAQ,GACrE;AAGT,YAAM,cAAc;AAAA,QAClB,GAAI,OAAO;AAAA,QACX,GAAI,mBAAmB,iBAAiB,EAAE,gBAAgB,CAAA;AAAA,MAAC;AAG7D,aAAO,OAAO,WAAW;AAAA,IAC3B;AAEA,WAAA,UAAU,cAAc,aAAa,IAAI,KAClC;AAAA,EACT;AAEA,SAAO,EAAE,gBAAA;AACX;"}
1
+ {"version":3,"file":"react.js","sources":["../../src/lib/react.tsx"],"sourcesContent":["import { pascalCase } from 'es-toolkit'\nimport { directiveFromMarkdown, directiveToMarkdown } from 'mdast-util-directive'\nimport { directive } from 'micromark-extension-directive'\nimport type { ComponentType, ReactNode } from 'react'\nimport type { Processor } from 'unified'\nimport type { Node } from 'unist'\nimport { visit } from 'unist-util-visit'\nimport {\n type DefineDirectiveOptions,\n DIRECTIVE_TYPES,\n type DirectiveName,\n type DirectivePropsMap,\n DirectiveSchemas,\n type DirectiveType,\n} from '../_internal'\n\n// ============================================================================\n// Types\n// ============================================================================\n\ntype DirectiveProps<T extends DirectiveName> = DirectivePropsMap[T]\n\nexport interface NodeProps {\n node?: { properties?: Record<string, unknown> }\n}\n\nexport type DirectiveRenderProps<\n TName extends DirectiveName,\n TApplication = undefined,\n TRequiresApplication extends boolean = true,\n> = DirectiveProps<TName> & {\n isInline: boolean\n} & (TRequiresApplication extends true\n ? TApplication extends undefined\n ? {}\n : { application: TApplication }\n : {})\n\nexport type { DefineDirectiveOptions }\n\nexport type DirectiveComponentProps<TName extends DirectiveName> = DirectiveProps<TName> & {\n source?: string\n} & NodeProps\n\n// ============================================================================\n// Remark Plugin Types\n// ============================================================================\n\ninterface DirectiveNode extends Node {\n type: 'textDirective' | 'leafDirective' | 'containerDirective'\n name: string\n attributes?: Record<string, string>\n children?: DirectiveNode[]\n value?: string\n data?: {\n hName?: string\n hProperties?: Record<string, unknown>\n }\n}\n\nconst isDirectiveNode = (node: Node): node is DirectiveNode =>\n 'name' in node &&\n typeof (node as DirectiveNode).name === 'string' &&\n /^[a-zA-Z-]+$/.test((node as DirectiveNode).name) &&\n DIRECTIVE_TYPES.includes(node.type as DirectiveType)\n\nconst isListItemWithOnlyDirective = (node: Node): boolean => {\n if (node.type !== 'listItem') return false\n const children = (node as DirectiveNode).children || []\n return children.length === 1 && children[0].type === 'leafDirective'\n}\n\n// ============================================================================\n// Remark Plugin\n// ============================================================================\n\n/**\n * Remark plugin that transforms directive syntax into React components.\n *\n * - Validates directive names (letters and hyphens only)\n * - Extracts directives from list items\n * - Groups consecutive leaf directives into DirectivesStack wrappers\n * - Filters incomplete streaming directives\n * - Converts directive names to PascalCase component names\n */\nfunction remarkDirectivesTransform() {\n return (tree: Node) => {\n // Remove invalid directive nodes (e.g., \":02\" in \"14:02\")\n visit(\n tree,\n DIRECTIVE_TYPES,\n (node: Node, index: number | undefined, parent: Node | undefined) => {\n if (!isDirectiveNode(node)) {\n const parentNode = parent as DirectiveNode | undefined\n if (parentNode?.children && typeof index === 'number') {\n const nodeName = 'name' in node ? String((node as DirectiveNode).name) : ''\n parentNode.children[index] = {\n type: 'text' as DirectiveType,\n name: '',\n value: `:${nodeName}`,\n } as DirectiveNode\n }\n }\n },\n )\n\n // Extract directives from list items\n visit(\n tree,\n 'list',\n (node: Node, index: number | undefined, parent: Node | undefined): number | undefined => {\n const listNode = node as DirectiveNode\n if (!listNode.children) return undefined\n\n const transformed: DirectiveNode[] = []\n let hasTransformed = false\n\n for (const child of listNode.children) {\n if (isListItemWithOnlyDirective(child) && child.children?.[0]) {\n transformed.push(child.children[0])\n hasTransformed = true\n } else {\n transformed.push(child)\n }\n }\n\n const parentNode = parent as DirectiveNode | undefined\n if (hasTransformed && transformed.every((n) => isDirectiveNode(n))) {\n if (parentNode?.children && typeof index === 'number') {\n parentNode.children.splice(index, 1, ...transformed)\n return index + transformed.length\n }\n }\n return undefined\n },\n )\n\n // Group consecutive leaf directives into DirectivesStack\n visit(tree, (node: Node) => {\n const dirNode = node as DirectiveNode\n if (!dirNode.children?.length || dirNode.data?.hName === 'DirectivesStack') return\n\n const newChildren: DirectiveNode[] = []\n let group: DirectiveNode[] = []\n\n for (const child of dirNode.children) {\n if (isDirectiveNode(child) && child.type === 'leafDirective') {\n group.push(child)\n } else {\n if (group.length > 0) {\n newChildren.push({\n type: 'leafDirective',\n name: 'DirectivesStack',\n data: { hName: 'DirectivesStack' },\n children: group,\n })\n group = []\n }\n newChildren.push(child)\n }\n }\n\n if (group.length > 0) {\n newChildren.push({\n type: 'leafDirective',\n name: 'DirectivesStack',\n data: { hName: 'DirectivesStack' },\n children: group,\n })\n }\n\n dirNode.children = newChildren\n })\n\n // Filter incomplete streaming directives\n visit(tree, 'text', (node: Node) => {\n const textNode = node as DirectiveNode\n if (typeof textNode.value === 'string') {\n if (textNode.value.match(/::?\\w+[{[]/) && !textNode.value.match(/::?\\w+[{[].+[}\\]]/)) {\n textNode.value = ''\n }\n }\n })\n\n // Transform directives to React components\n visit(tree, DIRECTIVE_TYPES, (node: Node) => {\n if (!isDirectiveNode(node)) return\n\n node.data = node.data || {}\n node.data.hName = pascalCase(node.name)\n node.data.hProperties = {\n ...node.attributes,\n isInline: node.type === 'textDirective',\n }\n })\n }\n}\n\ninterface RemarkData {\n micromarkExtensions?: unknown[]\n fromMarkdownExtensions?: unknown[]\n toMarkdownExtensions?: unknown[]\n}\n\n/**\n * Remark plugin for agent directive support.\n * Parses and transforms directive syntax into React components.\n *\n * @example\n * ```tsx\n * import { remarkAgentDirectives } from '@sanity/agent-directives/react'\n *\n * <ReactMarkdown remarkPlugins={[remarkAgentDirectives]}>\n * {content}\n * </ReactMarkdown>\n * ```\n */\nexport function remarkAgentDirectives(this: Processor) {\n const data = this.data() as RemarkData\n\n if (!data.micromarkExtensions) data.micromarkExtensions = []\n if (!data.fromMarkdownExtensions) data.fromMarkdownExtensions = []\n if (!data.toMarkdownExtensions) data.toMarkdownExtensions = []\n\n data.micromarkExtensions.push(directive())\n data.fromMarkdownExtensions.push(directiveFromMarkdown())\n data.toMarkdownExtensions.push(directiveToMarkdown())\n\n return remarkDirectivesTransform()\n}\n\n// ============================================================================\n// Directive Kit\n// ============================================================================\n\nexport interface DirectiveKitOptions<TApplication> {\n useApplication?: (source: string | undefined) => TApplication | undefined\n}\n\n/**\n * Creates a directive kit with application injection.\n *\n * @example\n * ```tsx\n * // kit.ts\n * export const { defineDirective } = createDirectiveKit<Application>({\n * useApplication: (source) => useThreadApplication(source),\n * })\n *\n * // Document.tsx\n * export const DocumentDirective = defineDirective('document', ({ id, application }) => {\n * return <DocumentCard id={id} app={application} />\n * })\n * ```\n */\nexport function createDirectiveKit<TApplication = undefined>(\n options: DirectiveKitOptions<TApplication> = {},\n) {\n const { useApplication } = options\n\n function defineDirective<\n TName extends DirectiveName,\n TRequiresApplication extends boolean = true,\n >(\n name: TName,\n render: (props: DirectiveRenderProps<TName, TApplication, TRequiresApplication>) => ReactNode,\n directiveOptions: DefineDirectiveOptions = {},\n ): ComponentType<DirectiveComponentProps<TName>> {\n const requiresContext =\n directiveOptions.requiresContext ?? directiveOptions.requiresApplication ?? true\n\n function Directive(props: DirectiveComponentProps<TName>): ReactNode {\n const { node, source, ...rest } = props\n\n const application = useApplication?.(source)\n if (requiresContext && useApplication && !application) return null\n\n const merged = { ...(node?.properties ?? {}), ...rest }\n\n const schema = DirectiveSchemas[name]\n const result = schema.safeParse(merged)\n if (!result.success) {\n console.error(`Directive \"${name}\" validation error:`, result.error.format())\n return null\n }\n\n const renderProps = {\n ...(result.data as DirectiveProps<TName>),\n ...(requiresContext && useApplication ? { application } : {}),\n isInline: merged.isInline === true,\n } as DirectiveRenderProps<TName, TApplication, TRequiresApplication>\n\n return render(renderProps)\n }\n\n Directive.displayName = `Directive(${name})`\n return Directive\n }\n\n return { defineDirective }\n}\n"],"names":[],"mappings":";;;;;AA4DA,MAAM,kBAAkB,CAAC,SACvB,UAAU,QACV,OAAQ,KAAuB,QAAS,YACxC,eAAe,KAAM,KAAuB,IAAI,KAChD,gBAAgB,SAAS,KAAK,IAAqB,GAE/C,8BAA8B,CAAC,SAAwB;AAC3D,MAAI,KAAK,SAAS,WAAY,QAAO;AACrC,QAAM,WAAY,KAAuB,YAAY,CAAA;AACrD,SAAO,SAAS,WAAW,KAAK,SAAS,CAAC,EAAE,SAAS;AACvD;AAeA,SAAS,4BAA4B;AACnC,SAAO,CAAC,SAAe;AAErB;AAAA,MACE;AAAA,MACA;AAAA,MACA,CAAC,MAAY,OAA2B,WAA6B;AACnE,YAAI,CAAC,gBAAgB,IAAI,GAAG;AAC1B,gBAAM,aAAa;AACnB,cAAI,YAAY,YAAY,OAAO,SAAU,UAAU;AACrD,kBAAM,WAAW,UAAU,OAAO,OAAQ,KAAuB,IAAI,IAAI;AACzE,uBAAW,SAAS,KAAK,IAAI;AAAA,cAC3B,MAAM;AAAA,cACN,MAAM;AAAA,cACN,OAAO,IAAI,QAAQ;AAAA,YAAA;AAAA,UAEvB;AAAA,QACF;AAAA,MACF;AAAA,IAAA,GAIF;AAAA,MACE;AAAA,MACA;AAAA,MACA,CAAC,MAAY,OAA2B,WAAiD;AACvF,cAAM,WAAW;AACjB,YAAI,CAAC,SAAS,SAAU;AAExB,cAAM,cAA+B,CAAA;AACrC,YAAI,iBAAiB;AAErB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UAAU,cAAc,aAAa,IAAI,KAClC;AAAA,EACT;AAEA,SAAO,EAAE,gBAAA;AACX;"}
@@ -44,7 +44,9 @@ declare function createDirectiveKit<TContext, TOutput>(): {
44
44
  registry: DirectiveRegistry<TContext, TOutput>;
45
45
  };
46
46
  interface DirectiveRenderResult<TBlock = unknown> {
47
+ /** Markdown text to parse and insert (e.g., `[title](url)` becomes a link) */
47
48
  text?: string;
49
+ /** Blocks to collect (e.g., Slack buttons) */
48
50
  blocks?: TBlock[];
49
51
  }
50
52
  type DirectiveOutput<TBlock = unknown> = string | DirectiveRenderResult<TBlock> | null;
@@ -68,5 +70,37 @@ type RenderFn<TContext, TOutput> = (node: DirectiveNode, context: TContext) => P
68
70
  * ```
69
71
  */
70
72
  declare function createRemarkRenderer<TContext, TBlock>(renderDirective: RenderFn<TContext, DirectiveRenderResult<TBlock> | null>): (context: TContext, collector: BlockCollector<TBlock>) => (tree: Root) => Promise<void>;
71
- export { BlockCollector, type DefineDirectiveOptions, type DirectiveName, DirectiveNode, DirectiveOutput, DirectiveRenderFn, DirectiveRenderProps, DirectiveRenderResult, RegisteredDirective, createDirectiveKit, createRemarkRenderer, normalizeOutput };
72
- //# sourceMappingURL=string.d.ts.map
73
+ interface ProcessDirectivesResult<TBlock> {
74
+ /** Processed markdown with directives replaced */
75
+ markdown: string;
76
+ /** Collected blocks from directive outputs (e.g., Slack buttons, Teams cards) */
77
+ blocks: TBlock[];
78
+ }
79
+ /**
80
+ * Process markdown containing directives and return the result.
81
+ *
82
+ * This is a convenience function that assembles the remark pipeline for you.
83
+ * Use this instead of manually creating the pipeline with createRemarkRenderer.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * // Slack client
88
+ * const { markdown, blocks } = await processDirectives(
89
+ * input,
90
+ * context,
91
+ * renderDirective
92
+ * )
93
+ * return { text: slackifyMarkdown(markdown), blocks }
94
+ *
95
+ * // MS Teams client
96
+ * const { markdown, blocks } = await processDirectives(
97
+ * input,
98
+ * context,
99
+ * renderDirective
100
+ * )
101
+ * return { text: markdown, cards: blocks }
102
+ * ```
103
+ */
104
+ declare function processDirectives<TContext, TBlock>(input: string, context: TContext, renderFn: RenderFn<TContext, DirectiveRenderResult<TBlock> | null>): Promise<ProcessDirectivesResult<TBlock>>;
105
+ export { BlockCollector, type DefineDirectiveOptions, type DirectiveName, DirectiveNode, DirectiveOutput, DirectiveRenderFn, DirectiveRenderProps, DirectiveRenderResult, ProcessDirectivesResult, RegisteredDirective, createDirectiveKit, createRemarkRenderer, normalizeOutput, processDirectives };
106
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","names":[],"sources":["../../src/lib/server.ts"],"mappings":";;KAqBK,cAAA,WAAyB,aAAA,IAAiB,iBAAA,CAAkB,CAAA;AAAA,UAEhD,aAAA;EACf,IAAA;EACA,IAAA;EACA,UAAA,GAAa,MAAA;EACb,QAAA;AAAA;AAAA,UAGe,oBAAA,eAAmC,aAAA;EAClD,KAAA,EAAO,cAAA,CAAe,KAAA;EACtB,OAAA,EAAS,QAAA;EACT,QAAA;EACA,QAAA;AAAA;AAAA,KAGU,iBAAA,eAAgC,aAAA,wBAC1C,IAAA,EAAM,oBAAA,CAAqB,KAAA,EAAO,QAAA,MAC/B,OAAA,UAAiB,OAAA,CAAQ,OAAA;AAAA,UAQb,mBAAA;EACf,IAAA,EAAM,aAAA;EACN,MAAA,EAAQ,iBAAA,CAAkB,aAAA,EAAe,QAAA,EAAU,OAAA;EACnD,OAAA,EAAS,sBAAA;AAAA;AAAA,KAGN,iBAAA,sBAAuC,MAAA,SAAe,mBAAA,CAAoB,QAAA,EAAU,OAAA;;;;;AAvBzF;;;;;;;;;;;;;;iBA8DgB,kBAAA,mBAAA,CAAA;kCAGyB,aAAA,EAAa,IAAA,EAC5C,KAAA,EAAK,MAAA,EACH,iBAAA,CAAkB,KAAA,EAAO,QAAA,EAAU,OAAA,GAAQ,OAAA,GAC1C,sBAAA,KAAsB,mBAAA,CAAA,QAAA,EAAA,OAAA;0BAWI,aAAA,EAAa,OAAA,EAAW,QAAA,KAAW,OAAA,CAAQ,OAAA;;;UA4BjE,qBAAA;EAvGP;EAyGR,IAAA;EAtG2B;EAwG3B,MAAA,GAAS,MAAA;AAAA;AAAA,KAGC,eAAA,8BAA6C,qBAAA,CAAsB,MAAA;AAAA,iBAE/D,eAAA,QAAA,CACd,MAAA,EAAQ,eAAA,CAAgB,MAAA,IACvB,qBAAA,CAAsB,MAAA;AAAA,UAoCR,cAAA;EACf,MAAA,EAAQ,MAAA;AAAA;AAAA,KAGL,QAAA,uBACH,IAAA,EAAM,aAAA,EACN,OAAA,EAAS,QAAA,KACN,OAAA,CAAQ,OAAA;;;;;;;;;;;;;;;iBAgBG,oBAAA,kBAAA,CACd,eAAA,EAAiB,QAAA,CAAS,QAAA,EAAU,qBAAA,CAAsB,MAAA,aAEnB,OAAA,EAAS,QAAA,EAAU,SAAA,EAAW,cAAA,CAAe,MAAA,OACpE,IAAA,EAAM,IAAA,KAAO,OAAA;AAAA,UA2Dd,uBAAA;EA/NmB;EAiOlC,QAAA;EAhOM;EAkON,MAAA,EAAQ,MAAA;AAAA;;;;;;;;;;;;;;;;;;;AA/NT;;;;;;;iBA2PqB,iBAAA,kBAAA,CACpB,KAAA,UACA,OAAA,EAAS,QAAA,EACT,QAAA,EAAU,QAAA,CAAS,QAAA,EAAU,qBAAA,CAAsB,MAAA,YAClD,OAAA,CAAQ,uBAAA,CAAwB,MAAA"}
@@ -1,5 +1,9 @@
1
+ import remarkDirective from "remark-directive";
2
+ import remarkParse from "remark-parse";
3
+ import remarkStringify from "remark-stringify";
4
+ import { unified } from "unified";
1
5
  import { visit } from "unist-util-visit";
2
- import { DirectiveSchemas } from "../index.js";
6
+ import { DIRECTIVE_TYPES, DirectiveSchemas } from "../_chunks-es/formatters.js";
3
7
  function extractChildren(node) {
4
8
  if (!node.children || !Array.isArray(node.children))
5
9
  return "";
@@ -36,7 +40,9 @@ function createDirectiveKit() {
36
40
  function normalizeOutput(output) {
37
41
  return output === null ? null : typeof output == "string" ? { text: output } : output;
38
42
  }
39
- const DIRECTIVE_TYPES = ["textDirective", "leafDirective", "containerDirective"];
43
+ function parseMarkdown(markdown) {
44
+ return unified().use(remarkParse).parse(markdown);
45
+ }
40
46
  function isDirectiveNode(node) {
41
47
  return typeof node == "object" && node !== null && "type" in node && typeof node.type == "string" && DIRECTIVE_TYPES.includes(node.type) && "name" in node && typeof node.name == "string";
42
48
  }
@@ -56,10 +62,16 @@ function createRemarkRenderer(renderDirective) {
56
62
  for (let i = replacements.length - 1; i >= 0; i--) {
57
63
  const { node, index, parent, output } = replacements[i];
58
64
  if (output?.blocks && collector.blocks.unshift(...output.blocks), output?.text) {
59
- parent.children.splice(index, 1, {
60
- type: "paragraph",
61
- children: [{ type: "text", value: output.text }]
62
- });
65
+ const parsed = parseMarkdown(output.text);
66
+ if (node.type === "textDirective") {
67
+ const firstNode = parsed.children[0];
68
+ if (firstNode?.type === "paragraph") {
69
+ const paragraph = firstNode;
70
+ parent.children.splice(index, 1, ...paragraph.children);
71
+ } else
72
+ parent.children.splice(index, 1, ...parsed.children);
73
+ } else
74
+ parent.children.splice(index, 1, ...parsed.children);
63
75
  continue;
64
76
  }
65
77
  if (node.type === "containerDirective" && node.children && node.children.length > 0) {
@@ -72,9 +84,17 @@ function createRemarkRenderer(renderDirective) {
72
84
  };
73
85
  };
74
86
  }
87
+ async function processDirectives(input, context, renderFn) {
88
+ const collector = { blocks: [] }, remarkRenderDirectives = createRemarkRenderer(renderFn), file = await unified().use(remarkParse).use(remarkDirective).use(remarkRenderDirectives, context, collector).use(remarkStringify).process(input);
89
+ return {
90
+ markdown: String(file),
91
+ blocks: collector.blocks
92
+ };
93
+ }
75
94
  export {
76
95
  createDirectiveKit,
77
96
  createRemarkRenderer,
78
- normalizeOutput
97
+ normalizeOutput,
98
+ processDirectives
79
99
  };
80
- //# sourceMappingURL=string.js.map
100
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sources":["../../src/lib/server.ts"],"sourcesContent":["import type { Paragraph, Parent, Root, RootContent } from 'mdast'\nimport type { ContainerDirective } from 'mdast-util-directive'\nimport remarkDirective from 'remark-directive'\nimport remarkParse from 'remark-parse'\nimport remarkStringify from 'remark-stringify'\nimport { unified } from 'unified'\nimport { visit } from 'unist-util-visit'\nimport {\n type DefineDirectiveOptions,\n DIRECTIVE_TYPES,\n type DirectiveName,\n type DirectivePropsMap,\n DirectiveSchemas,\n} from '../_internal'\n\nexport type { DirectiveName }\n\n// ============================================================================\n// Types\n// ============================================================================\n\ntype DirectiveProps<T extends DirectiveName> = DirectivePropsMap[T]\n\nexport interface DirectiveNode {\n type: 'textDirective' | 'leafDirective' | 'containerDirective'\n name: string\n attributes?: Record<string, string | null | undefined>\n children?: unknown[]\n}\n\nexport interface DirectiveRenderProps<TName extends DirectiveName, TContext> {\n props: DirectiveProps<TName>\n context: TContext\n children: string\n isInline: boolean\n}\n\nexport type DirectiveRenderFn<TName extends DirectiveName, TContext, TOutput> = (\n args: DirectiveRenderProps<TName, TContext>,\n) => TOutput | null | Promise<TOutput | null>\n\nexport type { DefineDirectiveOptions }\n\n// ============================================================================\n// Directive Kit\n// ============================================================================\n\nexport interface RegisteredDirective<TContext, TOutput> {\n name: DirectiveName\n render: DirectiveRenderFn<DirectiveName, TContext, TOutput>\n options: DefineDirectiveOptions\n}\n\ntype DirectiveRegistry<TContext, TOutput> = Record<string, RegisteredDirective<TContext, TOutput>>\n\nfunction extractChildren(node: DirectiveNode): string {\n if (!node.children || !Array.isArray(node.children)) {\n return ''\n }\n\n function extractText(n: unknown): string {\n if (!n || typeof n !== 'object') return ''\n if ('type' in n && n.type === 'text' && 'value' in n && typeof n.value === 'string') {\n return n.value\n }\n if ('children' in n && Array.isArray(n.children)) {\n return n.children.map(extractText).join('')\n }\n return ''\n }\n\n return node.children.map(extractText).join('')\n}\n\n/**\n * Creates a directive kit for server-side rendering.\n *\n * @example\n * ```ts\n * // kit.ts\n * export const { defineDirective, renderDirective } = createDirectiveKit<\n * SlackContext,\n * SlackOutput\n * >()\n *\n * // document.ts\n * defineDirective('document', async ({ props, context }) => {\n * const doc = await fetchDocument(props.id, context.accessToken)\n * return { blocks: [linkButton(doc.title, url)] }\n * })\n * ```\n */\nexport function createDirectiveKit<TContext, TOutput>() {\n const registry: DirectiveRegistry<TContext, TOutput> = {}\n\n function defineDirective<TName extends DirectiveName>(\n name: TName,\n render: DirectiveRenderFn<TName, TContext, TOutput>,\n options: DefineDirectiveOptions = {},\n ) {\n const directive: RegisteredDirective<TContext, TOutput> = {\n name,\n render: render as DirectiveRenderFn<DirectiveName, TContext, TOutput>,\n options,\n }\n registry[name] = directive\n return directive\n }\n\n async function renderDirective(node: DirectiveNode, context: TContext): Promise<TOutput | null> {\n const directive = registry[node.name]\n if (!directive) return null\n\n const schema = DirectiveSchemas[node.name as DirectiveName]\n if (!schema) return null\n\n const result = schema.safeParse(node.attributes ?? {})\n if (!result.success) {\n console.error(`Directive \"${node.name}\" validation error:`, result.error.format())\n return null\n }\n\n return directive.render({\n props: result.data as DirectiveProps<DirectiveName>,\n context,\n children: extractChildren(node),\n isInline: node.type === 'textDirective',\n })\n }\n\n return { defineDirective, renderDirective, registry }\n}\n\n// ============================================================================\n// Output Utilities\n// ============================================================================\n\nexport interface DirectiveRenderResult<TBlock = unknown> {\n /** Markdown text to parse and insert (e.g., `[title](url)` becomes a link) */\n text?: string\n /** Blocks to collect (e.g., Slack buttons) */\n blocks?: TBlock[]\n}\n\nexport type DirectiveOutput<TBlock = unknown> = string | DirectiveRenderResult<TBlock> | null\n\nexport function normalizeOutput<TBlock>(\n output: DirectiveOutput<TBlock>,\n): DirectiveRenderResult<TBlock> | null {\n if (output === null) return null\n if (typeof output === 'string') return { text: output }\n return output\n}\n\n/**\n * Parse a markdown string into AST nodes.\n * Used to convert directive text output into proper AST.\n */\nfunction parseMarkdown(markdown: string): Root {\n return unified().use(remarkParse).parse(markdown)\n}\n\n// ============================================================================\n// Remark Plugin\n// ============================================================================\n\nfunction isDirectiveNode(node: unknown): node is DirectiveNode {\n return (\n typeof node === 'object' &&\n node !== null &&\n 'type' in node &&\n typeof node.type === 'string' &&\n DIRECTIVE_TYPES.includes(node.type as (typeof DIRECTIVE_TYPES)[number]) &&\n 'name' in node &&\n typeof node.name === 'string'\n )\n}\n\ninterface DirectiveLocation {\n node: DirectiveNode\n index: number\n parent: Parent\n}\n\nexport interface BlockCollector<TBlock = unknown> {\n blocks: TBlock[]\n}\n\ntype RenderFn<TContext, TOutput> = (\n node: DirectiveNode,\n context: TContext,\n) => Promise<TOutput | null>\n\n/**\n * Creates a remark plugin that renders directives and collects output blocks.\n *\n * @example\n * ```ts\n * const remarkRenderDirectives = createRemarkRenderer<SlackContext, SlackBlock>(renderDirective)\n *\n * unified()\n * .use(remarkParse)\n * .use(remarkDirective)\n * .use(remarkRenderDirectives, context, collector)\n * .use(remarkStringify)\n * ```\n */\nexport function createRemarkRenderer<TContext, TBlock>(\n renderDirective: RenderFn<TContext, DirectiveRenderResult<TBlock> | null>,\n) {\n return function remarkRenderDirectives(context: TContext, collector: BlockCollector<TBlock>) {\n return async (tree: Root): Promise<void> => {\n const locations: DirectiveLocation[] = []\n\n visit(tree, DIRECTIVE_TYPES, (node, index, parent) => {\n if (!isDirectiveNode(node)) return\n if (!parent || typeof index !== 'number') return\n locations.push({ node, index, parent })\n })\n\n const replacements = await Promise.all(\n locations.map(async (loc) => ({\n ...loc,\n output: normalizeOutput(await renderDirective(loc.node, context)),\n })),\n )\n\n for (let i = replacements.length - 1; i >= 0; i--) {\n const { node, index, parent, output } = replacements[i]\n\n if (output?.blocks) {\n collector.blocks.unshift(...output.blocks)\n }\n\n if (output?.text) {\n // Parse the markdown text into AST nodes\n const parsed = parseMarkdown(output.text)\n\n if (node.type === 'textDirective') {\n // For inline directives, extract inline content from the parsed paragraph\n const firstNode = parsed.children[0]\n if (firstNode?.type === 'paragraph') {\n const paragraph = firstNode as Paragraph\n parent.children.splice(index, 1, ...paragraph.children)\n } else {\n parent.children.splice(index, 1, ...parsed.children)\n }\n } else {\n // For block directives, insert all parsed nodes\n parent.children.splice(index, 1, ...parsed.children)\n }\n continue\n }\n\n if (node.type === 'containerDirective' && node.children && node.children.length > 0) {\n const containerNode = node as unknown as ContainerDirective\n parent.children.splice(index, 1, ...(containerNode.children as RootContent[]))\n continue\n }\n\n parent.children.splice(index, 1)\n }\n }\n }\n}\n\n// ============================================================================\n// Process Directives Helper\n// ============================================================================\n\nexport interface ProcessDirectivesResult<TBlock> {\n /** Processed markdown with directives replaced */\n markdown: string\n /** Collected blocks from directive outputs (e.g., Slack buttons, Teams cards) */\n blocks: TBlock[]\n}\n\n/**\n * Process markdown containing directives and return the result.\n *\n * This is a convenience function that assembles the remark pipeline for you.\n * Use this instead of manually creating the pipeline with createRemarkRenderer.\n *\n * @example\n * ```ts\n * // Slack client\n * const { markdown, blocks } = await processDirectives(\n * input,\n * context,\n * renderDirective\n * )\n * return { text: slackifyMarkdown(markdown), blocks }\n *\n * // MS Teams client\n * const { markdown, blocks } = await processDirectives(\n * input,\n * context,\n * renderDirective\n * )\n * return { text: markdown, cards: blocks }\n * ```\n */\nexport async function processDirectives<TContext, TBlock>(\n input: string,\n context: TContext,\n renderFn: RenderFn<TContext, DirectiveRenderResult<TBlock> | null>,\n): Promise<ProcessDirectivesResult<TBlock>> {\n const collector: BlockCollector<TBlock> = { blocks: [] }\n const remarkRenderDirectives = createRemarkRenderer(renderFn)\n\n const processor = unified()\n .use(remarkParse)\n .use(remarkDirective)\n .use(remarkRenderDirectives, context, collector)\n .use(remarkStringify)\n\n const file = await processor.process(input)\n\n return {\n markdown: String(file),\n blocks: collector.blocks,\n }\n}\n"],"names":[],"mappings":";;;;;;AAuDA,SAAS,gBAAgB,MAA6B;AACpD,MAAI,CAAC,KAAK,YAAY,CAAC,MAAM,QAAQ,KAAK,QAAQ;AAChD,WAAO;AAGT,WAAS,YAAY,GAAo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,GAAG;AACnF,gBAAM,gBAAgB;AACtB,iBAAO,SAAS,OAAO,OAAO,GAAG,GAAI,cAAc,QAA0B;AAC7E;AAAA,QACF;AAEA,eAAO,SAAS,OAAO,OAAO,CAAC;AAAA,MACjC;AAAA,IACF;AAAA,EACF;AACF;AAsCA,eAAsB,kBACpB,OACA,SACA,UAC0C;AAC1C,QAAM,YAAoC,EAAE,QAAQ,CAAA,EAAC,GAC/C,yBAAyB,qBAAqB,QAAQ,GAQtD,OAAO,MANK,QAAA,EACf,IAAI,WAAW,EACf,IAAI,eAAe,EACnB,IAAI,wBAAwB,SAAS,SAAS,EAC9C,IAAI,eAAe,EAEO,QAAQ,KAAK;AAE1C,SAAO;AAAA,IACL,UAAU,OAAO,IAAI;AAAA,IACrB,QAAQ,UAAU;AAAA,EAAA;AAEtB;"}
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Streaming utilities for handling directive syntax during real-time text streaming.
3
+ *
4
+ * When streaming text that contains directives (e.g., `::document{id="123"}`),
5
+ * we need to buffer potential directive content and strip complete directives
6
+ * from the streamed output, since they'll be rendered as blocks/components
7
+ * at the end of the stream.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { createDirectiveStreamBuffer } from '@sanity/agent-directives/streaming'
12
+ *
13
+ * const buffer = createDirectiveStreamBuffer()
14
+ *
15
+ * for await (const chunk of stream) {
16
+ * const textToStream = buffer.process(chunk)
17
+ * if (textToStream) {
18
+ * await sendToClient(textToStream)
19
+ * }
20
+ * }
21
+ *
22
+ * // Flush any remaining content when stream ends
23
+ * const remaining = buffer.flush()
24
+ * if (remaining) {
25
+ * await sendToClient(remaining)
26
+ * }
27
+ * ```
28
+ */
29
+ interface DirectiveStreamBuffer {
30
+ /**
31
+ * Process incoming text chunk and return text safe to stream.
32
+ * Directives are buffered and stripped from output.
33
+ *
34
+ * @param chunk - Incoming text chunk
35
+ * @returns Text safe to stream (directives removed)
36
+ */
37
+ process(chunk: string): string;
38
+ /**
39
+ * Flush any remaining buffered content.
40
+ * Call this when streaming is complete.
41
+ *
42
+ * @returns Any remaining text (with complete directives stripped)
43
+ */
44
+ flush(): string;
45
+ /**
46
+ * Reset the buffer to initial state.
47
+ */
48
+ reset(): void;
49
+ }
50
+ /**
51
+ * Parsed directive information passed to the processor callback.
52
+ */
53
+ interface ParsedDirective {
54
+ name: string;
55
+ attributes: Record<string, string>;
56
+ children: string;
57
+ }
58
+ /**
59
+ * Callback function for processing a complete directive.
60
+ * Returns the text to insert in place of the directive syntax.
61
+ */
62
+ type DirectiveProcessor = (directive: ParsedDirective) => Promise<string>;
63
+ /**
64
+ * A streaming processor that transforms directives using a callback.
65
+ * Unlike DirectiveStreamBuffer which strips directives, this processes them.
66
+ */
67
+ interface DirectiveStreamProcessor {
68
+ /**
69
+ * Process incoming text chunk and return text with directives transformed.
70
+ * Complete directives are passed to the processor callback.
71
+ *
72
+ * @param chunk - Incoming text chunk
73
+ * @returns Text with complete directives replaced by processor output
74
+ */
75
+ process(chunk: string): Promise<string>;
76
+ /**
77
+ * Flush any remaining buffered content.
78
+ * Call this when streaming is complete.
79
+ *
80
+ * @returns Any remaining text with directives processed
81
+ */
82
+ flush(): Promise<string>;
83
+ /**
84
+ * Reset the processor to initial state.
85
+ */
86
+ reset(): void;
87
+ }
88
+ /**
89
+ * Creates a buffer for handling directive syntax during streaming.
90
+ *
91
+ * Directives have the pattern: `::name{prop="value" prop2="value2"}`
92
+ *
93
+ * During streaming, the buffer:
94
+ * 1. Streams non-directive text immediately
95
+ * 2. Buffers potential directive content until it can determine if it's complete
96
+ * 3. Strips complete directives from output (they'll be rendered as blocks at the end)
97
+ *
98
+ * This approach is similar to the React implementation which filters incomplete
99
+ * directives using pattern matching, but adapted for incremental streaming where
100
+ * we can't "unsend" text that was already streamed.
101
+ */
102
+ declare function createDirectiveStreamBuffer(): DirectiveStreamBuffer;
103
+ /**
104
+ * Creates a streaming processor that transforms directives using a callback.
105
+ *
106
+ * Unlike `createDirectiveStreamBuffer` which strips directives from output,
107
+ * this processor passes complete directives to a callback and replaces them
108
+ * with the callback's return value. This enables clients to render directives
109
+ * as links, cards, or other formats during streaming.
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * import { createDirectiveStreamProcessor } from '@sanity/agent-directives/streaming'
114
+ *
115
+ * // Slack client example
116
+ * const processor = createDirectiveStreamProcessor(async ({ name, attributes }) => {
117
+ * if (name === 'document') {
118
+ * const doc = await fetchDocument(attributes.id)
119
+ * return `[${doc.title}](${doc.url})`
120
+ * }
121
+ * return `[${name}]`
122
+ * })
123
+ *
124
+ * for await (const chunk of llmStream) {
125
+ * const text = await processor.process(chunk)
126
+ * if (text) {
127
+ * await streamToClient(text)
128
+ * }
129
+ * }
130
+ *
131
+ * const remaining = await processor.flush()
132
+ * if (remaining) {
133
+ * await streamToClient(remaining)
134
+ * }
135
+ * ```
136
+ */
137
+ declare function createDirectiveStreamProcessor(processDirective: DirectiveProcessor): DirectiveStreamProcessor;
138
+ export { DirectiveProcessor, DirectiveStreamBuffer, DirectiveStreamProcessor, ParsedDirective, createDirectiveStreamBuffer, createDirectiveStreamProcessor };
139
+ //# sourceMappingURL=streaming.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming.d.ts","names":[],"sources":["../../src/lib/streaming.ts"],"mappings":"AA6BA;;;;;;;;;;AA2BA;;;;;;;;;;AAUA;;;;;;;;AArCA,UAAiB,qBAAA;EA2CA;;;;;;;EAnCf,OAAA,CAAQ,KAAA;EAmDC;;;;AAsBX;;EAjEE,KAAA;EAiE6C;;AA0M/C;EAtQE,KAAA;AAAA;;;;UAMe,eAAA;EACf,IAAA;EACA,UAAA,EAAY,MAAA;EACZ,QAAA;AAAA;;;;;KAOU,kBAAA,IAAsB,SAAA,EAAW,eAAA,KAAoB,OAAA;;;;;UAMhD,wBAAA;;;;;;;;EAQf,OAAA,CAAQ,KAAA,WAAgB,OAAA;;;;;;;EAQxB,KAAA,IAAS,OAAA;;;;EAKT,KAAA;AAAA;;;;;;;;;;;;;;;iBAiBc,2BAAA,CAAA,GAA+B,qBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0M/B,8BAAA,CACd,gBAAA,EAAkB,kBAAA,GACjB,wBAAA"}
@@ -0,0 +1,185 @@
1
+ function createDirectiveStreamBuffer() {
2
+ let buffer = "";
3
+ function process(chunk) {
4
+ return buffer += chunk, extractStreamableText();
5
+ }
6
+ function flush() {
7
+ const remaining = buffer;
8
+ return buffer = "", stripCompleteDirectives(remaining);
9
+ }
10
+ function reset() {
11
+ buffer = "";
12
+ }
13
+ function extractStreamableText() {
14
+ let output = "";
15
+ for (; buffer.length > 0; ) {
16
+ const colonIndex = buffer.indexOf(":");
17
+ if (colonIndex === -1) {
18
+ output += buffer, buffer = "";
19
+ break;
20
+ }
21
+ if (colonIndex > 0 && (output += buffer.slice(0, colonIndex), buffer = buffer.slice(colonIndex)), buffer === ":")
22
+ break;
23
+ if (buffer.startsWith("::")) {
24
+ if (!looksLikeBlockDirectiveStart(buffer)) {
25
+ output += "::", buffer = buffer.slice(2);
26
+ continue;
27
+ }
28
+ const blockMatch = buffer.match(BLOCK_DIRECTIVE);
29
+ if (blockMatch) {
30
+ buffer = buffer.slice(blockMatch[0].length);
31
+ continue;
32
+ }
33
+ if (isDefinitelyNotBlockDirective(buffer)) {
34
+ output += buffer.slice(0, 2), buffer = buffer.slice(2);
35
+ continue;
36
+ }
37
+ break;
38
+ }
39
+ if (looksLikeInlineDirectiveStart(buffer)) {
40
+ const inlineMatch = buffer.match(INLINE_DIRECTIVE);
41
+ if (inlineMatch) {
42
+ buffer = buffer.slice(inlineMatch[0].length);
43
+ continue;
44
+ }
45
+ if (isDefinitelyNotInlineDirective(buffer)) {
46
+ output += ":", buffer = buffer.slice(1);
47
+ continue;
48
+ }
49
+ break;
50
+ }
51
+ output += ":", buffer = buffer.slice(1);
52
+ }
53
+ return output;
54
+ }
55
+ function stripCompleteDirectives(text) {
56
+ return text.replace(new RegExp(BLOCK_DIRECTIVE.source, "g"), "").replace(new RegExp(INLINE_DIRECTIVE.source, "g"), "");
57
+ }
58
+ return {
59
+ process,
60
+ flush,
61
+ reset
62
+ };
63
+ }
64
+ function parseDirectiveAttributes(attrString) {
65
+ const attrs = {};
66
+ for (const match of attrString.matchAll(/(\w+)="([^"]*)"/g))
67
+ attrs[match[1]] = match[2];
68
+ return attrs;
69
+ }
70
+ const BLOCK_DIRECTIVE = /::([a-zA-Z]\w*)(?:\[([^\]]*)\])?\{([^}]*)\}/, INLINE_DIRECTIVE = /:([a-zA-Z]\w*)\[([^\]]*)\]\{([^}]*)\}/;
71
+ function looksLikeBlockDirectiveStart(text) {
72
+ return text.startsWith("::") ? text.length === 2 ? !0 : /[a-zA-Z[{]/.test(text[2]) : !1;
73
+ }
74
+ function looksLikeInlineDirectiveStart(text) {
75
+ return text.length === 1 ? !0 : /[a-zA-Z]/.test(text[1]);
76
+ }
77
+ function isDefinitelyNotBlockDirective(text) {
78
+ const nameMatch = text.match(/^::([a-zA-Z]\w*)/);
79
+ return nameMatch ? isDefinitelyNotDirectiveAfterName(text.slice(nameMatch[0].length), !1) : text.length > 2 && !/[a-zA-Z]/.test(text[2]);
80
+ }
81
+ function isDefinitelyNotInlineDirective(text) {
82
+ const nameMatch = text.match(/^:([a-zA-Z]\w*)/);
83
+ return nameMatch ? isDefinitelyNotDirectiveAfterName(text.slice(nameMatch[0].length), !0) : text.length > 1 && !/[a-zA-Z]/.test(text[1]);
84
+ }
85
+ function isDefinitelyNotDirectiveAfterName(afterName, requireBrackets) {
86
+ if (afterName.length === 0) return !1;
87
+ if (afterName[0] === "{") return requireBrackets;
88
+ if (afterName[0] === "[") {
89
+ const bracketEnd = afterName.indexOf("]");
90
+ if (bracketEnd === -1) return !1;
91
+ const afterBracket = afterName.slice(bracketEnd + 1);
92
+ return afterBracket.length === 0 ? !1 : afterBracket[0] !== "{";
93
+ }
94
+ return !0;
95
+ }
96
+ function createDirectiveStreamProcessor(processDirective) {
97
+ let buffer = "";
98
+ async function process(chunk) {
99
+ buffer += chunk;
100
+ let output = "";
101
+ for (; buffer.length > 0; ) {
102
+ const colonIndex = buffer.indexOf(":");
103
+ if (colonIndex === -1) {
104
+ output += buffer, buffer = "";
105
+ break;
106
+ }
107
+ if (colonIndex > 0 && (output += buffer.slice(0, colonIndex), buffer = buffer.slice(colonIndex)), buffer === ":") break;
108
+ if (buffer.startsWith("::")) {
109
+ if (!looksLikeBlockDirectiveStart(buffer)) {
110
+ output += "::", buffer = buffer.slice(2);
111
+ continue;
112
+ }
113
+ const blockMatch = buffer.match(BLOCK_DIRECTIVE);
114
+ if (blockMatch) {
115
+ const [fullMatch, name, children, attrString] = blockMatch, attributes = parseDirectiveAttributes(attrString);
116
+ output += await processDirective({ name, attributes, children: children ?? "" }), buffer = buffer.slice(fullMatch.length);
117
+ continue;
118
+ }
119
+ if (isDefinitelyNotBlockDirective(buffer)) {
120
+ output += buffer.slice(0, 2), buffer = buffer.slice(2);
121
+ continue;
122
+ }
123
+ break;
124
+ }
125
+ if (looksLikeInlineDirectiveStart(buffer)) {
126
+ const inlineMatch = buffer.match(INLINE_DIRECTIVE);
127
+ if (inlineMatch) {
128
+ const [fullMatch, name, children, attrString] = inlineMatch, attributes = parseDirectiveAttributes(attrString);
129
+ output += await processDirective({ name, attributes, children }), buffer = buffer.slice(fullMatch.length);
130
+ continue;
131
+ }
132
+ if (isDefinitelyNotInlineDirective(buffer)) {
133
+ output += ":", buffer = buffer.slice(1);
134
+ continue;
135
+ }
136
+ break;
137
+ }
138
+ output += ":", buffer = buffer.slice(1);
139
+ }
140
+ return output;
141
+ }
142
+ async function flush() {
143
+ const remaining = buffer;
144
+ buffer = "";
145
+ let output = "", text = remaining;
146
+ for (; text.length > 0; ) {
147
+ const inlineMatch = text.match(INLINE_DIRECTIVE), blockMatch = text.match(BLOCK_DIRECTIVE), inlineIdx = inlineMatch?.index ?? 1 / 0, blockIdx = blockMatch?.index ?? 1 / 0;
148
+ if (inlineIdx === 1 / 0 && blockIdx === 1 / 0) {
149
+ output += text;
150
+ break;
151
+ }
152
+ if (inlineIdx <= blockIdx && inlineMatch) {
153
+ output += text.slice(0, inlineMatch.index);
154
+ const attributes = parseDirectiveAttributes(inlineMatch[3]);
155
+ output += await processDirective({
156
+ name: inlineMatch[1],
157
+ attributes,
158
+ children: inlineMatch[2]
159
+ }), text = text.slice(inlineMatch.index + inlineMatch[0].length);
160
+ } else if (blockMatch) {
161
+ output += text.slice(0, blockMatch.index);
162
+ const attributes = parseDirectiveAttributes(blockMatch[3]);
163
+ output += await processDirective({
164
+ name: blockMatch[1],
165
+ attributes,
166
+ children: blockMatch[2] ?? ""
167
+ }), text = text.slice(blockMatch.index + blockMatch[0].length);
168
+ }
169
+ }
170
+ return output;
171
+ }
172
+ function reset() {
173
+ buffer = "";
174
+ }
175
+ return {
176
+ process,
177
+ flush,
178
+ reset
179
+ };
180
+ }
181
+ export {
182
+ createDirectiveStreamBuffer,
183
+ createDirectiveStreamProcessor
184
+ };
185
+ //# sourceMappingURL=streaming.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming.js","sources":["../../src/lib/streaming.ts"],"sourcesContent":["/**\n * Streaming utilities for handling directive syntax during real-time text streaming.\n *\n * When streaming text that contains directives (e.g., `::document{id=\"123\"}`),\n * we need to buffer potential directive content and strip complete directives\n * from the streamed output, since they'll be rendered as blocks/components\n * at the end of the stream.\n *\n * @example\n * ```ts\n * import { createDirectiveStreamBuffer } from '@sanity/agent-directives/streaming'\n *\n * const buffer = createDirectiveStreamBuffer()\n *\n * for await (const chunk of stream) {\n * const textToStream = buffer.process(chunk)\n * if (textToStream) {\n * await sendToClient(textToStream)\n * }\n * }\n *\n * // Flush any remaining content when stream ends\n * const remaining = buffer.flush()\n * if (remaining) {\n * await sendToClient(remaining)\n * }\n * ```\n */\n\nexport interface DirectiveStreamBuffer {\n /**\n * Process incoming text chunk and return text safe to stream.\n * Directives are buffered and stripped from output.\n *\n * @param chunk - Incoming text chunk\n * @returns Text safe to stream (directives removed)\n */\n process(chunk: string): string\n\n /**\n * Flush any remaining buffered content.\n * Call this when streaming is complete.\n *\n * @returns Any remaining text (with complete directives stripped)\n */\n flush(): string\n\n /**\n * Reset the buffer to initial state.\n */\n reset(): void\n}\n\n/**\n * Parsed directive information passed to the processor callback.\n */\nexport interface ParsedDirective {\n name: string\n attributes: Record<string, string>\n children: string\n}\n\n/**\n * Callback function for processing a complete directive.\n * Returns the text to insert in place of the directive syntax.\n */\nexport type DirectiveProcessor = (directive: ParsedDirective) => Promise<string>\n\n/**\n * A streaming processor that transforms directives using a callback.\n * Unlike DirectiveStreamBuffer which strips directives, this processes them.\n */\nexport interface DirectiveStreamProcessor {\n /**\n * Process incoming text chunk and return text with directives transformed.\n * Complete directives are passed to the processor callback.\n *\n * @param chunk - Incoming text chunk\n * @returns Text with complete directives replaced by processor output\n */\n process(chunk: string): Promise<string>\n\n /**\n * Flush any remaining buffered content.\n * Call this when streaming is complete.\n *\n * @returns Any remaining text with directives processed\n */\n flush(): Promise<string>\n\n /**\n * Reset the processor to initial state.\n */\n reset(): void\n}\n\n/**\n * Creates a buffer for handling directive syntax during streaming.\n *\n * Directives have the pattern: `::name{prop=\"value\" prop2=\"value2\"}`\n *\n * During streaming, the buffer:\n * 1. Streams non-directive text immediately\n * 2. Buffers potential directive content until it can determine if it's complete\n * 3. Strips complete directives from output (they'll be rendered as blocks at the end)\n *\n * This approach is similar to the React implementation which filters incomplete\n * directives using pattern matching, but adapted for incremental streaming where\n * we can't \"unsend\" text that was already streamed.\n */\nexport function createDirectiveStreamBuffer(): DirectiveStreamBuffer {\n let buffer = ''\n\n function process(chunk: string): string {\n buffer += chunk\n return extractStreamableText()\n }\n\n function flush(): string {\n const remaining = buffer\n buffer = ''\n // Strip any complete directives from remaining text\n return stripCompleteDirectives(remaining)\n }\n\n function reset(): void {\n buffer = ''\n }\n\n function extractStreamableText(): string {\n let output = ''\n\n while (buffer.length > 0) {\n // Find potential directive start (colon that might become ::)\n const colonIndex = buffer.indexOf(':')\n\n if (colonIndex === -1) {\n // No colon found - safe to output everything\n output += buffer\n buffer = ''\n break\n }\n\n // Output text before the colon\n if (colonIndex > 0) {\n output += buffer.slice(0, colonIndex)\n buffer = buffer.slice(colonIndex)\n }\n\n // Now buffer starts with ':'\n // If buffer is just a single ':', keep it buffered - might become '::' with next chunk\n if (buffer === ':') {\n break\n }\n\n // Block directive (::name{...})\n if (buffer.startsWith('::')) {\n if (!looksLikeBlockDirectiveStart(buffer)) {\n output += '::'\n buffer = buffer.slice(2)\n continue\n }\n const blockMatch = buffer.match(BLOCK_DIRECTIVE)\n if (blockMatch) {\n buffer = buffer.slice(blockMatch[0].length)\n continue\n }\n if (isDefinitelyNotBlockDirective(buffer)) {\n output += buffer.slice(0, 2)\n buffer = buffer.slice(2)\n continue\n }\n break\n }\n\n // Inline directive (:name[content]{...})\n if (looksLikeInlineDirectiveStart(buffer)) {\n const inlineMatch = buffer.match(INLINE_DIRECTIVE)\n if (inlineMatch) {\n buffer = buffer.slice(inlineMatch[0].length)\n continue\n }\n if (isDefinitelyNotInlineDirective(buffer)) {\n output += ':'\n buffer = buffer.slice(1)\n continue\n }\n break\n }\n\n // Not a directive\n output += ':'\n buffer = buffer.slice(1)\n }\n\n return output\n }\n\n function stripCompleteDirectives(text: string): string {\n return text\n .replace(new RegExp(BLOCK_DIRECTIVE.source, 'g'), '')\n .replace(new RegExp(INLINE_DIRECTIVE.source, 'g'), '')\n }\n\n return {\n process,\n flush,\n reset,\n }\n}\n\n/**\n * Parses directive attributes from a string like: id=\"123\" type=\"post\"\n */\nfunction parseDirectiveAttributes(attrString: string): Record<string, string> {\n const attrs: Record<string, string> = {}\n for (const match of attrString.matchAll(/(\\w+)=\"([^\"]*)\"/g)) {\n attrs[match[1]] = match[2]\n }\n return attrs\n}\n\n// ============================================================================\n// Directive regex patterns\n// ============================================================================\n\n// Matches block directives: ::name{attrs} or ::name[label]{attrs}\n// Captures: (1) name, (2) label or undefined, (3) attribute string\nconst BLOCK_DIRECTIVE = /::([a-zA-Z]\\w*)(?:\\[([^\\]]*)\\])?\\{([^}]*)\\}/\n\n// Matches inline directives: :name[label]{attrs}\n// Captures: (1) name, (2) label, (3) attribute string\nconst INLINE_DIRECTIVE = /:([a-zA-Z]\\w*)\\[([^\\]]*)\\]\\{([^}]*)\\}/\n\n// ============================================================================\n// Streaming detection helpers\n// ============================================================================\n\nfunction looksLikeBlockDirectiveStart(text: string): boolean {\n if (!text.startsWith('::')) return false\n if (text.length === 2) return true\n return /[a-zA-Z[{]/.test(text[2])\n}\n\nfunction looksLikeInlineDirectiveStart(text: string): boolean {\n if (text.length === 1) return true\n return /[a-zA-Z]/.test(text[1])\n}\n\nfunction isDefinitelyNotBlockDirective(text: string): boolean {\n const nameMatch = text.match(/^::([a-zA-Z]\\w*)/)\n if (!nameMatch) return text.length > 2 && !/[a-zA-Z]/.test(text[2])\n return isDefinitelyNotDirectiveAfterName(text.slice(nameMatch[0].length), false)\n}\n\nfunction isDefinitelyNotInlineDirective(text: string): boolean {\n const nameMatch = text.match(/^:([a-zA-Z]\\w*)/)\n if (!nameMatch) return text.length > 1 && !/[a-zA-Z]/.test(text[1])\n return isDefinitelyNotDirectiveAfterName(text.slice(nameMatch[0].length), true)\n}\n\n/**\n * Checks whether text after a directive name can still form a valid directive.\n * Block directives accept `{` (attrs) or `[` (label). Inline requires `[`.\n */\nfunction isDefinitelyNotDirectiveAfterName(afterName: string, requireBrackets: boolean): boolean {\n if (afterName.length === 0) return false\n if (afterName[0] === '{') return requireBrackets\n if (afterName[0] === '[') {\n const bracketEnd = afterName.indexOf(']')\n if (bracketEnd === -1) return false\n const afterBracket = afterName.slice(bracketEnd + 1)\n if (afterBracket.length === 0) return false\n return afterBracket[0] !== '{'\n }\n return true\n}\n\n/**\n * Creates a streaming processor that transforms directives using a callback.\n *\n * Unlike `createDirectiveStreamBuffer` which strips directives from output,\n * this processor passes complete directives to a callback and replaces them\n * with the callback's return value. This enables clients to render directives\n * as links, cards, or other formats during streaming.\n *\n * @example\n * ```ts\n * import { createDirectiveStreamProcessor } from '@sanity/agent-directives/streaming'\n *\n * // Slack client example\n * const processor = createDirectiveStreamProcessor(async ({ name, attributes }) => {\n * if (name === 'document') {\n * const doc = await fetchDocument(attributes.id)\n * return `[${doc.title}](${doc.url})`\n * }\n * return `[${name}]`\n * })\n *\n * for await (const chunk of llmStream) {\n * const text = await processor.process(chunk)\n * if (text) {\n * await streamToClient(text)\n * }\n * }\n *\n * const remaining = await processor.flush()\n * if (remaining) {\n * await streamToClient(remaining)\n * }\n * ```\n */\nexport function createDirectiveStreamProcessor(\n processDirective: DirectiveProcessor,\n): DirectiveStreamProcessor {\n let buffer = ''\n\n async function process(chunk: string): Promise<string> {\n buffer += chunk\n let output = ''\n\n while (buffer.length > 0) {\n const colonIndex = buffer.indexOf(':')\n\n if (colonIndex === -1) {\n output += buffer\n buffer = ''\n break\n }\n\n if (colonIndex > 0) {\n output += buffer.slice(0, colonIndex)\n buffer = buffer.slice(colonIndex)\n }\n\n if (buffer === ':') break\n\n // Block directive (::name{...})\n if (buffer.startsWith('::')) {\n if (!looksLikeBlockDirectiveStart(buffer)) {\n output += '::'\n buffer = buffer.slice(2)\n continue\n }\n const blockMatch = buffer.match(BLOCK_DIRECTIVE)\n if (blockMatch) {\n const [fullMatch, name, children, attrString] = blockMatch\n const attributes = parseDirectiveAttributes(attrString)\n output += await processDirective({ name, attributes, children: children ?? '' })\n buffer = buffer.slice(fullMatch.length)\n continue\n }\n if (isDefinitelyNotBlockDirective(buffer)) {\n output += buffer.slice(0, 2)\n buffer = buffer.slice(2)\n continue\n }\n break\n }\n\n // Inline directive (:name[content]{...})\n if (looksLikeInlineDirectiveStart(buffer)) {\n const inlineMatch = buffer.match(INLINE_DIRECTIVE)\n if (inlineMatch) {\n const [fullMatch, name, children, attrString] = inlineMatch\n const attributes = parseDirectiveAttributes(attrString)\n output += await processDirective({ name, attributes, children })\n buffer = buffer.slice(fullMatch.length)\n continue\n }\n if (isDefinitelyNotInlineDirective(buffer)) {\n output += ':'\n buffer = buffer.slice(1)\n continue\n }\n break\n }\n\n // Not a directive\n output += ':'\n buffer = buffer.slice(1)\n }\n\n return output\n }\n\n async function flush(): Promise<string> {\n const remaining = buffer\n buffer = ''\n let output = ''\n let text = remaining\n\n while (text.length > 0) {\n const inlineMatch = text.match(INLINE_DIRECTIVE)\n const blockMatch = text.match(BLOCK_DIRECTIVE)\n\n const inlineIdx = inlineMatch?.index ?? Infinity\n const blockIdx = blockMatch?.index ?? Infinity\n\n if (inlineIdx === Infinity && blockIdx === Infinity) {\n output += text\n break\n }\n\n if (inlineIdx <= blockIdx && inlineMatch) {\n output += text.slice(0, inlineMatch.index)\n const attributes = parseDirectiveAttributes(inlineMatch[3])\n output += await processDirective({\n name: inlineMatch[1],\n attributes,\n children: inlineMatch[2],\n })\n text = text.slice(inlineMatch.index! + inlineMatch[0].length)\n } else if (blockMatch) {\n output += text.slice(0, blockMatch.index)\n const attributes = parseDirectiveAttributes(blockMatch[3])\n output += await processDirective({\n name: blockMatch[1],\n attributes,\n children: blockMatch[2] ?? '',\n })\n text = text.slice(blockMatch.index! + blockMatch[0].length)\n }\n }\n\n return output\n }\n\n function reset(): void {\n buffer = ''\n }\n\n return {\n process,\n flush,\n reset,\n }\n}\n"],"names":[],"mappings":"AA8GO,SAAS,8BAAqD;AACnE,MAAI,SAAS;AAEb,WAAS,QAAQ,OAAuB;AACtC,WAAA,UAAU,OACH,sBAAA;AAAA,EACT;AAEA,WAAS,QAAgB;AACvB,UAAM,YAAY;AAClB,WAAA,SAAS,IAEF,wBAAwB,SAAS;AAAA,EAC1C;AAEA,WAAS,QAAc;AACrB,aAAS;AAAA,EACX;AAEA,WAAS,wBAAgC;AACvC,QAAI,SAAS;AAEb,WAAO,OAAO,SAAS,KAAG;AAExB,YAAM,aAAa,OAAO,QAAQ,GAAG;AAErC,UAAI,eAAe,IAAI;AAErB,kBAAU,QACV,SAAS;AACT;AAAA,MACF;AAUA,UAPI,aA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