UNPKG

feishu-mcp

Version:

Model Context Protocol server for Feishu integration

136 lines (135 loc) 4.15 kB
/** * 构建批量创建块的请求数据 * @param blocks 块内容数组 * @param index 插入位置索引 * @returns 请求数据对象 */ export function buildCreateBlocksRequest(blocks, index = 0) { return { children: blocks, index }; } /** * 创建文本块内容 * @param text 文本内容 * @param style 文本样式 * @param align 对齐方式:1左对齐,2居中,3右对齐 * @returns 文本块内容对象 */ export function createTextBlockContent(textContents, align = 1) { return { block_type: 2, // 2表示文本块 text: { elements: textContents.map(content => ({ text_run: { content: content.text, text_element_style: content.style || {} } })), style: { align: align // 1 居左,2 居中,3 居右 } } }; } /** * 创建代码块内容 * @param code 代码内容 * @param language 语言类型代码 * @param wrap 是否自动换行 * @returns 代码块内容对象 */ export function createCodeBlockContent(code, language = 0, wrap = false) { return { block_type: 14, // 14表示代码块 code: { elements: [ { text_run: { content: code, text_element_style: { bold: false, inline_code: false, italic: false, strikethrough: false, underline: false } } } ], style: { language: language, wrap: wrap } } }; } /** * 创建标题块内容 * @param text 标题文本 * @param level 标题级别(1-9) * @param align 对齐方式:1左对齐,2居中,3右对齐 * @returns 标题块内容对象 */ export function createHeadingBlockContent(text, level = 1, align = 1) { // 确保标题级别在有效范围内(1-9) const safeLevel = Math.max(1, Math.min(9, level)); // 根据标题级别设置block_type和对应的属性名 // 飞书API中,一级标题的block_type为3,二级标题为4,以此类推 const blockType = 2 + safeLevel; // 一级标题为3,二级标题为4,以此类推 const headingKey = `heading${safeLevel}`; // heading1, heading2, ... // 构建块内容 const blockContent = { block_type: blockType }; // 设置对应级别的标题属性 blockContent[headingKey] = { elements: [ { text_run: { content: text, text_element_style: {} } } ], style: { align: align, folded: false } }; return blockContent; } /** * 处理Markdown语法转换 * @param textContents 文本内容数组 * @returns 处理后的文本内容数组 */ export function processMarkdownSyntax(textContents) { return textContents.map(content => { let { text, style = {} } = content; // 创建一个新的style对象,避免修改原始对象 const newStyle = { ...style }; // 处理粗体 **text** if (text.match(/\*\*([^*]+)\*\*/g)) { text = text.replace(/\*\*([^*]+)\*\*/g, "$1"); newStyle.bold = true; } // 处理斜体 *text* if (text.match(/(?<!\*)\*([^*]+)\*(?!\*)/g)) { text = text.replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, "$1"); newStyle.italic = true; } // 处理删除线 ~~text~~ if (text.match(/~~([^~]+)~~/g)) { text = text.replace(/~~([^~]+)~~/g, "$1"); newStyle.strikethrough = true; } // 处理行内代码 `code` if (text.match(/`([^`]+)`/g)) { text = text.replace(/`([^`]+)`/g, "$1"); newStyle.inline_code = true; } return { text, style: newStyle }; }); }