Skip to content

[DOC-560] OpenAPI fields anchor links #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
)
}}

{{ $title = $title | urlize }}
{{ $yamlBlock := .Inner | transform.Unmarshal }}
{{ with $yamlBlock }}
<div class="openapi">
Expand All @@ -26,15 +26,15 @@
</div>

{{ with .parameters }}
{{- template "renderParameters" dict "content" .}}
{{- template "renderParameters" dict "title" $title "content" .}}
{{ end }}

{{ with .requestBody }}
{{- template "renderRequestBody" dict "content" .}}
{{- template "renderRequestBody" dict "title" $title "content" .}}
{{ end }}

{{ with .responses }}
{{- template "renderResponses" dict "content" . }}
{{- template "renderResponses" dict "title" $title "content" . }}
{{ end }}

{{end}}
Expand All @@ -43,13 +43,14 @@
{{ end }}

{{- define "renderParameters" }}
{{ $title := .title }}
{{ $content := .content }}
<div class="openapi-parameters regular-font">
<div class="openapi-table-title">Path Parameters</div>
<div>
<ul class="openapi-table">
{{ range $k, $v := $content }}
{{- template "renderParameter" dict "prop" $v.name "value" $v "category" "path" }}
{{- template "renderParameter" dict "title" $title "prop" $v.name "value" $v "category" "path" }}
{{ end }}
</ul>
</div>
Expand All @@ -59,14 +60,15 @@
<div>
<ul class="openapi-table">
{{ range $k, $v := $content }}
{{- template "renderParameter" dict "prop" $v.name "value" $v "category" "query" }}
{{- template "renderParameter" dict "title" $title "prop" $v.name "value" $v "category" "query" }}
{{ end }}
</ul>
</div>
</div>
{{ end }}

{{- define "renderRequestBody" }}
{{ $title := printf "%s_%s" .title "body" }}
{{ $content := .content }}
<div class="openapi-parameters regular-font">
<div class="openapi-table-title">
Expand All @@ -81,7 +83,7 @@
{{ range $k, $v := $content.content }}
{{ if $v.schema.items }}
{{ $required := false }}
{{- template "renderProperty" dict "prop" $k "value" $v.schema "required" $required }}
{{- template "renderProperty" dict "title" $title "prop" $k "value" $v.schema "required" $required }}
{{ else }}
{{ range $prop, $value := $v.schema.properties}}
{{ $required := false }}
Expand All @@ -90,7 +92,7 @@
{{ $required = true }}
{{ end }}
{{ end }}
{{- template "renderProperty" dict "prop" $prop "value" $value "required" $required }}
{{- template "renderProperty" dict "title" $title "prop" $prop "value" $value "required" $required }}
{{ end }}
{{ end }}
{{ end }}
Expand All @@ -100,17 +102,21 @@
{{ end }}

{{- define "renderResponses" }}
{{ $title := .title }}
{{ $content := .content }}
<div class="responses regular-font">
<div class="openapi-table-title">Responses</div>
<div class="openapi-responses">
<ul class="openapi-table">
{{ range $status, $content := $content }}
{{ $statusId := printf "%s_res_%s" $title $status }}
<li class="openapi-table-row regular-font">
<div class="openapi-prop collapsed">
<span class="openapi-property-name bold-text">{{ $status }}</span>
<details id="{{ printf "%s" $statusId }}">
<summary class="openapi-prop collapsed">
<span class="openapi-property-name bold-text">{{ $status }}</span><a href="{{ printf "#%s" $statusId }}"><i class="fa fa-link" style="font-size: 12px;"></i></a>
</summary>
{{ $description := $content.description | markdownify }}
<div class="openapi-prop-content hidden">
<div class="openapi-prop-content">
<p class="regular-font">{{ $description }}</p>
{{ if $content.content }}
<ul class="openapi-table">
Expand All @@ -119,7 +125,7 @@
{{ if $v.schema.items }}
{{ $required := false }}
{{ $v }}
{{- template "renderProperty" dict "prop" "" "value" $v.schema "required" $required }}
{{- template "renderProperty" dict "title" $statusId "prop" "" "value" $v.schema "required" $required }}
{{ else }}
{{ range $prop, $value := $v.schema.properties}}
{{ $required := false }}
Expand All @@ -128,14 +134,14 @@
{{ $required = true }}
{{ end }}
{{ end }}
{{- template "renderProperty" dict "prop" $prop "value" $value "required" $required }}
{{- template "renderProperty" dict "title" $statusId "prop" $prop "value" $value "required" $required }}
{{ end }}
{{ end }}
{{ end }}
</ul>
{{ end }}
</div>
</div>
</details>
</li>
{{ end }}
</ul>
Expand All @@ -144,57 +150,62 @@
{{ end }}

{{- define "renderParameter" }}
{{ $title := .title }}
{{ $category := .category }}
{{ $prop := .prop }}
{{ $value := .value }}

{{ if eq $category $value.in }}
{{ $structName := "" }}
{{ $anchor := printf "%s_%s_%s" $title $category $prop }}
<li class="openapi-table-row regular-font">
<div class="openapi-prop collapsed">
<span class="openapi-property-name bold-text">{{ $prop }}{{ if $value.required }}*{{ end }}</span> <span class="openapi-property-type" style="font-weight: bold;"> {{ $value.schema.type }} </span>
<details id="{{ $anchor }}">
<summary class="openapi-prop collapsed"><span class="openapi-property-name bold-text">{{ $prop }}{{ if $value.required }}*{{ end }}</span><span class="openapi-property-type" style="font-weight: bold;"> {{ $value.schema.type }} </span><a href="{{ printf "#%s" $anchor }}"><i class="fa fa-link" style="font-size: 12px;"></i></a>
</summary>
{{ $description := $value.description | markdownify }}
<div class="openapi-prop-content hidden">
<div class="openapi-prop-content">
<p class="regular-font">{{ $description }}</p>
</div>
</div>
</details>
</li>
{{ end }}
{{ end }}

