%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/local/sakura-blog/themes/theme-basic/orange/v1.1.5/assets/js/
Upload File :
Create Path :
Current File : //usr/local/sakura-blog/themes/theme-basic/orange/v1.1.5/assets/js/toc.js

document.addEventListener('DOMContentLoaded', function() {
  // 本文にある見出しを全て取得
  const heads = document.querySelector("section.article__content").querySelectorAll("h1, h2, h3, h4, h5, h6");
  // 見出しがない場合、目次の枠組み自体を消す
  if (heads.length === 0) {
    document.querySelector(".table-of-content").remove();
    return;
  }

  let target = null;
  heads.forEach((v, i) => {
    // 各見出しにidを振る
    v.id = `heading-${i+1}`;
  
    if (this.location.hash === `#${v.id}`) {
      target = heads[i];
    }

    // 目次を作っていく
    let tocItem = document.createElement("li");
    tocItem.classList.add("table-of-content__list-item", v.localName);
  
    let tocLink = document.createElement("a");
    tocLink.href = `#${v.id}`;
    tocLink.innerText = v.innerText;
    tocItem.appendChild(tocLink);
    
    document.querySelector(".table-of-content__list").appendChild(tocItem);
  });
  
  if (target) {
    target.scrollIntoView({ behavior: 'smooth', block: 'start' });
  }
})

Zerion Mini Shell 1.0