There used to be a different blog here. But it wasn't very good, [code](https://github.com/jetstream0/Markup-to-HTML) *and* writing-wise. So I decided to rewrite everything. The old blog was an express server that looked for `.md` files in a directory, converted them to HTML, and served it. That worked mostly fine. However, the converter didn't support many Markdown features, and was pretty buggy. Using an express server also meant that the site wasn't static, and had limited options for hosting. Replit's free tier was pretty unreliable, and Render only allowed one free web service per account, which I was already using to run my [faucet](https://faucet.prussia.dev). I could've created a new Render account, but they recently started requiring credit card verification, which I didn't really want to do. Besides, making the blog static instead of relying on an express server wouldn't be that hard. A month before, I had already wrote a better (?) or at least more fully featured Markdown to HTML parser, [Makoto](https://github.com/jetstream0/Makoto-Markdown-to-HTML), so that was already one of the problems with the old blog solved. Anyways, once I decided to start completely rewriting the blog, I established some goals that I wanted the new blog to accomplish. ## Technical Goals - Static, so it can be deployed by eg, Github Pages or Cloudflare Pages - Built from scratch with no third-party dependencies (builtin modules like `path` and `fs` are ok of course, `makoto` is not third party, also doesn't count) - Use Typescript - No Javascript running client side - the web pages should be pure HTML and CSS - Style-wise, should be minimalistic, nothing fancy ## Non-Technical Goals - Make two things I can call "Ryuji" and "Saki" to go along with "Makoto" (those are the three main characters of one of the best manga series ever) - Move over some of the old blog posts (only the stuff I like), after rewriting them - Start writing stuff on the blog again, at least semi-regularly ## Code Hedgeblog (oh, that's what I'm calling it by the way) is made of three components: Makoto (Markdown to HTML parser), Ryuji (templating language), and Saki (build system). You can find the code on [Github](https://github.com/jetstream0/hedgeblog). ### Makoto Makoto is the Markdown-to-HTML parser, made with no dependencies. It was made around two months before Ryuji and Saki, and is meant to be more of a standalone thing. This is the sole npm dependency of the project. I `npm install`ed it instead of just copying the file over mostly because I published Makoto to npm and wanted to make sure it worked. Also, it has a different license, documentation and stuff. All the standard Markdown are supported (headers, bolds, italics, images, links, blockquotes, unordered lists, ordered lists, code, code blocks...), as well as superscripts and tables (although tables are probably buggy). Makoto also does some pretty neat stuff like passing on the language of the code block (if given) as a class in the resulting div: `code-`, or automatically add ids to headers, so they can have anchor links. It also has a very cool warnings feature, which isn't used in this project, but can be seen in action if you use the [Makoto Web Editor](https://makoto.prussia.dev). ### Ryuji Ryuji is a simple templating system that supports `if` statements, `for` loops, components, and inserting variables. It isn't quite as fully featured as Jinja/Nunjucks, but on the upside, Ryuji is around just 200 lines of code, and worked very well for my usecase. I think it's pretty cool. Here's a quick overview of the syntax: ```html [[ component:navbar ]]

You can insert variables. My favourite food is: [[ favourite_food ]]

And make sure the variable is truthy then do something.

[[ if:show_secrets ]] [[ endif ]]

Variables are by default sanitized so HTML/CSS/JS can't actually be executed, but you can disable this.

[[ html:html_from_database ]]
[[ for:members:member ]]

[[ member ]] is a proud member of our group!

[[ endfor ]] ``` After finishing writing Ryuji, and writing some tests to make sure it all worked, I realized that I needed a few features that were missing. There was no way to see the current index in a for loop, or the max index (length-1) of whatever variable we were looping over. This was needed for the tag links in the post. In addition, if statements only checked if the variable was truthy (not `false` or `0` or `""`), but I needed if statements making sure two variables were equal, as well as if statements making sure two variables were *not* equal. You can see this being used in the post's tags along with the new for loop features, as well as the "Next Post" link at the bottom of the post. So, I added those features. Let's take a look of these new features being used to show and link post tags. Formatting the code a little nicer, this is what it looks like: ``` [[ for:post.tags:tag:index:max ]] [[ tag ]][[ if:index:!max ]], [[ endif ]] [[ endfor ]] ``` Ok, in the first line (`for:post.tags:tag:index:max`), we are looping over the variable `post.tags`, and assigning each item in `post.tags` as the variable `tag`. That's nothing new, what's new is the `:index:max` portion. `index` is the index variable, starting at 0 and incrementing every loop, while `max` is the maximum index (the length of the variable to loop over - 1). If you look at the Ryuji code, now you can see that it is looping over the tags of the post, and creating a link for each tag. If the tag is *not* the last tag (`index` is not equal to `max`), we will also add a comma (and a space). Here's the equivalent python code, if it helps: ```python html = "" max = len(post.tags)-1 index = 0 for tag in post.tags: html += ""+tag+"" if index != max: html += "," index += 1 ``` While Ryuji is meant for HTML, there is no reason it can't be used for other formats. Take a look at the [docs](/posts/ryuji-docs) for Ryuji. ### Saki Saki is the build system that uses Ryuji templates to generate all the HTML files, and then outputs everything (including static files, of course) to the `build` directory. Even more simple than Ryuji, it is just around 70 lines of code. Here are the very short [docs](/posts/saki-docs) for Saki. ## Putting It All Together When building, the program (`index.ts`) reads `/posts/_metadata.json` and passes all the post metadata information to the `templates/index.html` template, which is the home page! Then, it renders all the posts with the `templates/post.html` template and outputs to `/posts/*`. Next, it looks again at the post metadata and gets all the tags used. Once it has all the tags, pages for the tags are generated at `/tags/*`. That page lists all the posts with that tag. Scroll up and try it! It also outputs the `static` directory, as well, static files. > ### Tip: Serve Without The `.html` > Say we want to serve a HTML file at `/posts/example` instead of `/posts/example.html`. Just create a directory called `example` and put `example.html` inside it, renaming it `index.html`. > Basically, `/posts/example.html` becomes `/posts/example/index.html`, and now the HTML file is served at `/posts/example`. You probably already knew that, but if you didn't now you know ^\[citation needed]^. ## Buttons Without Javascript If you think back to around 950 words ago, you may recall that this site has zero frontend Javascript (or WebAssembly, as cool as it is). If you didn't recall that, I just reminded you. I will be sending an invoice later. So how does the "Show MD", "Fancy Title", and Dark/Light theme toggle work? The key thing here is that all three of these are checkboxes. Yes, even the Dark/Light theme toggle is a checkbox. That toggle hides its checkbox and takes advantage of the fact you can click on a `