• Stars
    star
    149
  • Rank 241,110 (Top 5 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A radically simple, zero-configuration static site generator in JavaScript

tinyjam

A bare-bones, zero-configuration static site generator that deliberately has no features, an experiment in radical simplicity. Essentially a tiny, elegant glue between EJS templates and Markdown with freeform structure (enabling incremental adoption) and convenient defaults, written in under 120 lines of JavaScript.

Build Status Install Size Simply Awesome

Example

# source directory
β”œβ”€β”€ posts
β”‚   β”œβ”€β”€ 01.md
β”‚   β”œβ”€β”€ 02.md
β”‚   └── item.ejs
β”œβ”€β”€ _header.ejs
└── index.ejs

# output
β”œβ”€β”€ posts
β”‚   β”œβ”€β”€ 01.html
β”‚   └── 02.html
└── index.html

An example template:

<%- include('_header.ejs') %>

<% for (const [name, {date, title}] of Object.entries(posts)) { %>
    <h3><%= date.toDateString() %>: <a href="posts/<%= name %>.html"><%= title %></a></h3>
<% } %>

Browse the full example and see the generated website.

Documentation

Getting started

npx tinyjam source_dir output_dir

Tinyjam doesn't impose any folder structure, processing any data files (*.md and *.yml) and templates (*.ejs) it encounters and copying over anything else.

Data files

All *.md and *.yml files inside the working directory are interpreted as data, available for any templates all at once as JavaScript objects. For example, given the following folder structure:

β”œβ”€β”€ posts
β”‚   β”œβ”€β”€ 01.md
β”‚   β”œβ”€β”€ 02.md
β”œβ”€β”€ data.yml
└── about.md

A template in this folder will have the contents available as:

posts: {
  "01": {title: "First post", date: new Date("2020-02-20"), body: "Hello world"},
  "02": {title: "Second post", date: new Date("2020-02-21"), body: "Hello there"}
},
data: {author: "Vladimir Agafonkin"},
about: {body: "This is an awesome blog about me."}

Markdown is rendered according to the GitHub Flavored Markdown specification.

Templates

Tinyjam uses EJS (through yeahjs, a fast EJS subset), a templating system where you can use plain JavaScript, so it's both powerful and easy to learn. All *.ejs files it encounters are rendered with the collected data in the following way:

  • <filename>.ejs files are rendered as <filename>.html.
  • item.ejs has a special meaning: all data files in the same folder (e.g. <filename>.md) are rendered with this template as <filename>.html with the corresponding file's data.
  • Templates starting with _ (e.g. _header.ejs) are skipped (to be used as EJS includes).
  • Templates are rendered as html by default, but you can use other extensions, e.g. main.css.ejs will be rendered as main.css.

In addition to the collected data, templates have access to the following properties:

  • rootPath is a relative path to the root of the project β€” useful as a prefix for links in includes as they may be used on different nesting levels (e.g. <%= rootPath %>/index.css).
  • root references all of the project's data, which is useful for accessing data in includes or outside of the current template's folder.

Command line

Install with NPM to use tinyjam as a CLI: npm install -g tinyjam. Usage:

tinyjam source_dir [output_dir] [--breaks] [--smartypants] [--silent]
  • --breaks: Add single line breaks as <br> in Markdown.
  • --smartypants: Convert quotes, dashes and ellipses in Markdown to typographic equivalents.
  • --silent: Run silently (unless there's an error).

If output_dir is not provided, it's assumed equal to source_dir. This is useful for incrementally converting static sites without changing deployment folders.

Node.js API

import tinyjam from 'tinyjam';

tinyjam(sourceDir, outputDir, {
    log: false,         // log the progress (like in the CLI)
    breaks: false,      // Markdown: add single line breaks (like in GitHub comments)
    smartypants: false, // Markdown: convert quotes, dashes and ellipses to typographic equivalents
    highlight: null     // a code highlighting function: (code, lang) => html
});

Note that the project only supports Node v12.17+.

FAQ

Why build yet another static site generator?

I wanted to add some templating to my personal static websites to make them easier to maintain (e.g. my band's album page, which is pure HTML/CSS/JS), but never found a static site generator that would be simple and unobtrusive enough for my liking.

A tool I envisioned would not involve meticulous configuration, special folder structure, reading through hundreds of documentation pages, bringing in a ton of dependencies, or making you learn a new language. At the same time, it would be flexible enough to make multilingual websites without plugins and convoluted setup.

Ideally, I would just rename some html files to ejs, move some content to Markdown files, add light templating and be done with it. So I decided to build my own minimal tool for this, but will be happy if anyone else finds it useful.

Can you add <feature X>?

Sorry β€” probably not, unless it fits the concept of a minimal, zero-configuration tool.

How fast is Tinyjam?

Pretty fast. I didn't see a point in benchmarking because most of the time is spent parsing Markdown/YAML and rendering EJS anyway, but corresponding dependencies (marked, js-yaml, yeahjs) are very well optimized.

Why EJS for templating, and can I use another templating system?

EJS is also an extremely simple, minimal system, and it allows you to use plain JavaScript for templates, making it pretty powerful with almost no learning curve. To make it even faster, I crafted my own implementation (yeahjs). No plans to support other template engines.

How do I make a reactive single-page app with dynamic routing, hydration, bundle splitting and service worker caching?

There's no need for all that in a static website. If you do have a case for it, you'll need a different tool.

How do I make a multilingual website?

Tinyjam gives you freedom to approach this in many different ways, but here's an example:

en.ejs: <%- include('_content.ejs', {lang: 'en'}) %>
fr.ejs: <%- include('_content.ejs', {lang: 'fr'}) %>
_content.ejs: <%= content[lang].body %> (use either content/en.md or content/fr.md)

How do I add pagination?

At the moment, you can't. It's not a great UI pattern anyway β€” make an archive page with links to all content instead.

How do I do asset optimization, CSS preprocessing, TypeScript transpilation, etc.?

Do all the preprocessing in the source directory prior to running tinyjam.

How do I add code syntax highlighting?

Here's an example using the tinyjam API with highlight.js:

import tinyjam from 'tinyjam';
import {highlight} from 'highlight.js';

tinyjam(sourceDir, outputDir, {
    highlight: (code, lang) => highlight(lang, code).value
});

More Repositories

1

suncalc

A tiny JavaScript library for calculating sun/moon positions and phases.
JavaScript
3,005
star
2

rbush

RBush β€” a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles
JavaScript
2,374
star
3

simplify-js

High-performance JavaScript polyline simplification library
JavaScript
2,249
star
4

bullshit.js

A bookmarklet for translating marketing speak into human-readable text. πŸ’©
JavaScript
1,811
star
5

flatbush

A very fast static spatial index for 2D points and rectangles in JavaScript 🌱
JavaScript
1,378
star
6

simpleheat

A tiny JavaScript library for drawing heatmaps with Canvas
JavaScript
923
star
7

dead-simple-grid

Dead Simple Grid is a responsive CSS grid micro framework that is just that. Dead simple.
HTML
750
star
8

kdbush

A fast static index for 2D points
JavaScript
608
star
9

projects

A list of awesome open source projects Volodymyr Agafonkin is involved in.
398
star
10

tinyqueue

The smallest and simplest priority queue in JavaScript.
JavaScript
384
star
11

geokdbush

The fastest spatial index for geographic locations in JavaScript
JavaScript
332
star
12

road-orientation-map

A visualization of road orientations on an interactive map
JavaScript
296
star
13

robust-predicates

Fast robust predicates for computational geometry in JavaScript
JavaScript
290
star
14

rbush-knn

k-nearest neighbors search (KNN) for RBush
JavaScript
205
star
15

delaunator-rs

Fast 2D Delaunay triangulation in Rust. A port of Delaunator.
Rust
189
star
16

flatqueue

A very fast and simple JavaScript priority queue
JavaScript
134
star
17

quickselect

A fast selection algorithm in JavaScript.
JavaScript
82
star
18

seidel

[DEPRECATED] A JS polygon triangulation library
JavaScript
82
star
19

icomesh

Fast JavaScript icosphere mesh generation library for WebGL visualizations
JavaScript
51
star
20

bbtree

Self-balancing Binary Search Trees in JavaScript
JavaScript
49
star
21

yeahjs

A tiny, modern, fast EJS templating library
JavaScript
44
star
22

geoflatbush

Geographic kNN extension for Flatbush
JavaScript
43
star
23

kdbush.hpp

A fast static spatial index for 2D points in C++11
C++
32
star
24

worker-data-load

A test that shows the benefit of loading large amounts of data directly in a worker instead of a page.
JavaScript
32
star
25

Leaflet.TouchHover

A plugin for adding hover-like interaction to Leaflet maps on mobile devices
JavaScript
27
star
26

suncalc-go

SunCalc written in Go
Go
19
star
27

eslint-config-mourner

A strict ESLint config for my JavaScript projects
JavaScript
18
star
28

yeahml

A tiny subset of YAML for JavaScript
JavaScript
8
star
29

serenity-tm

Serenity is a light, minimal syntax highlighting theme for Sublime Text and Textmate.
8
star
30

pbf-split

Splits a Node stream of protocol buffer messages
JavaScript
7
star
31

hello-lib

Simple boilerplate for my small JS libraries.
JavaScript
7
star
32

color-metrics

JavaScript
6
star
33

agafonkin.com

My little personal page
HTML
5
star
34

fanny

A simple and fast multilayer feedforward neural network implementation in JS, made for learning purposes.
JavaScript
5
star
35

hain

Hain triangulation algorithm in JS (work in progress)
C++
4
star
36

mourner

2
star
37

mourner.github.com

Nothing here yet.
1
star