Skip to content

Commit cc98bc3

Browse files
authored
Merge pull request #215 from dockersamples/labspace-defined-variables
Add labspace-defined variables for easier maintenance
2 parents 9bccffe + 1279082 commit cc98bc3

4 files changed

Lines changed: 59 additions & 2 deletions

File tree

components/interface/api/src/services/labspace.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export class LabspaceService {
3535
.replace(/[^a-z0-9\s-]/g, "") // remove special characters except spaces and dashes
3636
.replace(/\s+/g, "-"), // replace spaces with dashes
3737
}));
38+
39+
if (this.config.variables) {
40+
this.variables = { ...this.config.variables };
41+
}
3842
}
3943

4044
getLabspaceDetails() {
@@ -85,7 +89,8 @@ export class LabspaceService {
8589
const value = has ? this.variables[key] : undefined;
8690
if (value === undefined || value === null) return key;
8791
return String(value);
88-
});
92+
})
93+
.replace(/\\\$\\\$/g, "$$$$"); // Allow the usage of \$\$ to render as $$ in the markdown
8994

9095
return {
9196
id: section.id,

docs/markdown-options.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,26 @@ For values, the following options are available:
183183
- `requiredValue`: the value to match
184184
- `hasValue`: if set, the content will be shown if the variable has any value
185185
- `hasNoValue`: if set, the content will be shown if the variable is undefined or empty
186+
187+
### Labspace-defined variables
188+
189+
To support easier maintenance for values that may need to be updated in multiple locations, variables can also be defined in the `labspace.yaml` file.
190+
191+
For example, the following `labspace.yaml` configuration will define a variable named `pythonImageTag`:
192+
193+
```yaml
194+
variables:
195+
pythonImageTag: 3.14-alpine
196+
```
197+
198+
It can be injected using the same variable syntax throughout the markdown:
199+
200+
```dockerfile
201+
FROM python:$$pythonImageTag$$
202+
```
203+
204+
will then render as:
205+
206+
```dockerfile
207+
FROM python:3.14-alpine
208+
```

sample-content-repo/labspace/04-variables.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,26 @@ Variables can be used to support the conditional displaying of text.
5757
> [!TIP]
5858
> It appears the conditional is set and this content is displayed!
5959
:::
60+
61+
## Default Labspace variables
62+
63+
As a help to authors, variables can also be defined in the `labspace.yaml` file to reduce maintenance where a value may appear in multiple places throughout the writeup.
64+
65+
For example, the following `labspace.yaml` configuration will define a variable named `pythonImageTag`:
66+
67+
```yaml
68+
variables:
69+
pythonImageTag: 3.14-alpine
70+
```
71+
72+
It can be injected using the same variable syntax throughout the markdown:
73+
74+
```dockerfile
75+
FROM python:\$\$pythonImageTag\$\$
76+
```
77+
78+
will then render as:
79+
80+
```dockerfile
81+
FROM python:$$pythonImageTag$$
82+
```

sample-content-repo/labspace/labspace.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ services:
2828
- id: app
2929
title: App
3030
url: http://localhost
31-
icon: web
31+
icon: web
32+
33+
# Define variables that will be injected into the markdown context. Using variables makes maintenance easier
34+
# when the same value is used in multiple places. If desired, you can also allow these values to be overridden
35+
# by the end users.
36+
variables:
37+
pythonImageTag: 3.14-alpine

0 commit comments

Comments
 (0)