5

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.

Mihai
  • 2,807
  • 4
  • 28
  • 53
  • I tried your code and it works for me... (I am running it inside a yeoman vscode extension project built by the CLI tool) (I have no other markdown plugins installed) have you tried installing this micro extension config into a fresh VS-Code installation? maybe some other config/installed thing is getting in the way – Ryu S. Nov 04 '21 at 01:21

0 Answers0