Fossil-FossMD

ToC for Fossil-FossMD
Login

ToC for Fossil-FossMD

Auto-generated Table of Contents in Mardkdown documents for Fossil-FossMD (and hopefully for Fossil as well)

See README.fossil-fossmd.toc.longlong.md for the long long ToC story (useless for most readers).

Example ToC

What you have: Example Markdown (only headers, other text omitted)


  #  Using a mouse
  ##  Basic mouse actions
  ###  Left button
  ###  Right button
  #### Context menus
  ##  Selecting
  ###  Click and drag
  ###  Double click

What you get: The rendered HTML Title and ToC (ToC at top of page by default)


Using a mouse

  1.  Basic mouse actions
    1.1.  Left button
    1.2.  Right button
      1.2.1. Context menus
  2.  Selecting
    2.1.  Click and drag
    2.2.  Double click

Note that the first header in a Markdown document, in this case # Using a mouse, is considered the document's title, so it should not be included in the ToC.

Install

The FossMD ToC is not enabled by default (for now, at least), so it must configured before compiled:

./configure ---with-md-toc
make

Usage

If compiled as mentioned above, ToC is enabled and rendered by default with any Markdown document in a Fossil repository.
No additional configuration is needed, and any existing Markdown documents do not have to be modified for the ToC to render.
If one or more ToC markers are inserted somewhere in the Markdown document, the ToC is rendered at the marker instead of after the document title.
See marker details in the "Supported ToC marker syntaxes" section.

Configuration flags

Configuration is only needed to customize the ToC.
The configuration syntax is the same as for any other Fossil settings.
The ToC configuration is a string composed by 5 configuration flags:

    fossil settings [--global] md-toc <depth|auto|trailing|top|style>

Example which resets the ToC configuration to the default settings:

    fossil unset [--global] md-toc

Examples which sets the ToC configuration to the default settings (same as the example above, but set explicitly):

    fossil settings [--global] md-toc 51111

To display the current settings:

    fossil settings md-toc

When setting the configuration, any invalid value is ignored. When reading an invalid value, the default settings are used.

Flag: depth

Chapter/section/subsection level to include in ToC.

Values:

Examples of how the depth flag affects the rendered ToC:

depth=1:


Using a mouse

  1.  Basic mouse actions
  2.  Selecting

depth=2:


Using a mouse

  1.  Basic mouse actions
    1.1.  Left button
    1.2.  Right button
  2  Selecting
    2.1. Click and drag
    2.2  Double click

depth=5:


Using a mouse

  1.  Basic mouse actions
    1.1.  Left button
    1.2.  Right button
      1.2.1. Context menus
  2  Selecting
    2.1. Click and drag
    2.2  Double click

Flag: auto

Values:

Flag: trailing

Values:

Flag: top

Values:

Examples of how the top flag affects the rendered headers (not the ToC):

Markdown header:

    ## Selecting

Rendered HTML:

top=0:

    <h2 id="2."><span class="chapter-label">2. </span>Selecting</h2>

top=1:

    <h2 id="2."><span class="chapter-label">2. </span>Selecting</h2><a class="href-top" href="#toc-top">Top</a>

Flag: style

Style for the Chapter Label (the 1.2.3.4.5. chapter prefix)

Values (most of the list below has shamelessly been copied from the DocBook documentation):

Example configurations

  1. Disable ToC (depth=0):

    fossil settings [--global] md-toc 01111

  2. Change depth (depth=4):

    fossil settings [--global] md-toc 41111

    See "Flag: depth" section for example output.

  3. Use manual ToC (auto=0):

    fossil settings [--global] md-toc 50111

    Using this setting, a ToC is only rendered if a ToC marker ([toc]) is inserted into the Markdown document.
    See marker details in the "Supported ToC marker syntaxes" section.

  4. No trailing dot (trailing=0):

    fossil settings [--global] md-toc 51011

  5. No top-link (top=0):

    fossil settings [--global] md-toc 51101

  6. Use Uppercase letters (style=A):

    fossil settings [--global] md-toc 5111A

    Output:

    Using a mouse

    A. Basic mouse actions
    A.A. Left button
    A.B. Right button
    A.B.A. Context menus
    B Selecting
    B.A. Click and drag
    B.B Double click

  7. Use Uppercase Roman numerals (style=I):

    fossil settings [--global] md-toc 5111I

    Output:

    Using a mouse

    I. Basic mouse actions
    I.I. Left button
    I.II. Right button
    I.II.I. Context menus
    II Selecting
    II.I. Click and drag
    II.II Double click

    Note:
    Taste is personal, but IMHO, using either Letters (A/a) or Roman numerals (I/i) in a ToC soon becomes illegible when used with subchapters at deeper levels.
    Recommended only with Header 1 headers.

  8. Use Arabic-Indic numerals (style=x661):

    fossil settings [--global] md-toc 5111x661

    Output:

    Using a mouse

    ١. Basic mouse actions
    ١.١. Left button
    ١.٢. Right button
    ١.٢.١. Context menus
    ٢ Selecting
    ٢.١. Click and drag
    ٢.٢ Double click

    Note:
    Except numbers from 0 - 9, my Arabic-Indic knowledge is non-existing, which explains why the rest of the example ToC above is written in English.

  9. Use customized Unicode style: Dingbats, style=x2780x2781x2782x2783x2784x2785x2786x2787x2788x1f10b, renders to ➀➁➂➃➄➅➆➇➈🄋 :

    fossil settings [--global] md-toc 5111x2780x2781x2782x2783x2784x2785x2786x2787x2788x1f10b

    Output:

    Using a mouse

    ➀. Basic mouse actions
    ➀.➀. Left button
    ➀.➁. Right button
    ➀.➁.➀. Context menus
    ➁ Selecting
    ➁.➀. Click and drag
    ➁.➁ Double click

How to test the ToC feature

Command-line:

        fossil test-markdown-render toc-test.md
        ./fossil-fossmd test-markdown-render toc-test.md

Browser:

toc-test.md

Source code for Chapter Level demo: toc/hl2chl.c

Supported ToC marker syntaxes

This list of markers is heavily inspired by the CommonMark Spec.

There are no Proposed Extension for ToC.
Anyway, there are several Deployed Extensions ToC:
NOTE: Example 7. example in the list below is my own proposal, rendered by default as a "Markdown comment".
The advantage of that syntax is that it does not render any HTML if the Markdown parser in use doesn't support ToC.
The disadvantage is that the syntax is not intiutive. :-(

Marker:

  1. {toc}

  2. [toc]

  3. [[toc]]

  4. @[toc](heading)

  5. <!-- toc -->

  6. <!-- md-toc -->

  7. [//]:#(TOC)

Rendered: (note that marker 5., 6. and 7. do not render any HTML output when ToC is disabled)

  1. {toc}

  2. [toc]

  3. [[toc]]

  4. @toc

HTML changes when enabling ToC

The rendered HTML for ToC is a nested ordered list inside a nav element:

<nav class="toc" id="toc-top">
  <ol>
    <li><a href="#1.">1. Basic Mouse Actions</a>
      <ol>
        <li>...</li>
      </ol>
    </li>
  </ol>
</nav>

The HTML for each header also changes when enabling ToC:

## Basic Mouse Actions

normally renders to

<h2>Basic Mouse Actions</h2>

but when enabling ToC, the HTML output changes to

<h2 id="1."><span class="chapter-label">1. </span>Basic Mouse Actions</h2> <a class="href-top" href="#toc-top">Top</a>

Note the added "top-link" after each header is included by default, as it (hopefully) makes navigation easier.
Anyway, the "top-link" may be disabled by the top flag, see below. Adding additional ToC:s (see "Supported ToC marker syntaxes") to a Markdown document do not affect the "top-link" target, as it always will point to the first ToC in the document.

When setting the DEPTH flag to a value lower than 5, some headers (i.e. subchapters) may be excluded from ToC.
These excluded headers are not modified in the document either.

How the depth flag affects the HTML changes

depth=5

Title is included in ToC and modified in document:

depth=4

Title is not included in ToC and not modified in document:

Respecting existing 'id' attribute in raw HTML headers

A Markdown Header Level 2 normally is written this way:

## Basic Mouse Actions

But as raw HTML is also allowed in Markdown documents, the header may perfectly be written this way:

Rendered HTML:

<h2 id="mylittleid">Basic Mouse Actions</h2>

When parsing the raw HTML Header, we cannot change the id attribute, as it may be referred from somewhere else in the document. That means that we need to preserve any existing HTML Header id, and copy it to the ToC, like this:

<nav class="toc" id="toc-top">
  <ol>
    <li><a href="#mylittleid">1. Basic Mouse Actions</a>
      <ol>
        <li>...</li>
      </ol>
    </li>
  </ol>
</nav>

And the rendered HTML Header will look like this:

<h2 id="mylittleid"><span class="chapter-label">1. </span>Basic Mouse Actions</h2> <a class="href-top" href="#toc-top">Top</a>

Read more

See README.fossil-fossmd.md for more info about Fossil-FossMD.
See README.fossmd.md for all gory details about Markdown extensions.
CommonMark: ATX Header syntax
CommonMark: Setext Header syntax
CommonMark: Proposed Extension for Markdown
CommonMark: Deployed Extensions for Markdown