Skip to content

Lightweight Table of Contents generator for Ghost blog platform. Zero dependencies, collapsible functionality, works via code injection. Just add <toc> tag to your posts!

License

Notifications You must be signed in to change notification settings

vlavrynovych/ghost-toc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

29 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Ghost TOC

A lightweight, customizable Table of Contents generator for Ghost blog platform.

Note: While designed specifically for Ghost, this script will work on any webpage that has an <article> tag containing heading elements (H2-H6). The Ghost-specific features include filtering out Ghost's author name heading and integration with Ghost's code injection system.

Preview

Ghost TOC Example See it in action

Ghost TOC automatically generates a hierarchical table of contents from your article headings.

Features shown:
β€’ Clean, minimal design
β€’ Hierarchical structure (H2, H3)
β€’ Clickable navigation links
β€’ Customizable title

Features

  • πŸš€ Automatic TOC generation from article headings (H2-H6)
  • 🎨 Customizable styling with border and background colors
  • πŸ“± Collapsible functionality with show/hide toggle
  • πŸ’Ύ State persistence - remembers collapsed/expanded preference
  • 🎯 Heading level filtering - choose which levels to include
  • 🚫 Exclude specific headings by ID
  • πŸ“ Multiple list styles - bullets, numbers, or none
  • 🎨 Custom CSS classes for advanced styling
  • πŸ”— Smooth anchor navigation
  • β™Ώ Screen reader accessible with ARIA attributes
  • πŸ’ͺ Zero dependencies
  • πŸ“¦ Lightweight (~3KB minified)
  • 🌍 Multi-language support via customizable title attribute
  • πŸ”„ Single point of maintenance - update once, apply everywhere
  • πŸ’Ύ Reduces database size - no script duplication across posts

Installation

Using Code Injection (Recommended)

  1. Go to your Ghost Admin panel
  2. Navigate to Settings β†’ Code Injection
  3. In the Site Footer section, copy and paste the content from dist/ghost-toc.min.js
  4. Click Save

Manual Installation

Download the dist/ghost-toc.min.js file and include it in your Ghost theme's footer.

πŸ“– For detailed installation instructions with pro tips, see INSTALLATION.md

⚑ Want to get started quickly? Check out the Quick Start Guide

Usage

Basic Usage

Add the <toc> tag anywhere in your Ghost article where you want the table of contents to appear:

<toc title="Table of Contents"></toc>

Collapsible TOC

Create a collapsible table of contents with custom styling:

<toc
  title="Table of Contents"
  collapsible="true"
  show-text="Show"
  hide-text="Hide"
  border-color="gainsboro"
  bg-color="aliceblue">
</toc>

Configuration Options

Attribute Type Default Description
title string - Title displayed at the top of TOC
collapsible boolean false Enable collapsible functionality
show-text string "Show" Text for expand button
hide-text string "Hide" Text for collapse button
default-state string "expanded" Initial state for collapsible TOC (collapsed or expanded)
levels string "2,3,4,5,6" Comma-separated heading levels to include (e.g., "2,3" for H2 and H3 only)
class string - Custom CSS classes to add to TOC container
remember-state boolean false Persist TOC state in localStorage across page loads
list-style string "bullets" List marker style (bullets, numbers, or none)
exclude string - Comma-separated heading IDs to exclude from TOC
border-color string "gainsboro" Border color for collapsible TOC
bg-color string "aliceblue" Background color for collapsible TOC

🎨 Want more customization options? See the Customization Guide

How It Works

Ghost TOC automatically:

  1. Scans your article for heading elements (H2-H6)
  2. Filters out Ghost's default author name heading
  3. Validates that headings have id attributes (skips those without, logs warning)
  4. Builds a hierarchical navigation structure
  5. Generates clickable links with anchor navigation
  6. Applies custom styling based on your configuration
  7. Adds ARIA attributes for screen reader accessibility

Examples

See the examples/ directory for complete implementation examples:

Why Use Ghost TOC?

Traditional Approach Problems

  • ❌ Script duplicated in every single post
  • ❌ Database bloat from repeated code
  • ❌ Updating requires editing all posts manually
  • ❌ Inconsistent behavior across posts

Ghost TOC Benefits

  • βœ… One-Time Setup: Install script once in footer, use everywhere
  • βœ… Database Efficient: Only a small <toc> tag per post
  • βœ… Easy Updates: Change footer script, all posts update automatically
  • βœ… Consistent Experience: Same behavior across your entire blog
  • βœ… Multi-language Ready: Different titles for different language versions

Real-World Use Cases

Technical Blogs

Long tutorials and documentation benefit from hierarchical navigation through complex topics.

Multi-language Sites

Easily customize TOC titles per language without maintaining separate scripts:

<!-- English post -->
<toc title="Table of Contents"></toc>

