⫶ On this page

Hacklab Interface

Hacklab Interface generates the static website for hacklab.nl. Images and text are compressed and combined, then synced to the server. The layout is styled with Tailwind CSS. Content is stored in Markdown files with frontmatter metadata.

Requirements

  • Maudit, a static site builder library
  • Rust
  • Tailwind CSS

Setup

Rust project

cargo new mywebsite
cd mywebsite
cargo add maudit
cargo add maud
cargo add serde

Tailwind CSS

npm add @tailwindcss/cli tailwindcss

Project structure

mkdir assets
mkdir -p content/articles
mkdir static

main.rs

use maud::{DOCTYPE, Markup, PreEscaped, html};
use maudit::assets::StyleOptions;
use maudit::content::markdown_entry;
use maudit::route::prelude::*;
use maudit::{
    AssetsOptions, BuildOptions, BuildOutput, PrefetchOptions, PrefetchStrategy,
    content::glob_markdown, content_sources, coronate, routes,
};

pub fn layout(content: Markup, hero: Option<Markup>, ctx: &mut PageContext) -> Markup {
    ctx.assets
        .include_style_with_options("assets/hli.css", StyleOptions { tailwind: true })
        .unwrap();
    html! {
        (DOCTYPE)
        html lang="en" {
            head {
                meta charset="utf-8";
                meta name="viewport" content="width=device-width, initial-scale=1";
                title { "hacklab" }
            }
            body.bg-blue-950.text-blue-100 {
                (hero.unwrap_or_default())
                div { (content) }
            }
        }
    }
}

#[markdown_entry]
pub struct ArticleContent {
    pub title: String,
    pub description: String,
    pub category: String,
}

#[route("/")]
pub struct Index;

impl Route for Index {
    fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> {
        let articles = ctx.content::<ArticleContent>("articles");
        let content = html! {
            ul {
                @for entry in articles.entries() {
                    @let article = entry.data(ctx);
                    @let url = &Article.url(ArticleParams { article: entry.id.clone() });
                    li {
                        a href=(url) { (article.title) }
                        p { (article.description) }
                    }
                }
            }
        };
        layout(content, None, ctx)
    }
}

#[route("/articles/[article]")]
pub struct Article;

#[derive(Params, Clone)]
pub struct ArticleParams {
    pub article: String,
}

impl Route<ArticleParams> for Article {
    fn pages(&self, ctx: &mut DynamicRouteContext) -> Pages<ArticleParams> {
        let articles = ctx.content::<ArticleContent>("articles");
        articles.into_pages(|entry| {
            Page::from_params(ArticleParams { article: entry.id.clone() })
        })
    }

    fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> {
        let params = ctx.params::<ArticleParams>();
        let articles = ctx.content::<ArticleContent>("articles");
        let article = articles.get_entry(&params.article);
        let content = html! {
            div.max-w-7xl.mx-auto.px-6.py-6 {
                h3.text-3xl.md:text-4xl.font-black.uppercase.mb-8 {
                    (article.data(ctx).title)
                }
                section.prose.prose-lg.max-w-none {
                    (PreEscaped(article.render(ctx)))
                }
            }
        };
        layout(content, None, ctx)
    }
}

fn main() -> Result<BuildOutput, Box<dyn std::error::Error>> {
    coronate(
        routes![Index, Article],
        content_sources![
            "articles" => glob_markdown::<ArticleContent>("content/articles/*.md")
        ],
        BuildOptions {
            assets: AssetsOptions {
                tailwind_binary_path: "./node_modules/.bin/tailwindcss".into(),
                ..Default::default()
            },
            prefetch: PrefetchOptions {
                strategy: PrefetchStrategy::None,
                ..Default::default()
            },
            ..Default::default()
        },
    )
}

assets/hli.css

@import "tailwindcss";

.prose ul {
  list-style-type: disc;
  margin-top: 1em;
  margin-bottom: 1em;
  padding-left: 1.5em;
}

.prose li {
  margin-top: 0.5em;
  margin-bottom: 0.5em;
}

.prose a {
  text-decoration-line: underline;
}

.prose p {
  margin-top: 1em;
  margin-bottom: 1em;
  line-height: 1.75;
}

Content

Articles are Markdown files with frontmatter in content/articles/.

---
title: Hacklab Interface
description: Hacklab Interface is a static website written in Rust and Markdown build with maudit.
category: web
---

## Hello

Development

The first build is slow while Cargo downloads and compiles dependencies.

maudit dev

Deploy

rm -rf dist
maudit build
rsync -vr dist/ you@server:/var/www/mysite