Skip to content

Code blocks

Code blocks show source code with syntax highlighting. NgDoc adds file names, code groups, highlighted lines, and code loaded from real files, so examples can't drift from the code.

 See it

greeting.tsTypeScriptLine 3
export const greet = (name: string): string => {

  return `Hello, ${name}!`;
};

This block loads its code from a real file, names it and highlights line 3.

 Use it

Write a fenced code block with a language, then add attributes after the language:

index.mdMarkdown
```typescript name="greeting.ts" {3}
export const greet = (name: string): string => {
  const greeting = `Hello, ${name}!`;
  return greeting;
};
```

Attributes are separated by spaces. Values go in double quotes. Markdown and template syntax lists every attribute.

AttributeEffect
name="…"Shows a file name above the code.
group="…"Puts the block in a code group with the same name.
activeOpens this block first in its code group.
icon="…"Shows an icon next to the name.
{1,3-5}Highlights lines 1, 3, 4 and 5.
file="./path"Loads the code from a file.

File names

index.mdMarkdown
```typescript name="my-file.ts"
const message = 'Hello world';
```
my-file.tsTypeScript
const message = 'Hello world';

Code groups

Blocks with the same group show as tabs. Each block's name is its tab label.

index.mdMarkdown
```bash group="install" name="npm"
npm install @ng-doc/core
```

```bash group="install" name="yarn"
yarn add @ng-doc/core
```
npm install @ng-doc/core

The first block is open by default. Add active to open another one:

index.mdMarkdown
```bash group="install-active" name="npm"
npm install @ng-doc/core
```

```bash group="install-active" name="yarn" active
yarn add @ng-doc/core
```
yarn add @ng-doc/core

Icons

icon shows an icon next to the name, in single blocks and in code groups.

index.mdMarkdown
```typescript name="app.ts" icon="angular"
@Component({ selector: 'app-root', template: '' })
export class App {}
```
app.tsTypeScript
@Component({ selector: 'app-root', template: '' })
export class App {}
💡 Tip

To add your own icons, see Icons.

Highlighted lines

Put line numbers in braces after the language. Separate them with commas, and use a dash for a range. With file, the numbers count the lines that the block shows: after the #L range is applied and hidden lines are removed.

index.mdMarkdown
```typescript name="ng-doc.page.ts" {1,3-6}
import { NgDocPage } from '@ng-doc/core';

const MyPage: NgDocPage = {
  title: 'My page',
  mdFile: './index.md',
};

export default MyPage;
```
ng-doc.page.tsTypeScriptLines 1, 3–6
import { NgDocPage } from '@ng-doc/core';

const MyPage: NgDocPage = {
  title: 'My page',
  mdFile: './index.md',
};

export default MyPage;

Code from a file

file loads the code from a file, relative to the Markdown file. Leave the block empty. NgDoc shows the code as it is in the file, without reformatting it. The examples on this page load examples/greeting.ts:

greeting.tsTypeScript
export const greet = (name: string): string => {

  return `Hello, ${name}!`;
};

export const farewell = (name: string): string => {
  return `Goodbye, ${name}!`;
};
index.mdMarkdown
```typescript name="greeting.ts" file="./examples/greeting.ts"

```

Add #L and line numbers right after the closing quote to load only some lines. The numbers refer to lines in the file.

SuffixLoads
#L8-L10Lines 8 to 10.
#L8Line 8 only.
#L8-Line 8 to the end of the file.
index.mdMarkdown
```typescript name="greeting.ts" file="./examples/greeting.ts"#L8-L10

```
greeting.tsTypeScript
export const farewell = (name: string): string => {
  return `Goodbye, ${name}!`;
};

Hiding lines

A comment with ng-doc-ignore-line hides lines from code that NgDoc loads from a file, from demos and from snippets. It removes the line with the comment and the line after it. Add a number to remove more lines after it: // ng-doc-ignore-line 3.

This is the source of examples/greeting.ts:

greeting.tsTypeScript
export const greet = (name: string): string => {
  // ng-doc-ignore-line
  console.debug('greet', name);

  return `Hello, ${name}!`;
};

Loaded with file, the comment and the console.debug line are gone:

greeting.tsTypeScript
export const greet = (name: string): string => {

  return `Hello, ${name}!`;
};

The comment works in //, /* */ and <!-- --> form, so you can use it in TypeScript, styles and templates.

 Gotchas

Warning

The comment also removes the line after it. A comment at the end of a line of code, such as foo(); // ng-doc-ignore-line, removes that line and the next one.

Next: Images, video and embeds

Edit this page