Skip to content

Commit 38e4ffb

Browse files
feat: update deprecation warnings
1 parent d50e39e commit 38e4ffb

File tree

7 files changed

+92
-18
lines changed

7 files changed

+92
-18
lines changed

src/components/templates/FuncTemplate.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ export const FuncTemplate: FC<FuncTemplateProps> = ({
4242
</small>
4343
</h1>
4444

45-
{content.deprecation && (
46-
<div className="mt-2">
47-
<DeprecationWarning message={content.deprecation} />
48-
</div>
49-
)}
45+
{<DeprecationWarning item={content} level="top" />}
5046

5147
<div class="my-4 text-gray-700">
5248
<HtmlContent html={content.details} />
@@ -121,7 +117,7 @@ function ScopedDefinitions({
121117
<code
122118
class="text-base font-medium"
123119
style={
124-
method.deprecation
120+
normalizeDeprecation(method) !== null
125121
? { textDecoration: "line-through" }
126122
: undefined
127123
}
@@ -135,11 +131,7 @@ function ScopedDefinitions({
135131
</small>
136132
</h3>
137133

138-
{method.deprecation && (
139-
<div className="mt-1">
140-
<DeprecationWarning message={method.deprecation} />
141-
</div>
142-
)}
134+
{<DeprecationWarning item={method} level="scoped" />}
143135

144136
<div class="pl-2">
145137
<FunctionDisplay func={method} prefix={methodId} />
Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
11
import type { FC } from "hono/jsx";
2+
import { Translation } from "../../translation/";
3+
import type { WithDeprecation } from "../../types/model";
4+
import { normalizeDeprecation } from "../../utils/normalizeModel.js";
25
import { AlertTriangleIcon } from "../icons";
36

47
type DeprecationWarningProps = {
5-
message: string;
8+
item: WithDeprecation;
9+
level: "top" | "scoped";
610
};
711

12+
/**
13+
* Generate a deprecation warning if the item deprecated; return null otherwise.
14+
*/
815
export const DeprecationWarning: FC<DeprecationWarningProps> = ({
9-
message,
16+
item,
17+
level,
1018
}) => {
11-
return (
19+
const deprecation = normalizeDeprecation(item);
20+
if (!deprecation) {
21+
return null;
22+
}
23+
24+
const body = (
1225
<small className="deprecation ml-2 flex items-center bg-yellow-50 px-2 py-1 rounded-md border border-yellow-200">
1326
<div className="w-4 h-4 text-yellow-500 mr-1.5 flex-shrink-0">
1427
<AlertTriangleIcon />
1528
</div>
16-
<span className="text-xs text-yellow-800">{message}</span>
29+
<span className="text-xs text-yellow-800">
30+
<Translation
31+
translationKey="deprecationWarning"
32+
message={deprecation.message}
33+
until={deprecation.until}
34+
/>
35+
</span>
1736
</small>
1837
);
38+
39+
return level === "top" ? (
40+
<div className="mt-1">{body}</div>
41+
) : (
42+
<div className="mt-2">{body}</div>
43+
);
1944
};

src/translation/en-US.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export const Translation: TranslationComponent = (props) => {
3737
return <Fragment>Default value:</Fragment>;
3838
case "stringValues":
3939
return <Fragment>Available string values</Fragment>;
40+
case "deprecationWarning":
41+
return props.until ? (
42+
<Fragment>
43+
{props.message}; it will be removed in Typst {props.until}
44+
</Fragment>
45+
) : (
46+
<Fragment>{props.message}</Fragment>
47+
);
4048
case "showExample":
4149
return <Fragment>Show example</Fragment>;
4250
case "tableOfContents":

src/translation/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type TranslationComponentKey =
5252
| "required"
5353
| "requiredDescription"
5454
// Other texts in documentation
55+
// Don't forget `deprecationWarning` and `showExample` declared below.
5556
| "tutorial"
5657
| "tutorialDescription"
5758
| "reference"
@@ -87,7 +88,13 @@ type TranslationComponentKey =
8788

8889
export type TranslationComponentProps =
8990
| { translationKey: TranslationComponentKey }
90-
| { translationKey: "definitionsOf"; name: string };
91+
| { translationKey: "definitionsOf"; name: string }
92+
| {
93+
translationKey: "deprecationWarning";
94+
message: string;
95+
/** A Typst version, e.g. "0.15.0". */
96+
until: string | null;
97+
};
9198

9299
/**
93100
* Translation component for UI text, descriptions, and other content to be embedded as JSX.

src/translation/ja-JP.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ export const Translation: TranslationComponent = (props) => {
4242
return <Fragment>デフォルト値:</Fragment>;
4343
case "stringValues":
4444
return <Fragment>使用可能な文字列値</Fragment>;
45+
case "deprecationWarning":
46+
return props.until ? (
47+
// TODO: Translate this. Don't forget to change `;` to `;`.
48+
<Fragment>
49+
{props.message}; it will be removed in Typst {props.until}
50+
</Fragment>
51+
) : (
52+
<Fragment>{props.message}</Fragment>
53+
);
4554
case "showExample":
4655
return <Fragment>例を表示</Fragment>;
4756
case "tableOfContents":

src/types/model.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,13 @@ type Shorthands = {
171171
math: Symbol[];
172172
};
173173

174-
type WithDeprecation =
174+
export type WithDeprecation =
175175
// Format since Typst v0.14.0-rc.1 (typst/typst#6617)
176-
| { deprecation_message: string | null; deprecation_until: string | null }
176+
| {
177+
// TODO: This message contains markdown. Check if this is a bug of the official typst-docs.
178+
deprecation_message: string | null;
179+
/** A Typst version without leading `v`. */
180+
deprecation_until: string | null;
181+
}
177182
// Format for Typst v0.13.1
178183
| { deprecation: string | null };

src/utils/normalizeModel.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Normalize models to the latest format.
3+
* @module
4+
*/
5+
6+
import type { WithDeprecation } from "../types/model";
7+
8+
/**
9+
* Return deprecation info if deprecated, return null otherwise.
10+
*/
11+
export function normalizeDeprecation(item: WithDeprecation): {
12+
message: string;
13+
until: string | null;
14+
} | null {
15+
if ("deprecation" in item) {
16+
// For v0.13.1
17+
return item.deprecation ? { message: item.deprecation, until: null } : null;
18+
}
19+
20+
if (item.deprecation_message) {
21+
return { message: item.deprecation_message, until: item.deprecation_until };
22+
}
23+
if (item.deprecation_until) {
24+
// This will never reach for Typst v0.14.0-rc.1 documentation.
25+
return { message: item.deprecation_until, until: null };
26+
}
27+
return null;
28+
}

0 commit comments

Comments
 (0)