HTML components within a JSON-LD structured schema

I have a FAQ component in my Phoenix 1.7 application. It asks and answers multiple questions. The answers are rendered as HEEx templates. This all works.

My problem is the generation of JSON-LD for a structured schema. I can’t call the answer_wann_sind_ferien_in/1 function from within the JSON-LD schema. The <%= @location.name %> part works.

Is there a way to use <.answer_wann_sind_ferien_in location={@location} /> within the JSON HEEx template?

Here’s the code:

  attr :location, :string, required: true

  defp answer_wann_sind_ferien_in(assigns) do
    ~H"""
    <div>Example answer.</div>
    """
  end

  def json_ld_location_faq(assigns) do
    ~H"""
    <script type="application/ld+json">
      {
      "@context": "https://schema.org",
      "@type": "FAQPage",
      "mainEntity": [
      {
      "@type": "Question",
      "name": "Wann sind Ferien in <%= @location.name %>?",
      "acceptedAnswer": {
      "@type": "Answer",
      "text": "<.answer_wann_sind_ferien_in
        location={@location}
      />"
      }
      }
      }
      ]
      }
    </script>
    """
  end

Here’s the resulting HTML:

<script type="application/ld+json">
  {
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [
  {
  "@type": "Question",
  "name": "Wann sind Ferien in Baden-Württemberg?",
  "acceptedAnswer": {
  "@type": "Answer",
  "text": "<.answer_wann_sind_ferien_in
    location={@location}
  />"
  }
  }
  }
  ]
  }
</script>

HEEx is built for composing HTML, not arbitrary data formats. I’d expect the HEEx compiler to treat everything within <script> tags as plain text + eex tags, so you only get the functionality of eex tags.

I’m not sure if there’s a way to render a function component to string with phoenix_view no longer around.