Skip to content

Commit 5821e91

Browse files
Docs: auto generate plugin schema reference (#1770)
Co-authored-by: Timur Olzhabayev <[email protected]>
1 parent 3148278 commit 5821e91

File tree

9 files changed

+163
-0
lines changed

9 files changed

+163
-0
lines changed

docusaurus/website/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"docusaurus": "docusaurus",
77
"start": "docusaurus start",
8+
"prebuild": "./scripts/download-schema.sh && ./scripts/generate-markdown.sh",
89
"build": "docusaurus build",
910
"swizzle": "docusaurus swizzle",
1011
"deploy": "docusaurus deploy",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
# Script to download the Grafana plugin schema JSON file
4+
# and store it locally in the same directory
5+
6+
# Exit on error
7+
set -e
8+
9+
# Define variables
10+
SCHEMA_URL="https://raw.githubusercontent.com/grafana/grafana/main/docs/sources/developers/plugins/plugin.schema.json"
11+
OUTPUT_FILE="$(dirname "$0")/plugin.schema.json"
12+
13+
echo "Downloading Grafana plugin schema..."
14+
15+
# Download the file
16+
if curl -s -f -o "$OUTPUT_FILE" "$SCHEMA_URL"; then
17+
echo "Schema successfully downloaded to $OUTPUT_FILE"
18+
else
19+
echo "Error: Failed to download schema from $SCHEMA_URL" >&2
20+
exit 1
21+
fi
22+
23+
# Verify the downloaded file
24+
if [ ! -s "$OUTPUT_FILE" ]; then
25+
echo "Error: Downloaded file is empty" >&2
26+
rm -f "$OUTPUT_FILE"
27+
exit 1
28+
fi
29+
30+
31+
echo "Schema download complete!"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
# Script generate markdown files from the Grafana plugin schema JSON file
4+
5+
# Exit on error
6+
set -e
7+
8+
INPUT_FILE="$(dirname "$0")/plugin.schema.json"
9+
PARTIALS="$(dirname "$0")/partials"
10+
OUTPUT_FILE="$(dirname "$0")/../../docs/reference/metadata.md"
11+
12+
# Verify the downloaded file
13+
if [ ! -s "$INPUT_FILE" ]; then
14+
echo "Error: input schema doesn't exist or is empty" >&2
15+
rm -f "$INPUT_FILE"
16+
exit 1
17+
fi
18+
19+
npx --yes jsonschema2mk --partials "$PARTIALS" --schema "$INPUT_FILE" > "$OUTPUT_FILE"
20+
21+
# Detect OS and set sed options accordingly
22+
if [[ "$OSTYPE" == "darwin"* ]]; then
23+
SED_OPTS="-i ''"
24+
else
25+
SED_OPTS="-i"
26+
fi
27+
28+
# Add docusaurus header to the top of the file
29+
sed $SED_OPTS "1i\\
30+
---\\
31+
id: plugin-json\\
32+
title: Metadata (plugin.json)\\
33+
description: Reference for the Grafana plugin.json metadata file.\\
34+
keywords:\\
35+
- grafana\\
36+
- plugins\\
37+
- documentation\\
38+
- plugin.json\\
39+
- API reference\\
40+
- API\\
41+
sidebar_position: 10\\
42+
---\\
43+
\\
44+
# Plugin metadata (plugin.json)
45+
" "$OUTPUT_FILE"
46+
47+
rm -f "$INPUT_FILE"
48+
49+
echo "Markdown generation complete!"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{#if path~}}
2+
<a name="{{{tolink (or path 'root')}}}"></a>
3+
{{{mdlevel path}}}{{#if path}} {{escape path}}{{/if}}
4+
{{/if~}}
5+
{{#if (or description deprecated)}}
6+
7+
{{#if deprecated}}(DEPRECATED) {{/if}}{{{description}}}
8+
9+
{{/if}}
10+
11+
{{> type . ~}}
12+
{{#each (get_examples .) ~}}
13+
{{> example ~}}
14+
{{/each ~}}
15+
{{#each (get_ref_items) ~}}
16+
{{>element . ~}}
17+
{{/each ~}}

docusaurus/website/scripts/partials/example.md

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{{#if (length properties) ~}}
2+
**{{prefix_text}}Properties**
3+
4+
{{> object_property_header}}
5+
{{#each properties ~}}
6+
{{> object_property (jsmk_property . path=(pathjoinobj ../path @key .) parent=.. name=@key)}}
7+
{{/each}}
8+
9+
{{/if}}
10+
{{#if (length patternProperties) ~}}
11+
**{{prefix_text}}Properties (Pattern)**
12+
13+
{{> object_property_header}}
14+
{{#each patternProperties ~}}
15+
{{> object_property (jsmk_property . path=(pathjoinobj ../path @key .) parent=.. name=@key)}}
16+
{{/each}}
17+
18+
{{/if}}
19+
20+
{{#if (isdefined minProperties)}}
21+
**{{prefix_text}}Minimal Properties:** {{escape minProperties}}{{br}}
22+
{{/if~}}
23+
{{#if (isdefined maxProperties)}}
24+
**{{prefix_text}}Maximal Properties:** {{escape maxProperties}}{{br}}
25+
{{/if~}}
26+
{{#if (and (isdefined propertyNames) (isdefined propertyNames.pattern))}}
27+
**{{prefix_text}}Property Name Pattern:** {{code (escapeRegexp propertyNames.pattern)}}{{br}}
28+
{{/if~}}
29+
{{#if (isdefined dependentRequired)}}
30+
{{#each dependentRequired}}
31+
**{{prefix_text}}If property _{{@key}}_ is defined**, property/ies {{#each this}}_{{this}}_{{#unless @last}}, {{/unless}}{{/each}} is/are required.{{br}}
32+
{{/each}}
33+
{{/if~}}
34+
{{#if (isdefined dependentSchemas)}}
35+
{{#each dependentSchemas}}
36+
**{{prefix_text}}If property _{{@key}}_ is defined**:
37+
38+
{{> element_part this type=(or type ../type) path=(pathjoin path (plus "dependentSchemas " @key))}}
39+
40+
{{/each}}
41+
{{/if~}}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
|
2+
{{~#mylink .}}**{{escape name}}**{{/mylink ~}}
3+
{{#if (and title (title_isnot_name .))}}<br/>({{escape title}}){{/if ~}}
4+
|
5+
{{~code display_type ~}}
6+
|
7+
{{~#if deprecated}}(DEPRECATED)<br/>{{/if}}
8+
{{~#if description ~}}
9+
{{firstline description .}}<br/>{{/if ~}}
10+
{{>extra_inline . ~}}
11+
|
12+
{{~#if (isdefined required)}}{{#if required}} ✅ {{/if}}{{/if~}}
13+
|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| Name | Type | Description | Required |
2+
| ---- | ---- | ----------- | :------: |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{#if (is_type type "object") ~}}
2+
{{> object . ~}}
3+
{{~else~}}
4+
{{#if (is_type type "array") ~}}
5+
{{> array . ~}}
6+
{{~else~}}
7+
{{> simple ~}}
8+
{{/if ~}}
9+
{{/if ~}}

0 commit comments

Comments
 (0)