Add support for rendering string formats (#1814)

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
pull/1818/head
Kévin Commaille 7 months ago committed by GitHub
parent 48f4c4954f
commit 98d85cf421
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1 @@
Add support for rendering string formats.

@ -31,3 +31,8 @@ mx-event-id:
title: Event ID title: Event ID
url: /appendices#event-ids url: /appendices#event-ids
# regex: "^\\$" # regex: "^\\$"
uri:
title: URI
url: http://tools.ietf.org/html/rfc3986
# no regex

@ -128,6 +128,8 @@ resolve-additional-types.)
* `anchor`: optional HTML element id for the target type, which will be used to link to it. * `anchor`: optional HTML element id for the target type, which will be used to link to it.
* `format`: optional string for the format of the type, used for strings.
*/}} */}}
{{ define "partials/property-type" }} {{ define "partials/property-type" }}
{{ $type := "" }} {{ $type := "" }}
@ -143,6 +145,15 @@ resolve-additional-types.)
{{ $items := .items }} {{ $items := .items }}
{{ $inner_type := partial "property-type" $items }} {{ $inner_type := partial "property-type" $items }}
{{ $type = delimit (slice "[" $inner_type "]") "" }} {{ $type = delimit (slice "[" $inner_type "]") "" }}
{{ else if eq .type "string" }}
{{ $type = "string" }}
{{/* If the string uses a known format, use it. */}}
{{ with .format }}
{{ with partial "custom-format" . }}
{{ $type = . }}
{{ end }}
{{ end }}
{{ else if or (reflect.IsSlice .type) .oneOf }} {{ else if or (reflect.IsSlice .type) .oneOf }}
{{/* {{/*
It's legal to specify an array of types. It's legal to specify an array of types.
@ -167,7 +178,7 @@ resolve-additional-types.)
{{ $type = delimit $types "|" }} {{ $type = delimit $types "|" }}
{{ else }} {{ else }}
{{/* A simple type like string or boolean */}} {{/* A simple type like integer or boolean */}}
{{ $type = (htmlEscape .type) }} {{ $type = (htmlEscape .type) }}
{{ end }} {{ end }}
@ -241,8 +252,8 @@ resolve-additional-types.)
{{ range $formatId, $formatType := $formatMap.Values }} {{ range $formatId, $formatType := $formatMap.Values }}
{{ $formatKey := "string" }} {{ $formatKey := "string" }}
{{ if ne $formatId "string" }} {{ if ne $formatId "string" }}
{{ with index site.Data "custom-formats" $formatId }} {{ with partial "custom-format" $formatId }}
{{ $formatKey = printf "<a href=\"%s\">%s</a>" (htmlEscape .url) (htmlEscape .title) }} {{ $formatKey = . }}
{{ else }} {{ else }}
{{ errorf "Unsupported value for `x-pattern-format`: %s" $formatId }} {{ errorf "Unsupported value for `x-pattern-format`: %s" $formatId }}
{{ end }} {{ end }}
@ -290,3 +301,18 @@ resolve-additional-types.)
{{ if (index .property "x-addedInMatrixVersion") }}{{ partial "added-in" (dict "v" (index .property "x-addedInMatrixVersion")) }}{{ end -}} {{ if (index .property "x-addedInMatrixVersion") }}{{ partial "added-in" (dict "v" (index .property "x-addedInMatrixVersion")) }}{{ end -}}
{{ if (index .property "x-changedInMatrixVersion") }}{{ partial "changed-in" (dict "changes_dict" (index .property "x-changedInMatrixVersion")) }}{{ end -}} {{ if (index .property "x-changedInMatrixVersion") }}{{ partial "changed-in" (dict "changes_dict" (index .property "x-changedInMatrixVersion")) }}{{ end -}}
{{ end }} {{ end }}
{{/*
Computes the type to display for a string format, given the identifier of
the format as a string.
*/}}
{{ define "partials/custom-format" }}
{{ $customFormat := "" }}
{{ with index site.Data "custom-formats" . }}
{{ $customFormat = printf "<a href=\"%s\">%s</a>" (htmlEscape .url) (htmlEscape .title) }}
{{ end }}
{{ return $customFormat }}
{{ end }}

Loading…
Cancel
Save