Franklin.jl list of pages

I am experimenting with Franklin.jl, with the intention of migrating my blog from Hugo.

Suppose I have a bunch of blog posts in a folder structure like

pages/blog.md
pages/posts/on-why-julia-is-awesome/index.md
...

and I would like to autogenerate the list of all posts in blog.md, not unlike {{ range .Pages }} ... {{ end }} in Hugo.

How is that done in Franklin? Also, pointers to source code for websites that do this would be appreciated, all the examples I have seen add posts manually.

1 Like

Some examples:

2 Likes

This should also still work (something similar also powers help.juliahub.com).

1 Like

Hi! I am also trying to get the list of posts but the solution in this thread didn’t worked for me.

I am not really sure why. When I evaluate collect(keys(Franklin.ALL_PAGE_VARS)) within my project directory I got an empty list as if I had no pages at all but I do!

You can see my blog here, the code is store at GitHub.

You can create a custom function in Franklin in your utils.jl . This is mine from my blog. You can use it as a starting point

@delay function hfun_blogposts()
    today = Dates.today()
    curyear = year(today)
    curmonth = month(today)
    curday = day(today)

    list = readdir("content/posts")

    filter!(endswith(".md"), list)
    function sorter(p)
        ps = splitext(p)[1]
        url = "content/posts/$ps/"
        surl = strip(url, '/')
        pubdate = pagevar(surl, "rss_pubdate")
        #if isnothing(pubdate)
        #    return Date(Dates.unix2datetime(stat(surl * ".md").ctime))
        #end
        #return Date(pubdate, dateformat"yyyy-mm-dd")
    end
    sort!(list, by=sorter, rev=true)

    io = IOBuffer()
    #write(io, """<ul class="blog-posts">""")

    write(io, """<div class="franklin-content">""")
    for (i, post) in enumerate(list)
        if post == "content/index.md"
            continue
        end
        ps = splitext(post)[1]
        write(io, "<li>")
        url = "content/posts/$ps/"
        url_aux = "./posts/$ps/"
        surl = strip(url, '/')
        title = pagevar(surl, "title")
        pubdate = pagevar(surl, "rss_pubdate")
        description = pagevar(surl, "rss_description")
        if isnothing(pubdate)
            date = "$curyear-$curmonth-$curday"
        else
            #date = Date(pubdate, dateformat"yyyy-mm-dd")
        end
        write(io, """$pubdate<br>""")
        write(io, """<a href="$url_aux">$title</a></b><p>""")
    end
    write(io, "</div>")
    return String(take!(io))
end

Then you can go to your markdown page where you want to bring all the blogpost and just call the function using

{{blogposts}}

Notice that you don’t need to add the hfun_ to call the function .

2 Likes

Nice! Thanks. I was able to adequate it to my needs.

2 Likes