--- title: "Extending Contentful" date: 2020-05-12T17:25:55+08:00 categories: - "guide" tags: - "theme" - "extending" --- = Extending Contentful 2020-05-12 Extending a Hugo theme is nothing new and a https://gohugo.io/content-management/sections/[few] http://hugocodex.org/add-ons/[places] provide a place for the most common extensions. In this post, I'll be listing a few personal recipes I've always used for extending a Hugo theme. Though this only applies specifically to Contentful and may need some tweaking when applying it other themes. == Customizing your `
` Let's start with the most basic and perhaps most useful customization: modifying the `. This is useful for adding your own CSS and JavaScript files, changing certain metadata, or adding icons. First, copy the `head` partial from the theme (`theme/contentful/layouts/partials/head.html`) to your own (`layouts/partials/head.html`). We're simply taking advantage of https://gohugo.io/templates/lookup-order/[Hugo's lookup order] where we've override the `head` partial with our own copy. Then, feel free to add your own (or others') scripts and stylesheets, https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/The_head_metadata_in_HTML[icons and other metadata], or whatever suitable things. In my case, I often use certain JavaScript libraries like https://www.mathjax.org/[MathJax] for mathematical typesetting, https://prismjs.com/[Prism] for syntax highlighting, and https://github.com/francoischalifour/medium-zoom/[medium-zoom] for interactive image zooms. Here's the modified code. (The example code is snipped for brevity.) ```go {{- /* MathJax */ -}} {{- /* Prism.js */ -}} ``` Since most of the JavaScript libraries used here are not really a requirement (except for MathJax for mathematical typesetting), I've set them to be loaded at the end of the page loading with https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script[`defer` attribute]. If you have an inline script, you can simply wrap it in an event listener for page loading (`window.addEventListener("load", your_function_goes_here)`). If you want document-specific libraries, you have to pass some raw HTML through the parser of the document. For Goldmark, the default Markdown parser starting https://gohugo.io/news/0.60.0-relnotes/[Hugo v0.60.0], blocks raw HTML by default and you can disable it by setting `markup.goldmark.renderer.unsafe` to `true`. For Blackfriday, it parses even the raw HTML just fine. Though, you have to set it as the default Markdown parser. For https://asciidoctor.org/[Asciidoctor], you can use https://asciidoctor.org/docs/user-manual/#passthroughs[passthroughs] to get raw HTML through. == Twitter cards This will add https://developer.twitter.com/en/docs/tweets/optimize-with-cards/guides/getting-started[Twitter cards] for your webpages. (Be sure to copy the `head` partial first in your own layout folder.) Thankfully, Hugo already has https://gohugo.io/templates/internal/#twitter-cards[an internal template for Twitter cards]. Simply add `{{- template "_internal/twitter_cards.html" . -}}` somewhere in your own copy. (For reference, https://github.com/gohugoio/hugo/blob/25a6b33693992e8c6d9c35bc1e781ce3e2bca4be/tpl/tplimpl/embedded/templates/twitter_cards.html[here's the source code for the internal template].) You could also roll your own Twitter cards but I recommend to modify the internal template instead fitting your specific needs. (Copy the internal template from the given link, create it as a partial in `layouts/partials/twitter_cards.html`, modify it, and insert the template with `{{- partial "twitter_cards.html" -}}`.) == Open Graph protocol Next up, we're implementing https://opengraphprotocol.org/[Open Graph protocol] for our webpages. Commonly used for making suitable format when sharing the content on certain sites like Facebook. (Be sure to copy the `head` partial first in your own layout folder.) Similar to Twitter cards, Hugo has https://gohugo.io/templates/internal/#open-graph[an internal template for this]. Simply add `{{- template "_internal/opengraph.html" . -}}` somewhere in your own copy. (For reference, https://github.com/gohugoio/hugo/blob/25a6b33693992e8c6d9c35bc1e781ce3e2bca4be/tpl/tplimpl/embedded/templates/opengraph.html[here's the source code for the internal template].) If you want more control and customized version of the output, I recommend to copy the internal template and create a partial (e.g., `layouts/partials/opengraph.html`) and modify it. == An archive page This will add an archive page similar to archive pages https://davidtranscend.com/archives/[like] https://lukesmith.xyz/blogindex.html[these]. ```go {{- define "main" -}}