Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax highlighting for GFM fenced backtick code blocks #15

Closed
brianbuccola opened this issue Mar 10, 2017 · 7 comments
Closed

Syntax highlighting for GFM fenced backtick code blocks #15

brianbuccola opened this issue Mar 10, 2017 · 7 comments

Comments

@brianbuccola
Copy link

brianbuccola commented Mar 10, 2017

Now that Jekyll (since v3.3.0) has embraced GFM fenced backticks for code blocks (see, e.g., here and here), would it be possible to add syntax highlighting for it in vim-liquid, just like what already exists in vim-markdown?

Alternatively, if this is too Jekyll-specific, how about adding syntax highlighting for YAML frontmatter in vim-markdown?

As it stands, if I edit a file with ft=liquid, I don't get fenced backtick highlighting, but if I set ft=markdown, I don't get YAML frontmatter highlighting.

@tpope
Copy link
Owner

tpope commented Mar 10, 2017

So if I read this correctly, it's not that Liquid added any special support, but rather they are moving away from their own {% highlight %} declaration and instead encouraging the use of Markdown's syntax. If that's the case, shouldn't this be sufficient?

@brianbuccola
Copy link
Author

brianbuccola commented Mar 11, 2017

So what you're saying is that because vim-markdown already supports highlighting between fenced backticks, and because vim-liquid sets the liquid subtype to markdown, vim-liquid should thereby already be supporting highlighting between fenced backticks?

For some reason, it doesn't work for me. Code between fenced backticks is colored with a single color, regardless of whether a language is given. I also ran echo(b:liquid_subtype) and it correctly returns markdown.

@tpope
Copy link
Owner

tpope commented Mar 11, 2017

You have to set g:markdown_fenced_languages to a list of filetypes.

@brianbuccola
Copy link
Author

I've got

let g:markdown_fenced_languages=['bash=sh', 'css', 'haskell', 'html', 'latex=tex', 'python', 'ruby']

and also

let g:liquid_highlight_types=g:markdown_fenced_languages

for good measure. Syntax highlighting with fenced backticks works totally fine with set ft=markdown, and conversely, syntax highlighting with liquid {% highlight %} tags works totally fine with set ft=liquid (this , because g:liquid_highlight_types=g:markdown_fenced_languages).

What doesn't work is syntax highlighting with fenced backticks with set ft=liquid.

What am I missing?

@tpope
Copy link
Owner

tpope commented Mar 13, 2017

Uh, not sure. I can confirm it's not working for me either, and I would consider that a bug.

@craigmac
Copy link

I've just encountered this as well. Is there a quick way to get around this like copying some portion of the g:markdown_fenced_languages stuff in $VIMRUNTIME/syntax/markdown.vim into .vim/after/ftplugin/liquid.vim or something? Grepping $VIMRUNTIME I found that $VIMRUNTIME/syntax/rmd.vim does their own fenced highlighting, based on the value of g:markdown_fenced_languages, but I can't figure out how to adopt their approach. Any ideas?

@craigmac
Copy link

Adopting the example in $VIMRUNTIME/syntax/rmd.vim I was able to hack together this, which I put at the end of the vim-liquid/syntax/liquid.vim file, and now the code blocks are being highlighted in liquid files. I'm sure you'll be able to do a much better job, but it's a start:

if exists('g:markdown_fenced_languages')
  if !exists('g:liquid_fenced_languages')
    let g:liquid_fenced_languages = deepcopy(g:markdown_fenced_languages)
    let g:markdown_fenced_languages = []
  endif
else
  let g:liquid_fenced_languages = []
endif

if !empty(g:liquid_fenced_languages)
  for s:lang in g:liquid_fenced_languages
    " When we have an alias we want to split it, e.g. 'bash=sh'
    if s:lang =~# '='
      let s:syntax_file = substitute(s:lang, '.*=', '', '') " => 'sh'
      let s:codeblock_name = substitute(s:lang, '=.*', '', '') " => 'bash'
    else
      let s:syntax_file = s:lang
      let s:codeblock_name  = s:lang
    endif
    unlet! b:current_syntax
    " examples:
    " execute 'syntax include @Liquidbash syntax/sh.vim' when entry is 'bash=sh'
    " execute 'syntax include @Liquidcpp syntax/cpp.vim' when entry is 'cpp'
    execute 'syntax include @Liquid'.s:codeblock_name.' syntax/'.s:syntax_file.'.vim'
    " Define region INSIDE backquotes as match
    execute 'syntax region liquidFenced'.s:codeblock_name.' matchgroup=liquidCodeDelimiter start="^\s*```\s*'.s:codeblock_name.'\>.*$" matchgroup=liquidCodeDelimiter end="^\s*```\ze\s*$" keepend contains=@Liquid'.s:codeblock_name
    unlet! s:lang
  endfor
endif

hi def link liquidCodeDelimiter Delimiter

craigmac added a commit to craigmac/vimfiles that referenced this issue Mar 18, 2022
liquid wasn't doing fenced highlighting like markdown does, so
I hacked together an implementation by referencing how
/usr/local/share/vim/vim82/syntax/rmd.vim did it, and put it in
~/.vim/after/syntax/liquid.vim and it works now. Need to test more
before creating a PR for upsteam at https://github.com/tpope/vim-liquid.

See the issue tpope/vim-liquid#15 for
explanation and my solution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants