Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Elastic.Documentation.Site/Assets/hljs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,6 @@ export function initHighlight() {
hljs.highlightElement
)
}

// Export the configured hljs instance for reuse
export { hljs }
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { initCopyButton } from '../../../copybutton'
import { hljs } from '../../../hljs'
import { GeneratingStatus } from './GeneratingStatus'
import { References } from './RelatedResources'
import { ChatMessage as ChatMessageType } from './chat.store'
Expand All @@ -19,7 +20,6 @@ import {
} from '@elastic/eui'
import { css } from '@emotion/react'
import DOMPurify from 'dompurify'
import hljs from 'highlight.js/lib/core'
import { Marked, RendererObject, Tokens } from 'marked'
import * as React from 'react'
import { useEffect, useMemo } from 'react'
Expand All @@ -28,9 +28,15 @@ import { useEffect, useMemo } from 'react'
const createMarkedInstance = () => {
const renderer: RendererObject = {
code({ text, lang }: Tokens.Code): string {
const highlighted = lang
? hljs.highlight(text, { language: lang }).value
: hljs.highlightAuto(text).value
let highlighted: string
try {
highlighted = lang
? hljs.highlight(text, { language: lang }).value
: hljs.highlightAuto(text).value
} catch {
// Fallback to auto highlighting if the specified language is not found
highlighted = hljs.highlightAuto(text).value
}
return `<div class="highlight">
<pre>
<code class="language-${lang}">${highlighted}</code>
Expand Down
Loading