api.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.
  14. Returns an object with the following properties:
  15. * ``language``: language name, same as the one passed into a function, returned for consistency with ``highlightAuto``
  16. * ``relevance``: integer value
  17. * ``value``: HTML string with highlighting markup
  18. * ``top``: top of the current mode stack
  19. ``highlightAuto(value, languageSubset)``
  20. ----------------------------------------
  21. Highlighting with language detection.
  22. 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.
  23. Returns an object with the following properties:
  24. * ``language``: detected language
  25. * ``relevance``: integer value
  26. * ``value``: HTML string with highlighting markup
  27. * ``second_best``: object with the same structure for second-best heuristically detected language, may be absent
  28. ``fixMarkup(value)``
  29. --------------------
  30. 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``.
  31. Accepts a string with the highlighted markup.
  32. ``highlightBlock(block)``
  33. -------------------------
  34. Applies highlighting to a DOM node containing code.
  35. This function is the one to use to apply highlighting dynamically after page load
  36. or within initialization code of third-party Javascript frameworks.
  37. The function uses language detection by default but you can specify the language
  38. in the ``class`` attribute of the DOM node. See the :doc:`class reference
  39. </css-classes-reference>` for all available language names and aliases.
  40. ``configure(options)``
  41. ----------------------
  42. Configures global options:
  43. * ``tabReplace``: a string used to replace TAB characters in indentation.
  44. * ``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.
  45. * ``classPrefix``: a string prefix added before class names in the generated markup, used for backwards compatibility with stylesheets.
  46. * ``languages``: an array of language names and aliases restricting auto detection to only these languages.
  47. Accepts an object representing options with the values to updated. Other options don't change
  48. ::
  49. hljs.configure({
  50. tabReplace: ' ', // 4 spaces
  51. classPrefix: '' // don't append class prefix
  52. // … other options aren't changed
  53. })
  54. hljs.initHighlighting();
  55. ``initHighlighting()``
  56. ----------------------
  57. Applies highlighting to all ``<pre><code>..</code></pre>`` blocks on a page.
  58. ``initHighlightingOnLoad()``
  59. ----------------------------
  60. Attaches highlighting to the page load event.
  61. ``registerLanguage(name, language)``
  62. ------------------------------------
  63. Adds new language to the library under the specified name. Used mostly internally.
  64. * ``name``: a string with the name of the language being registered
  65. * ``language``: a function that returns an object which represents the
  66. language definition. The function is passed the ``hljs`` object to be able
  67. to use common regular expressions defined within it.
  68. ``listLanguages()``
  69. ----------------------------
  70. Returns the languages names list.
  71. .. _getLanguage:
  72. ``getLanguage(name)``
  73. ---------------------
  74. Looks up a language by name or alias.
  75. Returns the language object if found, ``undefined`` otherwise.