Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for highlight plugin to register additional languages via callback #3026

Merged
merged 3 commits into from
Oct 8, 2021

Conversation

rajgoel
Copy link
Contributor

@rajgoel rajgoel commented Oct 4, 2021

This PR adds an optional parameter callback to the configuration of the highlight plugin. If a callback function is provided, the function is called with hljs as argument before the plugin starts to highlight the code.

Sample configuration

Reveal.initialize({
  // ...
  highlight: {
    callback: registerAMPL 
  },
  // ...
});

A new language can then be added, by including the respective function as shown below:

function registerAMPL(hljs) {
  hljs.registerLanguage("ampl",function(hljs){
    return   {
      contains: [ 
        hljs.HASH_COMMENT_MODE,
        hljs.C_BLOCK_COMMENT_MODE,
        {
          className: "number",
          begin: String.raw`\b(\d*\.?\d+)\b`,
          relevance:0
        },
        {
          className: "strong",
          begin: String.raw`^ampl[\?:]`,
          relevance:0
        }
      ],
      keywords: { 
        keyword: 'maximize minimize subject to set within union inter diff symdiff cross sum in by mod var param default setof dimen symbolic if then else',
        type: 'binary integer',
        string: 'reset model solve data option solver solver_msg solution_precision display include print printf quit',
        built_in: 'abs max min sqrt round trunc ceil floor',
        number: 'Infinity',
        symbol: '_nvars _varname _var'
      } 
    };
  });
}

Fixes #2761

@hakimel hakimel merged commit ea14d4a into hakimel:dev Oct 8, 2021
@hakimel
Copy link
Owner

hakimel commented Oct 8, 2021

This is a good addition but I made two adjustments:

  1. Renamed the option to beforeHighlight to more clearly indicate when this is called.
  2. Moved the callback out of the forEach loop to avoid triggering once per code block.

Thanks!

@rajgoel
Copy link
Contributor Author

rajgoel commented Oct 11, 2021

Thanks a lot for including this 👍

@maxmeyer
Copy link

This is very helpful. @rajgoel Is there any place where this is documented?

@hakimel
Copy link
Owner

hakimel commented Nov 13, 2021

@Martinomagnifico
Copy link
Collaborator

Great addition, Asvin!

@maxmeyer
Copy link

@hakimel Great! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants