Menu

add keywords to javascript js.js dynamically

2016-09-25
2016-10-06
  • daniel savage

    daniel savage - 2016-09-25

    I'm trying to add a few words to the javascript highlighting dynamically.
    I added a custom section in the js.js file to highlight my own keywords. This works fine.

    When I try to add a few words dynamically. It adds them to the list, but they do not highlight.

    editAreaLoader.load_syntax["js"]["KEYWORDS"]["newwords"].push("newword1","newword2");
    

    Does anybody know how to get the added words to be seen by the highlighter?

     
  • daniel savage

    daniel savage - 2016-10-06

    I finally figured it out

    Just go into js.js and add your own section for example:

    ,'dynamic' : [
                'dynamic1', 'dynamic2'
            ]
    

    When you run editarea again it will highlight those words.

    Now there is a place to write new dynamic words
    to read out the dynamic words use

    re=editAreaLoader.syntax.js.keywords_reg_exp.dynamic;
    console.log(re)
    

    the regular expression that is returned is an object with properties
    re.source will give you the source or regular expression search string
    re.flags will give you the modifiers such as gi

    originalsource=re.source
    

    you can edit the source string with string functions to include your own words by inserting some new words between the | delimiters for example the source might look like
    (\b)(dynamic1|dynamic2)(\b)

    which you can modify to your own newstring using regular string functions

    newsource=originalsource.replace("dynamic2|","dynamic2|dynamic3|dynamic4";
    

    (\b)(dynamic1|dynamic2|dynamic3|dynamic4)(\b)

    then reset the regular expression

    editAreaLoader.syntax.js.keywords_reg_exp.dynamic = new RegExp(newsource,"gi");

    the new dynamic words should now highlight.

     

    Last edit: daniel savage 2016-10-06

Log in to post a comment.

MongoDB Logo MongoDB