Abelfubu logo

Create a blog with analog and angular

Abel de la Fuente
Abel de la Fuente 21 min read -
Create a blog with analog and angular
Photo by RetroSupply on Unsplash

Hey, Let’s Talk Blogging!

So, here’s the deal: I’m all about productivity hacks, from DIY keyboards to tinkering with Vim, building a knowledge management system with obsidian.md and devouring productivity books like “Atomic Habits.” But you know what’s been on my backburner forever? Starting my own blog. Then Analog 1.0 dropped, and being an Angular fan, I thought, “Why not give it a shot?”

Diving into Analog for my blogging adventure has been quite the journey. And hey, I’m here to share the scoop with you! Let’s walk through the process of creating your very own blog using Analog.

Let’s Get Rolling with Analog

It’s time to kick things off! Grab your favorite drink, open up your terminal, and let’s dive into the world of Analog.

To get started, enter the following command:

Terminal window
1
npm create analog@latest

Analog will ask you a few questions. When prompted to select a variant, be sure to choose the blog variant:

Terminal window
1
Select a template: » Analog
2
? Select a variant: » - Use arrow-keys. Return to submit.
3
angular-v17
4
> blog
5
angular-v16

Once you’ve made your selection, follow the instructions provided. Then, navigate to the newly created folder, install the necessary packages, and launch the project with:

Terminal window
1
npm run dev

And there you have it! If you visit the web, you’ll find a simple yet elegant application ready for your personal touch.

Front Matter Matters

Let’s delve into the details of your blog’s front matter. This might sound fancy, but it’s crucial stuff.

First, head to the src/content directory. There, you’ll discover your very first blog post, stored in markdown format.

Open it up, and you’ll see some front matter data containing essential information about your post:

1
title: My First Post
2
slug: 2022-12-27-my-first-post
3
description: My First Post Description
4
coverImage: https://images.unsplash.com/photo...

Here’s the exciting part: you can customize this front matter to your heart’s content. If you want to add more details, simply modify the interface in the src/post-attributes.ts file:

1
export default interface PostAttributes {
2
title: string;
3
slug: string;
4
description: string;
5
coverImage: string;
6
// Add more attributes here if you want!
7
}

Easy peasy, right? Your blog, your rules.

Search Engine Optimization SEO

Now, let’s ensure your blog gets noticed by search engines. In the src/app/pages/blog/[slug].page.ts file, you’ll find the key to optimizing your pages for search results.

Utilize the post$ observable to update important SEO elements like title and meta tags:

1
import { Meta, Title } from '@angular/platform-browser';
2
3
@Component(...)
4
export default class BlogPostComponent {
5
private readonly title = inject(Title);
6
private readonly meta = inject(Meta);
7
8
readonly post$ = injectContent<PostAttributes>().pipe(
9
tap(({ attributes: { title, description, coverImage } }) => {
10
this.title.setTitle(title);
11
this.meta.updateTag({ name: 'description', content: description });
12
this.meta.updateTag({ name: 'og:description', content: description });
13
this.meta.updateTag({ name: 'og:image', content: coverImage });
14
this.meta.updateTag({ name: 'og:title', content: title });
15
}),
16
);
17
}

With these changes, your blog posts will stand out in search engine results.

Sitemaps

Help search engines discover and index your content by implementing a sitemap.xml file. Analog and Vite make this process effortless.

Update your vite.config.ts file with the following snippet:

1
import { defineConfig } from "vite";
2
import analog from "@analogjs/platform";
3
4
export default defineConfig(({ mode }) => ({
5
plugins: [
6
analog({
7
prerender: {
8
routes: async () => ["/", "/blog"], // Add more routes here if needed
9
sitemap: {
10
host: "https://yourdomain.com/", // Don't forget to update your domain!
11
},
12
},
13
}),
14
],
15
}));

Now, Analog will automatically generate your sitemap.xml file, ensuring your blog stays visible to search engines.

Syntax highlighting

Display your code snippets with style using Prism.js. It’s already set up and ready to go in the template variant.

package.json
1
"prismjs": "^1.29.0",

To enhance your code’s appearance, add Prism.js styles to your styles.scss file:

1
@import "prismjs/themes/prism-okaidia.min.css";

Now, any code you include in your markdown files will be beautifully highlighted.

So, go ahead and sprinkle some code into your markdown files like so:

1
```typescript
2
function hello(name: string): void {
3
console.log(name);
4
}
5
```

Watch as your code comes to life in vibrant colors!

Comments

Encourage engagement by enabling comments on your blog posts. With Utterances, this process is straightforward.

Head to the Utterances website, set up the widget for your GitHub account, and choose a repository and theme.

Next, add the following code to your blog post component under src/app/pages/blog/[slug].page.ts:

1
<div #comments></div>

In the component we can take advantage of the new query signal API

1
comments = viewChild<ElementRef>("comments");
2
3
addCommentsEffect = effect(() => {
4
if (!this.comments()?.nativeElement) return;
5
6
const script = document.createElement("script");
7
script.src = "https://utteranc.es/client.js";
8
script.setAttribute("repo", "username/repo");
9
script.setAttribute("issue-term", "pathname");
10
script.setAttribute("theme", "dark-blue");
11
script.setAttribute("crossorigin", "anonymous");
12
script.async = true;
13
this.comments()?.nativeElement.appendChild(script);
14
});

Now, readers can share their thoughts directly on your blog posts.

Deploy your static site

Ready to share your blog with the world? Analog makes deploying your site a breeze.

Update your vite.config.ts file to ensure your posts are generated automatically:

1
...
2
plugins: [
3
analog({
4
prerender: {
5
routes: async () => [
6
'/blog',
7
{
8
contentDir: 'src/content',
9
transform: (file: PrerenderContentFile) => {
10
// do not include files marked as draft in frontmatter
11
if (file.attributes['draft']) {
12
return false;
13
}
14
// use the slug from frontmatter if defined, otherwise use the files basename
15
const slug = file.attributes['slug'] || file.name;
16
return `/blog/${slug}`;
17
},
18
},
19
],
20
sitemap: {
21
host: 'https://yourdomain.com/',
22
},
23
},
24
}),
25
...

And just like that, your blog posts will be set up and ready whenever you hit the build button.

Now, let’s showcase your blog to the world!

It’s time to take your blog live and share your work with the world! Thanks to Analog, deploying your site is straightforward. Whether you’re opting for a static generated site or SSR (Server-Side Rendering), Analog provides the tools you need.

Head over to the Analog docs and explore the deployment section. It offers various options to suit your needs: Analog Deployment Docs.

Let me share a popular choice: Render. It’s user-friendly and efficient. Just visit their website, create an account (if you haven’t already), and you’ll be ready to deploy your site.

  1. Create a new Static Site and select the repository that contains your code.

  2. Depending on your package manager, set the build command to yarn && yarn build, npm install && npm run build, or pnpm i —shamefully-hoist && pnpm build..

  3. Set the publish directory to the public directory inside of the dist build directory (e.g. dist/analog/public)

Click ‘Create Static Site’

DONE!

Conclusion

Congratulations on publishing your inaugural blog post! This marks the beginning of your blogging journey, and it’s an exciting milestone to celebrate.

As you navigate this new endeavor, remember that every blogger starts somewhere, and there’s always room to grow and learn along the way. Embrace the process, enjoy the journey, and don’t be afraid to share your experiences, thoughts, and insights with your readers.

Your blog is a space for exploration, self-expression, and connection. So, keep writing, keep experimenting, and most importantly, keep being yourself.

Here’s to the start of something special. Happy blogging!