(line: string, cardIds: readonly string[])
| 231 | * plain text with a warning rather than as a button that would navigate nowhere. |
| 232 | */ |
| 233 | export function segmentsOf(line: string, cardIds: readonly string[]): AnswerSegment[] { |
| 234 | const known = new Set(cardIds); |
| 235 | const out: AnswerSegment[] = []; |
| 236 | const pattern = /\[[^\]]{1,80}\]/g; |
| 237 | let cursor = 0; |
| 238 | let match = pattern.exec(line); |
| 239 | while (match !== null) { |
| 240 | const ids = citationsIn(match[0]); |
| 241 | if (ids.length === 0) { |
| 242 | match = pattern.exec(line); |
| 243 | continue; |
| 244 | } |
| 245 | if (match.index > cursor) { |
| 246 | out.push({ kind: 'text', text: line.slice(cursor, match.index) }); |
| 247 | } |
| 248 | for (const id of ids) { |
| 249 | out.push({ kind: 'citation', cardId: id, text: `[${id}]`, known: known.has(id) }); |
| 250 | } |
| 251 | cursor = match.index + match[0].length; |
| 252 | match = pattern.exec(line); |
| 253 | } |
| 254 | if (cursor < line.length) { |
| 255 | out.push({ kind: 'text', text: line.slice(cursor) }); |
| 256 | } |
| 257 | return out; |
| 258 | } |
no test coverage detected