diff --git a/src/docs/src/assets/js/example.js b/src/docs/src/assets/js/example.js index 57d384827..ccab242da 100644 --- a/src/docs/src/assets/js/example.js +++ b/src/docs/src/assets/js/example.js @@ -75,21 +75,22 @@ $(document).on('pathchange', function (e) { $(this).find('.icon').html(icons[$(this).data('icon-active')]); }); - // highlight code - $('code[class^=\'language\']').each(function () { - var $this = $(this); - if ( $this.attr('data-highlighted') === 'yes' ) { - // Remove the attribute or set it to 'no' - $this.removeAttr('data-highlighted'); - } - // Now you can re-highlight - else { - try { - hljs.configure({ ignoreUnescapedHTML: true }); - hljs.highlightElement(this); - } catch (e) { - console.error('Error: Failed to highlight.', e); - } + // Highlight code. Match the same `pre code` selector hljs.highlightAll() + // uses on full page load — selecting only `code[class^="language"]` skips + // fenced blocks written without a language tag, so they stayed unhighlighted + // after client-side navigation while showing up correctly on a hard reload. + // Scope to `.docs-content` so we only re-highlight the swapped-in docs body + // and leave code blocks elsewhere on the page untouched. + hljs.configure({ ignoreUnescapedHTML: true }); + $('.docs-content pre code').each(function () { + // Skip blocks hljs has already processed so repeated pathchanges don't + // log "Element previously highlighted" and re-run on unchanged nodes. + if ( $(this).attr('data-highlighted') === 'yes' ) return; + + try { + hljs.highlightElement(this); + } catch (e) { + console.error('Error: Failed to highlight.', e); } }); });