0

I've seen other questions on here asking about text markdown not displaying in Swift but nothing directly related to AWS Amplify. I've tried the solutions such as the one linked below but nothing has worked so far.

SwiftUI Text Markdown dynamic string not working

I'm using AWS Amplify to send news articles to my app and the text body of the article is formatted as an optional string. Here is how it is setup in my Model:

public struct Article: Model {
public var body: String?

I've tried using

Text(.init(article.body ?? "Body"))

and

Text(try! AttributedString(markdown: article.body ?? "Body"))

But each method doesn't seem to work. Below is a snippet of the text body that I'm trying to have display new lines.

The Cubs defeated the Reds 4-2 in the second-ever Field of Dreams MLB game in Iowa, and both teams relished the experience.\n\nThe Cubs won MLB's 2022 Field of Dreams game over the Reds 4-2. Here is a look at the best moments from Thursday's action.

Before integrating AWS Amplify I was testing with local JSON files that I was loading into my app and the formatting was displaying properly.

What am I missing and/or not understanding?

gavido
  • 43
  • 1
  • 4
  • Does your string actually have new line characters or does it literally have a slash and then an n? – jnpdx Aug 13 '22 at 16:08
  • It literally has a slash and then an n. This worked fine when I was loading the text into a non-optional string from a JSON file and displaying it with a Text object in swift. – gavido Aug 13 '22 at 16:18
  • 1
    That's because strings loaded from JSON have the two characters `\` and `n` translated into a newline character for you. If you want that behavior your have two options: 1) Put actual newline characters into AWS 2) Search and replace the two literal characters `\` `n` and replace them with a newline character – jnpdx Aug 13 '22 at 16:21
  • I just tried adding new line characters in AWS and it appears correct in the markdown but when the article loads in Swift they are getting ignored. So it looks like my example above but instead of having \n\n it appears as `the experience.The Cubs` – gavido Aug 13 '22 at 16:56
  • It's likely that you're still misunderstanding the difference. How did you enter the newline characters? By pressing `return`? Or did you once again enter `\n` as two characters? – jnpdx Aug 13 '22 at 16:57
  • Again, if you have literally two `\` `n` characters, you'll need to search and replace them with actual newline characters. See https://stackoverflow.com/questions/35053698/newline-n-not-working-in-swift – jnpdx Aug 13 '22 at 16:58
  • By the way, none of this is "markdown" -- a newline character is a common plain text feature. There's also nothing AWS specific about this besides perhaps how you are entering the values into the fields stored there. – jnpdx Aug 13 '22 at 17:03
  • I tried two different methods for entering the new line characters. I edited an existing article and pressed `return` and then I also tried creating a new article in swift and sending it to AWS using AWS.DataStore.save and the object looks like `let item = Article(body: "the experience.\n\nThe Cubs"` again it appears correctly in AWS when I edit the Article in AWS and select `Edit in markdown`. – gavido Aug 13 '22 at 17:13

0 Answers0