contentful / rich-text-renderer.swift

Render Contentful Rich Text fields to native strings and views
MIT License
29 stars 32 forks source link

plain-text-renderer? #8

Open andrewplummer opened 4 years ago

andrewplummer commented 4 years ago

Apologies for asking this here but I couldn't find any other information online... is there contentful sanctioned swift equivalent for the npm plain-text-render here? https://www.npmjs.com/package/@contentful/rich-text-plain-text-renderer

Thank you

basememara commented 4 years ago

The library was overkill for rich text, I guess because of linked entries. I believe linked entries should be made into a separate library and the rich text to NSAttributedString conversion should be made available in the core Contentful Swift SDK.

We ended up trying something like this without this library:

extension Array where Element == Node {

    /// Converts the rich text type to plain string.
    func string() -> String {
        reduce(into: "") { result, next in
            switch next {
            case let object as Text:
                result += object.value
            case let object as Paragraph:
                result += object.content.string()
            default:
                break
            }
        }
    }
}

This can be used like the following when you have a rich text property: object.body.content.string()