From c3fa28aecdb3d52dc40e1303a2c506e13e159685 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Wed, 17 Mar 2021 07:07:18 -0400 Subject: [PATCH] (chore) older API defers to highlightAll() now --- VERSION_11_UPGRADE.md | 5 +++++ docs/api.rst | 5 +++-- src/highlight.js | 19 ++++--------------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/VERSION_11_UPGRADE.md b/VERSION_11_UPGRADE.md index d6ac2853c5..da70df2724 100644 --- a/VERSION_11_UPGRADE.md +++ b/VERSION_11_UPGRADE.md @@ -3,6 +3,11 @@ - remove apache, http, nginx, properties, coffeescript from :common #2848 +### Behavior changes + +- `initHighlighting` and `initHighlightingOnLoad` call `highlightAll` +- Calling `highlightAll` over and over is not guarded against + ### Grammar's removed or renamed - remove the old htmlbars stub, use handlebars diff --git a/docs/api.rst b/docs/api.rst index 30dea5ce5e..ecab14a3f5 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -100,11 +100,12 @@ Accepts an object representing options with the values to updated. Other options :: hljs.configure({ - tabReplace: ' ', // 4 spaces + noHighlightRe: /^do-not-highlightme$/i, + languageDetectRe: /\bgrammar-([\w-]+)\b/i, // for `grammar-swift` style CSS naming classPrefix: '' // don't append class prefix // … other options aren't changed }); - hljs.initHighlighting(); + hljs.highlightAll(); highlightAll diff --git a/src/highlight.js b/src/highlight.js index 0abc2b74e6..a5bcadcc19 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -724,27 +724,16 @@ const HLJS = function(hljs) { options = inherit(options, userOptions); } - /** - * Highlights to all
 blocks on a page
-   *
-   * @type {Function & {called?: boolean}}
-   */
   // TODO: remove v12, deprecated
   const initHighlighting = () => {
-    if (initHighlighting.called) return;
-    initHighlighting.called = true;
-
-    logger.deprecated("10.6.0", "initHighlighting() is deprecated.  Use highlightAll() instead.");
-
-    const blocks = document.querySelectorAll('pre code');
-    blocks.forEach(highlightElement);
+    highlightAll();
+    logger.deprecated("10.6.0", "initHighlighting() deprecated.  Use highlightAll() now.");
   };
 
-  // Higlights all when DOMContentLoaded fires
   // TODO: remove v12, deprecated
   function initHighlightingOnLoad() {
-    logger.deprecated("10.6.0", "initHighlightingOnLoad() is deprecated.  Use highlightAll() instead.");
-    wantsHighlight = true;
+    highlightAll();
+    logger.deprecated("10.6.0", "initHighlightingOnLoad() deprecated.  Use highlightAll() now.");
   }
 
   let wantsHighlight = false;