|
| 1 | +# Utils |
| 2 | + |
| 3 | +Scully works well in combination with other tools and utils. |
| 4 | + |
| 5 | +1. [Syntax Highlighting using prismjs](#add-syntax-highlighting) |
| 6 | + |
| 7 | +## Syntax Highlighting Using Prismjs |
| 8 | + |
| 9 | +When you are using code blocks in your markdown files for your blog posts, you'll may define a language for the used code like this: |
| 10 | + |
| 11 | +<pre><code>```ts |
| 12 | +const foo = 'bar'; |
| 13 | +```</code></pre> |
| 14 | + |
| 15 | +Scully will parse the markdown using [_marked_](https://www.npmjs.com/package/marked). |
| 16 | +In the end the parsed result will look like this: |
| 17 | + |
| 18 | +```html |
| 19 | +<code class="language-ts"> |
| 20 | + <span class="token keyword">const</span> foo |
| 21 | + <span class="token operator">=</span> |
| 22 | + <span class="token string">'bar'</span><span class="token punctuation">;</span> |
| 23 | +</code> |
| 24 | +``` |
| 25 | + |
| 26 | +As you can see _marked_ will use the CSS class prefix `language-` for tagging the code block with the appropriate language. |
| 27 | + |
| 28 | +To highlight your code blocks, you can use [_prismjs_](https://prismjs.com) as it matches with the resulting class names. You can install prismjs using NPM and creating a service to highlight your code blocks: |
| 29 | + |
| 30 | +```bash |
| 31 | +npm i --save prismjs |
| 32 | +ng g s highlight |
| 33 | +``` |
| 34 | + |
| 35 | +The service will include all languages you need and probably the clipboard library to simply copy the code etc. |
| 36 | + |
| 37 | +It could look like this: |
| 38 | + |
| 39 | +```ts |
| 40 | +import {Injectable, Inject} from '@angular/core'; |
| 41 | + |
| 42 | +import {PLATFORM_ID} from '@angular/core'; |
| 43 | +import {isPlatformBrowser} from '@angular/common'; |
| 44 | + |
| 45 | +import 'clipboard'; |
| 46 | +import 'prismjs'; |
| 47 | +import 'prismjs/plugins/toolbar/prism-toolbar'; |
| 48 | +import 'prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard'; |
| 49 | +import 'prismjs/components/prism-bash'; |
| 50 | +import 'prismjs/components/prism-css'; |
| 51 | +import 'prismjs/components/prism-javascript'; |
| 52 | +import 'prismjs/components/prism-json'; |
| 53 | +import 'prismjs/components/prism-markup'; |
| 54 | +import 'prismjs/components/prism-typescript'; |
| 55 | +// ... probably more, check out node_modules/prismjs/components |
| 56 | + |
| 57 | +declare var Prism: any; |
| 58 | + |
| 59 | +@Injectable() |
| 60 | +export class HighlightService { |
| 61 | + constructor(@Inject(PLATFORM_ID) private platformId: Object) {} |
| 62 | + |
| 63 | + highlightAll() { |
| 64 | + if (isPlatformBrowser(this.platformId)) { |
| 65 | + Prism.highlightAll(); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +After this, you need the service to be injected in your `BlogComponent` that was generated by scully: |
| 72 | + |
| 73 | +```ts |
| 74 | +import {/* ... */, AfterViewChecked} from '@angular/core'; |
| 75 | +import {HighlightService} from '../highlight.service'; |
| 76 | +/* ... */ |
| 77 | +export class BlogComponent implements OnInit, AfterViewChecked { |
| 78 | + constructor( |
| 79 | + /* ... */ |
| 80 | + private highlightService: HighlightService |
| 81 | + ) {} |
| 82 | + /* ... */ |
| 83 | + ngAfterViewChecked() { |
| 84 | + this.highlightService.highlightAll(); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +The last step is to include a theme from prism: |
| 90 | + |
| 91 | +```css |
| 92 | +/* include CSS for prism toolbar */ |
| 93 | +@import '~prismjs/plugins/toolbar/prism-toolbar.css'; |
| 94 | +/* check node_modules/prismjs/themes/ for the available themes */ |
| 95 | +@import '~prismjs/themes/prism-tomorrow'; |
| 96 | +``` |
| 97 | + |
| 98 | +That's it, all code languages you have been imported in your `HighlightService` should be highlighted now. |
| 99 | +Check out also the official [docs of prismjs](https://prismjs.com/) |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +[Full Documentation ➡️](scully.md) |
0 commit comments