<h1>Creating a table of content plugin<aname="creating-a-table-of-content-plugin"href="#creating-a-table-of-content-plugin"class="heading-anchor">🔗</a></h1>
<p>This tutorial will guide you through the steps to create a table of content plugin for Joplin. It will display a view next to the current note that will contain links to the sections of a note. It will be possible to click on one of the header to jump to the relevant section.</p>
<p>Through this tutorial you will learn about several aspect of the Joplin API, including:</p>
<ul>
<li>The plugin API</li>
<li>How to create a webview</li>
<li>How to listen to changes in the user interface</li>
</ul>
<p>## Setting up your environment</p>
<p>Before getting any further, make sure your environment is setup correctly as described in the <ahref="https://github.com/laurent22/joplin/blob/dev/readme/api/get_started/plugins/">Get Started guide</a>.</p>
<h2>Registering the plugin<aname="registering-the-plugin"href="#registering-the-plugin"class="heading-anchor">🔗</a></h2>
<p>All plugins must <ahref="https://joplinapp.org/plugins/api/classes/joplinplugins.html">register themselves</a> and declare what events they can handle. To do so, open <code>src/index.ts</code> and register the plugin as below. We'll also need to run some initialisation code when the plugin starts, so add the <code>onStart()</code> event handler too:</p>
<p>In order to create the table of content, you will need to access the content of the currently selected note, and you will need to refresh the TOC every time the note changes. All this can be done using the <ahref="https://joplinapp.org/plugins/api/classes/joplinworkspace.html">workspace API</a>, which provides information about the active content being edited.</p>
<p>So within the <code>onStart()</code> event handler, add the following:</p>
<p>Try the above and you should see in the console the event handler being called every time a new note is opened, or whenever the note content changes.</p>
<h2>Getting the note sections and slugs<aname="getting-the-note-sections-and-slugs"href="#getting-the-note-sections-and-slugs"class="heading-anchor">🔗</a></h2>
<p>Now that you have the current note, you'll need to extract the headers from that note in order to build the TOC from it. Since the note content is plain Markdown, there are several ways to do so, such as using a Markdown parser, but for now a quick and dirty solution is to get all the lines that start with any number of <code>#</code> followed by a space. Any such line should be a header.</p>
<p>The function below, which you can copy anywhere in your file, will use this method and return an array of headers, with the text and level (H1, H2, etc.) of header:</p>
<p>Later you will also need a way to generate the slug for each header. A slug is an identifier which is used to link to a particular header. Essentially a header text like "My Header" is converted to "my-header". And if there's already a slug with that name, a number is appended to it. Without going into too much details, you will need the "slug" package to generate this for you, so install it using <code>npm i -s slug</code> from the root of your plugin directory.</p>
<p>Then this is the function you will need for Joplin, so copy it somewhere in your file:</p>
<p>In order to display the TOC in Joplin, you will need a <ahref="https://joplinapp.org/plugins/api/classes/joplinviewspanels.html">webview panel</a>. Panels are a simple way to add custom content to the UI using HTML/CSS and JavaScript. First you would create the panel object and get back a view handler. Using this handler, you can set various properties such as the HTML content.</p>
<h2>Styling the view<aname="styling-the-view"href="#styling-the-view"class="heading-anchor">🔗</a></h2>
<p>In order to better integrate the TOC to Joplin, you might want to style it using CSS. To do so, first add a <code>webview.css</code> file next to <code>index.ts</code>, then you will need to let Joplin know about this file. This is done using the <code>addScript()</code> function (which is also used to add JavaScript files as we'll see later), like so:</p>
<p>This file is just a plain CSS file you can use to style your view. Additionally, you can access from there a number of theme variables, which you can use to better integrate the view to the UI. For example, using these variables you can use a dark background in dark mode, and a light one in light mode.</p>
<h2>Making the webview interactive<aname="making-the-webview-interactive"href="#making-the-webview-interactive"class="heading-anchor">🔗</a></h2>
<p>The next step is to make the TOC interactive so that when the user clicks on a link, the note is scrolled to right header. This can be done using an external JavaScript file that will handle the click events. As for the CSS file, create a <code>webview.js</code> file next to <code>index.ts</code>, then add the script to the webview:</p>
<p>If everything works well, you should now see the slug in the console whenever you click on a header link. The next step will be to use that slug to scroll to the right header.</p>
<h2>Passing messages between the webview and the plugin<aname="passing-messages-between-the-webview-and-the-plugin"href="#passing-messages-between-the-webview-and-the-plugin"class="heading-anchor">🔗</a></h2>
<p>For security reason, webviews run within their own sandbox (iframe) and thus do not have access to the Joplin API. You can however send messages to and from the webview to the plugin, and you can call the Joplin API from the plugin.</p>
<p>From within a webview, you have access to the webviewApi object, which among others has a <code>postMessage()</code> function you can use to send a message to the plugin. Let's use this to post the slug info to the plugin:</p>
<p>Then from the plugin, in <code>src/index.ts</code>, you can listen to this message using the <code>onMessage()</code> handler. Then from this handler, you can call the <code>scrollToHash</code> command and pass it the slug (or hash).</p>
<p>Various improvements can be made such as improving the styling, making the header collapsible, etc. but that tutorial should provide the basic building blocks to do so. You might also want to check the <ahref="https://joplinapp.org/plugins/api/classes/joplin.html">plugin API</a> for further information or head to the <ahref="https://discourse.joplinapp.org/c/development/6">development forum</a> for support.</p>