( markdown: string, taskIndex: number )
| 240 | * counting tasks the same way the parser does so the index stays in lockstep. |
| 241 | * Used to move a task to another note. */ |
| 242 | export function takeTaskLineAtIndex( |
| 243 | markdown: string, |
| 244 | taskIndex: number |
| 245 | ): { line: string | null; body: string } { |
| 246 | if (taskIndex < 0) return { line: null, body: markdown } |
| 247 | const lines = markdown.split('\n') |
| 248 | let currentTaskIndex = 0 |
| 249 | let inFence = false |
| 250 | let fenceMarker: string | null = null |
| 251 | for (let i = 0; i < lines.length; i++) { |
| 252 | const line = lines[i] |
| 253 | const fenceMatch = line.match(FENCE_RE) |
| 254 | if (fenceMatch) { |
| 255 | const marker = fenceMatch[2] |
| 256 | if (!inFence) { |
| 257 | inFence = true |
| 258 | fenceMarker = marker |
| 259 | } else if (marker === fenceMarker) { |
| 260 | inFence = false |
| 261 | fenceMarker = null |
| 262 | } |
| 263 | continue |
| 264 | } |
| 265 | if (inFence) continue |
| 266 | if (!TASK_LINE_RE.test(line)) continue |
| 267 | if (currentTaskIndex !== taskIndex) { |
| 268 | currentTaskIndex += 1 |
| 269 | continue |
| 270 | } |
| 271 | const removed = lines[i] |
| 272 | lines.splice(i, 1) |
| 273 | return { line: removed, body: lines.join('\n') } |
| 274 | } |
| 275 | return { line: null, body: markdown } |
| 276 | } |
| 277 | |
| 278 | /** Delete the task line at `taskIndex` entirely. */ |
| 279 | export function removeTaskAtIndex(markdown: string, taskIndex: number): string { |
no outgoing calls
no test coverage detected