{{- define "renderProperty" }}
{{ $title := .title }}
{{ $prop := .prop }}
{{ $value := .value }}
{{ $required := .required }}
{{ $type := $value.type }}
{{ $anchor := printf "%s_%s" $title $prop }}
<li class="openapi-table-row regular-font">
{{ if eq $value.type "array" }}
{{ $type = printf "[%s]" $value.items.type }}
{{ else if eq $value.type "object" }}
{{ $type = "object" }}
{{ end }}
<div class="openapi-prop collapsed">
<span class="openapi-property-name bold-text {{ if eq $prop "" }} no-border {{ end }} ">{{ $prop }}{{ if $required }}*{{ end }}</span> <span class="openapi-property-type" style="font-weight: bold;"> {{ $type }} </span>
<details id="{{ $anchor }}">
<summary class="openapi-prop collapsed">
<span class="openapi-property-name bold-text {{ if eq $prop "" }} no-border {{ end }} ">{{ $prop }}{{ if $required }}*{{ end }}</span> <span class="openapi-property-type" style="font-weight: bold;"> {{ $type }} </span><a href="#{{ $anchor }}"><i class="fa fa-link" style="font-size: 12px;"></i></a>
</summary>
{{ $description := $value.description | markdownify }}
<div class="openapi-prop-content hidden">
<div class="openapi-prop-content">
<p class="regular-font">{{ $description }}</p>
{{ if $value.properties }}
<span class="openapi-table show-children bold-text"> Show Children</span>
<ul class="openapi-table hidden">
<ul class="openapi-table">
{{ range $k, $v := $value.properties }}
{{- template "renderProperty" dict "prop" $k "value" $v "required" $required }}
{{- template "renderProperty" dict "title" $anchor "prop" $k "value" $v "required" $required }}
{{ end }}
</ul>
{{ end }}
{{ if $value.items.properties }}
<span class="openapi-table show-children bold-text"> Show Children</span>
<ul class="openapi-table hidden">
<ul class="openapi-table">
{{ range $k, $v := $value.items.properties }}
{{- template "renderProperty" dict "prop" $k "value" $v "required" $required }}
{{- template "renderProperty" dict "title" $anchor "prop" $k "value" $v "required" $required }}
{{ end }}
</ul>
{{ end }}
</div>
</div>
</details>
</li>
{{ end }}
15 changes: 9 additions & 6 deletions site/themes/arangodb-docs-theme/static/css/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ a:hover {
text-decoration: underline;
}

.hidden {
display: none;
}

small {
font-size: 80%;
}
Expand Down Expand Up @@ -1722,7 +1718,7 @@ blockquote p {
color: var(--H2-COLOR);
}

.openapi-prop.collapsed::before {
.openapi-prop::before {
font-family: "Font Awesome 5 Free";
content: "\f054";
display: inline-block;
Expand All @@ -1733,7 +1729,7 @@ blockquote p {
color: grey;
}

.openapi-prop::before {
details[open] > .openapi-prop::before {
font-family: "Font Awesome 5 Free";
content: "\f078";
display: inline-block;
Expand All @@ -1743,6 +1739,13 @@ blockquote p {
font-size: 10px;
}

.openapi-prop > a > .fa-link {
opacity: 0;
}
.openapi-prop:hover > a > .fa-link {
opacity: 1;
}

.openapi-prop > code,
.openapi-prop > span {
pointer-events: none;
Expand Down
29 changes: 22 additions & 7 deletions site/themes/arangodb-docs-theme/static/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,6 @@ function loadPage(target) {
success: function(newDoc) {
replaceArticle(href, newDoc)
initArticle(href);
console.log(location.hash)
fragment = location.hash
if (fragment) {
fragment
document.getElementById(fragment.replace('#', '')).scrollIntoView();
}
return true;
}
});
Expand Down Expand Up @@ -197,7 +191,8 @@ $(window).on('hashchange', function (e) {
var _hsq = window._hsq = window._hsq || [];
_hsq.push(['setPath', window.location.href]);
_hsq.push(['trackPageView']);
console.log(e)

scrollToOpenApiFragment()
});


Expand Down Expand Up @@ -324,6 +319,25 @@ function hideEmptyOpenapiDiv() {
}
}

function scrollToOpenApiFragment() {
fragment = location.hash.replace("#", "")
if (fragment) {
var element = document.getElementById(fragment);
if (element.tagName == "DETAILS") {
method = fragment.split("_").slice(0,2).join("_")
fields = fragment.split("_").slice(2)
console.log(fields)
for (var i = 0; i < fields.length; i++) {
field = fields.slice(0, i+1).join("_")
var el = document.getElementById(method+"_"+field);
el.setAttribute("open", "")
el.childNodes[0].classList.remove("collapsed")
}
}
element.scrollIntoView();
}
}




Expand Down Expand Up @@ -436,4 +450,5 @@ window.onload = () => {

$('#show-page-loading').hide();
$('#page-wrapper').css("opacity", "1")
scrollToOpenApiFragment();
}