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
4 changes: 4 additions & 0 deletions Sources/Ink/API/Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public struct Markdown {
/// The HTML representation of the Markdown, ready to
/// be rendered in a web browser.
public var html: String
/// The Plain Text representation of the Markdown
public var plainText: String
/// The inferred title of the document, from any top-level
/// heading found when parsing. If the Markdown text contained
/// two top-level headings, then this property will contain
Expand All @@ -32,9 +34,11 @@ public struct Markdown {
private var titleStorage = TitleStorage()

internal init(html: String,
plainText: String,
titleHeading: Heading?,
metadata: [String : String]) {
self.html = html
self.plainText = plainText
self.titleHeading = titleHeading
self.metadata = metadata
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/Ink/API/MarkdownParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public struct MarkdownParser {
parse(markdown).html
}

public func plainText(from markdown: String) -> String {
parse(markdown).plainText
}

/// Parse a Markdown string into a `Markdown` value, which contains
/// both the HTML representation of the given string, and also any
/// metadata values found within it.
Expand Down Expand Up @@ -105,8 +109,14 @@ public struct MarkdownParser {
result.append(html)
}

let plainText = fragments.reduce(into: "", { result, wrapper in
let plainText = wrapper.fragment.plainText()
result.append("\(plainText)\n")
})

return Markdown(
html: html,
plainText: plainText,
titleHeading: titleHeading,
metadata: metadata?.values ?? [:]
)
Expand Down
6 changes: 5 additions & 1 deletion Tests/InkTests/CodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import Ink

final class CodeTests: XCTestCase {
func testInlineCode() {
let html = MarkdownParser().html(from: "Hello `inline.code()`")
let markdown = "Hello `inline.code()`";
let html = MarkdownParser().html(from: markdown)
let plainText = MarkdownParser().plainText(from: markdown)

XCTAssertEqual(html, "<p>Hello <code>inline.code()</code></p>")
XCTAssertEqual(plainText, "Hello inline.code()\n")
}

func testCodeBlockWithJustBackticks() {
Expand Down