-
Notifications
You must be signed in to change notification settings - Fork 12
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
Comments
Having the same issue. @sean-cherbone do you mind sharing your solution on how you're resolving links |
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.new( \
"entry-hyperlink" => Contentful::Renderers::HyperlinkRenderer,
"asset-hyperlink" => Contentful::Renderers::AssetHyperlinkRenderer \
).render(entry.description) |
Any way better to fix this issue? Facing the same point |
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 |
Oh thx~ I did it like this before :) And I figured out this just now by using recommanded |
When embedding image assets in rich text, a node of this form is introduced:
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 aHash
, it gets to this point in the render code and then falls to thefail
statement. There does not appear to be any mechanism for resolving theLink
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.The text was updated successfully, but these errors were encountered: