How to Develop a Hugo Theme Component
This article will demonstrate how to create a Hugo theme component by developing a caniuse shortcode.
Introduction
This article aims to outline the process of developing a component from scratch without delving too deeply into the concept and principles of components. For more information on Hugo theme components, please refer to the Contributing / Develop Theme Components page.
This case study is open-sourced on GitHub under the hugo-fixit organization.
Creating Component Skeleton
Creating a component is similar to creating a theme, and you can use the hugo new theme
command to create a new theme component.
|
|
The above command creates a folder named shortcode-caniuse
in the themes
directory, which has a complete Hugo theme directory structure.
Since we only need to develop a component that includes a shortcode, you can delete unnecessary files. The directory structure after deletion is as follows:
|
|
To be compatible with both Git submodule and Hugo Modules installation methods later on, you also need to initialize a Git repository and a go.mod
file:
|
|
Creating Shortcode
Create a file named caniuse.html
in the layouts/shortcodes
directory.
According to the usage instructions of The CanIUse Embed, write the shortcode content as follows:
|
|
Creating JS File
In order to change the theme color of the FixIt theme to black and light, we can also change the theme color of the shortcode by using JS.
Create a file named shortcode-caniuse.js
in the assets/js
directory and write the theme switch logic as follows:
|
|
Creating Partial
Create a file named shortcode-caniuse.html
in the layouts/partials/inject
directory.
Reference third-party plugins and the JS resources of the component itself, with the content as follows:
|
|
Publishing Component
Before publishing, update the LICENSE
, README.md
, and theme.toml
files, then commit to the remote repository.
How to Use
Install Component
The installation method is the same as installing a theme. There are several ways to install, choose one, for example, install through Hugo Modules:
|
|
Inject Partial
Inject the shortcode-caniuse.html
into the custom-assets
through the custom block opened by the FixIt theme in the layouts/partials/custom.html
file:
|
|
Use Shortcode
The caniuse
shortcode has the following named parameters:
- feature [required] (first positional parameter) Feature name
- past [optional] (second positional parameter) Show the past N versions that match the feature, range is
0 - 5
, default is2
- future [optional] (third positional parameter) Show the future N versions that match the feature, range is
0 - 3
, default is1
Click on the
#
next to a feature on thecaniuse.com
website, and thepathname
in the URL is thefeature
parameter.
Here is an example of usage:
|
|
The presentation effect is as follows:
|
|
References
- Can I use… Support tables for HTML5, CSS3, etc
- The CanIUse Embed — Add support tables to your site
- Contributing / Develop Theme Components
Acknowledgements
Related Content
- Develop Theme Components
- Develop FixIt Theme
- Advanced Usage
- Extended Shortcodes Overview
- Douyin Shortcode