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
15 changes: 14 additions & 1 deletion packages/starlight-codeblocks/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from 'node:crypto';
import { existsSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import type { ExpressiveCodePlugin } from '@expressive-code/core';
Expand Down Expand Up @@ -49,8 +50,14 @@ export function codeblocksIntegration({
'astro:config:setup'({ command, config, updateConfig, injectScript, logger }) {
// The shapes `isSatteriProcessor()` and `isUnifiedProcessor()` check, without depending on either package.
const processor = config.markdown.processor as
| { name?: string; options?: { mdastPlugins?: unknown[]; remarkPlugins?: unknown[] } }
| {
name?: string;
options?: { mdastPlugins?: unknown[]; remarkPlugins?: unknown[]; starlightCodeblocks?: string };
}
| undefined;
// The content layer keeps rendered Markdown until its digest of the Astro config changes. That digest
// skips integrations and functions but covers these options, which both processors otherwise ignore.
if (processor?.options) processor.options.starlightCodeblocks = optionsDigest(options);
if (processor?.name === 'unified') {
processor.options?.remarkPlugins?.push(remarkFromSatteri(mdastPlugins(options, logger)));
} else processor?.options?.mdastPlugins?.push(...mdastPlugins(options, logger));
Expand Down Expand Up @@ -81,6 +88,12 @@ export function codeblocksIntegration({
};
}

const optionsDigest = (options: ResolvedOptions) =>
createHash('sha256')
.update(JSON.stringify(options, (_, value) => (typeof value === 'function' ? String(value) : value)))
.digest('base64url')
.slice(0, 16);

/** The site's `ec.config.mjs`, imported fresh, as Expressive Code does. */
export async function loadEcConfig(root: URL) {
const url = new URL('./ec.config.mjs', root);
Expand Down
12 changes: 11 additions & 1 deletion packages/starlight-codeblocks/test/astro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { mdastPlugins, restoreDirectives } from '../src/satteri/index.ts';

afterEach(() => setRegistry(undefined));

type Processor = { name: string; options: { features?: { directive?: boolean }; mdastPlugins: unknown[] } };
type Processor = {
name: string;
options: { features?: { directive?: boolean }; mdastPlugins: unknown[]; starlightCodeblocks?: string };
};

async function setup(options: AstroCodeblocksOptions = {}, ecConfig?: string, others: string[] = []) {
const root = mkdtempSync(join(tmpdir(), 'scb-'));
Expand Down Expand Up @@ -57,6 +60,13 @@ test('changes nothing outside code blocks unless the site turns on code tabs or
expect(getRegistry()?.options).toMatchObject({ codeTabs: false, inlineHighlighting: false });
});

test('puts a digest of its options in the hashed Astro config, so that a change clears rendered Markdown', async () => {
const digest = async (options: AstroCodeblocksOptions) =>
(await setup(options)).processor.options.starlightCodeblocks;
expect(await digest({})).toBe(await digest({}));
expect(await digest({ footnotes: false })).not.toBe(await digest({}));
});

test('adds the inline code stylesheet on every page, and never the API card page styles', async () => {
const { scripts } = await setup({ inlineHighlighting: {} });
expect(scripts).toEqual(['import "virtual:starlight-codeblocks/inline-code.css";']);
Expand Down
Loading