hugo v0.138.0+extended+withdeploy darwin/arm64 BuildDate=unknown VendorInfo=nixpkgs
Title does the heavy lifting on describing the problem, but I can get more specific here:
I’m attempting to use my Twitter export JSON file to generate an archive. Each tweet is its own page in a “tweets” section. Mentions and hashtags are taxonomies for tweets. The tweet pages are working well, and there is no metadata associated with hashtags, so I don’t bother generating pages for them, but mentions are another story. I want to generate pages for each item in the mentions taxonomy because I want to make use of the name and screen_name properties.
The relevant parts of the data look like this:
[
{
"tweet": {
"entities" : {
"user_mentions" : [
{
"name" : "@ramsey@phpc.social",
"screen_name" : "ramsey",
"indices" : [
"0",
"7"
],
"id_str" : "7794552",
"id" : "7794552"
}
]
}
}
]
I’ve put together the following content adapter, which lives in /content/mentions/_content.gotempl:
{{ $data := slice }}
{{ with resources.Get "json/tweets.json" }}
{{ $data = . | transform.Unmarshal }}
{{ $mentions := dict }}
{{ range $data }}
{{ range .tweet.entities.user_mentions }}
{{ $mentions = merge $mentions (dict .screen_name (dict
"name" .name
"screen_name" .screen_name
"id" .id_str
))}}
{{ end }}
{{ end }}
{{ range $key, $value := $mentions }}
{{ $mentionParams := dict
"screen_name" $value.screen_name
}}
{{ $mentionPage := dict
"title" $value.name
"kind" "taxonomy"
"path" $value.screen_name
"params" $mentionParams
}}
{{ $.AddPage $mentionPage }}
{{ end }}
{{ end }}
This produces the pages for the taxonomy, but in the list.html template for my tweets section, I have the following:
<div>
<h3>Top mentions</h3>
<ol>
{{ range first 5 .Site.Taxonomies.mentions.ByCount }}
<li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a> ({{ .Count }})</li>
{{ end }}
</ol>
<p><a href="{{ .Site.Taxonomies.mentions.Page.RelPermalink }}">All mentions</a></p>
</div>
The range gets an empty slice, and the .Site.Taxonomies.mentions.Page.RelPermalink
line throws the following error:
ERROR Rebuild failed: render: failed to render pages: render of "section" failed: "/.../website/layouts/tweets/list.html:28:32": execute of template failed at <.Site.Taxonomies.mentions.Page.RelPermalink>: nil pointer evaluating page.Page.RelPermalink
In a previous iteration, I used Python to turn the archive into pages and this exact template code worked. I’m using the same templates and have made an effort to keep the content the same. My goal is to share this project with others, and I hoped to eliminate the Python dependency. I also wanted an excuse to learn about content adapters.
Anyway, curious if anyone has any idea what might be happening here. Any help is very appreciated.
Appending some additional info.
Here is my config.toml
baseURL = "https://nicksloan.com/"
languageCode = "en-us"
title = "Nick Sloan"
enableRobotsTXT = true
[params]
[params.author]
name = 'Nick Sloan'
[outputs]
home = ['html']
section = ['html', 'rss']
taxonomy = ['html', 'rss']
term = ['html', 'rss']
[taxonomies]
hashtag = "hashtags"
mention = "mentions"
[markup]
[markup.highlight]
style = 'dracula'
lineNos = true
noClasses = false
[markup.goldmark.renderer]
unsafe = true
Here is the tweets section _content.gotempl
{{ $data := slice }}
{{ with resources.Get "json/tweets.json" }}
{{ $data = . | transform.Unmarshal }}
{{ $pages := slice }}
{{ range $data }}
{{ $fullText := .tweet.full_text }}
{{ $replacements := slice }}
{{ $tweetMentions := slice }}
{{ $tweetHashTags := slice }}
{{ range .tweet.entities.user_mentions }}
{{ $tweetMentions = $tweetMentions | append .screen_name }}
{{ $substr := delimit (slice "@" .screen_name) "" }}
{{ $replacement := delimit (slice "<a href=\"/mentions/" .screen_name "\">" $substr "</a>") "" }}
{{ $fullText = replace $fullText $substr $replacement }}
{{ end }}
{{ range .tweet.entities.hashtags }}
{{ $tweetHashTags = $tweetHashTags | append .text }}
{{ $substr := delimit (slice "#" .text) "" }}
{{ $replacement := (delimit (slice "<a href=\"/hashtags/" .text "\">" $substr "</a>") "") }}
{{ $fullText = replace $fullText $substr $replacement }}
{{ end }}
{{ range .tweet.entities.urls }}
{{ $replacement := (delimit (slice "<a href=\"" .expanded_url "\">" .display_url "</a>") "") }}
{{ $fullText = replace $fullText .url $replacement }}
{{ end }}
{{ $content := dict
"value" $fullText
"mediaType" "text/html"
}}
{{ $dates := dict "date" (time .tweet.created_at) }}
{{ $params := dict
"hashtags" $tweetHashTags
"mentions" $tweetMentions
}}
{{ $page := dict
"content" $content
"kind" "page"
"path" .tweet.id_str
"title" ($fullText | plainify)
"dates" $dates
"params" $params
}}
{{ $.AddPage $page }}
{{ end }}
{{ end }}