UNPKG

@convo-lang/convo-lang

Version:
104 lines 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseConvoComponent = exports.parseConvoMessageComponents = exports.parseConvoComponentTransform = exports.getConvoMessageComponent = exports.getConvoMessageComponentMode = void 0; const common_1 = require("@iyio/common"); const convo_types_1 = require("./convo-types"); /** * Finds the component type of a message. */ const getConvoMessageComponentMode = (content) => { const types = (content ? /^\s*```([^\n]*).*```\s*$/s.exec(content) : null)?.[1]?.trim().split(' '); if (!types) { return undefined; } const last = types[types.length - 1]; return (0, convo_types_1.isConvoComponentMode)(last) ? last : undefined; }; exports.getConvoMessageComponentMode = getConvoMessageComponentMode; const convoComponentCacheKey = Symbol('convoComponentCacheKey'); /** * Parses message content as a convo component. Components are written in xml. The parsed component * is cached and stored on the message using a private symbol. * @param msg The message to parse */ const getConvoMessageComponent = (msg) => { if (!msg?.content) { return undefined; } const cached = msg[convoComponentCacheKey]; if (cached) { return cached; } const comp = (0, exports.parseConvoComponent)(msg.content); if (comp) { msg[convoComponentCacheKey] = comp; } return comp; }; exports.getConvoMessageComponent = getConvoMessageComponent; const transformComponentReg = /^(\w+)\s+(\w+)(\s+(\w+))?(\s+\?\s*(!?\s*.*))?$/; /** * Parses the value of the `transformComponent` tag * @param value [groupName] {componentName} {propType} [?[!] condition] */ const parseConvoComponentTransform = (value) => { if (!value) { return undefined; } const m = transformComponentReg.exec(value); if (!m) { return undefined; } const hasGroup = m[4] ? true : false; const ni = hasGroup ? 2 : 1; return { componentName: m[ni] ?? '', propType: m[hasGroup ? ni + 2 : 2] ?? '', groupName: (hasGroup ? m[1] : m[ni]) || undefined, condition: m[6]?.trim() || undefined, }; }; exports.parseConvoComponentTransform = parseConvoComponentTransform; const mdFenceReg = /^[\n\s]*```[^\n]*/; const jsonReg = /^[\n\s]*\{/; const parseConvoMessageComponents = (content, defaultComponentName) => { const fenceMatch = mdFenceReg.exec(content); if (fenceMatch) { content = content.substring(fenceMatch[0].length).trim(); if (content.endsWith('```')) { content = content.substring(0, content.length - 3); } } if (jsonReg.test(content)) { const json = JSON.parse(content); return [{ isJson: true, name: defaultComponentName ?? 'Json', atts: json ? ((typeof json === 'object') ? json : { value: json }) : { value: json }, }]; } else { return (0, common_1.parseXml)(content, { parseJsonAtts: true }).result; } }; exports.parseConvoMessageComponents = parseConvoMessageComponents; /** * Parses message content as a convo component. Components are written in xml. * @param content string content to parse */ const parseConvoComponent = (content) => { if (!content) { return undefined; } const codeBlockMatch = /^\s*```[^\n]*(.*)```\s*$/s.exec(content); if (codeBlockMatch?.[1]) { content = codeBlockMatch[1]; } const xml = (0, common_1.parseXml)(content, { parseJsonAtts: true, stopOnFirstNode: true }); if (xml.error) { console.error('convo component parsing failed', xml.error); } return xml.result?.[0]; }; exports.parseConvoComponent = parseConvoComponent; //# sourceMappingURL=convo-component-lib.js.map