Code

You can use standard Markdown syntax, this is called "code blocks." It is also possible to render code blocks in two different themes, add titles to them, highlight lines, simulate a terminal and wrap multiple languages for a UI with a language selector.

The "code examples" feature allows pulling code samples from separate files. This can be useful for autogenerated code samples or such that go through automated testing.

Code blocks
Clipboard icon

  • To add titles for the code blocks, pass title="my title" after the language tag.
  • The primary theme is the default. To render the code block in the secondary theme, pass secondaryTheme in the extended language tag syntax.

In its minimal form, code blocks are standard markdown as follows:

you write:md
```
The syntax is not highlighted as no language is indicated.
**Formatting syntax** like _this_ or <b>tag</b> will remain unparsed.
```
Clipboard icon
The syntax is not highlighted as no language is indicated.
**Formatting syntax** like _this_ or <b>tag</b> will remain unparsed.
Clipboard icon

Themes, title
Clipboard icon

Primary theme (default)
Clipboard icon

you write:md
```md title="Code block in primary theme"
Sample code goes here.
```
Clipboard icon
Code block in primary thememd
Sample code goes here.
Clipboard icon

Secondary theme
Clipboard icon

you write:md
```md title="Code block in secondary theme" secondaryTheme
Sample code goes here.
```
Clipboard icon
Code block in secondary thememd
Sample code goes here.
Clipboard icon

With highlighted lines
Clipboard icon

It is also possible to highlight specific lines in a code. To highlight, pass highlightLines in the syntax and enter the range or specific line to be highlighted.

you write:md
```json highlightLines="10"
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": 123.45,
"Sort": true,
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": null
}
}
}
}
}
```
Clipboard icon
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": 123.45,
"Sort": true,
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": null
}
}
}
}
}
Clipboard icon

Terminal / console with prompt
Clipboard icon

using language console instead of sh prefixes a $ as command prompt to each line:

curl -H "Content-Type: application/json" \
"https://api.europe-west1.gcp.commercetools.com"
Clipboard icon

To omit he prompt on certain lines, add the option noPromptLines="3-4".

cd project
cp -R \
dist \
public/
rm -rf dist
yarn start
Clipboard icon

No options, but you can check that the indentation in regards to the left prompt is aligned when the line wraps.

# fetch a token:
curl https://auth.sphere.io/oauth/token --basic --user "{client_id}:{client_secret}" -X POST -d "grant_type=client_credentials&scope=manage_project:{project_key}"
# now access the API:
curl -sH "Authorization: Bearer ACCESS_TOKEN" https://api.sphere.io/PROJECT_KEY/products
Clipboard icon

Passing the option title="Fetching an access token"

Fetching an access tokenTerminal
# fetch a token:
curl https://auth.sphere.io/oauth/token --basic --user "{client_id}:{client_secret}" -X POST -d "grant_type=client_credentials&scope=manage_project:{project_key}"
# now access the API:
curl -sH "Authorization: Bearer ACCESS_TOKEN" https://api.sphere.io/PROJECT_KEY/products
Clipboard icon

Multi language code blocks in markdown
Clipboard icon

It's possible to nest multiple markdown code blocks into one <MultiCodeBlock> element to have them display as a multi language code example without having to delegate the content into file using the <CodeExample> and <MultiCodeExample> features from the gatsby-theme-code-examples add-on.

you write:md
<MultiCodeBlock>
```js
console.log('this is it in javascript');
```
```php
echo "this is it in PHP"
```
</MultiCodeBlock>
Clipboard icon
console.log('this is it in javascript');
Clipboard icon

Code examples from files
Clipboard icon

The <CodeExample> and <MultiCodeExample> components allow pulling code examples from files in the repository. The files have to be placed into src/code-examples in the microsite folder. The path parameter refers to them relative to that folder, for example path="basics/example.json" refers to src/code-examples/basics/example.json

The language is derived from the file suffix.

Some parameters available on the markdown code blocks above can be passed as properties, for example highlightLines, noPromptLines. title has to be passed on the level of the multi code block, the layout does not allow for per-language titles.

Single language
Clipboard icon

you write:md
<CodeExample path="example.graphql" title="GraphQL code sample" />
Clipboard icon
GraphQL code samplegraphql
type Person {
id: ID!
name: String!
age: Int
}
Clipboard icon

Multiple languages
Clipboard icon

Similarly to standard code blocks, you can render multiple code examples combined into one code block, with a dropdown option to switch between the code examples.

Information icon

This functionality requires the Add-On for Code Examples to be installed.

Primary theme (default)

you write:md
<MultiCodeExample title="Multilanguage code samples">
<CodeExample path="example.js" highlightLines={[3]} />
<CodeExample path="example.java"/>
<CodeExample path="example.console" noPromptLines={[3, 4]} />
</MultiCodeExample>
Clipboard icon
Multi-language code samples in primary theme
// Arrow function sample
const arrowFuncSample = () => {
console.log('this is an arrow function example');
};
Clipboard icon

Secondary theme

<MultiCodeExample title="Multilanguage code samples" secondaryTheme>
<CodeExample path="example.js" highlightLines={[3]} />
<CodeExample path="example.java"/>
<CodeExample path="example.console" noPromptLines={[3, 4]} />
</MultiCodeExample>
Clipboard icon
Multi-language code samples in primary theme
// Arrow function sample
const arrowFuncSample = () => {
console.log('this is an arrow function example');
};
Clipboard icon