Creating an Inline-Block in Portable Text to Insert Current Date or Time

5 replies
Last updated: Aug 5, 2022
Here’s one I’m having a hard time with.
I would like to create an “inline-block” in my portable text, such that I could have a way to insert the current date or time as a block. As far as I can tell, I cannot do this as a standalone thing, I would have to annotate text and use a mark to say it should display the current date before it or instead of it.

Is there another solution here that I’m not thinking of?
Aug 5, 2022, 12:53 PM
I’m pretty sure you‘re correct, you’ll have to annotate the text and get rid of its content if you don’t use it.
Aug 5, 2022, 1:22 PM
gotcha, thank you for the reply. That’s what i ended up doing
Aug 5, 2022, 1:23 PM
You can make inline blocks though!
{
  type: 'block',
  of: [{type: 'currentDate' }] // <= must be an object type (I think)
}

Aug 5, 2022, 3:07 PM
The
currentDate
object can type can also have an initial value that puts a predefined string
Aug 5, 2022, 3:07 PM
Juxtaposition may be helpful to underline this, because regular custom blocks can seem inline, whereas what
user Y
is saying is truly inline.
On the left is a similar example from the docs, juxtaposed on the right with a custom block that
follows after the editor block in the array that houses them, instead of being a specific of qualifier.
The ones he is showing you how to do are really powerful
because they can be right in the middle of text and I am always surprised to see how uncommon they are because of the utility they provide.
Aug 5, 2022, 6:02 PM

Sanity.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Categorized in