Generating a list of recommended articles

Hey,

I’ve been feeling that my static blog was missing some recommended articles for a while, so I decided to implement a little thing that would tackle such issue.

I documented the process here with some explanation: Article recommendation using Hugo.

Do you have a better way of doing that?

Thxx!


The tl;dr is the following:

// grab the current article
{{ $currentArticle := . }}

// grab my blog posts (https://ops.tips/blog)
{{ $blogArticles := where (where $.Site.Pages ".Section" "eq" "blog") ".Kind" "eq" "page" }}

// grab my "gists" posts (https://ops.tips/gists)
{{ $gists := where (where $.Site.Pages ".Section" "eq" "gists") ".Kind" "eq" "page" }}

// put them altogether w/out the current one
{{ $articles :=  where ($blogArticles | append $gists) ".Permalink" "!=" $currentArticle.Permalink }}

// create two bags so we can give priority
// to some article
{{ $veryRelevantArticles := slice }}
{{ $relevantArticles := slice }}

// put articles in the bags according to the
// intersections in their tags
{{ range $idx, $article := $articles }}
        {{ $numberOfIntersections := len (intersect $article.Params.tags $currentArticle.Params.Tags) }}

        {{ if (ge $numberOfIntersections 2) }}
                {{ $veryRelevantArticles = $veryRelevantArticles | append $article }}
        {{ else if (eq $numberOfIntersections 1) }}
                {{ $relevantArticles = $relevantArticles | append $article }}
        {{ end }}
{{ end }}

// put everything in a list
{{ $recommendedArticles := slice }}
{{ range $veryRelevantArticles }} {{ $recommendedArticles = $recommendedArticles | append . }} {{ end }}
{{ range $relevantArticles }} {{ $recommendedArticles = $recommendedArticles | append . }} {{ end }}

// display
<ul>
{{ range (shuffle (first 5 $recommendedArticles)) }}
<li>
  <a href="{{ .Permalink }}">
    {{ .Title }}
  </a>
</li>
{{ end }}
</ul>
2 Likes

Can it be done using different indices for related content?