Skip to content

Commit

Permalink
Path segment links in browsable api
Browse files Browse the repository at this point in the history
  • Loading branch information
stevelacey committed Mar 30, 2023
1 parent 247e758 commit bef1e26
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion worf/templates/worf/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
<main>
{% block content %}
<nav class="bg-gray font-mono break-all my-4 px-4 py-2">
<strong>{{ request.method }}</strong> {{ request.get_full_path }}
<strong>{{ request.method }}</strong>
<span id="path">{{ request.get_full_path }}</span>
</nav>

<div class="my-4">
Expand Down Expand Up @@ -153,6 +154,37 @@
{% endif %}
</main>
</div>
<script>
const path = document.getElementById('path')
const parts = path.innerText.split('/')
const links = document.createElement('span')

for (let i = 0; i < parts.length; i++) {
const part = parts[i]

if (i === 0) {
continue;
}

links.appendChild(document.createTextNode('/'))

if (!part.length) {
continue;
}

const a = document.createElement('a');
const text = document.createTextNode(part)
const url = parts.slice(0, i + 1).join('/')
a.setAttribute('class', 'text-white')
a.setAttribute('href', parts[i + 1] !== undefined ? url + '/' : url);
a.setAttribute('rel', 'nofollow');
a.appendChild(text)

links.appendChild(a)
}

path.parentNode.replaceChild(links, path);
</script>
{% endblock %}
</body>
</html>

0 comments on commit bef1e26

Please sign in to comment.