@@ -93,8 +93,14 @@ const HLJS = function(hljs) {
9393 /**
9494 * Core highlighting function.
9595 *
96- * @param {string } languageName - the language to use for highlighting
97- * @param {string } code - the code to highlight
96+ * OLD API
97+ * highlight(lang, code, ignoreIllegals, continuation)
98+ *
99+ * NEW API
100+ * highlight(code, {lang, ignoreIllegals})
101+ *
102+ * @param {string } codeOrlanguageName - the language to use for highlighting
103+ * @param {string | HighlightOptions } optionsOrCode - the code to highlight
98104 * @param {boolean } [ignoreIllegals] - whether to ignore illegal matches, default is to bail
99105 * @param {CompiledMode } [continuation] - current continuation mode, if any
100106 *
@@ -106,7 +112,24 @@ const HLJS = function(hljs) {
106112 * @property {CompiledMode } top - top of the current mode stack
107113 * @property {boolean } illegal - indicates whether any illegal matches were found
108114 */
109- function highlight ( languageName , code , ignoreIllegals , continuation ) {
115+ function highlight ( codeOrlanguageName , optionsOrCode , ignoreIllegals , continuation ) {
116+ let code = "" ;
117+ let languageName = "" ;
118+ if ( typeof optionsOrCode === "object" ) {
119+ code = codeOrlanguageName ;
120+ ignoreIllegals = optionsOrCode . ignoreIllegals ;
121+ languageName = optionsOrCode . language ;
122+ // continuation not supported at all via the new API
123+ // eslint-disable-next-line no-undefined
124+ continuation = undefined ;
125+ } else {
126+ // old API
127+ logger . deprecated ( "10.7.0" , "highlight(lang, code, ...args) has been deprecated." ) ;
128+ logger . deprecated ( "10.7.0" , "Please use highlight(code, options) instead.\nhttps://github.com/highlightjs/highlight.js/issues/2277" ) ;
129+ languageName = codeOrlanguageName ;
130+ code = optionsOrCode ;
131+ }
132+
110133 /** @type {BeforeHighlightContext } */
111134 const context = {
112135 code,
@@ -133,14 +156,12 @@ const HLJS = function(hljs) {
133156 * private highlight that's used internally and does not fire callbacks
134157 *
135158 * @param {string } languageName - the language to use for highlighting
136- * @param {string } code - the code to highlight
137- * @param {boolean } [ignoreIllegals] - whether to ignore illegal matches, default is to bail
138- * @param {CompiledMode } [continuation] - current continuation mode, if any
159+ * @param {string } codeToHighlight - the code to highlight
160+ * @param {boolean? } [ignoreIllegals] - whether to ignore illegal matches, default is to bail
161+ * @param {CompiledMode? } [continuation] - current continuation mode, if any
139162 * @returns {HighlightResult } - result of the highlight operation
140163 */
141- function _highlight ( languageName , code , ignoreIllegals , continuation ) {
142- const codeToHighlight = code ;
143-
164+ function _highlight ( languageName , codeToHighlight , ignoreIllegals , continuation ) {
144165 /**
145166 * Return keyword data if a match is a keyword
146167 * @param {CompiledMode } mode - current mode
@@ -708,7 +729,7 @@ const HLJS = function(hljs) {
708729
709730 node = element ;
710731 const text = node . textContent ;
711- const result = language ? highlight ( language , text , true ) : highlightAuto ( text ) ;
732+ const result = language ? highlight ( text , { language , ignoreIllegals : true } ) : highlightAuto ( text ) ;
712733
713734 // support for v10 API
714735 fire ( "after:highlightElement" , { el : element , result, text } ) ;
0 commit comments