preservim / vim-markdown

Markdown Vim Mode
4.68k stars 521 forks source link

Allow fenced code block highlighting with vim-liquid #608

Closed m-pilia closed 1 year ago

m-pilia commented 1 year ago

Hi! This issue is somewhat similar to #159, but specific to fenced code block highlighting.

When using tpope's vim-liquid (for instance when editing Markdown files with Liquid templates), the buffer filetype is set to liquid but actual syntax highlighting is delegated to syntax/markdown. And that works great, except for fenced code blocks.

How to reproduce

A minimal working example to reproduce the issue:

  1. Install vim-markdown and vim-liquid
  2. Edit a file named example.md with the following content

    ---
    layout: post
    tags: [foo, bar]
    ---
    
    Example fenced block:
    ```vim
    let g:foo = 'bar'
    ```

Expected result: The fenced code block is highlighted with vim syntax highligting. Actual result: The fenced code block is highlighted as a generic mkdCode.

Issue

From what I see in ftplugin/markdown.vim, fenced blocks are only highlighted when filetype matches markdown, so that prevents it from running on liquid buffers. The filetype to match is hard-coded, so there is no way to customise this from the user's point of view.

function! s:MarkdownRefreshSyntax(force)
    if &filetype =~# 'markdown' && line('$') > 1 && &syntax != 'OFF'
        call s:MarkdownHighlightSources(a:force)
    endif
endfunction

Proposed solution

It would be nice to extend this feature to support liquid files by checking the value of b:liquid_subtype. This way, the plugin would be compatible with vim-liquid out-of-the-box. I implemented this proposal in #609.

A more general solution could also be to add a setting variable that allows the user to enable MarkdownHighlightSources on a custom list of filetypes. That would be a viable alternative if you do not want to create any coupling at all with other plugins such as vim-liquid, and rather let the user sort the issue out through their own configuration. This might be an overkill however compared to just adding compatibility with vim-liquid (a fairly popular plugin from a well-known author), since it adds maintenance cost (new settings, documentation, etc.) and I am not sure how useful such setting would be outside of this specific use case.