(node: MarkdownNode)
| 356 | } |
| 357 | |
| 358 | const nodeToPlainText = (node: MarkdownNode): string => { |
| 359 | switch (node.type) { |
| 360 | case 'root': |
| 361 | return getChildrenText((node as Root).children as MarkdownNode[]) |
| 362 | |
| 363 | case 'paragraph': |
| 364 | return getChildrenText((node as Paragraph).children as MarkdownNode[]) + '\n\n' |
| 365 | |
| 366 | case 'text': |
| 367 | return (node as Text).value |
| 368 | |
| 369 | case 'strong': |
| 370 | return getChildrenText((node as Strong).children as MarkdownNode[]) |
| 371 | |
| 372 | case 'emphasis': |
| 373 | return getChildrenText((node as Emphasis).children as MarkdownNode[]) |
| 374 | |
| 375 | case 'inlineCode': |
| 376 | return (node as InlineCode).value |
| 377 | |
| 378 | case 'heading': { |
| 379 | const heading = node as Heading |
| 380 | const prefix = '#'.repeat(Math.max(1, Math.min(heading.depth, 6))) |
| 381 | const content = getChildrenText(heading.children as MarkdownNode[]) |
| 382 | return `${prefix} ${content}\n\n` |
| 383 | } |
| 384 | |
| 385 | case 'list': { |
| 386 | const list = node as List |
| 387 | return ( |
| 388 | list.children |
| 389 | .map((item, idx) => { |
| 390 | const marker = list.ordered ? `${(list.start ?? 1) + idx}. ` : '- ' |
| 391 | const text = getChildrenText((item as ListItem).children as MarkdownNode[]).trimEnd() |
| 392 | return marker + text |
| 393 | }) |
| 394 | .join('\n') + '\n\n' |
| 395 | ) |
| 396 | } |
| 397 | |
| 398 | case 'listItem': |
| 399 | return getChildrenText((node as ListItem).children as MarkdownNode[]) |
| 400 | |
| 401 | case 'blockquote': { |
| 402 | const blockquote = node as Blockquote |
| 403 | const content = blockquote.children |
| 404 | .map((child) => nodeToPlainText(child).replace(/^/gm, '> ')) |
| 405 | .join('') |
| 406 | return `${content}\n\n` |
| 407 | } |
| 408 | |
| 409 | case 'code': { |
| 410 | const code = node as Code |
| 411 | const header = code.lang ? `\`\`\`${code.lang}\n` : '```\n' |
| 412 | return `${header}${code.value}\n\`\`\`\n\n` |
| 413 | } |
| 414 | |
| 415 | case 'break': |
no test coverage detected