krik 0.1.21

A fast static site generator written in Rust with internationalization, theming, and modern web features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
---
title: "Documentation"
toc: true
pdf: true
---

# Krik Documentation

This page provides comprehensive documentation for using Krik, the static site
generator.

## Quick Start

### Installation

#### From Crates.io (Recommended)

```bash
# Install globally from crates.io
cargo install krik

# No additional setup required - themes and content are embedded!
```

#### From Source

```bash
# Clone the repository
git clone <repository-url>
cd krik

# Build the project
cargo build --release

# The executable will be at target/release/kk
```

### Getting Started

#### Initialize a New Site

Create a new Krik site with sample content and default theme:

```bash
kk init my-blog             # Create new site in 'my-blog' directory
cd my-blog
kk server                   # Start development server
```

Or initialize in the current directory:

```bash
kk init                     # Initialize in current directory
kk init --force             # Overwrite existing files
```

#### Create Content

Create new blog posts and pages quickly:

```bash
kk post "My Great Post"           # Create new blog post
kk post                           # Create with default title "New post"
kk post "Custom Post" -f my-slug  # Custom filename

kk page "About Us"                # Create new page
kk page                           # Create with default title "New page"
kk page "Contact" -f contact      # Custom filename
```

#### Development Server

Start the development server with live reload:

```bash
kk server                    # Start on port 3000 with live reload
kk server --port 8080        # Custom port
kk server --no-live-reload   # Disable live reload (useful for mobile devices)
```

#### Generate Static Site

```bash
# Generate site from current directory
kk

# Generate with custom paths
kk --input ./content --output ./_site --theme ./themes/custom
```

## Linting Content

Use the linter to validate front matter, filenames, and conventions:

```bash
kk lint                    # Lint default content directory
kk lint --input content    # Lint a specific directory
kk lint --strict           # Treat warnings as errors
```

### What it checks

- Title: required and non-empty
- Language codes: must match filename suffix (e.g., `hello.it.md``it`)
- Slugs: filename stem must be slug-like (lowercase, numbers, hyphens)
- Layout: warns on unrecognized values and directory/layout mismatches
- Date: warns if missing for posts; warns if > 1 year in the future
- Tags: array of non-empty strings; warns when tags are not slug-like
- TOC: warns if `toc` is not a boolean
- Duplicate slugs: within the same directory and language
- Duplicate titles: warns within the same directory and language

The command exits non-zero on errors. In `--strict` mode, warnings are also
treated as errors.

## Content Organization

### Directory Structure

```
content/
├── site.toml           # Site configuration
├── posts/              # Blog posts
│   ├── hello.md
│   └── hello.es.md     # Spanish translation
├── pages/              # Static pages
│   └── about.md
└── images/             # Static assets
    └── logo.png
```

### Site Configuration

Create a `site.toml` file in your content directory:

```toml
title = "My Website"
base_url = "https://example.com"  # Optional, for feed generation
```

**Configuration Options:**

- `title`: Site title (displayed in navigation and feeds)
- `base_url`: Base URL for proper Atom feed link resolution

## Front Matter

All markdown files can include YAML front matter for metadata:

```yaml
---
title: "Page Title"
date: 2024-01-15T10:30:00Z
layout: post
tags: ["rust", "web", "static-site"]
toc: true
draft: false
custom_field: "custom value"
---
# Your content here
```

### Front Matter Fields

| Field         | Type     | Description                                 |
| ------------- | -------- | ------------------------------------------- |
| `title`       | String   | Page/post title                             |
| `date`        | ISO 8601 | Publication date (falls back to file mtime) |
| `layout`      | String   | Template to use (`post`, `page`, or custom) |
| `tags`        | Array    | Tags for categorization                     |
| `toc`         | Boolean  | Enable table of contents generation         |
| `pdf`         | Boolean  | Enable PDF generation                       |
| `draft`       | Boolean  | Skip file from processing when `true`       |
| Custom fields | Any      | Additional metadata accessible in templates |

## Templates and Layouts

### Automatic Template Selection

- **Posts**: Files in `content/posts/` use the `post` template
- **Pages**: Files in `content/pages/` or root use the `page` template
- **Index**: Homepage uses the `index` template

### Manual Template Override

Use the `layout` field in front matter:

```yaml
---
title: "Special Page"
layout: custom
---
```

### Template Features

