jaspervdj / hakyll

A static website compiler library in Haskell
jaspervdj.be/hakyll
Other
2.69k stars 410 forks source link

How to link to external URL in blog post list? #797

Closed WillSewell closed 3 years ago

WillSewell commented 3 years ago

I'm using the following pattern to build the list of links to my blog posts:

posts <- recentFirst =<< loadAll "posts/*"
let blogCtx =
    listField "posts" postCtx (return posts) <>
    constField "title" "Blog" <>
    defaultContext
makeItem "" >>= loadAndApplyTemplate "templates/blog.html" blogCtx >>=
    loadAndApplyTemplate "templates/default.html" blogCtx >>=
    relativizeUrls

I would like to interleave links to blog posts on external sites. To be more concrete, I would like to do something like:

let externalPosts = [
    ExternalPost {
        date = "2020-09-03",
        title = "Example",
        url = "example.com"
    }
]
posts <- recentFirst =<< externalPosts <> loadAll "posts/*"

How feasible is it to do something like this?

jaspervdj commented 3 years ago

I have two examples of this. In both cases I'm putting files in the filesystem rather than interleaving them in Haskell, which is a bit easier since the metadata is provided in a more uniform way.

  1. On the Hakyll website, there are a number of external tutorials that contain a url key, e.g.:

    https://github.com/jaspervdj/hakyll/blob/master/web/tutorials/robertwpearce-01-setup-and-initial-customization.html

    they get included in the index here (there's not really any special code here):

    https://github.com/jaspervdj/hakyll/blob/master/web/site.hs#L55

    And here is the template:

    https://github.com/jaspervdj/hakyll/blob/master/web/templates/tutorials.html

  2. On my personal website, my photoblog is simply a mirror of my Tumblr page. They also just contain minimal metadata:

    https://github.com/jaspervdj/jaspervdj/blob/master/photos/2020-09-29-wengistrasse-ii.md

    And they get included in this template:

    https://github.com/jaspervdj/jaspervdj/blob/master/templates/photo.html

WillSewell commented 3 years ago

Excellent - thanks for the recommendations! I hadn't considered putting the external URL in the post metadata. That's a nicer (more consistent with other posts) way of doing it than defining the links in the Haskell source and merging with existing post links.

I'll close this since this since my question is answered.

WillSewell commented 3 years ago

One issue I find with the url tag approach, is the default template for feed-items assumes that the url is relative to the feedRoot.

https://github.com/jaspervdj/hakyll/blob/e9a8139152b166eae75083259fc3e824675de6fb/data/templates/atom-item.xml#L3

It would be nice if FeedConfiguration allowed this behaviour to be changed, or if renderFeed was exported so custom templates could be used. Do you have any preference? I'd be happy to submit a PR.

WillSewell commented 3 years ago

A workaround for now is to add

root: ''

to the post.