api.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. Library API
  2. ===========
  3. Highlight.js exports a few functions as methods of the ``hljs`` object.
  4. ``highlight(name, value, 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 one passed into a function, returned for consistency with ``highlightAuto``
  20. * ``relevance``: integer value
  21. * ``value``: HTML string with highlighting markup
  22. * ``top``: top of the current mode stack
  23. ``highlightAuto(value, languageSubset)``
  24. ----------------------------------------
  25. Highlighting with language detection.
  26. 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.
  27. Returns an object with the following properties:
  28. * ``language``: detected language
  29. * ``relevance``: integer value
  30. * ``value``: HTML string with highlighting markup
  31. * ``second_best``: object with the same structure for second-best heuristically detected language, may be absent
  32. ``fixMarkup(value)``
  33. --------------------
  34. 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``.
  35. Accepts a string with the highlighted markup.
  36. ``highlightBlock(block)``
  37. -------------------------
  38. Applies highlighting to a DOM node containing code.
  39. This function is the one to use to apply highlighting dynamically after page load
  40. or within initialization code of third-party Javascript frameworks.
  41. The function uses language detection by default but you can specify the language
  42. in the ``class`` attribute of the DOM node. See the :doc:`class reference
  43. </css-classes-reference>` for all available language names and aliases.
  44. ``configure(options)``
  45. ----------------------
  46. Configures global options:
  47. * ``tabReplace``: a string used to replace TAB characters in indentation.
  48. * ``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.
  49. * ``classPrefix``: a string prefix added before class names in the generated markup, used for backwards compatibility with stylesheets.
  50. * ``languages``: an array of language names and aliases restricting auto detection to only these languages.
  51. Accepts an object representing options with the values to updated. Other options don't change
  52. ::
  53. hljs.configure({
  54. tabReplace: ' ', // 4 spaces
  55. classPrefix: '' // don't append class prefix
  56. // … other options aren't changed
  57. })
  58. hljs.initHighlighting();
  59. ``initHighlighting()``
  60. ----------------------
  61. Applies highlighting to all ``<pre><code>..</code></pre>`` blocks on a page.
  62. ``initHighlightingOnLoad()``
  63. ----------------------------
  64. Attaches highlighting to the page load event.
  65. ``registerLanguage(name, language)``
  66. ------------------------------------
  67. Adds new language to the library under the specified name. Used mostly internally.
  68. * ``name``: a string with the name of the language being registered
  69. * ``language``: a function that returns an object which represents the
  70. language definition. The function is passed the ``hljs`` object to be able
  71. to use common regular expressions defined within it.
  72. ``listLanguages()``
  73. ----------------------------
  74. Returns the languages names list.
  75. .. _getLanguage:
  76. ``getLanguage(name)``
  77. ---------------------
  78. Looks up a language by name or alias.
  79. Returns the language object if found, ``undefined`` otherwise.
  80. ``requireLanguage(name)``
  81. ---------------------
  82. Looks up a language by name or alias.
  83. This should be used when one language definition depends on another.
  84. Using this function (vs ``getLanguage``) will provide better error messaging
  85. when a required language is missing.
  86. Returns the language object if found, raises a hard error otherwise.
  87. ``debugMode()``
  88. ---------------
  89. 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.
  90. For example, if a new version suddenly had a serious bug (or breaking change) that affected only a single language:
  91. * **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).
  92. * **In Debug Mode**: All highlighting would stop when an error was encountered and a JavaScript error would be thrown.