- **Post Template**: Tags, "Back to Home" link, language selector, scroll-to-top
- **Page Template**: Clean layout, language selector (if translations
  available), scroll-to-top
- **Index Template**: Post listing, theme toggle, scroll-to-top

## Internationalization (i18n)

### Creating Translations

Add language codes to filenames:

- `hello.md` - Default language (English)
- `hello.it.md` - Italian translation
- `hello.es.md` - Spanish translation
- `hello.fr.md` - French translation

### Supported Languages

Krik maintains an internal language map used for validation and display names.
Out of the box it supports: `en`, `it`, `es`, `fr`, `de`, `pt`, `ja`, `zh`,
`ru`, `ar`. Language names in the UI are resolved from this map.

### Language Navigation & Index Selection

- Pages with translations automatically show a language selector dropdown.
- The posts index shows one entry per post base name. If multiple language
  versions exist, the index prefers the default language. If only a non-default
  language exists (e.g., only `foo.it.md`), that translation is included.

## Advanced Features

### Table of Contents

Enable TOC generation with `toc: true` in front matter:

```yaml
---
title: "Long Article"
toc: true
---
```

**Features:**

- Automatic ID generation for headings (AST-based)
- Hierarchical structure preservation
- Clickable navigation links
- Smooth scrolling to sections

### Footnotes

Krik supports enhanced footnotes with bidirectional navigation:

```markdown
This has a footnote[^1].

[^1]: This is the footnote content.
```

**Features:**

- Click footnote numbers to jump to definitions
- Click return arrows (↩) to return to text
- Smooth scrolling for all footnote navigation

### Scroll-to-Top Button

Automatically appears on longer pages with smart visibility:

- Hidden by default until scrolling >300px
- Fixed position in bottom-right corner
- Smooth scrolling animation
- Theme-aware styling
- Mobile-optimized size and positioning

### SEO and Discovery

Krik automatically generates SEO-optimized files for search engines and web
crawlers:

#### Atom Feed Generation

Automatically generates an RFC 4287 compliant Atom feed at `feed.xml`:

**Features:**

- Only includes posts (content with `post` template)
- Limited to 20 most recent posts
- Full HTML content with proper XML escaping
- xml:base support when `base_url` is configured
- Proper metadata (titles, dates, IDs)

#### XML Sitemap Generation

Automatically generates a comprehensive XML sitemap at `sitemap.xml`:

**Features:**

- XML Schema validation with proper namespaces
- Multilingual support with `<xhtml:link>` alternate language declarations
- One entry per content piece with canonical URLs (prefers English)
- Proper priority and change frequency settings
- Excludes draft content automatically

#### robots.txt Generation

Automatically generates SEO-optimized robots.txt:

**Features:**

- References sitemap.xml location
- Allows all content by default (good for most static sites)
- Blocks access to system files and build directories
- Includes bot-specific rules for major search engines
- Blocks known problematic crawlers/scrapers
- Includes polite crawl delay settings

## PDF Generation

Krik supports automatic PDF generation for your content using pandoc and the
typst engine. This feature allows you to provide downloadable PDF versions of
your posts and pages.

### Prerequisites

Before using PDF generation, you need to install the required external tools:

#### Install pandoc

**macOS:**

```bash
brew install pandoc
```

**Ubuntu/Debian:**

```bash
sudo apt install pandoc
```

