Skip to content

Commit 5ca657e

Browse files
[Issue #291] Website - Extend schema tab component to include links to source files (#295)
1 parent 9324f3c commit 5ca657e

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

website/src/components/SchemaFormatTabs.astro

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,57 @@ const jsonSchemaCode = jsonSchema ? extractCode(jsonSchema) : null;
4949
const typeSpecCode = typeSpec ? extractCode(typeSpec) : null;
5050
const pythonCode = python ? extractCode(python) : null;
5151
52+
const jsonSchemaUrl = jsonSchema ? extractSchemaUrl(jsonSchema) : null;
53+
const typeSpecUrl = typeSpec ? extractGithubUrl(typeSpec) : null;
54+
const pythonUrl = python ? extractGithubUrl(python) : null;
55+
56+
/**
57+
* Constructs a schema URL from file path.
58+
* @param props - The code block configuration
59+
* @returns The schema URL for the file, or null if no file is specified
60+
*/
61+
function extractSchemaUrl(props: CodeBlockConfig): string | null {
62+
if (!props.file) {
63+
return null;
64+
}
65+
66+
const baseUrl = "https://commongrants.org/";
67+
const pathPrefix = "website/public/";
68+
let filePath = props.file.path;
69+
70+
// Trim public directory prefix if present
71+
if (filePath.startsWith(pathPrefix)) {
72+
filePath = filePath.substring(pathPrefix.length);
73+
}
74+
75+
return `${baseUrl}${filePath}`;
76+
}
77+
78+
/**
79+
* Extracts a GitHub URL from file configuration.
80+
* @param props - The code block configuration
81+
* @returns The GitHub URL for the file, or null if no file is specified
82+
*/
83+
function extractGithubUrl(props: CodeBlockConfig): string | null {
84+
if (!props.file) {
85+
return null;
86+
}
87+
88+
const baseUrl = "https://github.com/HHS/simpler-grants-protocol/tree/main/";
89+
let url = `${baseUrl}/${props.file.path}`;
90+
91+
if (props.file.startLine || props.file.endLine) {
92+
const start = props.file.startLine || 1;
93+
const end = props.file.endLine || start;
94+
url += `#L${start}`;
95+
if (end > start) {
96+
url += `-L${end}`;
97+
}
98+
}
99+
100+
return url;
101+
}
102+
52103
/**
53104
* Extracts code from either a literal string or a file.
54105
* @param props - The code block configuration
@@ -94,23 +145,23 @@ function extractCode(props: CodeBlockConfig): string {
94145
{
95146
jsonSchemaCode && (
96147
<TabItem label="JSON Schema">
97-
<p>The JSON Schema for this model.</p>
148+
<p>The {jsonSchemaUrl && <a href={jsonSchemaUrl} target="_blank" rel="noopener noreferrer">JSON Schema</a>} for this model.</p>
98149
<Code code={jsonSchemaCode} lang="yaml" />
99150
</TabItem>
100151
)
101152
}
102153
{
103154
typeSpecCode && (
104155
<TabItem label="TypeSpec">
105-
<p>The TypeSpec code for this model.</p>
156+
<p>The {typeSpecUrl ? <a href={typeSpecUrl} target="_blank" rel="noopener noreferrer">TypeSpec code</a> : "TypeSpec code"} for this model.</p>
106157
<Code code={typeSpecCode} lang="typespec" />
107158
</TabItem>
108159
)
109160
}
110161
{
111162
pythonCode && (
112163
<TabItem label="Python">
113-
<p>The Python code for this model.</p>
164+
<p>The {pythonUrl ? <a href={pythonUrl} target="_blank" rel="noopener noreferrer">Python code</a> : "Python code"} for this model.</p>
114165
<Code code={pythonCode} lang="python" />
115166
</TabItem>
116167
)

website/src/content/docs/protocol/filters/date.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ dateRangeFilter:
2222
python:
2323
file:
2424
path: "lib/python-sdk/common_grants_sdk/schemas/filters/date.py"
25-
startLine: 26
26-
endLine: 33
25+
startLine: 37
26+
endLine: 52
2727
dateComparisonFilter:
2828
example:
2929
code: |
@@ -42,8 +42,8 @@ dateComparisonFilter:
4242
python:
4343
file:
4444
path: "lib/python-sdk/common_grants_sdk/schemas/filters/date.py"
45-
startLine: 36
46-
endLine: 43
45+
startLine: 55
46+
endLine: 78
4747
---
4848

4949
import SchemaFormatTabs from "@/components/SchemaFormatTabs.astro";

website/src/content/docs/protocol/filters/money.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ moneyComparisonFilter:
5151
python:
5252
file:
5353
path: "lib/python-sdk/common_grants_sdk/schemas/filters/money.py"
54-
startLine: 64
55-
endLine: 71
54+
startLine: 72
55+
endLine: 87
5656
---
5757

5858
import SchemaFormatTabs from "@/components/SchemaFormatTabs.astro";

website/src/content/docs/protocol/filters/numeric.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ numberRangeFilter:
4242
python:
4343
file:
4444
path: "lib/python-sdk/common_grants_sdk/schemas/filters/numeric.py"
45-
startLine: 38
46-
endLine: 45
45+
startLine: 46
46+
endLine: 61
4747
numberArrayFilter:
4848
example:
4949
code: |
@@ -62,8 +62,8 @@ numberArrayFilter:
6262
python:
6363
file:
6464
path: "lib/python-sdk/common_grants_sdk/schemas/filters/numeric.py"
65-
startLine: 48
66-
endLine: 57
65+
startLine: 64
66+
endLine: 81
6767
---
6868

6969
import SchemaFormatTabs from "@/components/SchemaFormatTabs.astro";

website/src/content/docs/protocol/filters/string.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ stringComparisonFilter:
1919
python:
2020
file:
2121
path: "lib/python-sdk/common_grants_sdk/schemas/filters/string.py"
22-
startLine: 23
23-
endLine: 30
22+
startLine: 31
23+
endLine: 49
2424
stringArrayFilter:
2525
example:
2626
code: |
@@ -40,7 +40,7 @@ stringArrayFilter:
4040
file:
4141
path: "lib/python-sdk/common_grants_sdk/schemas/filters/string.py"
4242
startLine: 13
43-
endLine: 20
43+
endLine: 28
4444
---
4545

4646
import SchemaFormatTabs from "@/components/SchemaFormatTabs.astro";

0 commit comments

Comments
 (0)