Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(docsite): autolink `astryx docs <topic>` references to their pages (#4766)

Joeyffa89f423711
diff --git a/apps/docsite/src/__tests__/code-linkifiers.test.ts b/apps/docsite/src/__tests__/code-linkifiers.test.ts
new file mode 100644
index 0000000000..71c0e9c009
--- /dev/null
+++ b/apps/docsite/src/__tests__/code-linkifiers.test.ts
@@ -0,0 +1,83 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Unit tests for the inline-code linkifiers.
+ *
+ * Run: pnpm -F @astryxdesign/docsite test
+ */
+
+import {describe, it, expect} from 'vitest';
+import {linkifyCode} from '../components/codeLinkifiers';
+import {docTopics} from '../generated/docsRegistry';
+import {components} from '../generated/componentRegistry';
+
+describe('doc topic linkifier', () => {
+ it.each([
+ ['astryx docs tokens', '/docs/tokens'],
+ ['astryx docs styling-libraries', '/docs/styling-libraries'],
+ ['npx astryx docs icons', '/docs/icons'],
+ [' astryx docs theme ', '/docs/theme'],
+ // The docs index, for a bare command.
+ ['astryx docs', '/docs'],
+ // A section argument and CLI-only flags still belong to the topic page.
+ ['astryx docs tokens spacing', '/docs/tokens'],
+ ['astryx docs styling --dense', '/docs/styling'],
+ ['astryx docs styling --zh', '/docs/styling'],
+ ])('links %s', (code, href) => {
+ expect(linkifyCode(code)).toBe(href);
+ });
+
+ it('links every topic the site serves', () => {
+ for (const {topic} of docTopics) {
+ expect(linkifyCode(`astryx docs ${topic}`)).toBe(`/docs/${topic}`);
+ }
+ });
+
+ it.each([
+ // A renamed topic degrades to plain code rather than a 404.
+ ['astryx docs nonexistent-topic'],
+ // Other commands have no one-to-one page.
+ ['astryx component Button'],
+ ['astryx theme build'],
+ ['astryx upgrade --apply'],
+ // Ordinary backticked prose and identifiers.
+ ['xstyle'],
+ ['--color-text-primary'],
+ ['See astryx docs tokens for the full reference'],
+ ['other-cli docs tokens'],
+ [''],
+ ])('leaves %s as plain code', code => {
+ expect(linkifyCode(code)).toBeNull();
+ });
+});
+
+/** Every backticked `astryx docs ...` reference in a chunk of authored data. */
+function docReferences(json: string): string[] {
+ const found = [...json.matchAll(/`((?:npx )?astryx docs[^`]*)`/g)];
+ return [...new Set(found.map(m => m[1]))];
+}
+
+/**
+ * Guards the failure mode the linkifier cannot catch at runtime: if a topic is
+ * renamed, an unresolvable reference degrades to plain code instead of 404ing,
+ * so nobody notices the cross-reference went stale.
+ *
+ * Scanning the serialized registries rather than walking their shapes keeps
+ * this short and covers every authored field, not just the ones rendered
+ * today.
+ */
+describe('doc references in shipped content', () => {
+ const corpora = {
+ 'doc topics': JSON.stringify(docTopics),
+ 'component docs': JSON.stringify(components),
+ };
+
+ it.each(Object.entries(corpora))('scans %s', (_name, json) => {
+ expect(docReferences(json).length).toBeGreaterThan(0);
+ });
+
+ it('links every reference to a page the site serves', () => {
+ const all = Object.values(corpora).flatMap(docReferences);
+ expect(all.filter(ref => linkifyCode(ref) == null)).toEqual([]);
+ });
+});
diff --git a/apps/docsite/src/components/InlineCode.tsx b/apps/docsite/src/components/InlineCode.tsx
new file mode 100644
index 0000000000..60957c39e7
--- /dev/null
+++ b/apps/docsite/src/components/InlineCode.tsx
@@ -0,0 +1,45 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+/**
+ * @file InlineCode.tsx
+ * @input The text content of an inline code span.
+ * @output A `Code` chip, or a link when a linkifier recognizes the span as a
+ * reference to a page this site serves.
+ * @position Renders every inline code span on the docs site — /docs pages go
+ * through inlineMarkdown.tsx, component pages through MarkdownText.tsx's
+ * `inlineCode` override.
+ *
+ * Spans no linkifier claims — every other backticked prop name, token, and
+ * snippet — render as an ordinary code chip.
+ */
+
+import * as stylex from '@stylexjs/stylex';
+import {Code} from '@astryxdesign/core/CodeBlock';
+import {Link} from '@astryxdesign/core/Link';
+import {linkifyCode} from './codeLinkifiers';
+
+const styles = stylex.create({
+ // Only ever seen on hover, where Link draws its underline. Nudged clear of
+ // the descenders in commands like `astryx docs typography`.
+ link: {textUnderlineOffset: '0.2em'},
+});
+
+export function InlineCode({children}: {children: string}) {
+ const href = linkifyCode(children);
+ if (href == null) {
+ return <Code>{children}</Code>;
+ }
+
+ // The chip is unchanged at rest — same background, padding, and colour as
+ // any other inline code. Link contributes the hit target and the pointer
+ // cursor, and reveals an underline on hover. `type="inherit"` keeps the
+ // surrounding text's metrics; Code keeps its own colour, so wrapping it
+ // changes nothing visually.
+ return (
+ <Link href={href} type="inherit" xstyle={styles.link}>
+ <Code>{children}</Code>
+ </Link>
+ );
+}
diff --git a/apps/docsite/src/components/MarkdownText.tsx b/apps/docsite/src/components/MarkdownText.tsx
index dcb872ab5f..0f463c5737 100644
--- a/apps/docsite/src/components/MarkdownText.tsx
+++ b/apps/docsite/src/components/MarkdownText.tsx
@@ -4,11 +4,18 @@
import type {ComponentProps} from 'react';
import {Markdown} from '@astryxdesign/core/Markdown';
+import type {MarkdownComponents} from '@astryxdesign/core/Markdown';
import {VStack} from '@astryxdesign/core/Layout';
import {Text} from '@astryxdesign/core/Text';
+import {InlineCode} from './InlineCode';
type TextProps = ComponentProps<typeof Text>;
+// Component-page prose (prop descriptions, dos and don'ts, theming notes)
+// cites doc topics the same way /docs prose does, e.g. "Icon name — see
+// `astryx docs icons`". Route those spans through the same linkifiers.
+const MARKDOWN_COMPONENTS: MarkdownComponents = {inlineCode: InlineCode};
+
interface MarkdownTextProps {
children: string;
type?: TextProps['type'];
@@ -39,7 +46,9 @@ export function MarkdownText({
weight={weight}
display="block"
style={style}>
- <Markdown display="inline">{paragraphs[0]}</Markdown>
+ <Markdown display="inline" components={MARKDOWN_COMPONENTS}>
+ {paragraphs[0]}
+ </Markdown>
</Text>
);
}
@@ -54,7 +63,9 @@ export function MarkdownText({
color={color}
weight={weight}
display="block">
- <Markdown display="inline">{paragraph}</Markdown>
+ <Markdown display="inline" components={MARKDOWN_COMPONENTS}>
+ {paragraph}
+ </Markdown>
</Text>
))}
</VStack>
diff --git a/apps/docsite/src/components/codeLinkifiers.ts b/apps/docsite/src/components/codeLinkifiers.ts
new file mode 100644
index 0000000000..b0b2ede3ee
--- /dev/null
+++ b/apps/docsite/src/components/codeLinkifiers.ts
@@ -0,0 +1,60 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file codeLinkifiers.ts
+ * @input The text content of an inline code span.
+ * @output The docs-site href that span points at, or null to leave it as
+ * plain code.
+ * @position Consumed by InlineCode.tsx, which renders every inline code span
+ * on the site. Kept separate so the rules are unit-testable without React.
+ *
+ * Docs prose cites sibling topics as the literal CLI command — "Semantic
+ * tokens, not hardcoded values (see `astryx docs tokens`)". That is what an
+ * agent should type, so it stays copy-pasteable code; linkifying it gives a
+ * human somewhere to click.
+ *
+ * To teach the site a new kind of cross-reference, write a CodeLinkifier and
+ * append it to LINKIFIERS.
+ */
+
+import {docTopics} from '../generated/docsRegistry';
+
+/** Returns an href for a code span it recognizes, or null to pass. */
+type CodeLinkifier = (code: string) => string | null;
+
+const KNOWN_TOPICS = new Set(docTopics.map(d => d.topic));
+
+/**
+ * `astryx docs tokens` -> /docs/tokens, `astryx docs` -> the docs index.
+ *
+ * Anything after the topic — a section argument, or a flag like `--dense`
+ * that only changes CLI output — still belongs to the topic's page. Unknown
+ * topics return null so a renamed doc degrades to plain code, not a 404.
+ */
+const docTopic: CodeLinkifier = code => {
+ const match = /^(?:npx )?astryx docs(?:\s+([a-z][\w-]*))?(?:\s.*)?$/.exec(
+ code,
+ );
+ if (!match) {
+ return null;
+ }
+ const topic = match[1];
+ if (topic == null) {
+ return '/docs';
+ }
+ return KNOWN_TOPICS.has(topic) ? `/docs/${topic}` : null;
+};
+
+const LINKIFIERS: CodeLinkifier[] = [docTopic];
+
+/** Resolve an inline code span to a docs href, or null to leave it alone. */
+export function linkifyCode(code: string): string | null {
+ const text = code.trim();
+ for (const linkify of LINKIFIERS) {
+ const href = linkify(text);
+ if (href != null) {
+ return href;
+ }
+ }
+ return null;
+}
diff --git a/apps/docsite/src/components/docs/inlineMarkdown.tsx b/apps/docsite/src/components/docs/inlineMarkdown.tsx
index 905f12ba81..fe93b15c5f 100644
--- a/apps/docsite/src/components/docs/inlineMarkdown.tsx
+++ b/apps/docsite/src/components/docs/inlineMarkdown.tsx
@@ -4,7 +4,7 @@
import {Fragment, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
-import {Code} from '@astryxdesign/core/CodeBlock';
+import {InlineCode} from '../InlineCode';
const TOKEN = /(`([^`]+)`|\[([^\]]+)\]\(([^)]+)\))/g;
@@ -57,7 +57,7 @@ export function renderInlineMarkdown(text: string) {
const linkLabel = match[3];
const linkHref = match[4];
if (code != null) {
- nodes.push(<Code key={match.index}>{code}</Code>);
+ nodes.push(<InlineCode key={match.index}>{code}</InlineCode>);
} else if (linkLabel != null && linkHref != null) {
nodes.push(renderLink(linkLabel, linkHref, match.index));
}