mzlogin / vim-markdown-toc

A vim 7.4+ plugin to generate table of contents for Markdown files.
http://www.vim.org/scripts/script.php?script_id=5460
MIT License
609 stars 59 forks source link

Adds new option to create anchors on headings #70

Open rderik opened 4 years ago

rderik commented 4 years ago

Currently, when the Table of Contents is created the links are built in the table of contents but no anchor is created on the titles.

This pull requests contains an optional feature that allows us to create an anchor with the following format:

<a name="SLUG-GENERATED-BY-THE-PLUGIN"></a>

The anchor is enclosed in the fence-text so it can be updated if the g:vmt_auto_update_on_save option is enabled. So it can be deleted and updated in the same manner as the Table of Contents.

To activate the new anchor creation options we do it by setting the variable:

g:vmt_insert_anchors = 1

Developer notes:

To implement this new feature we modify the return value of the folllowing function:

function! s:GetHeadingLines()

Now we return an array containing a touple(represented by an array) with the following content:

["heading", lineNum]

The lineNum represents the position of this heading, so we can later decide to insert the anchor before the title.

The anchor option supports auto update when enabled.