Skip to content

Commit

Permalink
Ignore empty lines and use the correct string to format.
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroalhonen committed Jul 10, 2021
1 parent f3e0982 commit 5f25232
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Sources/NiftyMarkdownFormatter/NiftyMarkdownFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public func formattedMarkdownArray(markdown: String) -> [Text] {
var formattedTextViews: [Text] = []
let splitStrings: [String] = markdown.components(separatedBy: "\n")
for string in splitStrings {
if markdown.starts(with: "#") {
let headingSize = markdown.distance(
from: markdown.startIndex,
to: markdown.firstIndex(of: " ") ?? markdown.startIndex
if string.starts(with: "#") {
let headingSize = string.distance(
from: string.startIndex,
to: string.firstIndex(of: " ") ?? string.startIndex
)
var headingText = string
headingText.removeSubrange(
Expand All @@ -34,15 +34,17 @@ public func formattedMarkdownArray(markdown: String) -> [Text] {
headingSize: headingSize
))
formattedTextViews.append(heading)
} else if string.count == 0 {
// Ignore empty lines
} else {
if #available(iOS 15, macOS 12, *) {
if let attributedString = try? AttributedString(markdown: markdown) {
if let attributedString = try? AttributedString(markdown: string) {
formattedTextViews.append(Text(attributedString))
} else {
formattedTextViews.append(Text(markdown))
formattedTextViews.append(Text(string))
}
} else {
formattedTextViews.append(Text(markdown))
formattedTextViews.append(Text(string))
}
}
}
Expand Down

0 comments on commit 5f25232

Please sign in to comment.