From 1e1349fd0bac9822438e74deb5769a0b568a338f Mon Sep 17 00:00:00 2001 From: jetstream0 <49297268+jetstream0@users.noreply.github.com> Date: Tue, 1 Aug 2023 22:25:48 -0700 Subject: [PATCH] add starting posts and gh workflow --- .github/workflows/main.yaml | 44 +++++++++ .gitignore | 3 +- posts/_metadata.json | 36 ++++--- posts/another_example.md | 14 --- posts/example.md | 19 ---- posts/meta.md | 183 ++++++++++++++++++++++++++++++++++++ posts/ryuji_docs.md | 1 + posts/saki_docs.md | 1 + 8 files changed, 253 insertions(+), 48 deletions(-) create mode 100644 .github/workflows/main.yaml delete mode 100644 posts/another_example.md delete mode 100644 posts/example.md create mode 100644 posts/meta.md create mode 100644 posts/ryuji_docs.md create mode 100644 posts/saki_docs.md diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml new file mode 100644 index 0000000..c43d480 --- /dev/null +++ b/.github/workflows/main.yaml @@ -0,0 +1,44 @@ +name: Build Saki and Deploy to Github Pages + +on: + push: + branches: ["gh-pages"] + workflow_dispatch: + +permissions: + contents: read + pages: write + id-token: write + +concurrency: + group: "pages" + cancel-in-progress: true + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - uses: actions/configure-pages@v1 + id: pages + - uses: actions/setup-node@v3 + with: + node-version: 18 + cache: 'npm' + - run: npm ci + - run: npm run build + - name: Upload artifact + uses: actions/upload-pages-artifact@v1 + with: + path: ./build + + deploy: + runs-on: ubuntu-latest + needs: build + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + steps: + - uses: actions/deploy-pages@v1 + id: deployment \ No newline at end of file diff --git a/.gitignore b/.gitignore index df2e8ca..44aab12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules build *.js -!static/*.js \ No newline at end of file +!static/*.js +/posts/_draft_*.md \ No newline at end of file diff --git a/posts/_metadata.json b/posts/_metadata.json index 744be47..92dced5 100644 --- a/posts/_metadata.json +++ b/posts/_metadata.json @@ -1,18 +1,26 @@ { - "example": { - "title": "Example!", - "slug": "example", - "filename": "example", - "date": "30/12/1999", - "author": "Prussia", - "tags": ["example", "fake", "please remember to remove"] + "meta": { + "title": "meta", + "slug": "meta", + "filename": "meta", + "date": "01/08/2023", + "author": "jetstream0/Prussia", + "tags": ["code", "project", "web", "markdown"] }, - "another_example": { - "title": "Another Example!", - "slug": "another-example", - "filename": "another_example", - "date": "31/07/2023", - "author": "John Dough", - "tags": ["example", "jia", "please remember to remove"] + "ryuji-docs": { + "title": "Ryuji Documentation", + "slug": "ryuji-docs", + "filename": "ryuji_docs", + "date": "02/08/2023", + "author": "jetstream0/Prussia", + "tags": ["code", "project", "web", "docs"] + }, + "saki-docs": { + "title": "Saki Documentation", + "slug": "saki-docs", + "filename": "saki_docs", + "date": "02/08/2023", + "author": "jetstream0/Prussia", + "tags": ["code", "project", "web", "build", "docs"] } } \ No newline at end of file diff --git a/posts/another_example.md b/posts/another_example.md deleted file mode 100644 index 31c5e0d..0000000 --- a/posts/another_example.md +++ /dev/null @@ -1,14 +0,0 @@ -Have some lorem ipsum. On the house! -... -No, I **insist**. Don't be shy. - -1. lorem -2. ipsum - -Repellat et vel consequuntur et. Suscipit animi ipsam tempora consequatur hic ea tenetur. Culpa rerum quos eum vero ea. -Aperiam quia facilis doloremque ducimus. Amet quas similique officia quas et enim aut. Non ut vel sint distinctio consectetur ipsa. -Illum id sit laboriosam corrupti veritatis et quam. Ut ea quaerat omnis doloribus enim. Sed atque ad est doloribus esse. - -Molestias omnis ut voluptatem. Eius vel deleniti quia quam odio. Adipisci voluptas dolor nulla voluptatem. Molestiae sunt veritatis qui ex atque molestiae nobis. Expedita repellat dolores adipisci. - -Deleniti totam molestiae necessitatibus rem dolores. Natus ut quos beatae aut. Corrupti eum provident perferendis eum dolores maiores eos. diff --git a/posts/example.md b/posts/example.md deleted file mode 100644 index 22255bc..0000000 --- a/posts/example.md +++ /dev/null @@ -1,19 +0,0 @@ -Hello! This is an **example** post *not meant* to be actually served on the blog. Go to the `gh-pages` branch of the repo for the actual blog posts and stuff. - -[look its a link](https://example.com) - -![and an image too??? no way, this is a very safe hedgehog btw](/favicon.ico) - -## Wow a header - -```js -console.log("code block!!!"); -//todo: syntax highlighting -``` - -> ## woah a blockquote -> Yeehaw ^\[1]^ - ----- - -- \[1]: Source? Me. diff --git a/posts/meta.md b/posts/meta.md new file mode 100644 index 0000000..4476372 --- /dev/null +++ b/posts/meta.md @@ -0,0 +1,183 @@ +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 `