透過 100 多個技巧的集合學習 Nuxt!

nuxt-toc
nuxt-toc

用於 Nuxt Content 專案中目錄 (TOC) 元件的 Nuxt 模組

中文

nuxt-toc

npm versionnpm downloadsLicenseNuxt

logo

一個用於 Nuxt Content 專案中目錄 (TOC) 元件的 Nuxt 模組。

功能 ✨

  • 🎨 高度可自訂:量身打造以符合您的獨特需求。
  • 🔍 活動目錄反白:輕鬆查看您所在的區塊。
  • 📦 開箱即用:只需最少的設定即可使用。
  • 🔗 章節連結:在您的內容中順暢導覽。
  • ARIA 支援:確保所有使用者皆可存取。
  • 🆓 免費且開放原始碼 (MIT 授權):享有使用、修改和散佈的自由。

快速開始 🔧

  1. 將模組安裝到您的 Nuxt 應用程式
npx nuxi module add nuxt-toc
  1. 在您需要目錄的地方新增 <TableOfContents />
<template>
    <ContentDoc />
    <TableOfContents />
</template>

您也可以自行傳入目錄,以防止重複提取。

<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>

Props

Prop類型預設值描述
tocJSONnull使用提供的 toc 資料。如果傳入 toc,此元件將不會自行提取目錄資訊,並且 path prop 將被忽略
path字串''要產生目錄的內容路徑。如果未設定,預設為使用當前 URI
isSublistShown布林值true決定是否顯示目錄中的子列表。
isTitleShownWithNoContent布林值布林值false
title字串'目錄'目錄的標題。

樣式

ID/類別類型描述
toc-containerID目錄 (TOC) 的容器。
toc-titleID目錄的標題。
toc-item類別目錄項目的通用類別。
toc-topitem類別頂層目錄項目的特定類別。
active-toc-item類別套用於活動目錄項目。
active-toc-topitem類別套用於活動頂層目錄項目。
toc-link類別目錄連結的通用類別。
toc-toplink類別頂層目錄連結的特定類別。
toc-sublist類別設定目錄中子列表的樣式。
toc-subitem類別子層目錄項目的特定類別。
active-toc-subitem類別套用於活動子層目錄項目。
toc-sublink類別子層目錄連結的特定類別。
toc-item-${link.id}ID基於 link.id 為每個目錄項目動態生成的 ID。
toc-topitem-and-sublist類別設定頂層目錄項目及其子列表的樣式。

!注意 <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;
}

範例

範例一

活動項目的自訂顏色和子項目的自訂內距

<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

範例二

在每個項目的左側加上一個條

<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

範例三

當任何第二層標題處於活動狀態時,第一層標題也處於活動狀態。

<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 授權。