How to inject a TextMate grammar to a Markdown heading in VS Code?

543 views Asked by At

I want to select [ ] for syntax highlighting within a markdown file in VS Code.

I can target [ ] using the following regex: (?<=\s)\]|\[(?=\s). However [ ] doesn't get matched when it is part of a heading.

As a minimal example, in the package.json I have the following:

"contributes": {
    "grammars": [
        {
            "scopeName": "markdown.todo",
            "path": "./syntaxes/todo.markdown.json",
            "injectTo": ["text.html.markdown"]
        }
    ]
}

Then, in the todo.markdown.json I have:

{
    "scopeName": "markdown.todo",
    "injectionSelector": "L:text.html.markdown",
    "patterns": [
        { "include": "#item-todo" }
    ],
    "repository": {
        "item-todo": {
            "name": "item.todo",
            "match": "(?<=\\s)\\]|\\[(?=\\s)"
        }
    }
}

Finally, in the VS Code settings.json I apply a color using:

"editor.tokenColorCustomizations": {
    "textMateRules": [
        {
            "scope": "item.todo",
            "settings": {
                "foreground": "#FF0000"
            }
        }
    ]
}

I can see below that [ ] gets selected, but not when it is within a heading.

enter image description here

When I inspect the tokens and scopes I see several textmate scopes. I am not sure if this is related, but it seems that VS Code is highlighting the markdown headings based on the markup.heading scope. However, markup.heading is not present in the textmate scopes array.

enter image description here

I tried changing to "injectionSelector": "L:heading.1.markdown" and/ or "injectTo": ["heading.1.markdown"], but no matter what selectors I specify I cannot seem to be able to match [ ] within a heading.

0

There are 0 answers