<!-- Spanish post -->
<toc title="Tabla de Contenidos"></toc>

Long-form Content

Articles with multiple sections become more navigable and user-friendly.

Documentation Sites

Perfect for Ghost-powered documentation with structured content.

The Story Behind Ghost TOC

Ghost TOC was born from a simple frustration: Ghost didn't have built-in table of contents functionality, and the existing solutions weren't good enough.

The Problem (2024)

When writing comprehensive articles, I needed a way to help readers navigate through sections. Manual TOCs were tedious and error-proneβ€”every time you changed a heading, you had to update the links. The recommended solution, Tocbot, had its own issues:

  • Only worked in sidebars, limiting design flexibility
  • Required loading external dependencies from CDNs
  • Needed direct server file modifications (default.hbs, post.hbs) that many Ghost users couldn't access

There had to be a better way.

Version 1.0 - The Beginning

The first version (v1.0 Gist, blog post) solved the core problem:

  • βœ… Pure JavaScriptβ€”no external dependencies
  • βœ… Works anywhere in your post, not just sidebars
  • βœ… Installed via Ghost's Code Injection (no server access needed)
  • βœ… Simple <toc> tag in your posts
  • πŸ“ Supported H2 and H3 headings (2 levels)

But there was a catch: the script had to be duplicated in every single post. This meant database bloat and maintenance headaches when updates were needed.

Version 2.0 - The Evolution

V2 (v2.1 Gist, blog post) revolutionized the approach:

  • πŸ”„ One script in the footer, unlimited use in postsβ€”just add the <toc> tag where needed
  • πŸ“Š Full heading hierarchy (H2-H6) added in May 2024
  • πŸ“± Collapsible functionality added in December 2024
  • 🌍 Multi-language support through customizable titles
  • πŸ’Ύ Database efficiencyβ€”no more duplicated code

Today - The Repository

What started as a Gist has grown into a full-fledged project with:

  • πŸ—οΈ Automated build system with minification
  • πŸ“š Comprehensive documentation and examples
  • 🎨 Advanced customization options
  • 🌐 Compatibility beyond Ghost (works on any HTML page)

Ghost TOC proves that sometimes the best solutions come from scratching your own itch. What began as a simple workaround is now used by bloggers worldwide to make their content more navigable and user-friendly.

Migration from Gist

If you're currently using the Gist version, simply replace it with the code from this repository's dist/ghost-toc.min.js file. The API and functionality remain the same - no changes needed to your <toc> tags!

Using Outside of Ghost

While Ghost TOC is designed for the Ghost platform, it can be used on any website with the following requirements:

Requirements

  1. Your page must have an <article> tag containing your content
  2. Headings must be properly structured (H2-H6) with id attributes for anchor links
    • Note: Headings without id attributes will be automatically skipped with a console warning
  3. Include the script before the closing </body> tag

Generic HTML Usage

<!DOCTYPE html>
<html>
<head>
    <title>My Article</title>
</head>
<body>
    <article>
        <toc title="Table of Contents"></toc>

        <h2 id="section-1">Section 1</h2>
        <p>Content here...</p>

        <h3 id="subsection-1-1">Subsection 1.1</h3>
        <p>More content...</p>

        <h2 id="section-2">Section 2</h2>
        <p>Content here...</p>
    </article>

    <script src="path/to/ghost-toc.js"></script>
</body>
</html>

WordPress, Drupal, or Static Sites

Simply include the script in your theme's footer and add the <toc> tag where needed. Most modern CMS platforms automatically generate heading IDs, making this script compatible out of the box.

Development

Building from Source

To build the minified distribution file from source:

# Install dependencies
npm install

# Build minified version
npm run build

This will minify src/ghost-toc.js and output to dist/ghost-toc.min.js.

Automated Workflows

GitHub Actions automatically handles builds:

  • Auto Build: When you push changes to src/, the minified file is automatically rebuilt and committed
  • Release: When you create a GitHub release, the minified file is automatically attached as a downloadable asset

See .github/workflows/README.md for details.

For detailed build instructions, see BUILD.md.

Project Structure

ghost-toc/
β”œβ”€β”€ src/
β”‚   └── ghost-toc.js              # Source code (edit this)
β”œβ”€β”€ dist/
β”‚   └── ghost-toc.min.js          # Minified distribution (auto-generated)
β”œβ”€β”€ docs/                         # Documentation
β”œβ”€β”€ examples/                     # Working examples
└── scripts/                      # Build scripts

License & Credits

License: MIT License - see LICENSE file for details

Author: Volodymyr Lavrynovych

Community:

About

Lightweight Table of Contents generator for Ghost blog platform. Zero dependencies, collapsible functionality, works via code injection. Just add <toc> tag to your posts!

Topics

Resources

License

Stars

Watchers

Forks