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

[Legacy API] toAttributedString() adding trailing newline via toHTML() #219

Closed
1 task done
gsbernstein opened this issue Jun 23, 2020 · 8 comments
Closed
1 task done
Labels

Comments

@gsbernstein
Copy link

gsbernstein commented Jun 23, 2020

Please help prevent duplicate issues before submitting a new one:

  • I've searched other open/closed issues for duplicates before opening up this new issue.

Report

What did you do?

Example code:

            let down = Down(markdownString: "test")            
            let attributedString = try! down.toAttributedString()

What did you expect to happen?

attributedString.string == "test"

What happened instead?

attributedString.string == "test\n"

Seems like internally toAttributedString() uses toHTML(), and toHTML() is the one adding the newline.

@gsbernstein
Copy link
Author

It seems like your NSAttributedStringTests even expect the trailing newline to be added. Is this intended behavior?

    func testAttributedStringBindingsWork() {
        let attributedString = try? Down(markdownString: "## [Down](https://github.com/iwasrobbed/Down)").toAttributedString()
        XCTAssertNotNil(attributedString)
        XCTAssertTrue(attributedString!.string == "Down\n")
    }

    func testInstantiation() {
        let attributedString = try? NSAttributedString(htmlString: "<html><body><p>Oh Hai</p></body></html>")
        XCTAssertNotNil(attributedString)
        XCTAssertTrue(attributedString!.string == "Oh Hai\n")
    }

@iwasrobbed-ks iwasrobbed-ks changed the title toAttributedString() adding trailing newline [Legacy API] toAttributedString() adding trailing newline via toHTML() Jun 23, 2020
@iwasrobbed-ks
Copy link
Collaborator

Hi @gsbernstein 👋 I would likely switch to using the attributed string styler API (which is background thread safe as well) rather than that legacy API which does indeed use toHTML() under the hood. We have no control over how they render their string w/ that API but with the attributed string styler you can do whatever you wish

See #177

and examples in the test suite, e.g. https://github.com/iwasrobbed/Down/blob/master/Tests/Styler/StylerTestSuite.swift#L64

@gsbernstein
Copy link
Author

Thanks Rob! Does that API support using a stylesheet? Our current full code looks like this

let style = "* {font-family: \(cssFont.family.cssString); color: \(cssColor); font-size: \(cssFont.size.rawValue)px; } a { color: \(cssLinkColor); text-decoration: none; } *:last-child { margin-bottom:0; padding-bottom: 0;}"
let attributedString = try down.toAttributedString(stylesheet: style)

And I'm not sure if DownStylerConfiguration lets you do all of that (or how to translate it)

@johnxnguyen
Copy link
Owner

@gsbernstein unfortunately no it currently does not support using a stylesheet.

@iwasrobbed-ks
Copy link
Collaborator

No style sheets, but you could set general styles like John does here https://github.com/iwasrobbed/Down/blob/master/Tests/Styler/StylerTestSuite.swift#L90

If you need more custom styling, you could also fork the repo and add it in (such as your last-child styling) but there are already some APIs for spacing between list items

@o15a3d4l11s2
Copy link

o15a3d4l11s2 commented Sep 10, 2020

This is quite old thread, but since I needed to render Markdown + superscripts and subscripts, which are HTML, I ended up using down.toAttributedString(DownOptions.unsafe) and manually trimming the new line character at the end. As a starting point I used the ideas from https://stackoverflow.com/questions/38737920/swift-nsattributedstring-trim.

@iwasrobbed-ks, is there any better way to support both Markdown and HTML than allowing unsafe and rendering as attributedString?

@iwasrobbed-ks
Copy link
Collaborator

@o15a3d4l11s2 I'm curious why you wouldn't just use an attributed string transform for sub/superscript instead of opting into HTML? E.g. https://github.com/Flinesoft/HandyUIKit/blob/3353856a1d50df5ffd370ded078c1a21ffb18a68/Frameworks/HandyUIKit/Extensions/NSAttributedStringExt.swift#L55

Otherwise, no, handling any sort of HTML requires switching into unsafe mode since HTML code isn't safe for raw rendering without opening up your app to various scripting attacks

@o15a3d4l11s2
Copy link

o15a3d4l11s2 commented Sep 10, 2020

@iwasrobbed-ks, thanks for the fast response and the suggested alternative. I have external constraints, so I need to support <sup> and <sub> explicitly. I resolved the end newline issue and all works properly now. Thank you for creating this library!

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

No branches or pull requests

4 participants