Skip to content

Commit 10ec043

Browse files
d-koppenhagenVillanuevand
authored andcommitted
docs(scully): describe syntax highlighting
* docs(blog): describe syntax highlighting * docs(scully): update text for include prism CSS * docs(scully): separate syntax highlighting doc * docs(scully): add paragraph with link to utils.md
1 parent ba52add commit 10ec043

2 files changed

Lines changed: 112 additions & 8 deletions

File tree

docs/blog.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ We have a schematic that will add the necessary pieces to your project to enable
66
Angular app.
77

88
1. [Adding Blog Support](#adding-blog-support)
9-
2. [Generating New Blog Posts](#generating-new-blog-posts)
9+
2. [Generating New Blog Posts](#generating-new-blog-posts)
1010

1111
## Adding Blog Support
1212

@@ -16,32 +16,33 @@ To add blog support to your app, run the following command:
1616
ng g @scullyio/init:blog
1717
```
1818

19-
This will add the blog components/modules/routes to your Angular app, as well as a folder for your blog markdown files.
20-
If you don't want the folder to be called 'blog', you can run a longer command to provide your own custom names/folders.
19+
This will add the blog components/modules/routes to your Angular app, as well as a folder for your blog markdown files.
20+
If you don't want the folder to be called 'blog', you can run a longer command to provide your own custom names/folders.
2121

2222
To name your blog folders and components another name, run the following command with your own name:
2323

2424
```bash
2525
ng g @scullyio/init:markdown --name=my-test --slug=my-slug-id
2626
```
2727

28-
or
28+
or
2929

3030
```bash
3131
ng g @scullyio/init:markdown --name="my text" --slug="my slug id"
3232
```
3333

34+
> If your markdown content will include code blocks, you may want the [code to be highlighted](utils.md).
3435
3536
## Generating New Blog Posts
3637

37-
To add a new blog post, run the following command.
38+
To add a new blog post, run the following command.
3839

39-
```
40+
```bash
4041
ng g @scullyio/init:post --name="This is my post"
4142
```
4243

43-
---
44-
44+
[Check how to integrate Scully with other tools.](utils.md)
4545

46+
---
4647

4748
[Full Documentation ➡️](scully.md)

docs/utils.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)