From f628456c06b0a57ebbf0a413f1d0d58fb37a9c48 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Wed, 18 Mar 2020 16:30:43 -0600 Subject: [PATCH] improve implementation for #99 and add example to preview site --- preview-src/index.adoc | 5 +++++ src/js/02-fragment-jumper.js | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/preview-src/index.adoc b/preview-src/index.adoc index 25df1c1..d07cf0f 100644 --- a/preview-src/index.adoc +++ b/preview-src/index.adoc @@ -211,6 +211,11 @@ Ex nam suas nemore dignissim, vel apeirian democritum et. .Antora is a multi-repo documentation site generator image::multirepo-ssg.svg[Multirepo SSG,250] +Make the switch today! + +[#english+中文] +== English + 中文 + Altera atomorum his ex, has cu elitr melius propriae. Eos suscipit scaevola at. diff --git a/src/js/02-fragment-jumper.js b/src/js/02-fragment-jumper.js index e3b6ed5..5f9462a 100644 --- a/src/js/02-fragment-jumper.js +++ b/src/js/02-fragment-jumper.js @@ -4,6 +4,10 @@ var article = document.querySelector('article.doc') var toolbar = document.querySelector('.toolbar') + function decodeFragment (hash) { + return hash && (~hash.indexOf('%') ? decodeURIComponent(hash) : hash).slice(1) + } + function computePosition (el, sum) { if (article.contains(el)) { return computePosition(el.offsetParent, el.offsetTop + sum) @@ -21,8 +25,8 @@ } window.addEventListener('load', function jumpOnLoad (e) { - var hash, target - if ((hash = window.location.hash) && (target = document.getElementById(hash.slice(1)))) { + var fragment, target + if ((fragment = decodeFragment(window.location.hash)) && (target = document.getElementById(fragment))) { jumpToAnchor.bind(target)() setTimeout(jumpToAnchor.bind(target), 0) } @@ -30,8 +34,8 @@ }) Array.prototype.slice.call(document.querySelectorAll('a[href^="#"]')).forEach(function (el) { - var hash, target - if ((hash = decodeURIComponent(el.hash.slice(1))) && (target = document.getElementById(hash))) { + var fragment, target + if ((fragment = decodeFragment(el.hash)) && (target = document.getElementById(fragment))) { el.addEventListener('click', jumpToAnchor.bind(target)) } }) -- 2.11.0