(markdown: string, modelId: string)
| 130 | * would be misleading. |
| 131 | */ |
| 132 | export function extractModelSection(markdown: string, modelId: string): string | null { |
| 133 | if (!markdown || !modelId) return null; |
| 134 | |
| 135 | const headingPattern = /^model:\s*(.+)$/i; |
| 136 | |
| 137 | const compileRegex = (pattern: string): RegExp | null => { |
| 138 | const trimmed = pattern.trim(); |
| 139 | if (!trimmed) return null; |
| 140 | |
| 141 | if (trimmed.startsWith("/") && trimmed.lastIndexOf("/") > 0) { |
| 142 | const lastSlash = trimmed.lastIndexOf("/"); |
| 143 | const source = trimmed.slice(1, lastSlash); |
| 144 | const flags = trimmed.slice(lastSlash + 1); |
| 145 | try { |
| 146 | return new RegExp(source, flags || undefined); |
| 147 | } catch { |
| 148 | return null; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | try { |
| 153 | return new RegExp(trimmed, "i"); |
| 154 | } catch { |
| 155 | return null; |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | const matches = extractSectionsByHeading(markdown, (headingText) => { |
| 160 | const match = headingPattern.exec(headingText); |
| 161 | if (!match) return false; |
| 162 | const regex = compileRegex(match[1] ?? ""); |
| 163 | return Boolean(regex?.test(modelId)); |
| 164 | }); |
| 165 | return joinMatchedSections(matches); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Extract the content under every heading titled "Tool: <tool_name>" (case-insensitive), |
no test coverage detected