Skip to content

Commit

Permalink
Embed metadata about the current page instead of reading from the ren…
Browse files Browse the repository at this point in the history
…dered html
  • Loading branch information
Nemo157 authored and syphar committed Mar 20, 2024
1 parent 1ee11ec commit 3f7b37b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
19 changes: 4 additions & 15 deletions static/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
function formatCrateName(crateTitleAndVersion) {
const stringParts = crateTitleAndVersion.split(" ", 2);
return stringParts[0] + ' = "' + stringParts[1] + '"';
}

(function() {
const clipboard = document.getElementById("clipboard");
if (clipboard) {
Expand All @@ -14,23 +9,17 @@ function formatCrateName(crateTitleAndVersion) {
clipboard.innerHTML = resetClipboardIcon;
}

function copyTextHandler() {
const crateTitleAndVersion = document.getElementById("crate-title");
// On rustdoc pages, we use `textTransform: uppercase`, which copies as uppercase.
// To avoid that, reset the styles temporarily.
const oldTransform = crateTitleAndVersion.style.textTransform;
crateTitleAndVersion.style.textTransform = "none";
const temporaryInput = document.createElement("input");
async function copyTextHandler() {
const metadata = JSON.parse(document.getElementById("crate-metadata").innerText)

const temporaryInput = document.createElement("input");
temporaryInput.type = "text";
temporaryInput.value = formatCrateName(crateTitleAndVersion.innerText);
temporaryInput.value = `${metadata.name} = "${metadata.version}"`;

document.body.append(temporaryInput);
temporaryInput.select();
document.execCommand("copy");

temporaryInput.remove();
crateTitleAndVersion.style.textTransform = oldTransform;

clipboard.textContent = "✓";
if (resetClipboardTimeout !== null) {
Expand Down
8 changes: 8 additions & 0 deletions templates/rustdoc/topbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
{%- include "header/topbar_begin.html" -%}{#
extra whitespace unremovable, need to use html tags unaffacted by whitespace T_T
#}<ul class="pure-menu-list">
<script id="crate-metadata" type="application/json">
{# the only text that needs escaping in a <script> is `</`, none of the
values below can include that sequence #}
{
"name": {{ metadata.name | json_encode() | safe }},
"version": {{ metadata.version | json_encode() | safe }}
}
</script>
{%- if krate -%}
<li class="pure-menu-item pure-menu-has-children">
<a href="#" class="pure-menu-link crate-name" title="{{ krate.description }}">
Expand Down

0 comments on commit 3f7b37b

Please sign in to comment.