Skip to content

Commit

Permalink
fix(highlight): warn about languages dynamic loading (#1291)
Browse files Browse the repository at this point in the history
Co-authored-by: Yaël Guilloux <yael.guilloux@gmail.com>
  • Loading branch information
farnabaz and Tahul committed Jun 27, 2022
1 parent d0ee386 commit 514e5cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/runtime/server/api/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { createError, defineLazyEventHandler, useBody } from 'h3'
import { getHighlighter, BUNDLED_LANGUAGES, BUNDLED_THEMES, Lang, Theme } from 'shiki-es'
import { useLogger } from '@nuxt/kit'
import { HighlightParams, HighlightThemedToken } from '../../types'
import mdcTMLanguage from '../../assets/mdc.tmLanguage.json'
import { useRuntimeConfig } from '#imports'

// Re-create logger locally as utils cannot be imported from here
export const logger = useLogger('@nuxt/content')

/**
* Resolve Shiki compatible lang from string.
*
Expand Down Expand Up @@ -42,8 +46,8 @@ const resolveBody = (body: Partial<HighlightParams>) => {
// Remove trailing carriage returns
code: body.code.replace(/\n+$/, ''),
// Resolve lang & theme (i.e check if shiki supports them)
lang: resolveLang(body.lang),
theme: resolveTheme(body.theme)
lang: resolveLang(body.lang || ''),
theme: resolveTheme(body.theme || '')
}
}

Expand Down Expand Up @@ -87,7 +91,16 @@ export default defineLazyEventHandler(async () => {

// Load supported language on-demand
if (!highlighter.getLoadedLanguages().includes(lang)) {
await highlighter.loadLanguage(lang)
let message = 'Content Highlighter Error\n\n'
message = message + `Language "${lang}" is not loaded Shiki. Falling back to plain code.\n\n`
message = message + `Please make sure you add "${lang}" to the 'preload' list in your Nuxt config.\n\n`
message = message + 'See: https://content.nuxtjs.org/api/configuration#highlight'
logger.warn(message)

// TODO: Enable autoloading of language when upstream Shiki supports it\
// See: https://github.com/nuxt/content/issues/1225#issuecomment-1148786924
// await highlighter.loadLanguage(lang)
return [[{ content: code }]]
}

// Load supported theme on-demand
Expand Down Expand Up @@ -119,15 +132,20 @@ export default defineLazyEventHandler(async () => {
key: color.key,
tokens: color.tokens[line]
})
}, coloredTokens[0].tokens[line])
}, coloredTokens[0].tokens[line] as HighlightThemedToken[])
}

return highlightedCode
}
})

function mergeLines (line1, line2) {
const mergedTokens = []
interface HighlightThemedTokenLine {
key: string
tokens: HighlightThemedToken[]
}

function mergeLines (line1: HighlightThemedTokenLine, line2: HighlightThemedTokenLine) {
const mergedTokens: HighlightThemedToken[] = []
const getColors = (h, i) => typeof h.tokens[i].color === 'string' ? { [h.key]: h.tokens[i].color } : h.tokens[i].color

const [big, small] = line1.tokens.length > line2.tokens.length ? [line1, line2] : [line2, line1]
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { ModuleOptions, MountOptions } from './module'
import type { MarkdownPlugin } from './runtime/types'

export const logger = useLogger('@nuxt/content')

/**
* Internal version that represents cache format.
* This is used to invalidate cache when the format changes.
Expand Down

0 comments on commit 514e5cc

Please sign in to comment.