Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(highlight): warn about languages dynamic loading #1291

Merged
merged 4 commits into from
Jun 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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