Correctly output page text into json

I’m adding structured data to my site, using inline JSON which looks like this (i trimmed it for clarity):

<script type="application/ld+json">
{ "@context": "http://schema.org",
 "@type": "Article",
 ...
 "articleBody": "<?= $page->pagetext() ?>"
 ...
 }
</script>

My question is this: the articleBody can have the entire page text in it, but my worry is that this is JSON, and the text can in theory contain html tags (generated by Kirby tags etc). I should probably escape those tags, right? Is there an automagical way to do that?

And i’m guessing the double quotes in the tags will mess things up too. How do i solve it?

As far as I can see from the specs, the articleBody should only contain plain text.

Your right of course, I must confess, I didn’t look at the specs, i was following examples from here.

I’m guessing stripping the tags out will be a nightmare?

Using this <?= $page->pagetext()->kirbytext()->escape() ?> for now but i’m guessing it will be seen as plain text, and not converted back to tags when Google reads it.

Why should it be converted back to tags? I can only see plain text in the examples you linked to.

I’d strip the tags completely:

<?= strip_tags($page->text()->kt()); ?>

Well, I don’t know. No idea what Google does with it on their end.

Awesome. Wasn’t nightmare at all. I was thinking id have to get funky with regex or something. Thanks :slight_smile: