ysk24ok / jekyll-linkpreview

Jekyll plugin to generate link preview
https://rubygems.org/gems/jekyll-linkpreview
MIT License
30 stars 8 forks source link

Pass variable - e.g. front-matter into tag #2

Closed MattKevan closed 4 years ago

MattKevan commented 5 years ago

I'm looking for a way of passing a variable into the tag, such as front matter. This will allow the tag to be used in templates, rather than having to be added manually.

Ideally it would look something like {% linkpreview linkvariable %}

I've tried a couple of approaches, including firstly putting the front-matter variable directly into the tag, and secondly assigning the front matter to a different variable and putting that in the tag. Both times the variable text is output as a string rather than a variable.

Is there any way to do this?

ysk24ok commented 5 years ago

Thank you for the feedback.

As of v0.1.0, this plugin only supports directly passing url as a string.

I mean, this does work.

{% linkpreview https://github.com %}

But this does not work because github_url is recognized as a string, not a variable name.

{% assign github_url = 'https://github.com' %]
{% linkpreview github_url %}

I wanna support this in next version by updating LinkpreviewTag.render() to get the variable from the context object.


including firstly putting the front-matter variable directly into the tag

I have a question. This plugin only supports rendering link preview from a url, but is what you're trying to do rendering the front-matter? It's not a url. It would be helpful if you provide me a concrete example, thank you.

MattKevan commented 5 years ago

Hi, thanks for your reply. It’s good to know you have plans for a v2!

My idea was to use the preview tag in a template partial. That would allow me to put the url to be previewed in a post’s front matter, and have the preview rendered as normal. The way my templates work it’s not possible to put the tag in manually each time.

I guess when v2 is ready, the way to do it would be to assign the front matter to a variable and pass that to the tag.

If that makes sense.

ysk24ok commented 5 years ago

I've released v0.2.0!

Now you can pass a url variable like this.

{% assign github_url = 'https://github.com' %]
{% linkpreview github_url %}

Try this new version, and if this satisfies what you want, would you close this issue?

MattKevan commented 5 years ago

Hi, sorry it's taken me so long to reply – this is really cool, thank you. However, I can't get it to work just yet. Have I done something wrong?

I've added the latest version to my Gemfile and run 'bundle install :

group :jekyll_plugins do
   gem "jekyll-feed", "~> 0.6"
   gem 'jekyll-sitemap'
   gem 'jekyll-seo-tag'
   gem 'jekyll-csv'
   gem 'jekyll-linkpreview', '~> 0.2.0'
end

And then called the tag with this code:

{% assign linkurl = page.link %}
{% linkpreview linkurl %}

But it's not building properly – I get the following error:

Liquid Exception: Failed to open TCP connection to linkurl:80 (getaddrinfo: nodename nor servname provided, or not known) in /_layouts/listing.html

Thanks for your help with this!

ysk24ok commented 5 years ago

Thank you for trying v0.2.0!

Why not directly pass page.link to the tag (if page.link points to a url string)? like this,

{% linkpreview page.link %}

Actually, that behavior is a bug. The tag must work with a variable which points to another varialble. I'll fix this later and release v0.2.1. But before that, you can try the workaround above.

MattKevan commented 5 years ago

Hi, thanks for your reply. I've tried as you suggested and it's just outputting page.link as a link.

Here's the tag:

{% linkpreview page.link %}

And here's the code it generates:

<div class="jekyll-linkpreview-wrapper">
  <p><a href="page.link" target="_blank">page.link</a></p>
</div>

Really appreciate you helping out with this, thanks.

ysk24ok commented 5 years ago

Sorry for the inconvenience and lack of explanation in README. That simple preview is generated from the page which doesn't have meta tags such as og:title, og:url, og:image or og:description. Does the page have these tags?

ysk24ok commented 5 years ago

https://shopify.github.io/liquid/tags/variable/#assign It seems assign tag does not support assigning another variable.

{% assign linkurl = page.link %}
{% linkpreview linkurl %}

That's why the code above does not work. So {% linkpreview page.link %} is not a workaround and the behavior is not a bug.

ysk24ok commented 4 years ago
{% assign page.link = 'https://github.com' %}
{% linkpreview page.link %}

This works fine with jekyll-linkpreview v0.2.0.

You said {% linkpreview page.link %} does not render preview in https://github.com/ysk24ok/jekyll-linkpreview/issues/2#issuecomment-526515024 . I think that's because the page does not have meta tags such as og:title.

Let me close this issue because there is no recent activity. Thank you very much for creating this issue. If you have something more to discuss, feel free to reopen this.