# Body Index Module

This Drupal 10 module automatically generates a hierarchical table of contents (TOC) from `h2` to `h6` HTML tags within a node's body field, provided these tags have an `id` attribute. The generated TOC is stored as JSON in a dedicated field and rendered as an HTML list with anchor links, allowing for easy navigation within long content.

## Features

*   Automatically generates a hierarchical index from `h2` to `h6` tags with `id` attributes in the body field.
*   Stores the index as JSON in a dedicated `string_long` field.
*   Provides a custom Field Formatter to render the JSON index as an HTML list with anchor links.
*   Allows per-content-type configuration to enable or disable the indexing functionality.

Note: If you encounter issues, ensure your Drupal cache is cleared after enabling.*

## Configuration

1.  **Add the Content Index field**: This module does not automatically create the field on content types. You need to manually add a new field to the desired content types:
    *   Navigate to `Structure > Content types` (`/admin/structure/types`).
    *   Click on `Reusing a existing field` for the content type you want to enable the index for (e.g., `Article` or `Page`).
    *   Select `content_index ` field.
    *   Set the label.
    +   Select limited to 1 the configuration.
    *   Save the field settings.

2.  **Configure Content Type Settings**: To enable the indexing functionality for a specific content type:
    *   Navigate to `Structure > Content types` (`/admin/structure/types`).
    *   Click `Edit` for the content type (e.g., `Article`).
    *   Scroll down to the `Body Index Settings` vertical tab.
    *   Check the `Enable Body Index for this content type` checkbox.
    *   Save the content type.

3.  **Configure Field Display**: To display the generated index on your content:
    *   Navigate to `Structure > Content types` (`/admin/structure/types`).
    *   Click `Manage display` for the content type (e.g., `Article`).
    *   Locate the `Content Index` field.
    *   Set its format to `Body Index`.
    *   Save the display settings.

4.  **Configure Form Display: To hidden the field on the form
    *   Navigate to admin/structure/types/manage/$node/form-display
    *   Disabled the new file.

## Usage

Once configured, when you create or edit content of the enabled type, the module will automatically scan the `Body` field for `h2`, `h3`, `h4`, `h5`, and `h6` tags that have an `id` attribute. It will then generate a hierarchical index based on these headings and store it in the `Content Index` field. The custom field formatter will render this JSON data as an interactive HTML list.

**Example HTML in your Body field:**

```html
<h2 id="introduction">Introduction</h2>
<p>Some introductory text.</p>
<h3 id="section-1">Section 1</h3>
<p>Content for section 1.</p>
<h4 id="subsection-1-1">Subsection 1.1</h4>
<p>Content for subsection 1.1.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Concluding remarks.</p>
```

This will generate a TOC similar to:

```html
<ul>
  <li><a href="#introduction">Introduction</a>
    <ul>
      <li><a href="#section-1">Section 1</a>
        <ul>
          <li><a href="#subsection-1-1">Subsection 1.1</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li><a href="#conclusion">Conclusion</a></li>
</ul>
```

## Recommendation

To easily add `id` attributes to your heading tags within the CKEditor, it is highly recommended to use the [CKEditor ID Attributes](https://www.drupal.org/project/ckeditor_id_attributes) module. This module provides a convenient way to manage IDs directly from the editor interface, streamlining the process of creating indexable content.
