Git Product home page Git Product logo

vim-markdown-toc's Introduction

vim-markdown-toc

A vim 7.4+ plugin to generate table of contents for Markdown files.

中文版使用指南

Table of Contents

Features

  • Generate table of contents for Markdown files.

    Supported Markdown parsers:

    • GFM (GitHub Flavored Markdown)
    • GitLab
    • Redcarpet
  • Update existing table of contents.

  • Auto update existing table of contents on save.

Note: This plugin only works in Markdown files, that usually have a .[md|mdown|mkd|mkdn|markdown|mdwn|mdx] suffix. To check file type, please run :set ft, Markdown files will echo filetype=markdown.

Installation

Suggest to manage your vim plugins via vim-plug or Vundle, so you can install it simply three steps:

vim-plug

  1. add the following line to your vimrc file

    Plug 'mzlogin/vim-markdown-toc'
    
  2. :so $MYVIMRC

  3. :PlugInstall

Vundle

  1. add the following line to your vimrc file

    Plugin 'mzlogin/vim-markdown-toc'
    
  2. :so $MYVIMRC

  3. :PluginInstall

Usage

Generate table of contents

Move the cursor to the line you want to append table of contents, then type a command below suit you. The command will generate headings after the cursor into table of contents.

  1. :GenTocGFM

    Generate table of contents in GFM link style.

    This command is suitable for Markdown files in GitHub repositories, like README.md, and Markdown files for GitBook.

  2. :GenTocRedcarpet

    Generate table of contents in Redcarpet link style.

    This command is suitable for Jekyll or anywhere else use Redcarpet as its Markdown parser.

  3. :GenTocGitLab

    Generate table of contents in GitLab link style.

    This command is suitable for GitLab repository and wiki.

  4. :GenTocMarked

    Generate table of contents for iamcco/markdown-preview.vim which use Marked markdown parser.

You can view here to know differences between GFM and Redcarpet style toc links.

Update existing table of contents

Generally you don't need to do this manually, existing table of contents will auto update on save by default.

The :UpdateToc command, which is designed to update toc manually, can only work when g:vmt_auto_update_on_save turned off, and keep insert fence.

Remove table of contents

:RemoveToc command will do this for you, just remember keep insert fence option by default.

Options

  1. g:vmt_auto_update_on_save

    default: 1

    This plugin will update existing table of contents on save automatic.

    You can close this feature by add the following line to your vimrc file:

    let g:vmt_auto_update_on_save = 0
  2. g:vmt_dont_insert_fence

    default: 0

    By default, the :GenTocXXX commands will add <!-- vim-markdown-toc --> fence to the table of contents, it is designed for feature of auto update table of contents on save and :UpdateToc command, it won't effect what your Markdown file looks like after parse.

    If you don't like this, you can remove the fence by add the following line to your vimrc file:

    let g:vmt_dont_insert_fence = 1

    But then you will lose the convenience of auto update tables of contents on save and :UpdateToc command. When you want to update toc, you need to remove existing toc manually and rerun :GenTocXXX commands.

  3. g:vmt_fence_text

    default: vim-markdown-toc

    Inner text of the fence marker for the table of contents, see g:vmt_dont_insert_fence.

  4. g:vmt_fence_closing_text

    default: g:vmt_fence_text

    Inner text of the closing fence marker. E.g., you could let g:vmt_fence_text = 'TOC' and let g:vmt_fence_closing_text = '/TOC' to get

    <!-- TOC -->
    [TOC]
    <!-- /TOC -->
    
  5. g:vmt_fence_hidden_markdown_style

    default: ''

    By default, vim-markdown-toc will add the markdown style into the fence of the text for the table of contents. You can avoid this and set a default markdown style with g:vmt_fence_hidden_markdown_style that is applied if a fence is found containing the g:vmt_fence_text without any markdown style. Obviously, g:vmt_fence_hidden_markdown_style has to be supported, i.e. currently one of ['GFM', 'Redcarpet', 'GitLab', 'Marked'].

  6. g:vmt_cycle_list_item_markers

    default: 0

    By default, * is used to denote every level of a list:

    * [Level 1](#level-1)
        * [Level 1-1](#level-1-1)
        * [Level 1-2](#level-1-2)
            * [Level 1-2-1](#level-1-2-1)
    * [Level 2](level-2)
    

    If you set:

    let g:vmt_cycle_list_item_markers = 1

    every level will instead cycle between the valid list item markers *, - and +:

    * [Level 1](#level-1)
        - [Level 1-1](#level-1-1)
        - [Level 1-2](#level-1-2)
            + [Level 1-2-1](#level-1-2-1)
    * [Level 2](level-2)
    

    This renders the same according to Markdown rules, but might appeal to those who care about readability of the source.

  7. g:vmt_list_item_char

    default: *

    The list item marker, it can be *, - or +.

  8. g:vmt_include_headings_before

    default: 0

    Include headings before the position you are inserting Table of Contents.

  9. g:vmt_list_indent_text

    default: ''

    The indent text of list item. By default, if expandtab is set, it will be shiftwidth([2, 5]) number of space, otherwise it will be \t. If you set this option, it will override the default behavior.

  10. g:vmt_link

    default: 1

    Whether to generate link for headings.

  11. g:vmt_min_level

    default: 1

    The minimum level of headings to be included in the table of contents.

  12. g:vmt_max_level

    default: 6

    The maximum level of headings to be included in the table of contents.

Screenshots

References

vim-markdown-toc's People

Contributors

dandersson avatar holem2k avatar junegunn avatar lambillda avatar lilydjwg avatar liuchengxu avatar mzlogin avatar ndaman avatar powerman avatar raimon49 avatar rsrchboy avatar scrooloose avatar talkingtab avatar wget avatar whitedwarfsun avatar wsdjeg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vim-markdown-toc's Issues

GFM ordered list style not honored when using custom list_item_char `1.`

First off

Thanks for this great plugin. I know GFMs way of handling ordered lists is pretty weird, but it still would be nice if this plugin could handle it out of the box.

Issue

I want a numbered TOC. I achieved this by following the advice in this issue on vimwiki, setting g:vmt_list_item_char = '1.'.

This seems to work fine, but GFM is acting very rude, requiring 4 whitespaces of indentation in sublevels. To save some energy, I tried not to be so mad about that.

I inserted the following option in my vimrc as a workaround: g:vmt_list_indent_text = repeat(' ', 4). This is good for now, but I think it would be great to support numbered lists in general.

One issue now is, that if I want to change my item char back to e.g. -, I have to change indent text as well, or live with inconsistent indentation in my document, as my usual shiftwidth is 2 for markdown.

Also, if I use this TOC in other markdown formats, where two whitespaces are permitted, my hardcoded intent text will still be picked up.

Feature request

So my minor suggestions, with low prio, is to offer a global flag to generate numbered TOC. Use 1. as a prefix in this case. This global flag could then be checked for in s:GetIndentText. If it's true AND GFM style is used AND &expandtab is not set, don't use the &shiftwidth, but rather 4 whitespaces. Otherwise use tab or shiftwidth.

include section the toc is under in the toc

Currently it looks like the plugin ignores all headings above the point where you insert it. This leads to the case that when it is inserted under a # heading and after the toc comes a ## heading, the generated markdown list starts with an indentation, which will not be rendered as part of the list. Therefore the plugin should have an option to also include all the headings that are before its insert point

Wrong parsing in comment

I made this kind of python syntax highlighting codes in a markdown file.

Then this plugin thinks that the "# mod1.py" is a heading level 1.
Can you skip that kind of syntax highlighting blocks?

# mod1.py
def add(a, b):
    return a + b


def sub(a, b):
    return a - b

Delete part of document when folded on save

Looks like if TOC is inside folded block then whole block is deleted while auto-updating TOC on save and TOC is added few lines below deleted block. For example (require foldutil plugin), starting from document:

# test

<!-- vim-markdown-toc GitLab -->

* [one](#one)
* [two](#two)
    * [subtwo](#subtwo)
* [three](#three)

<!-- vim-markdown-toc -->

## one

## two

### subtwo

## three

Doing :w makes no change, but after :FoldMatching ^# -1 next :w will change document to:

## one

<!-- vim-markdown-toc GitLab -->

* [two](#two)
    * [subtwo](#subtwo)
* [three](#three)

<!-- vim-markdown-toc -->
## two

### subtwo

## three

Invalid range error

I'm not sure if I did something wrong, but every time I try to generate the TOC or run :UpdateToc, I am getting this error:

2016-11-21 0 21 02

The TOC is still getting generated correctly, just this error appears. I am using silent! now to ignore the error. Any thoughts?

Add a black line before toc

in the latest version of this plugin, the toc is looks like

<!-- vim-markdown-toc GFM -->
* [安装](#安装)
* [更新](#更新)
* [文档](#文档)
    * [核心**](#核心**)
        * [记忆辅助](#记忆辅助)
        * [可视化交互](#可视化交互)
        * [一致性](#一致性)
        * [社区驱动](#社区驱动)

<!-- vim-markdown-toc -->

but, after use remark to formate markdown file, it looks like:

<!-- vim-markdown-toc GFM -->

- [安装](#安装)
- [更新](#更新)
- [文档](#文档)
  - [核心**](#核心**)
    - [记忆辅助](#记忆辅助)
    - [可视化交互](#可视化交互)
    - [一致性](#一致性)
    - [社区驱动](#社区驱动)

<!-- vim-markdown-toc -->

I think it looks better, maybe you do not need to change the list item from * to -, but an option can be added for it.

for more info see https://github.com/wooorm/remark/tree/master/packages/remark-stringify#optionslistitemindent

and I will try to send a PR for this issue, and the previous issue I have opened.

Keep undo history

It would be cool if TOC modification done automatically done on save won't be added to undo history, so :w and u will undo last change user has done before saving, not TOC modification.

As example, https://github.com/fatih/vim-go plugin correctly does this while reformatting code on save by running external tools like gofmt or goimports.

How to remove TOC ?

I generated the TOC and its working well .. For now I want to remove it till I finish formatting t he document and then regenerate the TOC .. Is there a command for removing the TOC ?

Thanks in advance

If the head is a link, the toc generated is not expected.

For example :

### [vim-airline](https://github.com/vim-airline/vim-airline)

then the generated toc is :

###     * [[vim-airline](https://github.com/vim-airline/vim-airline)](#vim-airlinehttpsgithubcomvim-airlinevim-airline)

This will lead to a direct visit to the link. However, I hope it lead to the right position in the current page.

So, what I expected is :

### * [vim-airline](#vim-airline)

Removing existed insert fence, then UpdateToc manually raises error.

Error detected while processing function <SNR>120_UpdateToc[5]..<SNR>120_DeleteExistingToc:
line   29:
Cannot find toc begin fence
Error detected while processing function <SNR>120_UpdateToc:
line    8:
Cannot find existing toc

It's noted that insert fence followed is neccessay for auto save, but I don't need it and like running :UpdateToc manually, in which situation the error above occurs. I think it should be coverd.

<!-- vim-markdown-toc GFM -->
toc
<!-- vim-markdown-toc -->

Generate ordered list of contents

I find a numerically ordered toc more useful than an unordered list; the document has an order and sections are usually arranged in order.

Could we have an option to generate an ordered list instead of an unordered one?

eg:

1. Features
2. Installation
3. Usage
    1. Generate table of contents
    2. Update existing table of contents

links generated for sections with hyphens not working for Gitlab

Since you do not support GitLab, my issue is mor a backlog for other users.

Since GitHub keeps hyphens and GitLab deletes them when converting a header to an anchor.

The following code is valid for GitHub but not for GitLab

# Test code
<!-- vim-markdown-toc GFM -->

* [This subsection has an - in it](#this-subsection-has-a---in-it)

<!-- vim-markdown-toc -->

## This subsection has a - in it

To make it work, I had to add the following regex in GetHeadingLinkGFM (line 123):

let l:headingLink = substitute(l:headingLink, "-\\+", "-", "g")  # Converts successive hyphens to only one

Maybe you could easily add support for GitLab too, rules to create header anchors are summarized here.

The file generating anchors for headers in GitHub is to be found here. I'm not proficient in ruby but I don't see anything about deleting successive hyphens.

Can not navigate to the content when click title

Use Vundle to manage my vim plugins. Use vim-instant-markdown to preview markdown. Since I know vim-markdown-toc is a good plugin then just have a try, but I can only generate ToC with vim-markdown-toc, click any title, just can not navigate to the content?

插件无法工作

系统:windows10
vim版本:vim 8.2
错误提示:输入:GenTocGFM后提示 E492:不是编辑器的命令: GentocGFM
_vimrc:
set rtp+=$VIM\vimfiles\bundle\Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'airblade/vim-gitgutter'
Plugin 'tpope/vim-surround'
Plugin 'scrooloose/nerdcommenter'
Plugin 'scrooloose/nerdtree'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'zxqfl/tabnine-vim'
Plugin 'godlygeek/tabular'
Plugin 'plasticboy/vim-markdown'
Plugin 'mzlogin/vim-markdown-toc'
Plugin 'iamcco/markdown-preview.vim'
call vundle#end()

please add a new feature of updating existing toc

First of all, thanks for your good work, it's very convenient to generate toc now. If you add a new feature like updating existing toc when triggering some hotkeys, it would be more useful. Is it right?
I have no vim script programming experience, but I think this is a good idea for some document which contains a long table of contents. If you add a updating-toc feature, we don't have to delete existing toc again. And I think inserting toc before the document is finished is useful too, because it can help us navigate the document.
So I think add a updating-toc feature is very useful. Thanks!

generated toc for image heading is not expected

For example:

# ![Awesome Adb](./assets/title.png)

Now generated toc is:

* [![Awesome Adb](./assets/title.png)](#awesome-adbassetstitlepng)

Result excepted:

* [Awesome Adb](#-num)

Deep thinking:

Image format is ![describe](image-link), if describe is null, how to cover it?

TOC links with non ascii characters

I'm using pandoc to convert my markdown files to HTML format. When using :GenTocRedcarpet command, generated TOC links looks like:
[Wstępna konfiguracja karty](#wstępna-konfiguracja-karty)

Pandoc creates <h> tags that looks like:
<h2 id="wstepna-konfiguracja-karty">Wstępna konfiguracja karty</h2>

As you see, there are no letters other than ascii characters inside id attribute.

Is it possible to to pass link part of TOC through command like iconv -t ascii//TRANSLIT, so letters like ę, ą, ś will be replaced by e, a, s?

Regards,

Andrzej.

ctag无法处理markdown文档

我在网上看到你的文字,想请问以下,我通过网上的步骤设置ctag和markdown2ctag后还是无法看到md文档的大纲。请问你知道怎样排错吗?谢谢。

trim trailing space

bad case:

### 2. ```git status -s```  

expect link

#2-git-status--s

but now result is

#2-git-status--s-

Update toc should not change the undotree

When use this plugin to edit a md file, after saving file, the toc is updated, but when I type u the cursor move to the toc menu, I think the undotree of current file is been updated.

Configuration option missing from README.md

I wanted to indent table item with 2 spaces instead of 4 and found an option on script that does that and is missing in the documentation.

Change

let g:vmt_list_indent_text = ''

To

let g:vmt_list_indent_text = ' '

[Feature Request] Option to add line number on TOC generation, for quick item navigation (ex.: 35g)

First of all, thank you for the work invested on this plugin, and for sharing it with us!

The issue

Quickly navigate to a markdown TOC item corresponding section using vim line numbers: 35g

The feature

Provide the hability for the vim-markdown-toc to add the relative item line number before or after each TOC item.

Providing an option like:

let g:vmt_add_line_number_reference = 1

...that would add the line number and deliver something like this:

<!--- vim-markdown-toc GFM -->

line   5     * Introduction
line  35       ** How To Install
line  45       ** Hello World

<!-- /vim-markdown-toc -->

With a feature like that would be super cool quick and easy to navigate within the markdown file with vim native support.

I appreciate your time on considering that!

ignore heading in code blocks

If a markdown file contains python code block, and the block has comments lines begin with #, the comments will add to the TOC, it is not matching expect.

Option to use tabs rather than spaces?

It seems that vim-markdown-toc always uses hard tabs to indent subsections within a table of contents, even if expandtab is turned on in vim (and thus the user prefers spaces). It would more be elegant and fit in better with the users' other preferences if it honored that option.

Thanks for your fine work on this plugin!

Error with Vimwiki: Range too large in character class

Does anyone use Vimwiki with VMT? I just upgraded my instance of Vimwiki to 2.5 and now, any generated TOC link caused the following error when jumping to the item it points to:

Error Detected while processing function vimwiki#base#follow_link[69] .. vimwiki#base#open_link[34] .. vimwiki#base#edit_file[32] .. <SNR>46_jump_to_anchor[12] .. <SNR>46_jump_to_segment: 
line 23:
E945: Range to large in character class

However, I have no problem jumping from links generated by Vimwiki. Any pointer on how to investigate this error further?

Best,
Yohannes

Prettier gaurds move

I’m not sure what is going on or the exact repro. The symptom is sometimes the prettier gaurds that were around my toc marks move. A “phychic debugging” is the toc is written a few lines below it’s original line. I’ve tried a few things to isolate this, but haven’t found a consistent repro. I think it’s more likely if I hit save above the TOC, as opposed to below it, but sometimes I get a repro below as well. See video.

A6D283B8-641C-40B9-A18F-8B1DE3337155

Q: Is vim-markdown-toc abandoned, dead? (A: Yes)

Last PR over 18 months ago.

Last closed issue over a year ago.

No responses to any Issues.

vim-markdown-toc is not working at all for me (installled, but vim will not acknowledge that it is there), but no point in raising an issue.

generate wrong toc

src:

# hello


<!-- vim-markdown-toc GFM -->
            * [heading1](#heading1)
* [](#)

<!-- vim-markdown-toc -->

## heading1

##

插件不起作用的问题

由于你的插件自动集成了vim对markdown的支持,我安装后由于插件冲突找了一上午原因,原谅我在这吐槽,你应该在github和文章开头介绍以下,需要注释或者删除

"Plugin 'godlygeek/tabular'"
Plugin 'plasticboy/vim-markdown'

这两个插件,被弄的好蛋疼

Add option to enable for different filetypes

I'm using vimwiki with markdown syntax.

To generate the html I'm using livedown which itself uses marked.

That works great with using :GenTocRedcarpet. The only problem is that I have to run :set ft=markdown so that this plug loads and then run :set ft=vimwiki to use all the vimwiki features.

Once I've done that I can use :UpdateToc and it works great. Long story short I'd love to have option that lets me load this plugin for a different filetype than markdown.

need an option to set listitemchars.

@mzlogin in spacevim, we use remark to format markdwon file. vim-markdown-toc generate toc like

<!-- vim-markdown-toc GFM -->

* [安装](#安装)
* [更新](#更新)
* [文档](#文档)
    * [核心**](#核心**)
        * [记忆辅助](#记忆辅助)
        * [可视化交互](#可视化交互)
        * [一致性](#一致性)
        * [社区驱动](#社区驱动)

<!-- vim-markdown-toc -->

but after using remark formatting current buffer, the toc change to

<!-- vim-markdown-toc GFM -->

- [安装](#安装)
- [更新](#更新)
- [文档](#文档)
  - [核心**](#核心**)
    - [记忆辅助](#记忆辅助)
    - [可视化交互](#可视化交互)
    - [一致性](#一致性)
    - [社区驱动](#社区驱动)

<!-- vim-markdown-toc -->

we need an option to keep the same format. I will try to add this feature.

toc跳转问题

可以不用打开Toc(:Toc)直接在markdown文件里面的Toc(:GenTocGFM)跳转到标题处吗?
比如说有一个链接是[title](#header-two),比如直接从这上面按回车就能跳转过去。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.