Code blocks

The :code directive displays copyable, syntax-highlighted code snippets and terminal output. It supports text, leaf and container declarations.

NameTypeDescriptionDefault
languagestringLanguage for syntax highlighting (leaf/container only). Full list below.-
showLineNumbersbooleanDisplays line numbers (leaf/container only).true if language is set
showCopyActionbooleanDisplays the copy button.true if showLineNumbers is true
highlightLinesstringComma-separated line numbers and ranges to highlight.-
highlightLinesStartnumberStarting index used to track highlighted lines.-
lineNumberStartnumberFirst line number to display (>= 1).1
copyAutoReturnbooleanAppends a newline to the copied text (auto-executes in a terminal).-

Rendered:

Your current region is us-west-2.

Source:

1
Your current region is :code[us-west-2]{showCopyAction=true}.

Rendered:

1
console.log('Hello world!')

Source:

1
::code[console.log('Hello world!')]{language=js}

Rendered:

1
2
3
4
5
6
7
package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

Source:

1
2
3
4
5
6
7
8
9
:::code{language=go}
package main

import "fmt"

func main() {
    fmt.Println("hello world")
}
:::

Rendered:

const MyCoolComponent = (props) => (
    <section>
        <h1>{props.title}</h1>
    </section>
);

Source:

1
2
3
4
5
6
7
:::code{language=jsx showLineNumbers=false showCopyAction=false}
const MyCoolComponent = (props) => (
    <section>
        <h1>{props.title}</h1>
    </section>
);
:::

Rendered:

Yes
No
Start
Is it?
OK
Rethink
End

Source:

1
2
3
4
5
6
7
8
:::code{language=mermaid}
graph TD
    A[Start] --> B{Is it?};
    B -- Yes --> C[OK];
    C --> D[Rethink];
    D --> B;
    B -- No ----> E[End];
:::

Rendered:

54
55
56
57
58
// Creates a distribution from an S3 bucket.
const myBucket = new s3.Bucket(this, 'myBucket');
new cloudfront.Distribution(this, 'myDist', {
  defaultBehavior: { origin: new origins.S3Origin(myBucket) },
});

Source:

1
2
3
4
5
6
7
:::code{language=ts lineNumberStart=54}
// Creates a distribution from an S3 bucket.
const myBucket = new s3.Bucket(this, 'myBucket');
new cloudfront.Distribution(this, 'myDist', {
  defaultBehavior: { origin: new origins.S3Origin(myBucket) },
});
:::

Highlight individual lines and ranges with highlightLines.

Rendered:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Creating a custom origin request policy for a Distribution -- all parameters optional
declare const bucketOrigin: origins.S3Origin;
const myOriginRequestPolicy = new cloudfront.OriginRequestPolicy(this, 'OriginRequestPolicy', {
  originRequestPolicyName: 'MyPolicy',
  comment: 'A default policy',
  cookieBehavior: cloudfront.OriginRequestCookieBehavior.none(),
  headerBehavior: cloudfront.OriginRequestHeaderBehavior.all('CloudFront-Is-Android-Viewer'),
  queryStringBehavior: cloudfront.OriginRequestQueryStringBehavior.allowList('username'),
});

new cloudfront.Distribution(this, 'myDistCustomPolicy', {
  defaultBehavior: {
    origin: bucketOrigin,
    originRequestPolicy: myOriginRequestPolicy,
  },
});

Source:

1
2
3
:::code{language=ts highlightLines=4-8,13,14}
// ... code here ...
:::

highlightLinesStart=1 tracks the highlighted lines from 1 regardless of the displayed lineNumberStart.

Rendered:

105
106
107
108
109
110
111
112
EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
        ImageId: !FindInMap [ AWSRegionArch2AMI, !Ref 'AWS::Region' , !FindInMap [ AWSInstanceType2Arch, !Ref InstanceType, Arch ] ]
        KeyName: !Ref KeyName
        InstanceType: !Ref InstanceType
        SecurityGroups:
        - !Ref Ec2SecurityGroup

Source:

1
2
3
:::code{language=yaml lineNumberStart=105 highlightLines=4-6,8 highlightLinesStart=1}
# ... yaml here ...
:::

copyAutoReturn=true appends a newline to the copied text so a command runs as soon as it is pasted into a terminal. It does not add a visible line to the display.

Rendered:

npm start

Source:

1
::code[npm start]{showCopyAction=true copyAutoReturn=true}

If you use a standard Markdown fenced block with a language, Workshop Studio enables line numbers and the copy action automatically.

Rendered:

1
2
3
4
# Some lovely python code
def hello_world():
    '''A hello world program'''
    print("Hello world!")

Source:

1
2
3
4
5
6
```python
# Some lovely python code
def hello_world():
    '''A hello world program'''
    print("Hello world!")
```

Use any of these values for language. Aliases in the right column resolve to the same highlighter.

LanguageAliases
arduinoino
bashsh, shell
c-
cedar-
cpp-
css-
csharpcs, dotnet
dart-
diff-
dockerdockerfile
git-
go-
graphql-
java-
javascriptjs
jq-
jsonwebmanifest
jsx-
kotlinkt, kts
less-
lispelisp, emacs, emacs-lisp
llvm-
lua-
makefile-
markdownmd
markupatom, html, mathml, rss, ssml, svg, xml
nginx-
objectivecobjc
perl-
php-
powershell-
pythonpy
r-
regex-
rubyrb
rust-
sass-
scss-
sql-
swift-
template-
toml-
tsx-
typescriptts
vim-
wasm-
yamlyml