Skip to content
Starlight Latest VersionLoading...

Programmatic Usage

Besides the components, the Starlight Latest Version plugin exposes the latest version programmatically through a Vite virtual module. This is useful when you need the raw version string, e.g. to inject it into a code block or to build a download URL.

The virtual:starlight-latest-version module exports an asynchronous getLatestVersion function which fetches the latest version using the source you configured for the plugin.

---
import { getLatestVersion } from "virtual:starlight-latest-version";
const version = await getLatestVersion();
---
{version.versionAvailable && <p>The latest version is {version.version}.</p>}

The returned object is the same context used internally by the plugin’s components. When a version is available (versionAvailable is true), it contains the following properties:

interface VersionContext {
versionAvailable: boolean; // Whether a version could be fetched.
version: string; // The full version, e.g. v1.2.3 or v1.2.3-beta.1.
versionWithoutPrefix: string; // The version without the leading v, e.g. 1.2.3.
versionMajor: number; // The major version number.
versionMinor: number; // The minor version number.
versionPatch: number; // The patch version number.
prerelease?: string; // The prerelease identifier, e.g. beta.1, if any.
isPrereleaseVersion: boolean; // Whether the version is a prerelease.
isStableVersion: boolean; // Whether the version is a stable release.
prefix?: string; // The prefix in front of the version, e.g. starlight-@, if any.
hasVPrefix: boolean; // Whether the original tag had a v prefix.
}

Example: inject the version into a code block

Section titled “Example: inject the version into a code block”

Because code blocks in MDX are static, you cannot render a component inside them. Instead, read a file with the ?raw suffix, replace a placeholder with the fetched version, and pass the result to the <Code> component:

---
import { Code } from "@astrojs/starlight/components";
import { getLatestVersion } from "virtual:starlight-latest-version";
import readme from "./README.md?raw";
const version = await getLatestVersion();
const code = version.versionAvailable
? readme.replaceAll("<VERSION>", version.versionWithoutPrefix)
: readme;
---
<Code code={code} lang="md" title="README.md" />
Preview
README.md
### Code Block Example
This is the current version: 0.9.0