Repository: apple/swift-markdown (swift)

Issue:
Cannot reliably get child element using child(through:) when there are multiple paragraphs
Consider the following markdown string

```swift
let source = """
This is a markup __*document*__ with *some* **more** attributes.

This is the *second* paragraph.
This is on a **new** line, but, continues on the same paragraph.

This is the *third* paragraph.
This is on a **new** line, but, continues on the same paragraph.
"""
```

This generates the following markdown tree (correctly)

```text
Document
├─ Paragraph
│  ├─ Text "This is a markup "
│  ├─ Strong
│  │  └─ Emphasis
│  │     └─ Text "document"
│  ├─ Text " with "
│  ├─ Emphasis
│  │  └─ Text "some"
│  ├─ Text " "
│  ├─ Strong
│  │  └─ Text "extended"
│  └─ Text " attributes."
├─ Paragraph
│  ├─ Text "This is the "
│  ├─ Emphasis
│  │  └─ Text "second"
│  ├─ Text " paragraph."
│  ├─ SoftBreak
│  ├─ Text "This is on a "
│  ├─ Strong
│  │  └─ Text "new"
│  └─ Text " line, but, continues on the same paragraph."
└─ Paragraph
   ├─ Text "This is the "
   ├─ Emphasis
   │  └─ Text "third"
   ├─ Text " paragraph."
   ├─ SoftBreak
   ├─ Text "This is on a "
   ├─ Strong
   │  └─ Text "new"
   └─ Text " line, but, continues on the same paragraph."
```

If we try to access the strong element (reads as new) on the second paragraph with `let secondStrong = document.child(through: [1, 5])` it will be nil. It is expected that this will be a strong node.

Trying to access any element whose second indexpath is greater than 3 (the number of paragraphs or children) on the root document returns nil.

Pull request description:
`func child(through path: TypedChildIndexPath) -> Markup?` uses the root document node's child count for comparison against the passed in path for valid reference to a child. Changed it to use the current iterated element.

fix #132

Bug/issue #, if applicable: 

## Summary

_Provide a description of what your PR addresses, explaining the expected user experience. 
Also, provide an overview of your implementation._

## Dependencies

_Describe any dependencies this PR might have, such as an associated branch in another repository._

## Testing

_Describe how a reviewer can test the functionality of your PR. Provide test content to test with if
applicable._

Steps:
1. _Provide setup instructions._
2. _Explain in detail how the functionality can be tested._

## Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

- [ ] Added tests
- [ ] Ran the `./bin/test` script and it succeeded
- [ ] Updated documentation if necessary

Patch:
diff --git a/Sources/Markdown/Base/Markup.swift b/Sources/Markdown/Base/Markup.swift
index 4b271bff..23c72e78 100644
--- a/Sources/Markdown/Base/Markup.swift
+++ b/Sources/Markdown/Base/Markup.swift
@@ -265,7 +265,7 @@ extension Markup {
     public func child(through path: TypedChildIndexPath) -> Markup? {
         var element: Markup = self
         for pathElement in path {
-            guard pathElement.index <= raw.markup.childCount else {
+            guard pathElement.index <= element.childCount else {
                 return nil
             }
 


Test patch:
diff --git a/Tests/MarkdownTests/Base/MarkupTests.swift b/Tests/MarkdownTests/Base/MarkupTests.swift
index 4258a921..15730437 100644
--- a/Tests/MarkdownTests/Base/MarkupTests.swift
+++ b/Tests/MarkdownTests/Base/MarkupTests.swift
@@ -283,6 +283,21 @@ final class MarkupTests: XCTestCase {
             )!.debugDescription()
         )
     }
+
+    func testChildThroughIndicesWithMultipleParagraphs() {
+        let source = """
+This is a markup __*document*__ with *some* **more** attributes.
+
+This is the *second* paragraph.
+This is on a **new** line, but, continues on the same paragraph.
+
+This is the *third* paragraph.
+This is on a **new** line, but, continues on the same paragraph.
+"""
+
+        let document = Document(parsing: source)
+        XCTAssertNotNil(document.child(through: [2, 5]) as? Strong)
+    }
     
     func testChildAtPositionHasCorrectType() throws {
         let source = "This is a [*link*](github.com). And some **bold** and *italic* text."