npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ngx-toc

v1.0.2

Published

Angular library that makes toc from heading elements.

Readme

ngx-toc

ngx-toc is an Angular library that makes table-of-contents from heading elements.

Installation

To add ngx-toc library to your package.json use the following command.

npm install ngx-toc --save

Then add imports to NgModule.

import { NgxTocModule } from 'ngx-toc';

@NgModule({
  ...
  imports: [
    NgxTocModule,
  ...
})

Usage

This library provides service to create toc.

Use with static html

import { AfterViewInit, Component, ElementRef, Renderer2, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { TocService } from 'ngx-toc';

@Component({
  template: `
  <div #toc id="toc"></div>
  <div id="toc-target">
    <h1 id="h1-1">h1-1</h1>
    <h1 id="h1-2">h1-2</h1>
    <h2 id="h2-1">h2-1</h2>
    <h2 id="h2-2">h2-2</h2>
    <h3 id="h3-1">h3-1</h3>
    <h1 id="h1-3">h1-3</h1>
  </div>`,
  styleUrls: ['./demo2.component.css']
})
export class Demo2Component implements AfterViewInit {

  @ViewChild('toc') 
  element!: ElementRef;

  constructor(private tocService: TocService, private renderer: Renderer2, private router: Router) {
  }
  ngAfterViewInit(): void {
    this.renderer.appendChild(this.element.nativeElement, this.tocService.createToc('toc-target', ['h1', 'h2', 'h3'], this.router.url, this.renderer));
  }
}

You can find example at Github and Demo.

Use with ngx-markdown

ngx-markdown is the library to use markdown in Angular. ngx-toc can collaborate with this.

  • html
<div #toc id="toc"></div>
<markdown id="toc-target" [src]="path/to/file.md" (ready)="onReady()"></markdown>
<!-- <markdown></markdown> is ngx-markdown component. -->
  • component
import { Component, ElementRef, Renderer2, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { TocService } from 'ngx-toc';

@Component({
  selector: 'app-demo2',
  templateUrl: './demo2.component.html',
  styleUrls: ['./demo2.component.css']
})
export class Demo2Component {

  @ViewChild('toc') 
  toc!: ElementRef;

  constructor(private tocService: TocService, private renderer: Renderer2, private router: Router) {
  }

  onReady() {
    this.renderer.appendChild(this.toc.nativeElement, this.tocService.createToc('toc-target', ['h1', 'h2', 'h3'], this.router.url, this.renderer));
  }

}

You can find example at Github and Demo.

Parameters

TocService.createToc(targetId: string, targetHeadings: string[], path: string, renderer: Renderer2): HTMLElement

| parameter | type | description | example | | :- | :- | :- | :- | | targetId | string | id of target element to create toc | 'toc-target'if you'd like to create toc of following element.<div id="toc-target"><h1 id="bar">bar</h1></div> | | targetHeadings | string[] | heading tags to crate toc from | ['h1', 'h2', 'h3']then create toc from h1, h2, h3 tags. | | path | string | path of href value | '/foo'then href of toc is href="/foo#id". | | renderer | Renderer2 | renderer to render toc | - |

License

Licensed under MIT.