From 15d306cf147061e4ff65d7ceedee72acac007c53 Mon Sep 17 00:00:00 2001 From: FujiHaruka Date: Sun, 4 Aug 2019 18:03:08 +0900 Subject: [PATCH] Enable to add anchor to header elements --- assets/js/scripts.js | 26 ++++++++++++++++++++++++++ assets/scss/components/_anchor.scss | 10 ++++++++++ assets/scss/style.scss | 1 + layouts/_default/single.html | 2 +- 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 assets/scss/components/_anchor.scss diff --git a/assets/js/scripts.js b/assets/js/scripts.js index 19d4ec6..0c697d0 100644 --- a/assets/js/scripts.js +++ b/assets/js/scripts.js @@ -7,3 +7,29 @@ menuTrigger.onclick = function() { menuTrigger.classList.toggle('is-active') body.classList.toggle('lock-scroll') } + +var content = document.querySelector('.content.anchor-link-enabled') +if (content) { + addHeaderAnchors(content); +} + +function addHeaderAnchors(content) { + var headers = content.querySelectorAll('h1, h2, h3, h4'); + // SVG data from https://iconmonstr.com/link-1-svg/ + var linkSvg = ' '; + var anchorForId = function (id) { + var anchor = document.createElement('a'); + anchor.classList.add('header-anchor'); + anchor.href = "#" + id; + anchor.innerHTML = linkSvg; + return anchor; + }; + + for (var h = 0; h < headers.length; h++) { + var header = headers[h]; + + if (typeof header.id !== "undefined" && header.id !== "") { + header.appendChild(anchorForId(header.id)); + } + } +} diff --git a/assets/scss/components/_anchor.scss b/assets/scss/components/_anchor.scss new file mode 100644 index 0000000..8b494e6 --- /dev/null +++ b/assets/scss/components/_anchor.scss @@ -0,0 +1,10 @@ +.content { + .header-anchor { + text-decoration: none; + fill: currentColor; + opacity: 0.6; + } + .header-anchor:hover { + opacity: 1; + } +} diff --git a/assets/scss/style.scss b/assets/scss/style.scss index 31373c2..ee54235 100755 --- a/assets/scss/style.scss +++ b/assets/scss/style.scss @@ -33,6 +33,7 @@ @import 'components/strip'; @import 'components/whitebox'; @import 'components/overview'; +@import 'components/anchor'; // Pages @import 'pages/home'; diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 3c930e1..67d1129 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -4,7 +4,7 @@ {{ define "main" }}

{{.Title}}

-
+
{{.Content}}