Markdown support #70

Closed
rscircus opened this Issue Mar 26, 2012 · 17 comments

Projects

None yet

8 participants

@rscircus

Is it possible to add markdown support easily? Thinking about a way via .ctags or .vimrc? Anyone any idea?

Label: Feature request

@cdlm
cdlm commented Mar 26, 2012

Language support is a matter of configuring ctags.

@rscircus

@cdlm
Yes, I was thinking about a .ctags w/

--langdef=markdown
--langmap=markdown:.mkd
--regex-markdown=/^#[ \t]+(.*)/\1/h,heading1/
--regex-markdown=/^##[ \t]+(.*)/\1/h,heading2/
--regex-markdown=/^###[ \t]+(.*)/\1/h,heading3/

but hope for more suggestions. :-)

@cdlm
cdlm commented Mar 26, 2012

Named link definitions, and maybe anchors (in markdown variants like kramdown)?

Other than that, I can't think about much more than the other heading levels and the two setext ones (with underlining), other file extensions (.md, .markdown at least, but there might be a couple more in use).

@majutsushi
Owner

One thing that would be nice of course is that the headings (and other tags) are correctly displayed as children of the higher-level ones they belong to. That would require a specialized script that keeps track of the last higher-level heading, but I don't think it should be too difficult. Any takers? :)

It's a bit unfortunate that ctags doesn't allow extending itself with small scripts like that so that you need to have half a dozen scripts for different languages lying around and need to call the correct one for each language.

@rscircus

@majutsushi
I'm on it (link). Currently I have a working thingy mixed into a local .ctags file and a tagbar-component in .vimrc which is quite close to the Scala-example, one can find on the net.

specialized script to keep track of headings

I don't know how nicely this blends into tagbar. Currently I don't have time to look into the tagbar code, I'll look into it at the weekend or somebody else does before ;-)

@majutsushi
Owner

You don't need to look at the Tagbar code, you just have to write a program/script in any language that outputs tag information in the same format as ctags. I've recently written a bit on how to do that at the bottom of :h tagbar-extend, it links to the supported format and which features Tagbar uses.

@tlvince
tlvince commented May 8, 2012

@rawland I tried your ~/.ctags and vimrc modifications to no avail. Is something other than simply copying in the g:tagbar_type_mkd rules required?

@majutsushi
Owner

@tlvince
The g:tagbar_type_mkd variable assumes that your markdown files have the filetype mkd. Is that the case for you? You can check what filetype Vim assigns to your markdown files by running :set filetype? while you're editing a markdown file. If the result is something other than mkd you will have to adjust the name of the tagbar type variable accordingly.

@tlvince
tlvince commented May 9, 2012

@majutsushi Thanks. My markdown files have the filetype markdown, so renaming g:tagbar_type_mkd to g:tagbar_type_markdown solved it.

@delaaxe
delaaxe commented Aug 29, 2012

@tlvince would you mind sharing your g:tagbar_type_markdown definition please?

edit: might as well add it to the wiki https://github.com/majutsushi/tagbar/wiki

@tlvince
tlvince commented Aug 30, 2012

@delaaxe, added to the wiki. Hope that helps.

@smancill

I use this in ~/.ctags:

--langdef=markdown
--langmap=markdown:.markdown
--regex-markdown=/^#[ \t]+(.*)/\. \1/h,heading1/
--regex-markdown=/^##[ \t]+(.*)/\.\. \1/h,heading2/
--regex-markdown=/^###[ \t]+(.*)/\.\.\. \1/h,heading3/

And this in ~/.vimrc:

    let g:tagbar_type_markdown = {
        \ 'ctagstype' : 'markdown',
        \ 'kinds' : [
            \ 'h:headings'
        \ ],
    \ 'sort' : 0,
    \ }

The result is something like the following (note that the dots are used to show the heading level):

headings
  . An h1 header
  .. An h2 header
  .. Another h2 header
  ... Now an h3 header
  . Another h1 header

I think it is better because it keeps the order of the headings in the document. It would be great to have proper folding by heading level, but I can live without it.

@jszakmeister
Contributor

I just pushed up a markdown2ctags script that will handle both the underline and ATX style headings. It also nests the headings correctly. The README includes how to use it with ctags as well.

I don't currently plan on supporting more than just headings, but maybe others can contribute if they'd like to see more tags supported.

I also have a similar script that I've been using for reStructuredText here.

@majutsushi
Owner

That's great, you should add them to the wiki :)
And I guess it means that I can close this issue ...

@jszakmeister
Contributor

Good idea. Done.

@majutsushi
Owner

Nice, thanks! By the way, I'm currently thinking about extracting the core ctags handling into a separate plugin that additional filetype plugins can register with, so the manual handling with the wiki would become unnecessary. But I'm not entirely sure about the best approach yet.

@majutsushi majutsushi closed this Nov 29, 2013
@anexusarchon

I use:

--langdef=markdown
--langmap=markdown:.md
--regex-markdown=/^(#+)[ \t]+([^#]*)/\1 \2/h,header,Markdown Headers/
--regex-markdown=/\[([^\[]+)\]\(([^\)]+)\)/\1/l,link,Markdown Links/
--regex-markdown=/!\[\]\(.*[\/ ](.*\.[a-z]{3})\)/\1/i,image,Markdown Image/

and

let g:tagbar_type_markdown = {
            \ 'ctagstype' : 'markdown',
            \ 'kinds' : [
                \ 'h:headings',
                \ 'l:links',
                \ 'i:images'
            \ ],
    \ "sort" : 0
\ }

Only issue is when you have 2 links on the same line, but thats an issue with ctags regex only being run once per line...

output looks like:

headings:
# Heading1
## SubHeading1
## SubHeading2
### MinorHeading1
### MinorHeading2

links:
Google
Github
Aardvarktopia

images:
screenshot.png
@sa-mm sa-mm referenced this issue in xndcn/symbols-tree-view Aug 14, 2015
Closed

Indent Markdown headings according to level #58

@tqmz tqmz pushed a commit to tqmz/dotfiles that referenced this issue Sep 27, 2016
Tim Quellmalz ctags markdown + tagbar config b3ea88f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment