This plugin for Scully provides a postRenderer
to generate a table of contents (TOC) for the rendered route content.
To install this library with npm
run:
npm i scully-plugin-toc --save-dev
To use the plugin you should import it in your Scully configuration file (scully.<project-name>.config.ts
) and define it as a postRenderer
for a route definition.
You can configure the plugin by using the toc
options:
import { ScullyConfig, setPluginConfig } from '@scullyio/scully';
import { getTocPlugin, TocConfig } = from './scully-plugins/toc';
const tocOptions: TocConfig = {
blogAreaSelector: '.blog-content', // where to search for TOC headings
insertSelector: '#toc', // where to insert the TOC
level: ['h2', 'h3'], // what heading levels to include
trailingSlash: true, // add trailing slash before the anker ('#')
scrollIntoViewOnClick: true // add event to each link that scrolls into view on click:
//
};
const TocPlugin = getTocPlugin();
setPluginConfig(TocPlugin, tocOptions);
export const config: ScullyConfig = {
projectRoot: './src',
projectName: 'your-project-name',
outDir: './dist/static',
routes: {
'/blog/:slug': {
type: 'contentFolder',
postRenderers: ['toc'],
slug: {
folder: './blog',
},
},
},
};
The TOC is generated by analyzing the headings (<h1>
, <h2>
, etc.) generated by Scully.
The above example configuration will look for an HTML element with the id toc
and insert the TOC at this place generated for headings <h2>
and <h3>
.
# my blog post
<div id="toc">
<h2>Table of contents</h2>
</div>
## heading 1
### subheading 1
## heading 2
### subheading 2
You can configure the scully-plugin-toc
by adding the toc
options to your route configuration.
The following table will explain the options in detail.
blogAreaSelector
: This defines the area in which thescully-plugin-toc
will search for headings. If you use for example<div class="blog"><scully-content></scully-content></div>
you should defineblogAreaSelector: ".blog"
to generate the TOC only from the blog content and not from the whole web page. If the parameter is not set, the plugin will search for heading at the whole page.insertSelector
: The selector defines the point where thescully-plugin-toc
will inset the generated TOC. By default, the plugin will use#toc
as selector. It will skip the TOC generation when there is no selector match. In fact to insert the TOC in a blog post, you should at least add a<div id="toc"></div>
in your blog post and this is the place where the TOC will be inserted.level
: This option defines the heading levels to include in the TOC. By default, the valuelevel: ['h2', 'h3']
is used. Only valid HTML headings are supported (h1
,h2
,h3
,h4
,h5
andh6
).trailingSlash
define weather a trailing slash will be added to the generated link just before the anker reference or not. Set this totrue
will lead into generated links like this:.../foo/#myTocLink
. If it'sfalse
(default), the generated link will look like this:.../foo#myTocLink
.scrollIntoViewOnClick
set totrue
will cause that to each link aonclick
event will be added that tries to scroll the target element id into the view:.