In the previous article: Getting Started With Scully - Angular Static Site Generator, We have seen what is Scully, How it works & How to use it to prerender routes.
In this article, we will see How Scully can help us to create a blogging site in angular?

Create Blogging Site With Angular And Scully

Scully is the best option to create a blogging site in Angular, because
  • It provides very good SEO support.
  • Superfast performance as pages are prerendered
  • Markdown support - Scully uses markdown files for blog content
  • Various plugins are available for syntax highlighting, google analytics integration, and many more.
  • Easy to create a new blog post : Once you have created the blog dashboard and blog page design setup in an angular application, you just need to add markdown files in your blog folder to create a new blog post
So let's get started
We will continue working on previous article example. If you want to start along then download/clone this GitHub repository.
  • We will add the blog support using Scully.
  • Create a Blog dashboard where it will show all the blogs.

Add Blog Support

Scully provides a schematic to add blog support in your existing angular application. execute the following command on command prompt or terminal.

(If you have not done Scully setup in your angular app, refer Getting Started With Scully.)
  1. ng generate @scullyio/init:blog
This will,
  • Update scully.[projectName].config.ts file
    1. // scully.portfolio.config.ts
    2. import { ScullyConfig } from '@scullyio/scully';
    3. export const config: ScullyConfig = {
    4. projectRoot: "./src",
    5. projectName: "portfolio",
    6. outDir: './dist/static',
    7. routes: {
    8. '/blog/:slug': {
    9. type: 'contentFolder',
    10. slug: {
    11. folder: "./blog"
    12. }
    13. },
    14. // Other Parameterized Route Config
    15. }
    16. }
    17. };
  • Create a blog component where markdown content will be rendered.
    1. //blog.component.ts
    2. import {Component, OnInit, ViewEncapsulation} from '@angular/core';
    3. import {ActivatedRoute, Router, ROUTES} from '@angular/router';
    4. declare var ng: any;
    5. @Component({
    6. selector: 'app-blog',
    7. templateUrl: './blog.component.html',
    8. styleUrls: ['./blog.component.scss'],
    9. preserveWhitespaces: true,
    10. encapsulation: ViewEncapsulation.Emulated
    11. })
    12. export class BlogComponent implements OnInit {
    13. ngOnInit() {}
    14. constructor(private router: Router, private route: ActivatedRoute) {
    15. }
    16. }
    1. <!-- blog.component.html -->
    2. <h3>ScullyIo content</h3>
    3. <hr>
    4. <!-- This is where Scully will inject the static HTML -->
    5. <scully-content></scully-content>
    6. <hr>
    7. <h4>End of content</h4>
    <scully-content> is a predefined component. Our blog content will be projected here. We can update the header and footer part as per our requirements.

  • Create blog.module.ts and blog-routing.module.ts for blog component route
    1. // blog-routing.module.ts
    2. import {NgModule} from '@angular/core';
    3. import {Routes, RouterModule} from '@angular/router';
    4. import {BlogComponent} from './blog.component';
    5. const routes: Routes = [
    6. {
    7. path: ':slug',
    8. component: BlogComponent,
    9. },
    10. {
    11. path: '**',
    12. component: BlogComponent,
    13. }
    14. ];
    15. @NgModule({
    16. imports: [RouterModule.forChild(routes)],
    17. exports: [RouterModule],
    18. })
    19. export class BlogRoutingModule {}
    1. // blog.module.ts
    2. import {CommonModule} from '@angular/common';
    3. import {NgModule} from '@angular/core';
    4. import {ScullyLibModule} from '@scullyio/ng-lib';
    5. import {BlogRoutingModule} from './blog-routing.module';
    6. import {BlogComponent} from './blog.component';
    7. @NgModule({
    8. declarations: [BlogComponent],
    9. imports: [CommonModule, BlogRoutingModule, ScullyLibModule],
    10. })
    11. export class BlogModule {}
  • Update the app-routing.module.ts to add the blog module route.
  • Add one sample blog markdown file with [current date] blog.md name in /blog folder
    1. ---
    2. title: 2021-04-03-blog
    3. description: blog description
    4. published: false
    5. ---
    6. # 2021-04-03-blog
    title, description, and published are meta details for this blog. These details can be used while we show the blog details on the blog dashboard. This can be accessible with the ScullyRoutesService. We can also add other meta properties as per our requirement, for example, authorName, authorTwitterId etc.

