I tried writing all this out here, but because I’m a new user I’m limited and it seems to think my code is URLs for some reason (And you’re limited to 2 urls and 1 image as a new user).
I’m wondering if this is a bug with jekyll, liquid, or if I’m just going about it incorrectly.
My structure is essentially this:
I have an index page that is looping through my posts and rendering a subset of their content (the post image and some content). Each truncated post on the index page is a link to the full post, ie the image and text are the content of the link itself. The post has an include that is rendering the post image along with a caption. This caption can potentially contain a link. If it does contain a link, it renders crazy html. If it doesn’t, things are fine.
Is this the right way to go about things? Is there another better way? Is this a bug?
What you have looks fine and should work… but without seeing the HTML it’s producing it’s hard to tell what’s going on from your screenshots.
What format are your posts in? Markdown? One thing to be mindful of when using Markdown mixed with HTML is indents matter. It will often take your HTML and treat them like code blocks or quotes because of the indent and mangles things. I’ve had this bite me a few times and had to remove any indents from the HTML inside of the include which isn’t that great.
The other thing I noticed is what you’re doing in the {% include %} added to each post, could easily be refactored using front matter instead. I would build the logic to do the image header directly into your post _layout instead of adding an include to every post.
To make this work you’d only need to make a few modifications… instead of include.variable it would be page.variable (you might need to scope the image variables to be more specific since page.url is already reserved for the actual page).
For example something like this you could add to your post _layout.
I went with includes because I was using this same concept not just for headers but for images all throughout the post. In the case of having more than one image in the post, putting it in frontmatter doesn’t really scale.
You’re nesting an anchor element inside of another anchor. It’s not obvious above, but inside of your post.content is the post where you’re adding the include with another anchor element. This explains why it works when you don’t use that anchor for the author.
The screenshot you posted above is likely what your browser’s dev tools is adjusting for and not the actual HTML… it’s a self corrected version of the DOM. I’m willing to bet if you view source instead it will be different and you’ll see an <a> wrapped around the post content, with another <a> inside of the figure element from the include.
I’m assuming you wrapped the entire post in an anchor to make it all clickable, but that’s going to bite you as any links inside of that will be invalid. You can instead do some cool stuff with pseudo elements on the post anchor and CSS. This post should give you some ideas.