diff --git a/packages/starlight-codeblocks/CHANGELOG.md b/packages/starlight-codeblocks/CHANGELOG.md index 29f7bdf..99dd72f 100644 --- a/packages/starlight-codeblocks/CHANGELOG.md +++ b/packages/starlight-codeblocks/CHANGELOG.md @@ -8,6 +8,7 @@ - Split diff: a **Unified** and **Split** switch in the title bar of each block that removes and adds lines. **Split** shows the old code on the left and the new code on the right, as a GitHub pull request does. `splitDiff.view`, `splitDiff.switch` and `splitDiff.sync` set the start view, remove the switch and stop it syncing across the page (#37) - Untitled blocks with title bar controls use the tab bar colours of titled blocks (#37) - Skipped code: `[!code skip]` on its own line shows a marker in place of code that a block leaves out, at the indentation of its line. The text after the directive goes in the marker, else it reads "Code not shown". The line keeps its line number, and copies as a comment with the text, such as `// Not shown: Imports`. A comment that holds only `...`, such as `// ...`, and `...` alone on a line in JSON, also show as markers and copy as written. `skippedCode.commentAliases` and `skippedCode.bareAliases` change these aliases for each language. `skippedCode: false` turns the feature off (#28) +- API links: a starlight-pydocs registry left over from an `astro dev` restart that removed starlight-pydocs from the config is dropped, so Python names no longer link to its old pages ## 1.4.1 diff --git a/packages/starlight-codeblocks/src/adapters/python.ts b/packages/starlight-codeblocks/src/adapters/python.ts index 3a2f009..0fee4bc 100644 --- a/packages/starlight-codeblocks/src/adapters/python.ts +++ b/packages/starlight-codeblocks/src/adapters/python.ts @@ -36,6 +36,13 @@ export interface PydocsSymbol { const PYDOCS = Symbol.for('starlight-pydocs'); +/** Drops the starlight-pydocs registry when the config has no starlight-pydocs: as a global, it outlives a dev restart. */ +export function dropStalePydocsRegistry(integrations: readonly { name: string }[]) { + if (!integrations.some((integration) => integration.name === 'starlight-pydocs')) { + delete (globalThis as { [PYDOCS]?: PydocsRegistry })[PYDOCS]; + } +} + const pydocsRegistry = () => { const registry = (globalThis as { [PYDOCS]?: PydocsRegistry })[PYDOCS]; return registry?.version === 1 ? registry : undefined; diff --git a/packages/starlight-codeblocks/src/integration.ts b/packages/starlight-codeblocks/src/integration.ts index 1d61d2c..c30b51e 100644 --- a/packages/starlight-codeblocks/src/integration.ts +++ b/packages/starlight-codeblocks/src/integration.ts @@ -5,6 +5,7 @@ import type { ExpressiveCodePlugin } from '@expressive-code/core'; import type { AstroIntegration } from 'astro'; import { AstroError } from 'astro/errors'; import type { MdastPluginEntry } from 'satteri'; +import { dropStalePydocsRegistry } from './adapters/python.ts'; import { apiCardPageStyles, CARD_CSS_ID } from './api-card-page.ts'; import { readClientModules } from './client-modules.ts'; import { PLUGIN_PREFIX } from './expressive-code/core.ts'; @@ -59,6 +60,7 @@ export function codeblocksIntegration({ options?: { mdastPlugins?: unknown[]; remarkPlugins?: unknown[]; starlightCodeblocks?: string }; } | undefined; + dropStalePydocsRegistry(config.integrations); const registry = getRegistry(); if (registry?.cacheDir) registry.readFiles = loadReadFiles(registry.cacheDir); const unified = processor?.name === 'unified'; diff --git a/packages/starlight-codeblocks/test/python-adapter.test.ts b/packages/starlight-codeblocks/test/python-adapter.test.ts index 111db81..e07df87 100644 --- a/packages/starlight-codeblocks/test/python-adapter.test.ts +++ b/packages/starlight-codeblocks/test/python-adapter.test.ts @@ -1,7 +1,7 @@ import { readFileSync } from 'node:fs'; import { deflateSync } from 'node:zlib'; import { afterEach, expect, test, vi } from 'vitest'; -import { type PydocsRegistry, type PydocsSymbol, python } from '../src/adapters/python.ts'; +import { dropStalePydocsRegistry, type PydocsRegistry, type PydocsSymbol, python } from '../src/adapters/python.ts'; import { readInventory } from '../src/adapters/python-index.ts'; import { type CodeblocksOptions, resolveOptions } from '../src/options.ts'; import { setRegistry } from '../src/registry.ts'; @@ -179,6 +179,15 @@ const myproject = () => ], ]); +test('drops a starlight-pydocs registry left by a config that no longer has starlight-pydocs', () => { + const registry: PydocsRegistry = { version: 1, packages: [] }; + pydocs(registry); + dropStalePydocsRegistry([{ name: 'starlight-pydocs' }]); + expect((globalThis as { [PYDOCS]?: PydocsRegistry })[PYDOCS]).toBe(registry); + dropStalePydocsRegistry([{ name: 'starlight-codeblocks' }]); + expect((globalThis as { [PYDOCS]?: PydocsRegistry })[PYDOCS]).toBeUndefined(); +}); + test('links the objects of a starlight-pydocs package through its registry, with the site base', async () => { pydocs({ version: 1, packages: [{ name: 'myproject', base: 'api/myproject', symbols: myproject() }] }); setRegistry({ options: resolveOptions(), plugins: [], base: '/docs' }); diff --git a/packages/starlight-codeblocks/test/remark.test.ts b/packages/starlight-codeblocks/test/remark.test.ts index 1ad9390..9583814 100644 --- a/packages/starlight-codeblocks/test/remark.test.ts +++ b/packages/starlight-codeblocks/test/remark.test.ts @@ -83,7 +83,7 @@ const setup = (processor: Processor, options = {}) => { p: unknown, ) => void; hook({ - config: { markdown: { processor }, base: '/', build: { assets: '_astro' } }, + config: { markdown: { processor }, base: '/', build: { assets: '_astro' }, integrations: [] }, updateConfig() {}, injectScript() {}, logger: { warn() {} }, diff --git a/packages/starlight-codeblocks/test/runnable.test.ts b/packages/starlight-codeblocks/test/runnable.test.ts index 3273036..30ce57c 100644 --- a/packages/starlight-codeblocks/test/runnable.test.ts +++ b/packages/starlight-codeblocks/test/runnable.test.ts @@ -821,7 +821,13 @@ test('in astro dev, a change to a read file refreshes the content layer and relo const noop = () => {}; await integration.hooks['astro:config:setup']?.({ command: 'dev', - config: { markdown: { processor }, build: { assets: '_astro' }, base: '/', root: new URL('file:///site/') }, + config: { + markdown: { processor }, + build: { assets: '_astro' }, + base: '/', + root: new URL('file:///site/'), + integrations: [], + }, updateConfig: noop, injectScript: noop, logger: { warn: noop },