diff --git a/Sources/SwiftHTMLtoMarkdown/BasicHTML.swift b/Sources/SwiftHTMLtoMarkdown/BasicHTML.swift index fa97bd7..6fa378c 100644 --- a/Sources/SwiftHTMLtoMarkdown/BasicHTML.swift +++ b/Sources/SwiftHTMLtoMarkdown/BasicHTML.swift @@ -108,6 +108,14 @@ public class BasicHTML: HTML { markdown += "\n```" return } + } else if node.nodeName() == "ul", node.childNodeSize() >= 1 { + for child in node.getChildNodes() { + if child.nodeName() == "li" { + markdown += "\n- " + try convertNode(child) + } + } + return } if node.nodeName() == "#text" && node.description != " " { diff --git a/Tests/SwiftHTMLtoMarkdownTests/BasicHTMLTests.swift b/Tests/SwiftHTMLtoMarkdownTests/BasicHTMLTests.swift index cfdfb3f..28ef7fc 100644 --- a/Tests/SwiftHTMLtoMarkdownTests/BasicHTMLTests.swift +++ b/Tests/SwiftHTMLtoMarkdownTests/BasicHTMLTests.swift @@ -5,12 +5,13 @@ // Created by Taylor Lineman on 8/23/23. // -import XCTest +import Foundation +import Testing @testable import SwiftHTMLtoMarkdown -final class BasicHTMLTests: XCTestCase { +final class BasicHTMLTests { - func testAll() throws { + @Test func all() throws { let raw = """

Heading level 1

Heading level 2

@@ -27,6 +28,11 @@ final class BasicHTMLTests: XCTestCase {

This text is really important.

+ +

This is some code Hello World!

Hello World
@@ -55,6 +61,8 @@ final class BasicHTMLTests: XCTestCase { A*cats*meow This text is ***really important***. + - This is the first list item + - This is the second list item This is some code `Hello World!` @@ -71,10 +79,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testHeaderLevelOne() throws { + @Test func headerLevelOne() throws { let raw = "

Heading level 1

" let correctOutput = """ # Heading level 1 @@ -86,10 +94,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testHeaderLevelTwo() throws { + @Test func headerLevelTwo() throws { let raw = "

Heading level 2

" let correctOutput = """ ## Heading level 2 @@ -101,10 +109,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testHeaderLevelThree() throws { + @Test func headerLevelThree() throws { let raw = "

Heading level 3

" let correctOutput = """ ### Heading level 3 @@ -116,10 +124,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testHeaderLevelFour() throws { + @Test func headerLevelFour() throws { let raw = "

Heading level 4

" let correctOutput = """ #### Heading level 4 @@ -131,10 +139,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testHeaderLevelFive() throws { + @Test func headerLevelFive() throws { let raw = "
Heading level 5
" let correctOutput = """ ##### Heading level 5 @@ -146,10 +154,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testHeaderLevelSix() throws { + @Test func headerLevelSix() throws { let raw = "
Heading level 6
" let correctOutput = """ ###### Heading level 6 @@ -161,10 +169,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testParagraph() throws { + @Test func paragraph() throws { let raw = "

Paragraphs are pretty fun

" let correctOutput = "Paragraphs are pretty fun" var document = BasicHTML(rawHTML: raw) @@ -172,10 +180,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testBold() throws { + @Test func bold() throws { let raw = "

I just love bold text.

" let correctOutput = "I just love **bold text**." var document = BasicHTML(rawHTML: raw) @@ -183,10 +191,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testBoldWithNoLeadingOrTrailingSpaces() throws { + @Test func boldWithNoLeadingOrTrailingSpaces() throws { let raw = "

Loveisbold

" let correctOutput = "Love**is**bold" var document = BasicHTML(rawHTML: raw) @@ -194,10 +202,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testItalicized() throws { + @Test func italicized() throws { let raw = "

Italicized text is the cat's meow.

" let correctOutput = "Italicized text is the *cat's meow*." var document = BasicHTML(rawHTML: raw) @@ -205,10 +213,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testItalicizedWithNoLeadingOrTrailingSpaces() throws { + @Test func italicizedWithNoLeadingOrTrailingSpaces() throws { let raw = "

Acatsmeow

" let correctOutput = "A*cats*meow" var document = BasicHTML(rawHTML: raw) @@ -216,10 +224,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testItalicizedBoldText() throws { + @Test func italicizedBoldText() throws { let raw = "

This text is really important.

" let correctOutput = "This text is ***really important***." var document = BasicHTML(rawHTML: raw) @@ -227,10 +235,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testFencedCodeBlockWithLanguage() throws { + @Test func fencedCodeBlockWithLanguage() throws { let raw = """
Hello World
""" @@ -246,10 +254,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testFencedCodeBlockWithoutLanguage() throws { + @Test func fencedCodeBlockWithoutLanguage() throws { let raw = """
Hello World
""" @@ -265,10 +273,10 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } - func testCode() throws { + @Test func code() throws { let raw = "

This is some code Hello World!

" let correctOutput = "This is some code `Hello World!`" @@ -278,7 +286,23 @@ final class BasicHTMLTests: XCTestCase { let markdown = try document.asMarkdown() print(markdown) - XCTAssertTrue(markdown == correctOutput) + #expect(markdown == correctOutput) } + @Test func unorderedLists() throws { + let raw = "" + + let correctOutput = """ + + - List item 1 + - List item 2 + """ + + var document = BasicHTML(rawHTML: raw) + try document.parse() + + let markdown = try document.asMarkdown() + print(markdown) + #expect(markdown == correctOutput) + } }