Deduplicate code for rendering examples

That way if we fix the rendering in one place, we fix it everywhere.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille 2025-02-21 11:26:40 +01:00
parent cb83c5a76e
commit 2cee8e0a98
No known key found for this signature in database
GPG key ID: 0C971D9DBC9D678D
5 changed files with 44 additions and 36 deletions

View file

@ -13,10 +13,4 @@
{{ $path := delimit (slice "event-schemas/examples" .name) "/" }}
{{ $example_content := partial "json-schema/resolve-refs" (dict "schema" .schema "path" $path) }}
{{ $example_json := jsonify (dict "indent" " ") $example_content }}
{{ $example_json = replace $example_json "\\u003c" "<" }}
{{ $example_json = replace $example_json "\\u003e" ">" | safeHTML }}
```json
{{ $example_json }}
```
{{ partial "render-example" (dict "example" $example_content) }}

View file

@ -73,24 +73,7 @@
{{ $example = $body.example }}
{{ end }}
{{ if eq $mime "application/json" }}
{{ $example_json := jsonify (dict "indent" " ") $example }}
{{ $example_json = replace $example_json "\\u003c" "<" }}
{{ $example_json = replace $example_json "\\u003e" ">" | safeHTML }}
```json
{{ $example_json }}
```
{{ else }}
{{ $example = $example | safeHTML }}
{{/*
We need to set a language for the code otherwise the styling
is different than other examples.
*/}}
```json
{{ $example }}
```
{{ end }}
{{ partial "render-example" (dict "example" $example "mime" $mime) }}
{{ end }}
{{ end }}

View file

@ -112,13 +112,7 @@
{{ $example = $json_body.examples.response.value }}
{{ end }}
{{ $example_json := jsonify (dict "indent" " ") $example }}
{{ $example_json = replace $example_json "\\u003c" "<" }}
{{ $example_json = replace $example_json "\\u003e" ">" | safeHTML }}
```json
{{ $example_json }}
```
{{ partial "render-example" (dict "example" $example) }}
{{ end }}
{{ else }}
{{/*

View file

@ -0,0 +1,40 @@
{{/*
Renders an example to be included in HTML, with support for pretty-printing
JSON.
Parameters:
* `example`: the example
* `mime`: the mime type of the example. Used to pretty-print JSON and for
syntax highlighting. If it is not provided, it is assumed to be
`application/json`.
*/}}
{{ $example := .example }}
{{/*
We need to convert the mime type to a recognized language.
For simplicity we only support JSON, which is also the default. Other mime
types are not highlighted.
*/}}
{{ $language := "json" }}
{{ if (and .mime (ne .mime "application/json")) }}
{{/*
`no-highlight` treats the value as plain text, but still styles the code
block like the ones with proper syntax highlighting.
*/}}
{{ $language = "no-highlight" }}
{{ end }}
{{ if eq $language "json" }}
{{ $example = jsonify (dict "indent" " ") $example }}
{{ $example = replace $example "\\u003c" "<" }}
{{ $example = replace $example "\\u003e" ">" }}
{{ end }}
```{{ $language }}
{{ $example | safeHTML }}
```

View file

@ -61,10 +61,7 @@
<h2>Examples</h2>
{{ $example := partial "json-schema/resolve-example" $definition }}
```json
{{ jsonify (dict "indent" " ") $example }}
```
{{ partial "render-example" (dict "example" $example) }}
</details>