Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to support strike through with like this ~text~ instead of ~~text~~ #115

Open
ajayalivecor opened this issue Jul 9, 2024 · 0 comments

Comments

@ajayalivecor
Copy link

For this i have created a custom class by conforming to protocol MarkDownCommonElement, the match method is not being called at all.

class MarkdownCustomStrikethrough: MarkdownCommonElement {
fileprivate static let regex = "(?<=\S)(~~)(?=\S)(.+?)(?<=\S)(\1)"

open var font: MarkdownFont?
open var color: MarkdownColor?
public var attributes: [NSAttributedString.Key: AnyObject] = [.strikethroughStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)]

open var regex: String {
    return MarkdownCustomStrikethrough.regex
}

public init(font: MarkdownFont? = nil, color: MarkdownColor? = nil) {
    self.font = font
    self.color = color
    print("MarkdownCustomStrikethrough init called")
}

func regularExpression() throws -> NSRegularExpression {
    print("MarkdownCustomStrikethrough regularExpression called")
    return try NSRegularExpression(pattern: regex, options: [])
}

func addAttributes(_ attributedString: NSMutableAttributedString, range: NSRange) {
    attributedString.addAttributes(attributes, range: range)
    
    if let font = font {
        attributedString.addAttribute(.font, value: font, range: range)
    }
    
    if let color = color {
        attributedString.addAttribute(.foregroundColor, value: color, range: range)
    }
}

func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
  // deleting trailing markdown
  attributedString.deleteCharacters(in: match.range(at: 4))
  // formatting string (may alter the length)
  addAttributes(attributedString, range: match.range(at: 3))
  // deleting leading markdown
  attributedString.deleteCharacters(in: match.range(at: 2))
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant