api.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. Library API
  2. ===========
  3. Highlight.js exports a few functions as methods of the ``hljs`` object.
  4. ``highlight(languageName, code, ignore_illegals, continuation)``
  5. ---------------------------------------------------------
  6. Core highlighting function.
  7. Accepts a language name, or an alias, and a string with the code to highlight.
  8. The ``ignore_illegals`` parameter, when present and evaluates to a true value,
  9. forces highlighting to finish even in case of detecting illegal syntax for the
  10. language instead of throwing an exception.
  11. The ``continuation`` is an optional mode stack representing unfinished parsing.
  12. When present, the function will restart parsing from this state instead of
  13. initializing a new one. This is used internally for `sublanguage` support.
  14. Note: `continuation` is NOT intended to support line-by-line highlighting
  15. because there is no requirement that a grammar handle linebreaks in any special
  16. way. It's quite possible for a grammar to have a single mode/regex that matches
  17. MANY lines at once. This is not discouraged and entirely up to the grammar.
  18. Returns an object with the following properties:
  19. * ``language``: language name, same as the name passed in ``languageName``, returned for consistency with ``highlightAuto``
  20. * ``relevance``: integer value representing the relevance score
  21. * ``value``: HTML string with highlighting markup
  22. * ``top``: top of the current mode stack
  23. * ``illegal``: boolean representing whether any illegal matches were found
  24. ``highlightAuto(code, languageSubset)``
  25. ----------------------------------------
  26. Highlighting with language detection.
  27. Accepts a string with the code to highlight and an optional array of language names and aliases restricting detection to only those languages. The subset can also be set with ``configure``, but the local parameter overrides the option if set.
  28. Returns an object with the following properties:
  29. * ``language``: detected language
  30. * ``relevance``: integer value representing the relevance score
  31. * ``value``: HTML string with highlighting markup
  32. * ``second_best``: object with the same structure for second-best heuristically detected language (may be absent)
  33. ``fixMarkup(value)``
  34. --------------------
  35. Post-processing of the highlighted markup. Currently consists of replacing indentation TAB characters and using ``<br>`` tags instead of new-line characters. Options are set globally with ``configure``.
  36. Accepts a string with the highlighted markup.
  37. ``highlightBlock(block)``
  38. -------------------------
  39. Applies highlighting to a DOM node containing code.
  40. This function is the one to use to apply highlighting dynamically after page load
  41. or within initialization code of third-party Javascript frameworks.
  42. The function uses language detection by default but you can specify the language
  43. in the ``class`` attribute of the DOM node. See the :doc:`class reference
  44. </css-classes-reference>` for all available language names and aliases.
  45. ``configure(options)``
  46. ----------------------
  47. Configures global options:
  48. * ``tabReplace``: a string used to replace TAB characters in indentation.
  49. * ``useBR``: a flag to generate ``<br>`` tags instead of new-line characters in the output, useful when code is marked up using a non-``<pre>`` container.
  50. * ``classPrefix``: a string prefix added before class names in the generated markup, used for backwards compatibility with stylesheets.
  51. * ``languages``: an array of language names and aliases restricting auto detection to only these languages.
  52. Accepts an object representing options with the values to updated. Other options don't change
  53. ::
  54. hljs.configure({
  55. tabReplace: ' ', // 4 spaces
  56. classPrefix: '' // don't append class prefix
  57. // … other options aren't changed
  58. })
  59. hljs.initHighlighting();
  60. ``initHighlighting()``
  61. ----------------------
  62. Applies highlighting to all ``<pre><code>..</code></pre>`` blocks on a page.
  63. ``initHighlightingOnLoad()``
  64. ----------------------------
  65. Attaches highlighting to the page load event.
  66. ``registerLanguage(name, language)``
  67. ------------------------------------
  68. Adds new language to the library under the specified name. Used mostly internally.
  69. * ``name``: a string with the name of the language being registered
  70. * ``language``: a function that returns an object which represents the
  71. language definition. The function is passed the ``hljs`` object to be able
  72. to use common regular expressions defined within it.
  73. ``listLanguages()``
  74. ----------------------------
  75. Returns the languages names list.
  76. .. _getLanguage:
  77. ``getLanguage(name)``
  78. ---------------------
  79. Looks up a language by name or alias.
  80. Returns the language object if found, ``undefined`` otherwise.
  81. ``requireLanguage(name)``
  82. ---------------------
  83. Looks up a language by name or alias.
  84. This should be used when one language definition depends on another.
  85. Using this function (vs ``getLanguage``) will provide better error messaging
  86. when a required language is missing.
  87. Returns the language object if found, raises a hard error otherwise.
  88. ``debugMode()``
  89. ---------------
  90. Enables *debug/development* mode. **This mode purposely makes Highlight.js more fragile! It should only be used for testing and local development (of languages or the library itself).** By default "Safe Mode" is used, providing the most reliable experience for production usage.
  91. For example, if a new version suddenly had a serious bug (or breaking change) that affected only a single language:
  92. * **In Safe Mode**: All other languages would continue to highlight just fine. The broken language would appear as a code block, but without any highlighting (as if it were plaintext).
  93. * **In Debug Mode**: All highlighting would stop when an error was encountered and a JavaScript error would be thrown.