mirror of
https://github.com/matrix-org/matrix-spec
synced 2026-03-13 06:54:10 +01:00
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:
parent
cb83c5a76e
commit
2cee8e0a98
|
|
@ -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) }}
|
||||
|
|
|
|||
|
|
@ -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 }}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 }}
|
||||
{{/*
|
||||
|
|
|
|||
40
layouts/partials/render-example.html
Normal file
40
layouts/partials/render-example.html
Normal 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 }}
|
||||
```
|
||||
|
|
@ -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>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue