Skip to content

Instantly share code, notes, and snippets.

@harryworld
Created May 12, 2018 08:16
Show Gist options
  • Save harryworld/5468f0d997a8925accef86bac1e9d090 to your computer and use it in GitHub Desktop.
Save harryworld/5468f0d997a8925accef86bac1e9d090 to your computer and use it in GitHub Desktop.
//
// Regex.swift
//
extension String {
public var byStrippingFormatCharacters: String {
var result = self
// #H1, #H2
Regex.headerRegex.matches(in: result, options: [], range: NSMakeRange(0, result.count)).forEach {
(match) in
let syntax = match.range(withName: "syntax")
let nsString = NSString(string: result)
result = nsString.replacingCharacters(in: syntax, with: "")
}
// * bullet
Regex.bulletRegex.matches(in: result, options: [], range: NSMakeRange(0, result.count)).forEach {
(match) in
let syntax = match.range(withName: "syntax")
let nsString = NSString(string: result)
result = nsString.replacingCharacters(in: syntax, with: "")
}
// *Bold*
Regex.boldRegex.matches(in: result, options: [], range: NSMakeRange(0, result.count)).forEach {
(match) in
let leading = match.range(withName: "leading")
let trailing = match.range(withName: "trailing")
let nsString = NSString(string: result)
let intermediate = nsString
.replacingCharacters(in: trailing, with: "")
result = NSString(string: intermediate)
.replacingCharacters(in: leading, with: "")
}
// -Strikethrough-
Regex.strikethroughRegex.matches(in: result, options: [], range: NSMakeRange(0, result.count)).forEach {
(match) in
let leading = match.range(withName: "leading")
let trailing = match.range(withName: "trailing")
let nsString = NSString(string: result)
let intermediate = nsString
.replacingCharacters(in: trailing, with: "")
result = NSString(string: intermediate)
.replacingCharacters(in: leading, with: "")
}
// _Underline_
Regex.underlineRegex.matches(in: result, options: [], range: NSMakeRange(0, result.count)).forEach {
(match) in
let leading = match.range(withName: "leading")
let trailing = match.range(withName: "trailing")
let nsString = NSString(string: result)
let intermediate = nsString
.replacingCharacters(in: trailing, with: "")
result = NSString(string: intermediate)
.replacingCharacters(in: leading, with: "")
}
// ::highlight::
Regex.highlightRegex.matches(in: result, options: [], range: NSMakeRange(0, result.count)).forEach {
(match) in
let leading = match.range(withName: "leading")
let trailing = match.range(withName: "trailing")
let nsString = NSString(string: result)
let intermediate = nsString
.replacingCharacters(in: trailing, with: "")
result = NSString(string: intermediate)
.replacingCharacters(in: leading, with: "")
}
result = result.byStrippingCheckboxes
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment