Building a website in Markdown with Pandoc
Styling with CSS
Styling should occur in a separate file, outside of the page. Any file you point to with the -H
option will be copied in, and that can include a CSS file as part of the final directory structure.
Usually, you will start by using an existing style. In this example, you can copy the Tufte CSS file into one of your sub-directories and add the link to a header file:
<link rel="stylesheet" href="Tufte/tufte.css"/>
Footer and Header
If you want your website to look tidy and professional, you might want the site name and URL to appear in a header or footer bar to let people know where they are regardless of which page they are on.
As mentioned previously, the options -B
(before the body) and -A
(after the body) let you create headers and footers. You could use these options to create a navigation bar at the top or a consistent footer for all pages. You can repeat these options as many times as you like, though the final result will suffer if you overdo things.
To create the header text you want, save the text in a file and specify the file name with the pandoc
command using the -B
option (Listing 2).
Listing 2
Specifying Headers and Footers
$ pandoc -t html\ -H head.html \ -s -o index.html \ -A footer.html \ -B toptext.html \ --metadata title="Kick Mundane Out" index.md \
Note that Listing 2 also brings in the CSS style sheet using the -H
parameter. In this case, the head.html
file only contains a link to the stylesheet.
Suppose I wish to add a copyright notice at the bottom. You can also imagine other things that might appear in a footer, such as social links and other legal information. The footer
id tag is common in CSS stylesheets (Listing 3).
Listing 3
footer.html Footer
footer.html <C> <div id="footer"> © 2021 Mats Tage Axelsson </div> <C>
A simple nav bar in the header could show the other pages on the site in a list (Listing 4).
Listing 4
toptext.html Header
<C> <div id="nav-bar"> <ul> <li>Home</li> <li>projects</li> <li>About</li> </ul> </div> <C>
Templates
When you convert to a new format, Pandoc will use a default template. Alternatively, you can create your own template. The easiest way to create a template is to write an existing template to your directory and edit it:
pandoc -D html > MyTemplate.tmpl
The simplest sites do not need templates. However, templates provide a means for incorporating variables and simple conditionals that add power and convenience to your web presence.
A template is a file in the final format that you seek – in this case, HTML files. The big difference between a template and an ordinary file is that templates have variables embedded. In short, variables are enclosed in dollar signs, and you assign values to those variables using your defaults file or command-line options.
For instance, you could use a metadata variable as a placeholder for a title. Then in the defaults file or as part of the pandoc
command, you could specify a title for the page:
--metadata title="Escape Mundane Life"
If the title shows up in the default template, it comes with conditions. Pandoc checks if title
is defined elsewhere, and if not, it puts it in (Listing 5).
Listing 5
Conditional Default Title
01 $if(title)$ 02 <header id="title-block-header"> 03 <h1 class="title">$title$</h1> 04 $if(subtitle)$ 05 <p class="subtitle">$subtitle$</p> 06 $endif$
As you can see, Listing 5 defines both the title and subtitle.
Some ready-made templates are available to help you add common features to your website. For instance, the pandoc-bootstrap
[3] template includes several elements that add value to your site. Bootstrap sets up CSS and connects to some JavaScript to give you a navigation bar and maybe a table of contents. You can also add an author name and a date.
In the code, you define where you want the variables to go:
$for(author-meta)$ <meta name="author" content="$author-meta$" /> $endfor$ $if(date-meta)$ <meta name="date" content="$date-meta$" /> $endif$
There are two ways of setting the values; one method is to add a YAML block [4] at the top of your document, in this case, index.md
.
The three dashes indicate the start and end of a block:
--- subtitle: "More creativity" author: "Mats Tage Axelsson" date: "2021-08-12" ---
The other way to set the variables is with the -V
parameter. For instance, note that the command in Listing 6 sets the date using the -V
variable.
Listing 6
Setting Variables with -V
pandoc index.md\ -o index.html\ -s\ -t html5+smart\ -V date="2021-08-15"\ -M subtitle="More Vitality"\ --template ../pandoc-bootstrap-adaptive-template/template.html\ -B toptext.html\ -A footer.html\ -c ../pandoc-bootstrap-adaptive-template/template.css\ --metadata title="Escape Mundane Life"
As you can see in Figure 2, the data is added to your web page.
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.