base markdown book with code block syntax highlighting and fully embedded assets

This commit is contained in:
2026-01-07 18:29:07 +01:00
commit 1c4e99b138
12 changed files with 3576 additions and 0 deletions

59
src/codeblocks.rs Normal file
View File

@@ -0,0 +1,59 @@
use pulldown_cmark::{CodeBlockKind, CowStr, Event, Parser as MarkdownParser, Tag, TagEnd};
use syntect::html::highlighted_html_for_string;
use crate::{SYNTAX_SET, THEME_SET};
// I found this at <https://github.com/pulldown-cmark/pulldown-cmark/issues/167#issuecomment-3700787117>
pub struct CodeblockRenderer<'a> {
inner: MarkdownParser<'a>,
}
impl<'a> CodeblockRenderer<'a> {
pub fn new(inner: MarkdownParser<'a>) -> Self {
Self { inner }
}
}
impl<'a> Iterator for CodeblockRenderer<'a> {
type Item = Event<'a>;
fn next(&mut self) -> Option<Self::Item> {
let event = self.inner.next()?;
// Intercept CodeBlock starts
let Event::Start(Tag::CodeBlock(kind)) = event else {
return Some(event);
};
let mut code_content = String::new();
while let Some(inner_event) = self.inner.next() {
match inner_event {
Event::End(TagEnd::CodeBlock) => break,
Event::Text(code) => code_content.push_str(&code),
_ => {}
}
}
let lang = match kind {
CodeBlockKind::Indented => "text",
CodeBlockKind::Fenced(ref language) => language.as_ref(),
};
let rendered_html = render_code_to_html(&code_content, lang);
Some(Event::Html(CowStr::Boxed(rendered_html.into_boxed_str())))
}
}
pub fn render_code_to_html(code: &str, lang: &str) -> String {
let syntax = SYNTAX_SET
.find_syntax_by_token(lang)
.unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text());
let theme = &THEME_SET.themes["Catppuccin Macchiato"];
highlighted_html_for_string(code, &SYNTAX_SET, syntax, theme)
.unwrap_or_else(|_| format!("<pre><code>{}</code></pre>", code))
}

200
src/main.rs Normal file
View File

@@ -0,0 +1,200 @@
use axum::{
Router,
extract::{Path, State},
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::get,
};
use clap::{Parser, Subcommand};
use lazy_static::lazy_static;
use pulldown_cmark::{Options, Parser as MarkdownParser, html};
use std::sync::Arc;
use std::{io::Cursor, path::PathBuf};
use syntect::{highlighting::ThemeSet, parsing::SyntaxSet};
use tera::{Context, Tera};
mod codeblocks;
use codeblocks::*;
lazy_static! {
pub static ref TEMPLATES: Tera = {
let mut tera = Tera::default();
tera.add_raw_templates(vec![
("page.html", include_str!("../templates/page.html")),
("style.css", include_str!("../templates/style.css")),
])
.unwrap();
tera
};
pub static ref SYNTAX_SET: SyntaxSet = SyntaxSet::load_defaults_newlines();
pub static ref THEME_SET: ThemeSet = {
let mut set = ThemeSet::load_defaults();
let theme_bytes = include_bytes!("../themes/Catppuccin-Macchiato.tmTheme");
let mut cursor = Cursor::new(theme_bytes);
match syntect::highlighting::ThemeSet::load_from_reader(&mut cursor) {
Ok(theme) => {
set.themes.insert("Catppuccin Macchiato".to_string(), theme);
}
Err(e) => {
tracing::error!("Failed to load embedded theme: {}", e);
}
}
set
};
}
#[derive(Parser)]
#[command(author, version, about = "A simple markdown book server")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Serve the markdown files in a directory
Serve {
/// Path to the directory containing SUMMARY.md
path: PathBuf,
/// Port to listen on
#[arg(short, long, default_value = "3456")]
port: u16,
},
}
struct AppState {
docs_dir: PathBuf,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
lazy_static::initialize(&TEMPLATES);
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
let cli = Cli::parse();
match cli.command {
Commands::Serve { path, port } => {
let abs_path = std::fs::canonicalize(&path)?;
let shared_state = Arc::new(AppState { docs_dir: abs_path });
let app = Router::new()
.route("/", get(render_summary))
.route("/{page}", get(render_page))
.route("/style.css", get(serve_css))
.with_state(shared_state);
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)).await?;
tracing::info!("Listening on http://localhost:{}", port);
axum::serve(listener, app).await?;
}
}
Ok(())
}
async fn render_summary(State(state): State<Arc<AppState>>) -> impl IntoResponse {
render_md_file("SUMMARY.md", state).await
}
async fn render_page(
State(state): State<Arc<AppState>>,
Path(page): Path<String>,
) -> impl IntoResponse {
let filename = if page.ends_with(".md") {
page
} else {
format!("{}.md", page)
};
render_md_file(&filename, state).await
}
async fn serve_css() -> impl IntoResponse {
match TEMPLATES.render("style.css", &tera::Context::new()) {
Ok(css) => Response::builder()
.header("content-type", "text/css")
.body(css.into())
.unwrap(),
Err(_) => (StatusCode::NOT_FOUND, "CSS not found").into_response(),
}
}
async fn render_md_file(filename: &str, state: Arc<AppState>) -> Html<String> {
let file_path = state.docs_dir.join(filename);
let content = match tokio::fs::read_to_string(&file_path).await {
Ok(c) => c,
Err(_) => return Html("<h1>404</h1><p>Page not found</p>".to_string()),
};
let mut options = Options::empty();
options.insert(Options::ENABLE_TABLES);
options.insert(Options::ENABLE_FOOTNOTES);
options.insert(Options::ENABLE_STRIKETHROUGH);
options.insert(Options::ENABLE_TASKLISTS);
let parser = MarkdownParser::new_ext(&content, options);
let renderer = CodeblockRenderer::new(parser);
let mut html_output = String::new();
html::push_html(&mut html_output, renderer);
let (prev_page, next_page) = get_nav_links(&state.docs_dir, filename);
let mut context = Context::new();
context.insert("title", filename);
context.insert("content", &html_output);
context.insert("prev_page", &prev_page);
context.insert("next_page", &next_page);
match TEMPLATES.render("page.html", &context) {
Ok(rendered) => Html(rendered),
Err(e) => Html(format!("<h1>Template Error</h1><pre>{}</pre>", e)),
}
}
fn get_nav_links(dir: &PathBuf, current_file: &str) -> (Option<String>, Option<String>) {
let mut files: Vec<String> = std::fs::read_dir(dir)
.unwrap()
.filter_map(|entry| {
let path = entry.ok()?.path();
if path.extension()? == "md" && path.file_name()? != "SUMMARY.md" {
Some(path.file_name()?.to_str()?.to_string())
} else {
None
}
})
.collect();
files.sort();
if current_file == "SUMMARY.md" {
return (None, files.first().cloned());
}
let pos = files.iter().position(|f| f == current_file);
match pos {
Some(i) => {
let prev = if i == 0 {
// If first page, point back to summary page
Some(".".to_string())
} else {
// Otherwise point to the previous file in the list
files.get(i - 1).cloned()
};
let next = files.get(i + 1).cloned();
(prev, next)
}
None => (None, None),
}
}