Skip to content

This auto-header-ids-toc VSCode extension generates HTML Table of Contents (TOC) (and Blogger post TOC) which includes auto-adding id attribute to header elements.

License

Notifications You must be signed in to change notification settings

ravisiyer/html-auto-header-ids

Repository files navigation

Auto Header IDs and TOC

This auto-header-ids-toc VSCode extension with display name of Auto Header IDs and TOC automatically adds an id attribute to HTML header elements in the current document and can also generate a Table of Contents (TOC).

This feature can be used to auto generate Table of Contents with internal links to h1 (Major Heading) and h2 (Heading) section headers in Blogger blog posts (involves editing post HTML in VSCode). For instructions on how to do that, please see my blog post.

Features

By default, this extension adds id attributes to <h1> and <h2> elements. It processes the header text to generate a unique, URL-friendly ID.

The ID value is generated by converting the header text to lowercase, replacing spaces and HTML entities (like &nbsp;) with hyphens, removing special characters, and truncating the result to a maximum of 15 characters to keep it concise. If a generated ID is not unique, a counter is appended to ensure uniqueness (e.g., section-1, section-2).

The extension provides three commands accessible from the Command Palette (F1 or Ctrl+Shift+P):

  • Auto Header IDs and TOC: Add IDs to Headers: Adds id attributes to headers that do not already have one. Headers with class="no-toc" are skipped.
  • Auto Header IDs and TOC: Create Table of Contents: Generates a Table of Contents based on headers that have an id and do not have the class="no-toc" attribute.
  • Auto Header IDs and TOC: Mark Configured Headers as no-toc: Adds the class="no-toc" attribute to headers, which prevents id attribute from being created for them and prevents them from being included in the TOC. A confirmation prompt will appear before this action. This command is useful for preparing HTML content which has its own headers (matching the headersToProcess Extension Setting), but which should not be included in the TOC. Once no-toc class is added to these headers, the associated HTML content can be safely added to the main document as both Add IDs to Headers command and Create Table of Contents command will ignore these headers. This command should be used with caution as it modifies all headers of the configured types in the document and so should not typically be used directly on documents which already have IDs added to configured headers by Add IDs to Headers command.

Note that all commands operate on the entire document, regardless of the current cursor position or selection. Only variation is that the Create Table of Contents command inserts the TOC at the current cursor position.


Installation

This extension is not yet available on the Visual Studio Code Marketplace. To install it, you must use the .vsix file from the releases page.

  1. Download the latest auto-header-ids-toc-*.vsix file from the releases.
  2. In VS Code, open the Extensions view (Ctrl+Shift+X).
  3. Click the three dots ... in the top right corner.
  4. Select "Install from VSIX..." and choose the downloaded file.

Extension Settings

This extension contributes the following setting, which you can modify in your VS Code settings.json file:

  • auto-header-ids-toc.headersToProcess: An array of strings that specifies which header elements should have IDs added.

    Example: To add IDs to only <h2> and <h3> tags, you would add the following to your settings.json:

    "auto-header-ids-toc.headersToProcess": ["2", "3"]

    The default value is ["1", "2"].


Source Code

The source code for this extension is available on GitHub: https://github.com/ravisiyer/html-auto-header-ids


0.0.3

  • Created blog post on how to use the extension to auto generate Table of Contents in Blogger posts.
  • Updated README with link to blog post.

0.0.2

  • Renamed extension to auto-header-ids-toc to reflect added TOC functionality.
  • Added new commands: auto-header-ids-toc.markHeadersAsNoTOC and auto-header-ids-toc.createTOC.
  • Improved slug generation logic.

0.0.1

Initial release of auto-header-ids.


Author

This extension was developed by Ravi S. Iyer with assistance from Gemini.


License

This extension is licensed under the MIT License. The full text of the license can be found in the LICENSE file in the project's repository.

About

This auto-header-ids-toc VSCode extension generates HTML Table of Contents (TOC) (and Blogger post TOC) which includes auto-adding id attribute to header elements.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published