Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Asset Links are not resolved #11

Open
sean-cherbone opened this issue Oct 10, 2019 · 5 comments
Open

Asset Links are not resolved #11

sean-cherbone opened this issue Oct 10, 2019 · 5 comments

Comments

@sean-cherbone
Copy link

When embedding image assets in rich text, a node of this form is introduced:

{
  "data": {
    "target": {
      "sys": {
        "id": "3qDQMymWqI8Lih4tBx5BXv",
        "type": "Link",
        "linkType": "Asset"
      }
    }
  },
  "content": [],
  "nodeType": "embedded-asset-block"
}

This node then gets passed to the AssetBlockRenderer and ultimately gets parsed by the render method within the parent AssetHyperlinkRenderer class.

Because the value of data.target is a Hash, it gets to this point in the render code and then falls to the fail statement. There does not appear to be any mechanism for resolving the Link to its full form, which seems to be what this method is expecting.

Because this requires resolving linked content and there does not appear to be a mechanism present for this in the Renderer, my current solution is to traverse the hash tree and resolve any links I encounter. But that's hacky, so hopefully a proper solution can be worked in. Thanks for the library.

@tanaylakhani
Copy link

Having the same issue. @sean-cherbone do you mind sharing your solution on how you're resolving links

@thimo
Copy link

thimo commented Jan 14, 2022

I've worked around this by fetching the linked asset content from Contentful, getting the URL from there and inserting it in the current node JSON. Feels a bit hacky, but works for me for now.

So I've created a custom AssetHyperlinkRenderer that inherits from this project's AssetHyperlinkRenderer:

module Contentful
  module Renderers
    class AssetHyperlinkRenderer < RichTextRenderer::AssetHyperlinkRenderer
      def render(node)
        if (id = node.dig("data", "target", "sys", "id")).present?
          linked_asset = Contentful::Client.new(...).asset(id)

          node["data"]["target"]["fields"] ||= {}
          node["data"]["target"]["fields"]["file"] ||= {}
          node["data"]["target"]["fields"]["file"]["url"] = linked_asset.url
        end

        super(node)
      end
    end
  end
end

The custom renderer added to RichTextRenderer::Renderer in the Slim template:

  == RichTextRenderer::Renderer.new( \
      "entry-hyperlink" => Contentful::Renderers::HyperlinkRenderer,
      "asset-hyperlink" => Contentful::Renderers::AssetHyperlinkRenderer \
    ).render(entry.description)

@Sheepeer
Copy link

Sheepeer commented Dec 11, 2023

Any way better to fix this issue? Facing the same point

@tanaylakhani
Copy link

We had created a wrapper class for contentful object, where fields other than assets would be delegated to contentful entry, asset url will be fetched by doing entry.main_image.url etc. This allowed us to control elements better. Downside is we now have schema in two places - contentful + app service class

@Sheepeer
Copy link

We had created a wrapper class for contentful object, where fields other than assets would be delegated to contentful entry, asset url will be fetched by doing entry.main_image.url etc. This allowed us to control elements better. Downside is we now have schema in two places - contentful + app service class

Oh thx~ I did it like this before :) And I figured out this just now by using recommanded createClient method to fetch data instead of fetch contetnful apis directly, then the response data contains data.target.fields.file.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants