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

New lines are concatenated and not broken, need two lines #341

Closed
3 tasks done
freak4pc opened this issue Aug 8, 2024 · 5 comments · Fixed by #342
Closed
3 tasks done

New lines are concatenated and not broken, need two lines #341

freak4pc opened this issue Aug 8, 2024 · 5 comments · Fixed by #342

Comments

@freak4pc
Copy link
Contributor

freak4pc commented Aug 8, 2024

Describe the bug
When I have a Markdown as follows:

Markdown(
    """
    Test1
    Test2
    Test3
    """
)

I get the following result:

image

If I add an extra line between them, they get separated:

To me, this seems like the wrong behavior, as you can see even from the markdown rendered here in GitHub:

Test1
Test2
Test3

Is there any way to fix it, or control this behavior?

Thanks :)

Checklist

  • I can reproduce this issue with a vanilla SwiftUI project.
  • I can reproduce this issue using the main branch of this package.
  • This bug hasn't been addressed in an existing GitHub issue.

Steps to reproduce
Shown in problem description

Expected behavior
Shown in problem description

Screenshots
Shown in problem description

Version information

  • MarkdownUI: [e.g. 2.0.0, or a commit hash]
  • OS: iOS 17.4
  • Xcode: 16 Beta 5

Additional context
Add any other context about the problem here.

@Coosis
Copy link

Coosis commented Aug 9, 2024

Also ran into this issue this morning.
Version: MarkdownUI 2.3.1
Xcode: 16.0 Beta

@gonzalezreal
Copy link
Owner

Hi @freak4pc,

Thanks for reporting this. There has been some discussion in the past about how MarkdownUI renders single line breaks.

In Markdown, single line breaks within a paragraph are treated as soft line breaks. Soft line breaks are typically rendered as spaces, which is consistent with what a browser would do.

For instance, the following HTML:

<p>foo
baz</p>

Is rendered as:

foo baz

GitHub's behavior varies depending on the context:

  • It renders soft breaks as line breaks in issue descriptions and comments, like you demonstrated.
  • It renders soft breaks as spaces in md files.

For instance, if you edit the repository's README.md file adding a line break on the first line, nothing changes in the preview.

Edit Preview

In Markdown, you can use a backslash \ at the end of a line to force a line break within a paragraph.

struct LineBreakView: View {
  var body: some View {
    Markdown {
      """
      Test1\\
      Test2\\
      Test3
      """
    }
  }
}

MarkdownUI also supports the <br /> tag.

struct LineBreakView: View {
  var body: some View {
    Markdown {
      """
      Test1<br />
      Test2<br />
      Test3
      """
    }
  }
}

Unfortunately, there is currently no workaround if you want to avoid changing the text as suggested above.

I don't have the time right now, but it should be feasible to add an environment value to let users configure how a Markdown view renders soft breaks.

@freak4pc
Copy link
Contributor Author

freak4pc commented Aug 9, 2024

Hi @freak4pc,

Thanks for reporting this. There has been some discussion in the past about how MarkdownUI renders single line breaks.

In Markdown, single line breaks within a paragraph are treated as soft line breaks. Soft line breaks are typically rendered as spaces, which is consistent with what a browser would do.

For instance, the following HTML:

<p>foo
baz</p>

Is rendered as:

foo baz

GitHub's behavior varies depending on the context:

  • It renders soft breaks as line breaks in issue descriptions and comments, like you demonstrated.
  • It renders soft breaks as spaces in md files.

For instance, if you edit the repository's README.md file adding a line break on the first line, nothing changes in the preview.

Edit Preview

In Markdown, you can use a backslash \ at the end of a line to force a line break within a paragraph.

struct LineBreakView: View {
  var body: some View {
    Markdown {
      """
      Test1\\
      Test2\\
      Test3
      """
    }
  }
}

MarkdownUI also supports the <br /> tag.

struct LineBreakView: View {
  var body: some View {
    Markdown {
      """
      Test1<br />
      Test2<br />
      Test3
      """
    }
  }
}

Unfortunately, there is currently no workaround if you want to avoid changing the text as suggested above.

I don't have the time right now, but it should be feasible to add an environment value to let users configure how a Markdown view renders soft breaks.

Thanks, appreciate the detailed answer :)
I'm happy to try and work on a PR for this - would you mind directing to me to the place in the code where a soft break is detected / handled ?

@freak4pc
Copy link
Contributor Author

freak4pc commented Aug 9, 2024

This seems like a reasonable place to allow configuration via environment, let me know if that strategy feels good to you and I'm happy to cook something up :)

image

freak4pc added a commit to freak4pc/swift-markdown-ui that referenced this issue Aug 9, 2024
@gonzalezreal
Copy link
Owner

gonzalezreal commented Aug 9, 2024

Thanks for working on this, @freak4pc! I really appreciate it.

I wouldn't add this configuration to the theme. I like the idea of having a soft break mode enum and add it to the environment using a dedicated view modifier.

struct LineBreakView: View {
  var body: some View {
    Markdown {
      """
      Test1
      Test2
      Test3
      """
    }
    .markdownSoftBreakMode(.lineBreak)
  }
}

I will have a look at your PR over the weekend. Thanks again for your contribution!

freak4pc added a commit to freak4pc/swift-markdown-ui that referenced this issue Aug 9, 2024
freak4pc added a commit to freak4pc/swift-markdown-ui that referenced this issue Aug 9, 2024
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

Successfully merging a pull request may close this issue.

3 participants