about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAyaz Hafiz <ayaz.hafiz.1@gmail.com>2020-07-02 21:33:46 -0700
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2021-08-17 21:55:26 -0500
commit3195d6c2b7fd2f958d823eb32bce0fb350bf6ec5 (patch)
tree2ddb565a8e5b036f8b06911142e3527b0a215af1
parent31c97ce0f067ddf5c01217cf172ef1ff260ca564 (diff)
downloadrust-3195d6c2b7fd2f958d823eb32bce0fb350bf6ec5.tar.gz
rust-3195d6c2b7fd2f958d823eb32bce0fb350bf6ec5.zip
docs: Preserve updated query param content and add config option links
- Preserve search and version parameter updates in the url
- Render headings with an anchor to itself for easier linking to config
  options, variants, etc.

Demo: https://5efeb81ca3f0d.htmlsave.net/
-rw-r--r--docs/index.html42
1 files changed, 34 insertions, 8 deletions
diff --git a/docs/index.html b/docs/index.html
index c221c6db71f..40fafd44428 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -41,6 +41,15 @@
         .searchCondition > div {
           margin-right: 30px;
         }
+        .header-link {
+          position: relative;
+        }
+        .header-link:hover::before {
+          position: absolute;
+          left: -2em;
+          padding-right: 0.5em;
+          content: '\2002\00a7\2002';
+        }
       </style>
     </head>
     <body>
@@ -137,12 +146,27 @@
                       }, []);
                   ast.links = {};
 
+                  queryParams.set('version', this.version);
+                  queryParams.set('search', this.searchCondition);
+                  const curUrl = window.location.pathname +
+                    '?' + queryParams.toString() + window.location.hash;
+                  history.pushState(null, '', curUrl);
+
+                  const renderer = new marked.Renderer();
+                  renderer.heading = function(text, level) {
+                    const id = htmlToId(text);
+                    return `<h${level}>
+                              <a href="#${id}" name="${id}" class="header-link">${text}</a>
+                            </h${level}>`;
+                  };
+
                   return marked.parser(ast, {
                     highlight(code, lang) {
                       return hljs.highlight(lang ? lang : 'rust', code).value;
                     },
                     headerIds: true,
-                    headerPrefix: ''
+                    headerPrefix: '',
+                    renderer,
                   });
                 }
               },
@@ -156,13 +180,10 @@
               },
               mounted() {
                 if (UrlHash === '') return;
-                const interval = setInterval(() => {
-                  const target = document.querySelector(`#${UrlHash}`);
-                  if (target != null) {
-                    target.scrollIntoView(true);
-                    clearInterval(interval);
-                  }
-                }, 100);
+                const target = document.querySelector(`#${UrlHash}`);
+                if (target != null) {
+                  target.scrollIntoView(true);
+                }
               }
             });
             const extractDepthOnes = (ast) => {
@@ -228,6 +249,11 @@
                 configurationDescriptions
               };
             }
+            function htmlToId(text) {
+              const tmpl = document.createElement('template');
+              tmpl.innerHTML = text.trim();
+              return encodeURIComponent(CSS.escape(tmpl.content.textContent));
+            }
         </script>
     </body>
 </html>