From a3f273839d1b45fe73b3bc48e1d7a8c6d1422187 Mon Sep 17 00:00:00 2001 From: Hiroshi Shirosaki Date: Mon, 18 Jan 2016 11:03:51 +0900 Subject: [PATCH] Add TOML/JSON front matter support Add new options to enable front matter. Fix #188, #242 --- Makefile | 8 ++++-- README.md | 22 +++++++++++++++-- syntax/markdown.vim | 17 +++++++++++++ test/run-tests.sh | 2 +- test/syntax.vader | 59 +++++++++++++++++++++++++++++++++++++++++++++ test/vimrc | 1 + 6 files changed, 104 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 5d23114..a50adc0 100644 --- a/Makefile +++ b/Makefile @@ -18,18 +18,22 @@ install: mkdir -pv ${REGISTRY} cp -v registry/markdown.yaml ${REGISTRY}/markdown.yaml -test: build/tabular build/vader.vim +test: build/tabular build/vim-toml build/vader.vim test/run-tests.sh .PHONY: test -update: build/tabular build/vader.vim +update: build/tabular build/vim-toml build/vader.vim cd build/tabular && git pull + cd build/vim-toml && git pull cd build/vader.vim && git pull .PHONY: update build/tabular: | build git clone https://github.com/godlygeek/tabular build/tabular +build/vim-toml: | build + git clone https://github.com/cespare/vim-toml build/vim-toml + build/vader.vim: | build git clone https://github.com/junegunn/vader.vim build/vader.vim diff --git a/README.md b/README.md index 1243577..60c58ec 100644 --- a/README.md +++ b/README.md @@ -137,14 +137,32 @@ Used as `$x^2$`, `$$x^2$$`, escapable as `\$x\$` and `\$\$x\$\$`. let g:vim_markdown_math = 1 ``` -#### YAML frontmatter +#### YAML Front Matter -Highlight YAML frontmatter as used by Jekyll: +Highlight YAML front matter as used by Jekyll or [Hugo](https://gohugo.io/content/front-matter/). ```vim let g:vim_markdown_frontmatter = 1 ``` +#### TOML Front Matter + +Highlight TOML front matter as used by [Hugo](https://gohugo.io/content/front-matter/). + +TOML syntax highlight requires [vim-toml](https://github.com/cespare/vim-toml). + +```vim +let g:vim_markdown_toml_frontmatter = 1 +``` + +#### JSON Front Matter + +Highlight JSON front matter as used by [Hugo](https://gohugo.io/content/front-matter/). + +```vim +let g:vim_markdown_json_frontmatter = 1 +``` + ## Mappings The following work on normal and visual modes: diff --git a/syntax/markdown.vim b/syntax/markdown.vim index 778e818..05cf751 100644 --- a/syntax/markdown.vim +++ b/syntax/markdown.vim @@ -110,6 +110,23 @@ syn match mkdRule /^\s*\*\{3,5}$/ if get(g:, 'vim_markdown_frontmatter', 0) syn include @yamlTop syntax/yaml.vim syn region Comment matchgroup=mkdDelimiter start="\%^---$" end="^---$" contains=@yamlTop + unlet! b:current_syntax +endif + +if get(g:, 'vim_markdown_toml_frontmatter', 0) + try + syn include @tomlTop syntax/toml.vim + syn region Comment matchgroup=mkdDelimiter start="\%^+++$" end="^+++$" transparent contains=@tomlTop + unlet! b:current_syntax + catch /E484/ + syn region Comment matchgroup=mkdDelimiter start="\%^+++$" end="^+++$" + endtry +endif + +if get(g:, 'vim_markdown_json_frontmatter', 0) + syn include @jsonTop syntax/json.vim + syn region Comment matchgroup=mkdDelimiter start="\%^{$" end="^}$" contains=@jsonTop + unlet! b:current_syntax endif if get(g:, 'vim_markdown_math', 0) diff --git a/test/run-tests.sh b/test/run-tests.sh index 731b3ce..ba00fc0 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -5,7 +5,7 @@ set -e cd "$( dirname "${BASH_SOURCE[0]}" )" -for dep in ../build/tabular ../build/vader.vim; do +for dep in ../build/tabular ../build/vim-toml ../build/vader.vim; do if [[ ! -d $dep ]]; then echo "Missing dependency: $dep" echo "You may just want to use 'make test'." diff --git a/test/syntax.vader b/test/syntax.vader index ecf36b7..f2fc936 100644 --- a/test/syntax.vader +++ b/test/syntax.vader @@ -489,6 +489,65 @@ Execute (rules are not mistaken by YAML frontmatter delimiters): let g:vim_markdown_frontmatter=1 syn off | syn on AssertEqual SyntaxAt(5, 1), 'mkdRule' + unlet g:vim_markdown_frontmatter + +# TOML frontmatter + +Given markdown; ++++ +a = "b" ++++ + +Execute (TOML frontmatter is controlled by the option): + syn off | syn on + AssertNotEqual SyntaxOf('b'), 'tomlString' + let g:vim_markdown_toml_frontmatter=1 + syn off | syn on + AssertEqual SyntaxOf('b'), 'tomlString' + let g:vim_markdown_toml_frontmatter=0 + syn off | syn on + AssertNotEqual SyntaxOf('b'), 'tomlString' + +Given markdown; + ++++ +a = "b" ++++ + +Execute (TOML frontmatter only works if it's the first thing in the file): + let g:vim_markdown_toml_frontmatter=1 + syn off | syn on + AssertNotEqual SyntaxOf('b'), 'tomlString' + unlet g:vim_markdown_toml_frontmatter + +# JSON frontmatter + +Given markdown; +{ + "a": "b" +} + +Execute (JSON frontmatter is controlled by the option): + syn off | syn on + AssertNotEqual SyntaxOf('a'), 'jsonKeyword' + let g:vim_markdown_json_frontmatter=1 + syn off | syn on + AssertEqual SyntaxOf('a'), 'jsonKeyword' + let g:vim_markdown_json_frontmatter=0 + syn off | syn on + AssertNotEqual SyntaxOf('a'), 'jsonKeyword' + +Given markdown; + +{ + "a": "b" +} + +Execute (JSON frontmatter only works if it's the first thing in the file): + let g:vim_markdown_json_frontmatter=1 + syn off | syn on + AssertNotEqual SyntaxOf('a'), 'jsonKeyword' + unlet g:vim_markdown_json_frontmatter # Header diff --git a/test/vimrc b/test/vimrc index 524fe6c..015febf 100644 --- a/test/vimrc +++ b/test/vimrc @@ -1,6 +1,7 @@ set nocompatible set rtp+=../ set rtp+=../build/tabular/ +set rtp+=../build/vim-toml/ set rtp+=../build/vader.vim/ let $LANG='en_US' filetype on -- 2.39.5