Test The Default Blog


We have added new components and modules. So we have to take a new angular build
  1. ng build --prod
Once it is done take Scully build
  1. npm run scully -- --scanRoutes
Scully maintains the cache for routes, if you want to discard the cache and recalculate the route, execute the command with --scanRoutes .

When published flag is false in a markdown file, and you take a Scully build it will add slugs property with ___UNPUBLISHED___[random-string]in markdown file and generate a blog HTML page in dist/static/blog folder with ___UNPUBLISHED___[random-string].
Unpublished blog path

When you want to publish this blog, set the published flag to true. and take a Scully build again with npm run scully.

Now serve the prerendered pages with npm run scully:serve and open the blog path in the browser.
Unpublished blog

Change The Blog Layout


We can customize the blog layout by doing related changes in BlogComponent. I have removed the default header and footer provided by Scully. I am using Bootstrap to apply some styling. You can do more customization as per your requirement.
  1. // blog.component.html
  2. <div class="row">
  3. <div class="col-md-12">
  4. <div class="card shadow-sm">
  5. <div class="card-body">
  6. <!-- This is where Scully will inject the static HTML -->
  7. <scully-content></scully-content>
  8. </div>
  9. </div>
  10. </div>
  11. </div>

Create Blog Dashboard

In BlogModule we will create BlogsComponent, In this component, we will show the list of blogs. We will update the route in blog-routing.module.ts to open BlogsComponent when the user open the /blog/ route.
  1. ng generate component blog/blogs
  1. //blogs.component.ts
  2. import { Component, OnInit } from '@angular/core';
  3. import { ScullyRoute, ScullyRoutesService } from '@scullyio/ng-lib';
  4. import { map } from 'rxjs/operators';
  5. @Component({
  6. selector: 'app-blogs',
  7. templateUrl: './blogs.component.html',
  8. styleUrls: ['./blogs.component.scss']
  9. })
  10. export class BlogsComponent implements OnInit {
  11. blogs$ = this.scullyService.allRoutes$.pipe(
  12. map(routes => routes.filter(r=> r.route.startsWith('/blog/')))
  13. );
  14. constructor(private scullyService: ScullyRoutesService) {
  15. }
  16. ngOnInit(): void {
  17. }
  18. }
We can access all routes details with ScullyRoutesService. We will filter the blog routes and render them on template.
  1. <!-- blogs.component.html -->
  2. <h1 class="text-center">Blogs</h1>
  3. <hr>
  4. <div class="row">
  5. <div class="col-md-12 p-2" *ngFor="let blog of blogs$|async">
  6. <div class="card shadow-sm">
  7. <div class="card-body">
  8. <h4 class="card-title">{{blog.title}}</h4>
  9. <p class="card-title">{{blog.description}}</p>
  10. <div>
  11. <span class="badge" [ngClass]="{'badge-info': blog.published, 'badge-dark': !blog.published}" >{{blog.published ? 'PUBLISHED' : 'DRAFT'}}</span>
  12. </div>
  13. <a class="btn btn-primary" [routerLink]="[blog.route]" >Read More</a>
  14. </div>
  15. </div>
  16. </div>
  17. </div>
We are done with our blog dashboard implementation. Now let's create few sample blogs and Test our blogging site.

Create A New Blog Post


We can create a new blog by creating a new markdown file in /blog folder. In this markdown file, we also have to mention the meta properties which is specified in about sample. We can do this manually, but instead of this Scully provides a schematic to create a new post, which will generate the file with meta properties.
  1. ng generate @scullyio/init:post
Create a new blog post
It will generate the markdown file with the following content,
  1. ---
  2. title: Getting Started with Scully
  3. description: blog description
  4. published: false
  5. ---
  6. # Getting Started with Scully
Here we will add the blog content and to publish this blog we will change published flag to true.

Final Output


I have added a few other blogs and changed all blog post published flags to true. Remove unpublished slug from markdown.

Take a new angular build and Scully build and start the scully static server.
  1. ng build --prod
  2. npm run scully -- --scanRoutes
  3. npm run scully:serve
Blogging Site With Scully and Angular

Great !!! We have successfully created a blogging site with Angular and Scully.
Get the Source Code at this GitHub Repository

Summary


In this article, We have seen how to create a blogging site with angular and Scully.
I hope you like this article, please provide your valuable feedback and suggestions🙂.