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
Turning content into string: str(content)
#2196
Comments
str(content)
You can indeed iterate over the children and collect the text recursively. It's definitely not perfect, but it works for your case: #let to-string(content) = {
if content.has("text") {
content.text
} else if content.has("children") {
content.children.map(to-string).join("")
} else if content.has("body") {
to-string(content.body)
} else if content == [ ] {
" "
}
}
#to-string[This _*cool*_ project] // => "This cool project" |
Thanks @EpicEricEE. That feels like should be the implementation of |
Internally, there is already a plain text functionality (via the Since the issue mostly comes up with Long-term, with get rules, this would also mean that more "standard" metadata like title, author, etc. could be specified with document set rules instead of on the template and templates could read that. For that, the title would also need to be content. |
I have pushed a commit that allows arbitrary content as the document title. It will then be turned into a string internally. This should cover your use case. I will also update the web app templates to default to a content block instead of a string with the next release. I'm not comfortable with exposing the internal plain-text functionality at the moment because it's very lossy for more complex content. |
Thank you for resolving this! |
@laurmaedje would it be possible to apply the same change for document author? |
Does it really make sense for an author name to have markup? |
in Hebrew for example, I might write |
Unfortunately the plain text conversion employed to convert the title to a string is very naive, so it wouldn't be able to handle show rules. I think it also doesn't properly handle smartquotes, but that would be fixable. |
Description
The
str()
function could accept content out of which it would extract the textual parts. For example:str([Hello _there_])
would be"Hello there"
.Use Case
In a few projects I want to set the document (PDF) title but also display the title in the doc itself. However, the title might be content and not a string. That means that it needs to be duplicated:
This is not a big deal but leads to these kinds of templates which feel weird because of the duplicity:
Ideally, one would be able to write the following, with conversion from content to string inside of the template.
I acknowledge that there are many vague cases and lots of complexity -- I'm just putting this out. Maybe there's also an existing solution to this which I overlooked, such as iterating the children of the content and only collecting the textual yield.
The text was updated successfully, but these errors were encountered: