Can I use the Migration / Updating a Page API to insert a hyperlink into a specific anchor text within a richText field (for one page)?

I’m in the process of migrating/updating content using the Migration API / “Updating a Page” script. I have a specific requirement and want to know whether it is supported or possible: Internal Netlinking through Migrate API?

  • I have a single page whose data includes a richText field.

  • Within that richText content, there is an existing block (e.g. a paragraph) with plain text.

  • I want to insert a hyperlink (a hyperlink span) targeting a specific substring / anchor text within that richText block. For example, if the paragraph contains "Click here to see more", I want just the "Click here" portion to become a link to https://example.com.

  1. Is this kind of in-place edit of a richText (adding a hyperlink span to part of the text) supported via the Migration API / “update document” path?
  2. If yes — what would the expected JSON “spans” format look like for that block? (i.e. how do I specify the start, end, type: "hyperlink", and associated link object)
  3. If it’s not supported directly, is there a recommended workaround? (e.g. re-serialize the full richText block with the needed hyperlink and send the entire block content, etc.)

To my understanding of the documentation: Migrating to Prismic — Prismic Docs

Seems like I would have to resubmit the full content of the richText to update everytime and i cannot have a grannular approach to update only specific sentence of that richText. is that correct?

Thanks in advance for any guidance!

Hey Marvin,

Short answer yes, but not as a tiny patch. With the Migration API you update the Rich Text by sending the full block/field value and adding a hyperlink span with start/end offsets for the exact characters you want linked. Example:

{
  "type": "paragraph",
  "text": "Click here to see more",
  "spans":[{
    "type":"hyperlink",
    "start":0,
    "end":10,
    "data":{"link_type":"Web","url":"https://example.com"}
  }]
}

More info here; Migration API Technical Reference — Prismic Docs and Migration API Technical Reference — Prismic Docs

1 Like

Interesting, thank you I’ll test it out!

I am looking to add internal links at scale across more than 500 pages. To do that, I need to update the richText field, but I would prefer not to rewrite the entire content just to insert a single link since that could easily introduce errors.

Is there any possible workaround for this?

Hi Marving,

For the moment the Migration API is the only way to achieve something like this at scale.

Although I would say that you could be secure in not changing all your info if you’re only targeting a sub string in whatever script you write and after you upload your changes everything is staged in a migration release so that you can verify your info in the builder and API before publishing any changes. The API will also point out any errors.