mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 16:09:06 +00:00
## Problem new surfaces display code blocks their own way while docs has the full implementation but it lives inside `apps/docs` ## Solution - moves the docs code block parts to `ui-patterns/CodeBlock` as separate imports - kb code blocks now show line numbers, which matches docs | state | preview | | -------|------| | before | <img width="913" height="509" alt="image" src="https://github.com/user-attachments/assets/76ff0096-433a-4b06-9eca-1d79c4940e24" /> | | after | <img width="913" height="509" alt="image" src="https://github.com/user-attachments/assets/4f5d91d9-13df-44a0-9e65-edf605773c5f" /> | <!-- ## Preview links If relevant, include links to changed pages for easy review access. Copy the preview base URL from the Vercel bot comment on this PR. Use the following table as an example template. | Site | Live | Preview | Search for | | -------------- | ------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ----------------------------- | | WWW | [/blog/your-post](https://supabase.com/blog/your-post) | [/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post) | unique phrase from the change | | Docs | [/docs/guides/your-page](https://supabase.com/docs/guides/your-page) | [/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page) | unique phrase from the change | | Studio | [/dashboard](https://supabase.com/dashboard) | [/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard) | unique phrase from the change | | Design system | [/design-system](https://supabase.com/design-system) | [/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system) | unique phrase from the change | | UI library | [/library](https://supabase.com/library) | [/library](https://ui-library-git-branch-name-supabase.vercel.app/library) | unique phrase from the change | | Knowledge base | [/kb/guides/your-page](https://supabase.com/kb/guides/your-page) | [/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page) | unique phrase from the change | --> <!-- ## Additional context Optionally add any other context or screenshots. --> ## Review instructions 1. visit /[docs/guides/database/tables#enabling-row-level-security ](https://docs-git-feat-shared-code-block-parts-supabase.vercel.app/docs/guides/database/tables#enabling-row-level-security)code blocks, line numbers, colors in light and dark mode, copy, and word wrap match 2. visit [/docs/guides/auth/passwords](https://docs-git-feat-shared-code-block-parts-supabase.vercel.app/docs/guides/auth/passwords) hover over a typed identifier tooltip should still show 3. visit a kb guide with code, for example [/kb/guides/migrate-from-mysql](https://kb-git-feat-shared-code-block-parts-supabase.vercel.app/kb/guides/migrate-from-mysql) 4. visit the [design system code block page](https://design-system-git-feat-shared-code-block-parts-supabase.vercel.app/design-system/docs/fragments/code-block) ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
89 lines
3.3 KiB
TypeScript
89 lines
3.3 KiB
TypeScript
import { bundledLanguages, createHighlighter, type Highlighter } from 'shiki'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import theme from './supabase-2.json' with { type: 'json' }
|
|
|
|
vi.mock('shiki', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('shiki')>()
|
|
return { ...actual, createHighlighter: vi.fn() }
|
|
})
|
|
|
|
const result = {
|
|
tokens: [[{ content: 'select', offset: 0, color: 'var(--code-token-keyword)', fontStyle: 1 }]],
|
|
}
|
|
const highlighter = {
|
|
codeToTokens: vi.fn<Highlighter['codeToTokens']>(),
|
|
loadLanguage: vi.fn(),
|
|
loadTheme: vi.fn(),
|
|
}
|
|
const create = vi.mocked(createHighlighter, { partial: true })
|
|
|
|
describe('shared code block highlighting', () => {
|
|
beforeEach(() => {
|
|
vi.resetModules()
|
|
vi.resetAllMocks()
|
|
create.mockResolvedValue(highlighter)
|
|
highlighter.codeToTokens.mockReturnValue(result)
|
|
})
|
|
|
|
it('initializes all languages once and never reloads languages or themes between blocks', async () => {
|
|
const { highlightCode } = await import('./CodeBlock.highlight')
|
|
expect(create).not.toHaveBeenCalled()
|
|
|
|
await highlightCode('echo hello', 'bash')
|
|
await highlightCode('select 42', 'sql')
|
|
await highlightCode('echo again', 'shell')
|
|
|
|
expect(create).toHaveBeenCalledExactlyOnceWith({
|
|
themes: [theme],
|
|
langs: Object.keys(bundledLanguages),
|
|
})
|
|
expect(create.mock.calls[0][0].themes?.[0]).not.toBe(theme)
|
|
expect(highlighter.loadLanguage).not.toHaveBeenCalled()
|
|
expect(highlighter.loadTheme).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it.each(['javascript', 'js', null] as const)(
|
|
'forwards the source and options for %s and returns the tokens unchanged',
|
|
async (lang) => {
|
|
const { highlightCode } = await import('./CodeBlock.highlight')
|
|
expect(await highlightCode('const value = 42', lang)).toBe(result)
|
|
expect(highlighter.codeToTokens).toHaveBeenCalledExactlyOnceWith('const value = 42', {
|
|
lang: lang ?? undefined,
|
|
theme: 'Supabase Theme',
|
|
tokenizeTimeLimit: 0,
|
|
tokenizeMaxLineLength: 100_000,
|
|
})
|
|
}
|
|
)
|
|
|
|
it('shares pending initialization across concurrent languages and aliases', async () => {
|
|
let resolve: (value: typeof highlighter) => void = () => {}
|
|
const promise = new Promise<typeof highlighter>((done) => (resolve = done))
|
|
create.mockReturnValue(promise)
|
|
const { highlightCode } = await import('./CodeBlock.highlight')
|
|
const pending = [
|
|
highlightCode('echo hello', 'bash'),
|
|
highlightCode('echo again', 'shell'),
|
|
highlightCode('select 42', 'sql'),
|
|
]
|
|
expect(create).toHaveBeenCalledTimes(1)
|
|
expect(highlighter.codeToTokens).not.toHaveBeenCalled()
|
|
|
|
resolve(highlighter)
|
|
expect(await Promise.all(pending)).toEqual([result, result, result])
|
|
expect(highlighter.codeToTokens).toHaveBeenCalledTimes(3)
|
|
})
|
|
|
|
it('retains initialization failures without adding retries', async () => {
|
|
const failure = new Error('Highlighter could not be initialized')
|
|
create.mockRejectedValue(failure)
|
|
const { highlightCode } = await import('./CodeBlock.highlight')
|
|
|
|
await expect(highlightCode('echo hello', 'bash')).rejects.toBe(failure)
|
|
await expect(highlightCode('select 42', 'sql')).rejects.toBe(failure)
|
|
expect(create).toHaveBeenCalledTimes(1)
|
|
expect(highlighter.codeToTokens).not.toHaveBeenCalled()
|
|
})
|
|
})
|