0

I am writing a .tmLanguage file to highlight the markdown file(.md), and there are some cpp codes in the markdown files. I want use the c++.tmLanguage (in Packages\C++) to solve the syntax problem. How can I do it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Qing Ye
  • 11
  • 1

1 Answers1

2

The best way to learn how to write language definitions is to look at existing ones. There are plenty of existing Markdown syntax definitions in Package Control, so I'd suggest you look through them to see how things are structured.

Assuming you're using PackageDev's .YAML-tmLanguage format, repositories for fenced code would look like this:

repository:
  fenced-c:
    name: markup.raw.block.markdown markup.raw.block.fenced.markdown
    begin: (\s*```)\s*(c)\s*$
    end: (\1)\n
    captures:
      '1': {name: punctuation.definition.fenced.markdown}
      '2': {name: variable.language.fenced.markdown}
    patterns:
    - include: source.c

  fenced-c++:
    name: markup.raw.block.markdown markup.raw.block.fenced.markdown
    begin: (\s*```)\s*(c\+\+)\s*$
    end: (\1)\n
    captures:
      '1': {name: punctuation.definition.fenced.markdown}
      '2': {name: variable.language.fenced.markdown}
    patterns:
    - include: source.c++

  fenced-coffee:
    name: markup.raw.block.markdown markup.raw.block.fenced.markdown
    begin: (\s*```)\s*(coffee)\s*$
    end: (\1)\n
    captures:
      '1': {name: punctuation.definition.fenced.markdown}
      '2': {name: variable.language.fenced.markdown}
    patterns:
    - include: source.coffee

  # etc.

Translated to XML using the built-in conversion tool, that would look like so:

<key>repository</key>
<dict>
    <key>fenced-c</key>
    <dict>
        <key>begin</key>
        <string>(\s*```)\s*(c)\s*$</string>
        <key>captures</key>
        <dict>
            <key>1</key>
            <dict>
                <key>name</key>
                <string>punctuation.definition.fenced.markdown</string>
            </dict>
            <key>2</key>
            <dict>
                <key>name</key>
                <string>variable.language.fenced.markdown</string>
            </dict>
        </dict>
        <key>end</key>
        <string>(\1)\n</string>
        <key>name</key>
        <string>markup.raw.block.markdown markup.raw.block.fenced.markdown</string>
        <key>patterns</key>
        <array>
            <dict>
                <key>include</key>
                <string>source.c</string>
            </dict>
        </array>
    </dict>
    <key>fenced-c++</key>
    <dict>
        <key>begin</key>
        <string>(\s*```)\s*(c\+\+)\s*$</string>
        <key>captures</key>
        <dict>
            <key>1</key>
            <dict>
                <key>name</key>
                <string>punctuation.definition.fenced.markdown</string>
            </dict>
            <key>2</key>
            <dict>
                <key>name</key>
                <string>variable.language.fenced.markdown</string>
            </dict>
        </dict>
        <key>end</key>
        <string>(\1)\n</string>
        <key>name</key>
        <string>markup.raw.block.markdown markup.raw.block.fenced.markdown</string>
        <key>patterns</key>
        <array>
            <dict>
                <key>include</key>
                <string>source.c++</string>
            </dict>
        </array>
    </dict>
    <key>fenced-coffee</key>
    <dict>
        <key>begin</key>
        <string>(\s*```)\s*(coffee)\s*$</string>
        <key>captures</key>
        <dict>
            <key>1</key>
            <dict>
                <key>name</key>
                <string>punctuation.definition.fenced.markdown</string>
            </dict>
            <key>2</key>
            <dict>
                <key>name</key>
                <string>variable.language.fenced.markdown</string>
            </dict>
        </dict>
        <key>end</key>
        <string>(\1)\n</string>
        <key>name</key>
        <string>markup.raw.block.markdown markup.raw.block.fenced.markdown</string>
        <key>patterns</key>
        <array>
            <dict>
                <key>include</key>
                <string>source.coffee</string>
            </dict>
        </array>
    </dict>
<!-- and so on... -->

I urge you to use PackageDev, as YAML is so much easier to work with than XML. Alternatively, you can use the new YAML-based .sublime-syntax format, but it is currently only supported in the Sublime Text 3 development builds 3084 and later, which means you need to be a registered user to access it. I'd recommend sticking with .YAML-tmLanguage for now, until a new public build is released with support for the new format.

You can look in the PackageDev Github repo to find more examples of .YAML-tmLanguage files. Additionally, I am the lead developer on the Python Improved project, and you can find all my source files on Github as well.

MattDMo
  • 100,794
  • 21
  • 241
  • 231