Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Language Definition for highlight.js

I want to define my own language in highlight.js, but I'm wondering how to do this. I found this guide

Language definition guide with the following example. Does anybody know where I to put this configuration so that highlight.js use this configuration?

{
  case_insensitive: true, // language is case-insensitive
  keywords: 'for if while',
  contains: [
    {
      className: 'string',
      begin: '"', end: '"'
    },
    hljs.COMMENT(
      '/\\*', // begin
      '\\*/', // end
      {
        contains: [
          {
            className: 'doc', begin: '@\\w+'
          }
        ]
      }
    )
  ]
}
like image 878
Thomas Krakow Avatar asked Oct 16 '25 17:10

Thomas Krakow


1 Answers

Define a language with hljs.registerLanguage.

First argument: name of language.

Second argument: a function that returns an object

hljs.registerLanguage('language name', function() {
  return {
    case_insensitive: true, // language is case-insensitive
    keywords: 'for if while',
    contains: [
      {
        className: 'string',
        begin: '"',
        end: '"'
      },
      hljs.COMMENT(
        '/\\*', // begin
        '\\*/', // end
        {
          contains: [{
            className: 'doc',
            begin: '@\\w+'
          }]
        }
      )
    ]
  }
})

With ES6 arrow function:

hljs.registerLanguage('language name', () => ({
    case_insensitive: true, // language is case-insensitive
    keywords: 'for if while',
    contains: [
      {
        className: 'string',
        begin: '"',
        end: '"'
      },
      hljs.COMMENT(
        '/\\*', // begin
        '\\*/', // end
        {
          contains: [{
            className: 'doc',
            begin: '@\\w+'
          }]
        }
      )
    ]
}))
like image 121
Nice18 Avatar answered Oct 18 '25 08:10

Nice18