mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Add function to strip Markdown-formatted images
This commit is contained in:
parent
60ce0868ca
commit
00bd18463e
@ -40,6 +40,7 @@ private extension PostListModel {
|
||||
func stripMarkdown(from string: String) -> String {
|
||||
var strippedString = string
|
||||
strippedString = stripHeadingOctothorpes(from: strippedString)
|
||||
strippedString = stripImages(from: strippedString, keepAltText: true)
|
||||
return strippedString
|
||||
}
|
||||
|
||||
@ -65,6 +66,39 @@ private extension PostListModel {
|
||||
return headinglessString
|
||||
}
|
||||
|
||||
func stripImages(from string: String, keepAltText: Bool = false) -> String {
|
||||
let pattern = #"!\[[\"]?(.*?)[\"|]?\]\(.*?\)"#
|
||||
var processedComponents: [String] = []
|
||||
let components = string.components(separatedBy: .newlines)
|
||||
for component in components {
|
||||
if component.isEmpty { continue }
|
||||
var processedString: String = component
|
||||
if keepAltText {
|
||||
let regex = try? NSRegularExpression(pattern: pattern, options: [])
|
||||
if let matches = regex?.matches(
|
||||
in: component, options: [], range: NSRange(location: 0, length: component.utf16.count)
|
||||
) {
|
||||
for match in matches {
|
||||
if let range = Range(match.range(at: 1), in: component) {
|
||||
processedString = "\(component[range])"
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let range = component.startIndex..<component.endIndex
|
||||
processedString = component.replacingOccurrences(
|
||||
of: pattern,
|
||||
with: "",
|
||||
options: .regularExpression,
|
||||
range: range
|
||||
)
|
||||
}
|
||||
if processedString.isEmpty { continue }
|
||||
processedComponents.append(processedString)
|
||||
}
|
||||
return processedComponents.joined(separator: "\n\n")
|
||||
}
|
||||
|
||||
func extractLede(from string: String) -> String {
|
||||
let terminatingCharacters = CharacterSet(charactersIn: ".。?").union(.newlines)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user