Syntax Highlighting In Markdown For Blog made with Scully

In many technical blogs, we use code snippets to share code with readers and explain the implementation. In a previous article, we have seen how to create a blogging site with Angular and Scully. If you are creating a technical blogging site you might also need to add code snippets to your blog. 

So In this article, we will see how to add a code block with syntax highlighting feature. We will continue working on the previous article example, If you want to code along with that example clone or download the code from this GitHub Repository

Previous Articles of Scully Series,
  1. Getting Started With Scully - Angular Static Site Generator
  2. Create Blogging Site With Angular and Scully
Adding Code Block in Markdown file

In the markdown file, we can add a code block with the following syntax, 
  1. ```    
  2. --- CODE BLOCK ---    
  3. ```  
Optionally you can also set the language as 
  1. ```language  
  2. --- CODE BLOCK ---  
  3. ```  
 language can be javascript, bash, etc.
 
For Example,
 
Add the following sample code block in  getting-started-with-scully.md file 
  1. ```javascript  
  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.   }  
  15. };  
  16. ```  
 Now take a Scully build and serve the Scully static server  with npm run scully && npm run scully:serve
 
 Code Block In Markdown file

As you can see it generates the code block with preformatted code, but it doesn't have any syntax highlighting.
 

Adding Syntax Highlighting Feature


Scully has a markdown plugin, which transforms markdown files to HTML files at the time of Scully build. This plugin provides an option to enable syntax highlighting at the time of Scully build. 

As you can see below, We will use the setPluginConfig function to configure plugins. set enableSyntaxHighlighting to true in the markdown plugin to enable syntax highlighting.
  1. // scully.[projectName].config.ts  
  2.   
  3. import { ScullyConfig, setPluginConfig } from '@scullyio/scully';  
  4.   
  5. setPluginConfig('md', { enableSyntaxHighlighting : true});  
  6.   
  7. export const config: ScullyConfig = {  
  8.   projectRoot: "./src",  
  9.   projectName: "portfolio",  
  10.   outDir: './dist/static',  
  11.   routes: {  
  12.     '/blog/:slug': {  
  13.       type: 'contentFolder',  
  14.       slug: {  
  15.         folder: "./blog"  
  16.       }  
  17.     },  
  18.   }  
  19. };  
Scully internally uses Prism.js for code block syntax highlighting. We need to import its styles in our styles.css file as below 
  1. /* include CSS for prism toolbar */  
  2. @import '~prismjs/plugins/toolbar/prism-toolbar.css';  
  3. /* check node_modules/prismjs/themes/ for the available themes */  
  4. @import '~prismjs/themes/prism-tomorrow';  
Great !!! We are done with the implementation, Now take and build and test it.
 
Final Output 

Take a new build of the angular app, as we have made changes in styles.css, then take a Scully build and start the static server using the following commands
  1. ng build --prod  
  2. npm run scully  
  3. npm run scully:serve  
Now check the blog in which you have added code snippets / code blocks :
 
Syntax Highlighting in markdown file

Great !!! Our code block syntax is highlighted ✨

Get the Source Code at this Github Repository
 

Summary

 
In this article, We have seen how to add a syntax highlighting feature for the markdown code block in a blog made with Scully. 

I hope you like this article, please provide your valuable feedback and suggestions🙂.
 
If you feel this is useful to others please like and share.


Similar Articles