Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/SwiftHTMLtoMarkdown/BasicHTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 != " " {
Expand Down
92 changes: 58 additions & 34 deletions Tests/SwiftHTMLtoMarkdownTests/BasicHTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
<h1>Heading level 1</h1>
<h2>Heading level 2</h2>
Expand All @@ -27,6 +28,11 @@ final class BasicHTMLTests: XCTestCase {

<p>This text is <em><strong>really important</strong></em>.</p>

<ul>
<li>This is the first list item</li>
<li>This is the second list item</li>
</ul>

<p>This is some code <code>Hello World!</code></p>

<pre><code><span class="hljs-attribute">Hello World</span></code></pre>
Expand Down Expand Up @@ -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!`

Expand All @@ -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 = "<h1>Heading level 1</h1>"
let correctOutput = """
# Heading level 1
Expand All @@ -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 = "<h2>Heading level 2</h2>"
let correctOutput = """
## Heading level 2
Expand All @@ -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 = "<h3>Heading level 3</h3>"
let correctOutput = """
### Heading level 3
Expand All @@ -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 = "<h4>Heading level 4</h4>"
let correctOutput = """
#### Heading level 4
Expand All @@ -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 = "<h5>Heading level 5</h5>"
let correctOutput = """
##### Heading level 5
Expand All @@ -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 = "<h6>Heading level 6</h6>"
let correctOutput = """
###### Heading level 6
Expand All @@ -161,76 +169,76 @@ 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 = "<p>Paragraphs are pretty fun</p>"
let correctOutput = "Paragraphs are pretty fun"
var document = BasicHTML(rawHTML: raw)
try document.parse()

let markdown = try document.asMarkdown()
print(markdown)
XCTAssertTrue(markdown == correctOutput)
#expect(markdown == correctOutput)
}

func testBold() throws {
@Test func bold() throws {
let raw = "<p>I just love <strong>bold text</strong>.</p>"
let correctOutput = "I just love **bold text**."
var document = BasicHTML(rawHTML: raw)
try document.parse()

let markdown = try document.asMarkdown()
print(markdown)
XCTAssertTrue(markdown == correctOutput)
#expect(markdown == correctOutput)
}

func testBoldWithNoLeadingOrTrailingSpaces() throws {
@Test func boldWithNoLeadingOrTrailingSpaces() throws {
let raw = "<p>Love<strong>is</strong>bold</p>"
let correctOutput = "Love**is**bold"
var document = BasicHTML(rawHTML: raw)
try document.parse()

let markdown = try document.asMarkdown()
print(markdown)
XCTAssertTrue(markdown == correctOutput)
#expect(markdown == correctOutput)
}

func testItalicized() throws {
@Test func italicized() throws {
let raw = "<p>Italicized text is the <em>cat's meow</em>.</p>"
let correctOutput = "Italicized text is the *cat's meow*."
var document = BasicHTML(rawHTML: raw)
try document.parse()

let markdown = try document.asMarkdown()
print(markdown)
XCTAssertTrue(markdown == correctOutput)
#expect(markdown == correctOutput)
}

func testItalicizedWithNoLeadingOrTrailingSpaces() throws {
@Test func italicizedWithNoLeadingOrTrailingSpaces() throws {
let raw = "<p>A<em>cats</em>meow</p>"
let correctOutput = "A*cats*meow"
var document = BasicHTML(rawHTML: raw)
try document.parse()

let markdown = try document.asMarkdown()
print(markdown)
XCTAssertTrue(markdown == correctOutput)
#expect(markdown == correctOutput)
}

func testItalicizedBoldText() throws {
@Test func italicizedBoldText() throws {
let raw = "<p>This text is <em><strong>really important</strong></em>.</p>"
let correctOutput = "This text is ***really important***."
var document = BasicHTML(rawHTML: raw)
try document.parse()

let markdown = try document.asMarkdown()
print(markdown)
XCTAssertTrue(markdown == correctOutput)
#expect(markdown == correctOutput)
}

func testFencedCodeBlockWithLanguage() throws {
@Test func fencedCodeBlockWithLanguage() throws {
let raw = """
<pre><code class="lang-swift"><span class="hljs-attribute">Hello World</span></code></pre>
"""
Expand All @@ -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 = """
<pre><code><span class="hljs-attribute">Hello World</span></code></pre>
"""
Expand All @@ -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 = "<p>This is some code <code>Hello World!</code></p>"

let correctOutput = "This is some code `Hello World!`"
Expand All @@ -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 = "<ul><li>List item 1</li><li>List item 2</li></ul>"

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)
}
}