Skip to content

Commit

Permalink
Update String+Markdown.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
linjie-firework committed Nov 19, 2024
1 parent 5c16daf commit a65a0b1
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions Sources/FireworkVideoUI/Utils/String+Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,69 @@ import Foundation

public extension String {
func parseMarkdown() -> String {
var resultMarkdownText = self
var result = self
// markdown unordered item
resultMarkdownText = resultMarkdownText.replacingOccurrences(of: "\n- ", with: "\n  • ")
resultMarkdownText = resultMarkdownText.replacingOccurrences(of: "\n* ", with: "\n  • ")
result = result.replacingOccurrences(of: "\n- ", with: "\n  • ")
result = result.replacingOccurrences(of: "\n* ", with: "\n  • ")
// markdown ordered item
resultMarkdownText = parse(
resultMarkdownText,
result = parse(
result,
pattern: "\n(\\d+?)\\. ",
createReplaceContent: { content in
"\n  \(content). "
}
)
// markdown strikethrough
resultMarkdownText = parse(
resultMarkdownText,
result = parse(
result,
pattern: "~~(.+?)~~",
createReplaceContent: { content in
"<del>\(content)</del>"
}
)
// markdown strong
resultMarkdownText = parse(
resultMarkdownText,
result = parse(
result,
pattern: "\\*\\*(.+?)\\*\\*",
createReplaceContent: { content in
"<strong>\(content)</strong>"
}
)
// markdown italic
resultMarkdownText = parse(
resultMarkdownText,
result = parse(
result,
pattern: "\\*(.+)\\*",
createReplaceContent: { content in
"<em>\(content)</em>"
}
)
// markdown h1
resultMarkdownText = parse(
resultMarkdownText,
result = parse(
result,
pattern: "\n# (.+)(?=($|\n))",
createReplaceContent: { content in
"\n<h1>\(content)</h1>"
}
)
// markdown h2
resultMarkdownText = parse(
resultMarkdownText,
result = parse(
result,
pattern: "\n## (.+)(?=($|\n))",
createReplaceContent: { content in
"\n<h2>\(content)</h2>"
}
)
// markdown h3
resultMarkdownText = parse(
resultMarkdownText,
result = parse(
result,
pattern: "\n### (.+?)(?=($|\n))",
createReplaceContent: { content in
"\n<h3>\(content)</h3>"
}
)
resultMarkdownText = parseMarkdownLink(resultMarkdownText)
resultMarkdownText = resultMarkdownText.replacingOccurrences(of: "\n", with: "<br />")
return resultMarkdownText
result = parseMarkdownLink(result)
result = result.replacingOccurrences(of: "\n", with: "<br />")
return result
}

private func parse(
Expand Down

0 comments on commit a65a0b1

Please sign in to comment.