**Windows:** Download from [pandoc.org](https://pandoc.org/installing.html) or
use:

```bash
winget install pandoc
```

#### Install typst

**All platforms:**

```bash
cargo install typst-cli
```

**Alternative for macOS:**

```bash
brew install typst
```

### Enabling PDF Generation

To enable PDF generation for a document, add `pdf: true` to your front matter:

```yaml
---
title: "My Article"
date: 2025-01-15T10:30:00Z
pdf: true
---
# My Article

This content will be available as both HTML and PDF.
```

### Features

#### Automatic PDF Links

When PDF generation is enabled, Krik automatically adds a PDF download link (📄)
to your HTML templates, positioned next to the theme switcher. The link appears
only for documents with `pdf: true` in their front matter.

#### Language-Aware Filenames

PDF files are generated with language-aware filenames:

- `welcome.md``welcome.pdf`
- `welcome.it.md``welcome.it.pdf`
- `about.fr.md``about.fr.pdf`

#### Conditional Appendix

When your site has a `base_url` configured in `site.toml`, PDFs include an
appendix with:

- **Download URL**: Link to the original web version
- **Generation timestamp**: When the PDF was created
- **Multi-language support**: Appendix text translated based on document
  language

Supported appendix languages: English, Italian, Spanish, French, German,
Portuguese, Japanese, Chinese, Russian, Arabic.

#### Image Path Resolution

Krik automatically resolves relative image paths in PDFs, handling complex
patterns like:

- `![Image](../images/photo.jpg)`
- `![Diagram](../../assets/diagrams/flow.png)`
- `![Logo](./logo.svg)`

### Configuration

PDF generation works with your existing site configuration. Make sure your
`site.toml` includes:

```toml
title = "My Site"
base_url = "https://mysite.com"  # Optional: enables PDF appendix with download URL
```

### Development Workflow

```bash
# 1. Add pdf: true to your document
echo '---
title: "My PDF Post"
pdf: true
---

# My PDF Post

Content goes here...' > content/posts/my-post.md

# 2. Generate site (requires pandoc and typst)
kk

# 3. Check generated files
ls _site/posts/
# Output: my-post.html, my-post.pdf
```

### Troubleshooting

**Error: "pandoc not found"**

- Install pandoc using the instructions above
- Ensure pandoc is in your system PATH

**Error: "typst not found"**

- Install typst-cli: `cargo install typst-cli`
- Verify installation: `typst --version`

**PDF links not appearing in HTML**

- Ensure `pdf: true` is set in the document's front matter
- Rebuild your site with `kk`
- Check that the PDF file was generated in the output directory

**Images missing in PDF**

- Ensure image paths are relative to the content source directory
- Use forward slashes in paths even on Windows
- Verify image files exist at the specified paths

## ⚠️ Error Handling

Krik uses typed errors for clear diagnostics and actionable messages.

### Error Types

- `ConfigError`: configuration files and parsing
- `IoError`: file and directory operations
- `MarkdownError`: front matter and markdown parsing
- `TemplateError`: template compilation and rendering (includes template name)
- `ThemeError`: theme loading and asset handling
- `ServerError`: development server issues
- `ContentError`: content creation and validation failures
- `GenerationError`: site generation pipeline failures

### Exit Codes

The CLI maps error types to exit codes:

- Config (2), I/O (3), Markdown (4), Template (5), Theme (6), Server (7),
  Content (8), Generation (9)

### Tips

- Use `-v/--verbose` for detailed logs
- Error messages include paths/template names for faster troubleshooting

## Theme System

### Light/Dark Mode

**Automatic Detection:**

- Detects OS theme preference via CSS `prefers-color-scheme`
- Supports all major platforms (Windows, macOS, Linux, iOS, Android)
- Real-time updates when OS theme changes

**Manual Toggle:**

- Theme button (🌙/☀️) in top navigation
- Saves preference to localStorage
- Overrides automatic detection
- Smooth 0.3s transitions

### Customization

The theme uses CSS custom properties for easy customization:

```css
:root {
  --bg-color: #ffffff;
  --text-color: #333333;
  --link-color: #0066cc;
  /* ... more variables */
}
```

## Command Line Reference

### Main Commands

```bash
kk [OPTIONS]              # Generate static site
kk init [DIR]             # Initialize new site
kk post [TITLE]           # Create new blog post
kk page [TITLE]           # Create new page
kk server [OPTIONS]       # Start development server
```

### Global Options

| Option               | Description      | Default          |
| -------------------- | ---------------- | ---------------- |
| `-i, --input <DIR>`  | Input directory  | `content`        |
| `-o, --output <DIR>` | Output directory | `_site`          |
| `-t, --theme <DIR>`  | Theme directory  | `themes/default` |
| `-h, --help`         | Show help        |                  |
| `-V, --version`      | Show version     |                  |

### Init Command

```bash
kk init [DIR] [OPTIONS]
```

| Option        | Description                                          |
| ------------- | ---------------------------------------------------- |
| `[DIR]`       | Directory to initialize (default: current directory) |
| `-f, --force` | Overwrite existing files                             |

### Post/Page Commands

```bash
kk post [TITLE] [OPTIONS]
kk page [TITLE] [OPTIONS]
```

| Option                  | Description                   | Default                 |
| ----------------------- | ----------------------------- | ----------------------- |
| `[TITLE]`               | Content title                 | "New post" / "New page" |
| `-f, --filename <NAME>` | Custom filename (without .md) | Generated from title    |
| `--content-dir <DIR>`   | Content directory path        | `content`               |

### Server Command

```bash
kk server [OPTIONS]
```

| Option               | Description         | Default             |
| -------------------- | ------------------- | ------------------- |
| `-i, --input <DIR>`  | Input directory     | `content`           |
| `-o, --output <DIR>` | Output directory    | `_site`             |
| `-t, --theme <DIR>`  | Theme directory     | `themes/default`    |
| `-p, --port <PORT>`  | Server port         | `3000`              |
| `--no-live-reload`   | Disable live reload | Live reload enabled |

## Generated Output

Krik generates a complete static site with:

- **HTML files**: Preserving directory structure
- **Language variants**: `file.lang.html` for translations
- **Static assets**: Images, CSS, etc. copied as-is
- **Theme assets**: CSS and JavaScript from theme directory
- **Atom feed**: `feed.xml` with proper link resolution
- **XML sitemap**: `sitemap.xml` with multilingual support
- **robots.txt**: SEO-optimized with sitemap reference
- **Navigation**: TOCs, footnote links, scroll-to-top buttons

### Example Output Structure

```
_site/
├── index.html          # Homepage
├── feed.xml           # Atom feed
├── sitemap.xml        # XML sitemap
├── robots.txt         # SEO robots file
├── assets/            # Theme assets
│   ├── css/main.css
│   └── js/main.js
├── posts/
│   ├── hello.html
│   └── hello.es.html  # Translation
├── pages/
│   └── about.html
└── images/
    └── logo.png       # Static assets
```

## Best Practices

### Content Organization

- Use `posts/` for blog entries and time-sensitive content
- Use `pages/` for static pages like About, Contact, etc.
- Keep assets organized in subdirectories
- Use consistent naming conventions for translations

### Front Matter

- Always include a `title` for better navigation
- Use `date` for posts to ensure proper chronological ordering
- Add `tags` to posts for better categorization
- Enable `toc` for longer articles with multiple sections

### Performance

- Optimize images before adding to content
- Use appropriate image formats (WebP when possible)
- Keep individual posts/pages to reasonable lengths
- Use drafts (`draft: true`) for work-in-progress content

### Accessibility

- Use proper heading hierarchy (H1 → H2 → H3)
- Include alt text for images
- Ensure good color contrast in custom themes
- Test with keyboard navigation

## Deployment

### GitHub Pages

You can automatically deploy your Krik site to GitHub Pages using GitHub
Actions. This workflow will build and deploy your site whenever you push to the
main branch.

#### Setup Steps

1. **Create the workflow file**: Add
   [.github/workflows/build-and-deploy.yml].github/workflows/build-and-deploy.yml
   to your repository.

2. **Enable GitHub Pages**:
   - Go to your repository settings
   - Navigate to "Pages" section
   - Under "Source", select "Deploy from a branch"
   - Choose the `gh-pages` branch
   - Select "/ (root)" as the folder
   - Click "Save"

3. **Configure your site**: Ensure your content is in the `content/` directory
   with proper structure

4. **Deploy**: Push to your main branch to trigger the deployment

#### What the Workflow Does

The GitHub Actions workflow automatically:

- **Installs dependencies**: Sets up Rust toolchain and installs Krik from
  crates.io
- **Generates the site**: Runs `kk` to build your static site
- **Creates gh-pages branch**: Sets up the deployment branch if it doesn't exist
- **Deploys files**: Copies generated files to the gh-pages branch
- **Adds .nojekyll**: Prevents GitHub from processing files with Jekyll
- **Pushes changes**: Commits and pushes the generated site

#### Workflow Features

- **Automatic deployment**: Triggers on every push to main branch
- **Manual trigger**: Can be run manually via GitHub Actions interface
- **Branch management**: Handles both new and existing gh-pages branches
- **Clean deployment**: Removes old files before deploying new ones
- **Skip empty deployments**: Only commits when there are actual changes

#### Repository Structure

Your repository should look like this:

```
your-repository/
├── .github/
│   └── workflows/
│       └── build-and-deploy.yml    # Deployment workflow
├── content/                        # Your Krik content
│   ├── site.toml
│   ├── posts/
│   │   └── *.md
│   └── pages/
│       └── *.md
└── README.md
```

After the first successful deployment, your site will be available at:
`https://yourusername.github.io/your-repository-name/`

---

This documentation covers all major features of Krik. For more examples, check
out the other posts and pages in this demo site!