#trim a content

16 messages · Page 1 of 1 (latest)

compact cobalt
#

in my template i have this line:
emph(secondaryHeadingContent)
now I want to trim the output to a certain length. there is str.trim(), but how do it convert the content to a string? these all fail, mainly because secondaryHeadingContent is of type content, not string:

#str(emph(secondaryHeadingContent)).trim(at: 3) 
#emph(secondaryHeadingContent).trim(at: 3)
#emph(secondaryHeadingContent.text.trim(at: 3))
#emph(secondaryHeadingContent.trim(at: 3)```
slow plover
#

are you looking for slice?

#let a-string = "this is actually a string"
Look at this #emph(str(a-string).slice(0, 3)) slice \
#let a-content = [this is actually *content*]
These are `a-content`'s children: #a-content.fields()
compact cobalt
#

same problem: I can't access the text of my content.

#
``` gives me `(text: "Lorem Ipsum")`
but 
```    #secondaryHeadingContent.text``` throws an error: `Content does not contain field "text"`
scenic ember
slow plover
#

Have you seen [this](#quick-questions message)? There's also repr but you don't really do yourself any favours there.
can you give some more context?

compact cobalt
#

what I actually just want to do is to shorten a content's text down to N characters, for which @slow plover pointed out that I probably need to use str.slice() – but I'm not getting that far.

I don't understand this error. is it legit?:

      #secondaryHeadingContent.text
    }

yields an error: content doesn't contain field "text"

scenic ember
#

it looks like the field "text" is only available if the content has no formattig

open pendant
#

correct

#

you cant generally obtain any kind of content as a string

#

after all, such content could be an image, for example

#

there are some workarounds to try to tackle that

#

but basically you'd have to write a function which goes through the content tree recursively

#

if the content is a sequence ([*a* _a_].func()) , i.e. has a children field, then you'll have to apply the function to each child and join the results
if the content is text, just .text (base case)

#

if the content has .body, apply the function to .body

#

otherwise you just return empty string cuz u dont know what to do