From be867877e3b589cafd125c5127b11ba98a487513 Mon Sep 17 00:00:00 2001 From: Bob Voorneveld Date: Thu, 8 May 2025 21:13:35 +0200 Subject: [PATCH 1/2] Adding unordered lists to be parsed. --- Sources/SwiftHTMLtoMarkdown/BasicHTML.swift | 8 ++ .../BasicHTMLTests.swift | 92 ++++++++++++------- 2 files changed, 66 insertions(+), 34 deletions(-) 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..b8d513a 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) + } } From bb58d7b1ef7484e2c1f062cbd1f2b5d95e4097f3 Mon Sep 17 00:00:00 2001 From: Bob Voorneveld Date: Mon, 12 May 2025 20:09:48 +0200 Subject: [PATCH 2/2] lowercasing --- .../BasicHTMLTests.swift | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Tests/SwiftHTMLtoMarkdownTests/BasicHTMLTests.swift b/Tests/SwiftHTMLtoMarkdownTests/BasicHTMLTests.swift index b8d513a..28ef7fc 100644 --- a/Tests/SwiftHTMLtoMarkdownTests/BasicHTMLTests.swift +++ b/Tests/SwiftHTMLtoMarkdownTests/BasicHTMLTests.swift @@ -11,7 +11,7 @@ import Testing final class BasicHTMLTests { - @Test func All() throws { + @Test func all() throws { let raw = """

Heading level 1

Heading level 2

@@ -82,7 +82,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func HeaderLevelOne() throws { + @Test func headerLevelOne() throws { let raw = "

Heading level 1

" let correctOutput = """ # Heading level 1 @@ -97,7 +97,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func HeaderLevelTwo() throws { + @Test func headerLevelTwo() throws { let raw = "

Heading level 2

" let correctOutput = """ ## Heading level 2 @@ -112,7 +112,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func HeaderLevelThree() throws { + @Test func headerLevelThree() throws { let raw = "

Heading level 3

" let correctOutput = """ ### Heading level 3 @@ -127,7 +127,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func HeaderLevelFour() throws { + @Test func headerLevelFour() throws { let raw = "

Heading level 4

" let correctOutput = """ #### Heading level 4 @@ -142,7 +142,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func HeaderLevelFive() throws { + @Test func headerLevelFive() throws { let raw = "
Heading level 5
" let correctOutput = """ ##### Heading level 5 @@ -157,7 +157,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func HeaderLevelSix() throws { + @Test func headerLevelSix() throws { let raw = "
Heading level 6
" let correctOutput = """ ###### Heading level 6 @@ -172,7 +172,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func Paragraph() throws { + @Test func paragraph() throws { let raw = "

Paragraphs are pretty fun

" let correctOutput = "Paragraphs are pretty fun" var document = BasicHTML(rawHTML: raw) @@ -183,7 +183,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func Bold() throws { + @Test func bold() throws { let raw = "

I just love bold text.

" let correctOutput = "I just love **bold text**." var document = BasicHTML(rawHTML: raw) @@ -194,7 +194,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func BoldWithNoLeadingOrTrailingSpaces() throws { + @Test func boldWithNoLeadingOrTrailingSpaces() throws { let raw = "

Loveisbold

" let correctOutput = "Love**is**bold" var document = BasicHTML(rawHTML: raw) @@ -205,7 +205,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func Italicized() 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) @@ -216,7 +216,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func ItalicizedWithNoLeadingOrTrailingSpaces() throws { + @Test func italicizedWithNoLeadingOrTrailingSpaces() throws { let raw = "

Acatsmeow

" let correctOutput = "A*cats*meow" var document = BasicHTML(rawHTML: raw) @@ -227,7 +227,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func ItalicizedBoldText() throws { + @Test func italicizedBoldText() throws { let raw = "

This text is really important.

" let correctOutput = "This text is ***really important***." var document = BasicHTML(rawHTML: raw) @@ -238,7 +238,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func FencedCodeBlockWithLanguage() throws { + @Test func fencedCodeBlockWithLanguage() throws { let raw = """
Hello World
""" @@ -257,7 +257,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func FencedCodeBlockWithoutLanguage() throws { + @Test func fencedCodeBlockWithoutLanguage() throws { let raw = """
Hello World
""" @@ -276,7 +276,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func Code() throws { + @Test func code() throws { let raw = "

This is some code Hello World!

" let correctOutput = "This is some code `Hello World!`" @@ -289,7 +289,7 @@ final class BasicHTMLTests { #expect(markdown == correctOutput) } - @Test func UnorderedLists() throws { + @Test func unorderedLists() throws { let raw = "" let correctOutput = """