I am styling some dynamic markdown, however the framework I am using for styling doesnt support nested tags for links.
I need to parse the string and close the styling markdown tags effectively this :
"__Some bold text [FIRST LINK](https://FIRSTLINK.COM \"FIRST LINK\"), more bold text.__\n\n additional text \n\n
*some italic text[SECOND LINK](https://SECONDLINK.COM) ending text,*"
to this:
"__Some bold text __[FIRST LINK](https://FIRSTLINK.COM \"FIRST LINK\")__, more bold text.__\n\n additional text \n\n
*some italic text*[SECOND LINK](https://SECONDLINK.COM)* ending text,*"
This is only really going to be for bold and italic text. I started going down the route of
var str = "__Some bold text [FIRST LINK](https://FIRSTLINK.COM \"FIRST LINK\"), more bold text.__\n\n additional text \n\n *some italic text[SECOND LINK](https://SECONDLINK.COM) ending text,*"
let bold = str.components(separatedBy: "__")
for var string in bold {
if let matchedIndex = string.index(of: "[") {
string.insert(contentsOf: "__", at: matchedIndex)
}
}
But wondered, is there a better way to do this in Swift?
Edit - for clarity - essentially I need to modify the existing string to have closed tags prior to a link tag and re opened after a link tag - this prevents the links from being nested with the style tags and allows the styler framework to apply attributed strings accordingly
EDIT --- in line with @Linus comment here is the results of the regex (note running these out side of an extension in order to be able to test in a playground
var str = "__Some bold text [FIRST LINK](https://FIRSTLINK.COM \"FIRST LINK\"), more bold text.__\n additional text \n *some italic text[SECOND LINK](https://SECONDLINK.COM) ending text,*\n__sfdadhfjkh [THIRD LINK](https://THIRDLINK.COM \"THIRD LINK\"), more bold text.__"
do {
var regex = try NSRegularExpression(pattern: "(\\[.*?\\))" , options: [.caseInsensitive])
var newString = regex.stringByReplacingMatches(in: str, options: [], range: NSMakeRange(0, str.utf16.count), withTemplate: "__$1__")
print("\nFirst regex __$1__ \n\n\(newString)")
regex = try NSRegularExpression(pattern: "(\\[.*?\\))" , options: [.caseInsensitive])
var newerString = regex.stringByReplacingMatches(in: str, options: [], range: NSMakeRange(0, str.utf16.count), withTemplate: "*$1*")
print("\nSecond Regex *$1* \n\n"+newerString)
} catch { print("ERROR: searchFor regex (\("(\\[.*?\\))")) on string (\(str)) failed") }
Printed results
First regex __$1__
__Some bold text __[FIRST LINK](https://FIRSTLINK.COM "FIRST LINK")__, more bold text.__
additional text
*some italic text__[SECOND LINK](https://SECONDLINK.COM)__ ending text,*
__sfdadhfjkh __[THIRD LINK](https://THIRDLINK.COM "THIRD LINK")__, more bold text.__
Second Regex *$1*
__Some bold text *[FIRST LINK](https://FIRSTLINK.COM "FIRST LINK")*, more bold text.__
additional text
*some italic text*[SECOND LINK](https://SECONDLINK.COM)* ending text,*
__sfdadhfjkh *[THIRD LINK](https://THIRDLINK.COM "THIRD LINK")*, more bold text.__
I need to have both Italic and strong tags amended on the same string in order to pass it to a view to be styled
__after the link's closing parenthesis.