Skip to content

Parse string with HTML tag and transform to Text #30

Answered by mbrandonw
aarsland asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, this is possible with the parsing library. You can parse directly into SwiftUI's Text, but it's easier to first parse into an intermediate type that is easier to handle (and equatable so that it's easier to test), and then map the intermediate type into Text.

The intermediate type could be as simple as this:

enum Result: Equatable {
  case text(String)
  case strong(String)
}

If you wanted to support more tags you would add onto this enum. Then you have to just describe how to parse from the beginning of an input string to generate values in Result. For example, to parse .text you just need to take everything up until you either reach a <strong> tag or until you reach the end of the …

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@mbrandonw
Comment options

@aarsland
Comment options

@mbrandonw
Comment options

@acosmicflamingo
Comment options

@acosmicflamingo
Comment options

Answer selected by aarsland
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants