Nuxt Nation カンファレンス開催!11月12日〜13日

nuxt-toc
nuxt-toc

Nuxt Content プロジェクトで目次 (TOC) コンポーネントを使用するためのNuxtモジュール

中国語

nuxt-toc

npm versionnpm downloadsLicenseNuxt

logo

Nuxt Content プロジェクトで目次 (TOC) コンポーネントを使用するためのNuxtモジュールです。

機能 ✨

  • 🎨 高度なカスタマイズ性:独自のニーズに合わせて調整できます。
  • 🔍 アクティブなTOCハイライト:現在表示中のセクションが簡単に分かります。
  • 📦 すぐに使える:最小限の設定で利用できます。
  • 🔗 セクションリンク:コンテンツ内をシームレスに移動できます。
  • ARIAサポート:すべてのユーザーのアクセシビリティを確保します。
  • 🆓 無料かつオープンソース (MITライセンス):自由に使用、変更、配布できます。

クイックスタート 🔧

  1. Nuxtアプリケーションにモジュールをインストールします。
npx nuxi module add nuxt-toc
  1. 目次が必要な場所に <TableOfContents /> を追加します。
<template>
    <ContentDoc />
    <TableOfContents />
</template>

重複したフェッチを防ぐために、自分でTOCを渡すこともできます。

<template>
    <ContentRenderer :value="data" />
    <TableOfContents :toc="data.body.toc" />
</template>

<script setup>
const route = useRoute()

const { data } = await useAsyncData('home', () => queryContent(route.path).findOne())
</script>

プロパティ

プロパティデフォルト説明
tocJSONnull提供された toc データを使用します。toc が渡された場合、このコンポーネントはTOC情報を自身でフェッチせず、path プロパティは無視されます
path文字列''TOCが生成されるコンテンツへのパスです。設定されていない場合は、現在のURIが使用されます
isSublistShownブール値trueTOC内のサブリストを表示するかどうかを決定します。
isTitleShownWithNoContentブール値falseTOCにコンテンツがない場合でもタイトルを表示するかどうかを決定します。
title文字列'Table of Contents'TOCのタイトルです。

スタイル

ID/クラス説明
toc-containerID目次 (TOC) のコンテナです。
toc-titleID目次(TOC)のタイトルです。
toc-itemクラスTOCアイテムの一般的なクラスです。
toc-topitemクラス最上位レベルのTOCアイテムの特定のクラスです。
active-toc-itemクラスアクティブなTOCアイテムに適用されます。
active-toc-topitemクラスアクティブな最上位レベルのTOCアイテムに適用されます。
toc-linkクラスTOCリンクの一般的なクラスです。
toc-toplinkクラス最上位レベルのTOCリンクの特定のクラスです。
toc-sublistクラスTOC内のサブリストをスタイル設定します。
toc-subitemクラスサブレベルのTOCアイテムの特定のクラスです。
active-toc-subitemクラスアクティブなサブレベルのTOCアイテムに適用されます。
toc-sublinkクラスサブレベルのTOCリンクの特定のクラスです。
toc-item-${link.id}IDlink.idに基づいて、各TOCアイテムに動的に生成されるIDです。
toc-topitem-and-sublistクラス最上位レベルのTOCアイテムとそのサブリストをスタイル設定します。

!注記 <TableOfContents /> コンポーネントのデフォルトのスタイルは

.active-toc-item {
  color: #fef08a;
}

.toc-sublist-item {
  padding-left: 1rem;
}

a {
  text-decoration: none;
  color: inherit;
}

ul,
ol {
  list-style: none;
  padding: 0;
  margin: 0;
}

スタイルをカスタマイズしたり、リセットしたりするには

.active-toc-item {
  color: initial;
}

.toc-sublist-item {
  padding-left: initial;
}

a {
  text-decoration: underline;
  color: initial;
}

ul,
ol {
  list-style: initial;
  padding: initial;
  margin: initial;
}

クックブック

例1

アクティブなアイテムのカスタムカラーとサブアイテムのカスタムパディング

<template>
    <ContentDoc />
    <TableOfContents />
</template>

<style>
/* Styling for active Table of Contents items */
.active-toc-item {
  color: #4ade80;
}

/* Indentation for second-level Table of Contents items */
.toc-sublist-item {
  padding-left: 1.5rem;
}
</style>

<!-- Or with Tailwind CSS
<style>
.active-toc-item {
  @apply text-green-300
}

.toc-sublist-item {
  @apply pl-1.5
}
</style>
-->

結果

example

例2

各アイテムの左側にバーを表示する

<template>
    <ContentDoc />
    <TableOfContents />
</template>

<style>
.toc-item {
  border-left-width: 2px;
  border-left-style: solid;
  border-color: #e5e7eb;
  padding-left: 0.25rem /* 4px */;
}

.active-toc-item {
  color: #60a5fa;
  border-color: #60a5fa;
}

.toc-sublist-item {
  padding-left: 1rem;
}
</style>

<!-- Or with Tailwind CSS
<style>
.toc-item {
  @apply border-l-2 pl-1
}

.active-toc-item {
  @apply text-blue-400 border-blue-400
}

.toc-sublist-item {
  @apply pl-4
}
</style>
-->

結果

example1

例3

2レベル目のタイトルがアクティブな場合、1レベル目のタイトルもアクティブにする

<template>
    <ContentDoc />
    <TableOfContents />
</template>

<style>
/* Sublist item is contained in sub list, which is top item's sibling */
.active-toc-item, .toc-topitem:has(+ .toc-sublist .active-toc-sublist-item) {
  color: #60a5fa
}

.active-toc-sublist-item {
  color: #4ade80
}

.toc-sublist-item {
  padding-left: 1rem /* 16px */;
}
</style>

<!-- Or with Tailwind CSS
<style>
.active-toc-item, .toc-topitem:has(+ .toc-sublist .active-toc-sublist-item) {
  @apply text-blue-400
}

.active-toc-sublist-item {
  @apply text-green-400
}

.toc-sublist-item {
  @apply pl-4
}
</style>
-->

結果

example2

ライセンス

このプロジェクトはMITライセンスです。