VIM syntax highlighting of html nested in yaml

前端 未结 4 2110
臣服心动
臣服心动 2021-01-04 06:38

Given a yaml file that contains html, like this:

template        : |+ 
    
Hello, world

Is

4条回答
  •  春和景丽
    2021-01-04 07:05

    I used Maxy-B's solution. My code, in particular, is a bit different so I thought to post it for posterity:

    ~/.vim/after/syntax/yaml.vim

    let b:current_syntax = ''
    unlet b:current_syntax
    syntax include @HTML syntax/html.vim
    
    syntax region htmlCode start=#^html:#hs=e+1 end=+^\w+he=s-1,me=s-1
        \ contains=@HTML
    
    let b:current_syntax = ''
    unlet b:current_syntax
    syntax include @TEX syntax/tex.vim
    
    syntax region texCode start=#^tex:#hs=e+1 end=+^\w+he=s-1,me=s-1
        \ contains=@TEX
    

    This highlights the top-level YAML nodes html and tex with those respective types of code. It's not very dynamic, but it suits my purpose and this may serve as helpful guideline for someone doing something similar. It'll highlight the following as expected (or at least, as I expect it), for example:

    regular:  # yaml
       - yaml # yaml
    html: 
        hello # html
    tex:
        \begin{document} # tex
        \end{document} # tex
    the-end: may be necessary # yaml
    

提交回复
热议问题