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

Combination of italic with custom markdown element not working #97

Open
S2V opened this issue Aug 20, 2021 · 4 comments
Open

Combination of italic with custom markdown element not working #97

S2V opened this issue Aug 20, 2021 · 4 comments

Comments

@S2V
Copy link

S2V commented Aug 20, 2021

Reproducible for the case when text has mixed text-decoration style with custom markdown element. In my case. I have the custom element that not supports by markdown be default - underline(++). I implement the custom markdown element for parsing (like MarkdownStrikethrough).

class MarkdownUnderline: MarkdownCommonElement {
	
	var font: MarkdownFont?
	var color: MarkdownColor?
	
	private static let regex = "(.?|^)(\\+\\+)(?=\\S)(.+?)(?<=\\S)(\\2)"

	var regex: String {
		return MarkdownUnderline.regex
	}
	
	public init(font: MarkdownFont?, color: MarkdownColor? = nil) {
		self.font = font
		self.color = color
	}
	
	public func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
		attributedString.deleteCharacters(in: match.range(at: 4))

		var attributes = attributedString.attributes(
			at: match.range(at: 3).location,
			longestEffectiveRange: nil,
			in: match.range(at: 3)
		)

		attributes[.underlineStyle] = NSNumber.init(value: NSUnderlineStyle.single.rawValue)

		attributedString.addAttributes(attributes, range: match.range(at: 3))

		attributedString.deleteCharacters(in: match.range(at: 2))
	}
}

But parsing result is not correct, for example for text **_++Lorem ipsum++_** will be produced the attributed text:
image

_++Lorem ipsum++_
image

If I change the regex to (.?|^)(\\*|_)(?=\\S)(.+?)(?<=\\S)(\\2) ,that was implemented for fix, the parsing result is correct.

@gaebel
Copy link
Contributor

gaebel commented Aug 20, 2021

I tried parsing with your custom element and it works fine. The last merged PRs haven't been published yet, though. Do you possibly use the latest version (1.7.1)? If yes, could you check if switching to the master branch fixes your issue?

@S2V
Copy link
Author

S2V commented Aug 20, 2021

@gaebel Yep, you are right. I provided a not correct example, sorry - that is my bad. If be more correct - parsing result for **_++ Lorem ipsum++_** is correct, but if you change the order of applying the styles to ++_**Lorem ipsum**_++ - result will be not correct.
Test text:

let test = """
 ++_**Mixed U/I/B Cras non erat dolor. In euismod laoreet iaculis. Aliquam viverra rutrum dui, at volutpat orci tempus et.**_++

 **_++Mixed B/I/U Cras non erat dolor. In euismod laoreet iaculis. Aliquam viverra rutrum dui, at volutpat orci tempus et.++_**
"""

Result 1.7.1:
image

Result master: 072e192:
image

Result master: 072e192 + modified regex for italic element to(.?|^)(\\*|_)(?=\\S)(.+?)(?<=\\S)(\\2):
image

Looks like order makes sense.

@gaebel gaebel closed this as completed in 761a30e Aug 20, 2021
@gaebel gaebel reopened this Aug 20, 2021
@gaebel
Copy link
Contributor

gaebel commented Aug 20, 2021

I've made some changes to the regex, and the attribute inheritance. This should be fixed now.

@Mr-Sergey-K
Copy link

@gaebel Release this fix please

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

3 participants