Generate static HTML
pages from Markdown
files.
This project is inspired by boot.dev's Build a Static Site Generator course that builds the SSG in Python.
Currently, files in ./content
directory, ./static/images/rivendell.png
and ./static/index.css
files are copied from that course.
-
Place static files (images, icons, css, js, etc.) in
./static
directory. They will get copied to./public
directory with the same directory structure. -
Link all static files (css, js, favicon, site manifest, etc.) in
template.html
. -
Place website page contents in
Markdown
file format inside./content
directory. They will be used to generateHTML
pages that will be added to the./public
directory with the same directory structure. -
If you want to use different directories or different template file to generate pages, update
staticDir
,contentDir
,destinationDir
andtemplatePath
variables in ./cmd/main.go file. -
To generate pages and serve them to localhost, run from project's root:
./main.sh # or make run # or go run ./cmd
-
Project copies all files and subdirectories from
./static
directory to./public
directory. -
Reads all
Markdown
files from./content
directory and for each of them:-
Extracts page title from the first heading tag (
# some title
) encountered in the file; -
Replaces title placeholder (
{{ Title }}
) in template with extracted title; -
Creates root
<div>
HTMLNode
node to build HTML node tree; -
Splits markdown file contents into following blocks:
- paragraph
- heading
- code
- quote
- unordered_list
- ordered_list
-
Creates associated
HTMLNode
for each block and adds it to the node tree's root node's children:Block HTMLNode paragraph <p>
nodeheading <h1>
, ... ,<h6>
nodecode <code>
node contained inside<pre>
nodequote <blockquote>
nodeunordered_list <li>
nodes contained inside<ul>
nodeordered_list <li>
nodes contained inside<ol>
node -
Splits each block's text (except
code
block) into following inline text nodes:- text
- bold
- italic
- code
- link
- image
-
Creates associated
HTMLNode
s for eachTextNode
and adds them to the node tree under their parent node:TextNode HTMLNode text inline text node bold <b>
nodeitalic <i>
nodecode <code>
nodelink <a>
nodeimage <img>
node -
Generates
HTML
string by parsing previously createdHTMLNode
node tree; -
Replaces content placeholder (
{{ Content }}
) in template with generated html code; -
Writes final
HTML
string to the file in./public
directory with the same name and directory structure as its source file.
-
There are tests available for every module that doesn't interact with file system.
Tests can be run by:
./test.sh
# or
make test
# or
go clean -testcache && go test ./...