Skip to content

Commit a230277

Browse files
authored
Chore: fix highlighting in add-template-variables-doc (#1616)
1 parent 35c7baf commit a230277

File tree

2 files changed

+351
-827
lines changed

2 files changed

+351
-827
lines changed

docusaurus/docs/how-to-guides/data-source-plugins/add-support-for-variables.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,15 @@ That's it! You can now try out the plugin by adding a [query variable](https://g
203203

204204
To interpolate template variables, you need to import the `getTemplateSrv()` function from the `@grafana/runtime` package:
205205

206-
```
206+
```ts
207207
import { getTemplateSrv } from '@grafana/runtime';
208208
```
209209

210210
The `getTemplateSrv()` function returns an instance of `TemplateSrv` which provides methods for working with template variables. The most important one, `replace()`, accepts a string containing variables as input and returns an interpolated string, where the variables have been replaced with the values that the users have selected.
211211

212212
For example, if you have a variable called `instance`, the following code replaces the variable with its corresponding value:
213213

214-
```
214+
```ts
215215
getTemplateSrv().replace("I'd like $instance, please!");
216216

217217
// I'd like server-1, please!
@@ -234,16 +234,16 @@ In the previous example, the variables only had one value, `server-1`. However,
234234

235235
For example, which of these different formats would suit your use case?
236236

237-
```
237+
```ts
238238
{server-1, server-2, server-3} (Graphite)
239239
["server-1", "server-2", "server-3"] (JSON)
240240
("server-1" OR "server-2" OR "server-3") (Lucene)
241241
```
242242

243243
Fortunately, the `replace()` method lets you pass a third argument to allow you to choose from a set of predefined formats, such as the CSV format:
244244

245-
```
246-
getTemplateSrv().replace("I'd like $instance, please!", {}, "csv");
245+
```ts
246+
getTemplateSrv().replace("I'd like $instance, please!", {}, 'csv');
247247

248248
// I'd like server-1, server-2, server-3, please!
249249
```
@@ -262,7 +262,7 @@ After reviewing the advanced variable format options, you may find that you want
262262

263263
You can pass an interpolation function to `replace()` instead of a string as the third argument. The following example uses a custom formatter function to add an `and` before the last element:
264264

265-
```
265+
```ts
266266
const formatter = (value: string | string[]): string => {
267267
if (typeof value == 'string') {
268268
return value;
@@ -289,7 +289,7 @@ There may be a case where you want to use a variable outside of a template. For
289289

290290
This helper function uses the `replace()` method to return the values as an array:
291291

292-
```
292+
```ts
293293
function getValuesForVariable(name: string): string[] {
294294
const values: string[] = [];
295295

@@ -307,7 +307,7 @@ function getValuesForVariable(name: string): string[] {
307307

308308
return values;
309309
}
310-
const instances = getValuesForVariable("instance");
310+
const instances = getValuesForVariable('instance');
311311

312312
for (var instance of instances) {
313313
console.log(instance);
@@ -320,7 +320,7 @@ for (var instance of instances) {
320320

321321
You can even go a step further and create an object that neatly contains all variables and their values:
322322

323-
```
323+
```ts
324324
function getAllVariables(): Record<string, string[]> {
325325
const entries = getTemplateSrv()
326326
.getVariables()

0 commit comments

Comments
 (0)