Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { unified } from 'unified'; |
| 2 | import remarkParse from 'remark-parse'; |
| 3 | import remarkRehype from 'remark-rehype'; |
| 4 | import { toHtml } from 'hast-util-to-html'; |
| 5 | import { markdownLanguageGroups, parseExampleDocument } from './example-document'; |
| 6 | import { toString } from 'mdast-util-to-string'; |
| 7 | |
| 8 | export type FaqAnswerPart = |
| 9 | | { type: 'html'; html: string } |
| 10 | | { type: 'languages'; tabs: { value: string; html: string }[] }; |
| 11 | |
| 12 | export type FaqItem = { question: string; answer: FaqAnswerPart[] }; |
| 13 | |
| 14 | function markdownToHtml(markdown: string): string { |
| 15 | const processor = unified().use(remarkParse).use(remarkRehype); |
| 16 | return toHtml(processor.runSync(processor.parse(markdown))); |
| 17 | } |
| 18 | |
| 19 | /** Language headings stay readable to agents and become persistent options in the FAQ. */ |
| 20 | export function parseToolkitFaq(source: string): FaqItem[] { |
| 21 | const headings = parseExampleDocument(source, 'faq.md').children.filter((node) => node.type === 'heading' && node.depth === 2); |
| 22 | return headings.map((heading, index) => { |
| 23 | const body = source.slice(heading.position?.end.offset ?? 0, headings[index + 1]?.position?.start.offset ?? source.length); |
| 24 | const answer: FaqAnswerPart[] = []; |
| 25 | let offset = 0; |
| 26 | for (const group of markdownLanguageGroups(body)) { |
| 27 | const before = body.slice(offset, group.start); |
| 28 | if (before.trim()) answer.push({ type: 'html', html: markdownToHtml(before) }); |
| 29 | answer.push({ type: 'languages', tabs: group.tabs.map((tab) => ({ value: tab.value, html: markdownToHtml(tab.source) })) }); |
| 30 | offset = group.end; |
| 31 | } |
| 32 | const after = body.slice(offset); |
| 33 | if (after.trim()) answer.push({ type: 'html', html: markdownToHtml(after) }); |
| 34 | return { question: toString(heading), answer }; |
| 35 | }); |
| 36 | } |