Skip to content

Commit 32177e2

Browse files
feat(docusaurus-theme): redirect to GH using types definition
1 parent c4e18ff commit 32177e2

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

packages/docusaurus-theme/src/components/prop_table/prop_table.tsx

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,57 @@ import {
1515
} from '@elastic/eui-docgen';
1616
import { useCallback, useMemo } from 'react';
1717
import { css } from '@emotion/react';
18-
import { PropTableExtendedTypes } from './extended_types';
1918
import Heading from '@theme/Heading';
19+
import types from '@elastic/eui-docgen/dist/types.json';
20+
21+
import { PropTableExtendedTypes } from './extended_types';
22+
23+
export interface Source {
24+
fileName: string;
25+
line: number;
26+
character: number;
27+
url: string;
28+
}
29+
30+
export interface Child {
31+
id: number;
32+
name: string;
33+
variant: string;
34+
kind: number;
35+
flags: Record<string, unknown>;
36+
children?: Child[];
37+
groups?: Group[];
38+
sources?: Source[];
39+
type?: {
40+
type: string;
41+
types?: { type: string; value?: string }[];
42+
declaration?: Record<string, unknown>;
43+
target?: { qualifiedName: string };
44+
name?: string;
45+
package?: string;
46+
};
47+
defaultValue?: string;
48+
}
49+
50+
export interface Group {
51+
title: string;
52+
children: number[];
53+
}
54+
55+
export interface Types {
56+
schemaVersion: string;
57+
id: number;
58+
name: string;
59+
variant: string;
60+
kind: number;
61+
flags: Record<string, unknown>;
62+
children: Child[];
63+
groups: Group[];
64+
packageName: string;
65+
readme: unknown[];
66+
symbolIdMap: Record<string, unknown>;
67+
files: Record<string, unknown>;
68+
}
2069

2170
export interface PropTableProps {
2271
definition: ProcessedComponent;
@@ -123,15 +172,30 @@ export const PropTable = ({
123172
value: ProcessedComponentProp['description'],
124173
prop: ProcessedComponentProp
125174
) {
175+
const result = value
176+
.replace(/{@link (\w+)}/g, (_, componentName) => {
177+
const componentSource = (types as Types).children.find(
178+
(item) => item.name === componentName
179+
)?.sources?.[0];
180+
181+
if (componentSource) {
182+
const { fileName, line } = componentSource;
183+
return `[${componentName}](https://github.com/elastic/eui/tree/main/packages/${fileName}#L${line})`;
184+
} else {
185+
return `\`${componentName}\``;
186+
}
187+
})
188+
.trim();
189+
126190
return (
127191
<EuiFlexGroup
128192
direction="column"
129193
alignItems="flexStart"
130194
gutterSize="s"
131195
>
132-
{value?.trim() && (
196+
{result && (
133197
<EuiMarkdownFormat css={styles.description}>
134-
{value}
198+
{result}
135199
</EuiMarkdownFormat>
136200
)}
137201
{prop.type && (

0 commit comments

Comments
 (0)