Sublime Forum

Syntax coloring Scala and YAML code blocks embedded in a Markdown document

#1

The Markdown syntax package for Sublime Text doesn’t handle Scala or YAML code blocks by default. It would be great if the package included this by default.

  • Fix was inspired by https://superuser.com/a/1047076
  • I installed the PackageResourceViewer via Package Control. Restart. Tools, Command Palette, prv to find PackageResourceViewer: Open Resource, choose the Markdown package, then open the Markdown.sublime-syntax resource. Search for .java. to find this block:
- match: |-
     (?x)
      {{fenced_code_block_start}}
      ((?i:java))
      \s*$\n?          # any whitespace until EOL
  captures:
    0: meta.code-fence.definition.begin.java.markdown-gfm
    2: punctuation.definition.raw.code-fence.begin.markdown
    3: constant.other.language-name.markdown
  embed: scope:source.java
  embed_scope: markup.raw.code-fence.java.markdown-gfm
  escape: '{{code_fence_escape}}'
  escape_captures:
    0: meta.code-fence.definition.end.java.markdown-gfm
    1: punctuation.definition.raw.code-fence.end.markdown
  • Copy and paste immediately after, replacing java with scala in the copy:
- match: |-
     (?x)
      {{fenced_code_block_start}}
      ((?i:scala))
      \s*$\n?          # any whitespace until EOL
  captures:
    0: meta.code-fence.definition.begin.scala.markdown-gfm
    2: punctuation.definition.raw.code-fence.begin.markdown
    3: constant.other.language-name.markdown
  embed: scope:source.scala
  embed_scope: markup.raw.code-fence.scala.markdown-gfm
  escape: '{{code_fence_escape}}'
  escape_captures:
    0: meta.code-fence.definition.end.scala.markdown-gfm
    1: punctuation.definition.raw.code-fence.end.markdown
  • Also add in YAML support similarly:
- match: |-
     (?x)
      {{fenced_code_block_start}}
      ((?i:yml|yaml))
      \s*$\n?          # any whitespace until EOL
  captures:
    0: meta.code-fence.definition.begin.yaml.markdown-gfm
    2: punctuation.definition.raw.code-fence.begin.markdown
    3: constant.other.language-name.markdown
  embed: scope:source.yaml
  embed_scope: markup.raw.code-fence.yaml.markdown-gfm
  escape: '{{code_fence_escape}}'
  escape_captures:
    0: meta.code-fence.definition.end.yaml.markdown-gfm
    1: punctuation.definition.raw.code-fence.end.markdown
  • On my Mac, the modified file will be saved as “${HOME}/Library/Application Support/Sublime Text 3/Packages/Markdown/Markdown.sublime-syntax” if you want to back it up.
0 Likes

Adding more syntax highlight to code blocks in markdown
#2

have you considered making a PR to https://github.com/sublimehq/Packages ?

0 Likes

#3

Ah, I didn’t realize that repo was available. Will do.

0 Likes