Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions gatsby/__tests__/toc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { mdxAstToToc } from "../toc";

describe("mdxAstToToc tag query parsing", () => {
it("does not emit a bogus ?undefined query when the image URL has no query string", () => {
const toc = mdxAstToToc(
[
{
type: "list",
children: [
{
type: "listItem",
children: [
{
type: "paragraph",
children: [
{ type: "text", value: "Data Service" },
{
type: "image",
alt: "PREVIEW",
url: "/media/tidb-cloud/blank_transparent_placeholder.png",
},
],
},
],
},
],
},
] as any,
"en/tidbcloud/master/TOC"
);

expect(toc[0].tag).toEqual({
value: "PREVIEW",
query: undefined,
});
});

it("does not create a tag when the TOC image has no alt text", () => {
const toc = mdxAstToToc(
[
{
type: "list",
children: [
{
type: "listItem",
children: [
{
type: "paragraph",
children: [
{ type: "text", value: "Data Service" },
{
type: "image",
alt: null,
url: "/media/tidb-cloud/blank_transparent_placeholder.png",
},
],
},
],
},
],
},
] as any,
"en/tidbcloud/master/TOC"
);

expect(toc[0].tag).toBeUndefined();
});

it("keeps tag query params when the image URL includes them", () => {
const toc = mdxAstToToc(
[
{
type: "list",
children: [
{
type: "listItem",
children: [
{
type: "paragraph",
children: [
{ type: "text", value: "Beta Feature" },
{
type: "image",
alt: "BETA",
url: "/media/tidb-cloud/blank_transparent_placeholder.png?color=%232d9cd2",
},
],
},
],
},
],
},
] as any,
"en/tidbcloud/master/TOC"
);

expect(toc[0].tag).toEqual({
value: "BETA",
query: "?color=%232d9cd2",
});
});
});
14 changes: 9 additions & 5 deletions gatsby/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ListItem,
List,
Link,
Image,
Paragraph,
Text,
Content,
Expand Down Expand Up @@ -157,11 +158,14 @@ function getContentFromLink(

const child = content.children[0] as Link | Text;
// use `image` as tag
const image = content.children.find((n) => n.type === "image");
const tag = image && {
value: image.alt!,
query: `?${image.url.split("?")[1]}`,
};
const image = content.children.find((n): n is Image => n.type === "image");
const imageQuery = image?.url.split("?")[1];
const tag = image?.alt
? {
value: image.alt,
query: imageQuery ? `?${imageQuery}` : undefined,
}
: undefined;

if (child.type === "link") {
if (child.children.length === 0) {
Expand Down
3 changes: 1 addition & 2 deletions locale/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
"tidbOperatorReleases": "TiDB Operator Releases",
"tiupReleases": "TiUP Releases",
"badge": {
"preview": "Preview",
"beta": "Beta"
"preview": "Preview"
},
"appdev": "App Dev",
"asktug": "Forum",
Expand Down
3 changes: 1 addition & 2 deletions locale/ja/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
"tidbOperatorReleases": "TiDB Operator リリース",
"tiupReleases": "TiUP リリース",
"badge": {
"preview": "Preview",
"beta": "Beta"
"preview": "Preview"
},
"appdev": "アプリ開発",
"asktug": "Forum",
Expand Down
3 changes: 1 addition & 2 deletions locale/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"tidbOperatorReleases": "TiDB Operator 版本发布记录",
"tiupReleases": "TiUP 版本发布记录",
"badge": {
"preview": "Preview",
"beta": "Beta"
"preview": "Preview"
},
"appdev": "开发指南",
"asktug": "社区",
Expand Down
29 changes: 29 additions & 0 deletions src/components/Badge/PreviewBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Chip from "@mui/material/Chip";
import { useTheme } from "@mui/material/styles";

const PreviewBadge = (props: { label: string }) => {
const theme = useTheme();

return (
<Chip
label={props.label}
size="small"
variant="outlined"
sx={{
height: "20px",
fontSize: "12px",
fontWeight: 400,
borderRadius: "10px",
pointerEvents: "none",
"& .MuiChip-label": {
paddingLeft: "8px",
paddingRight: "8px",
lineHeight: "20px",
color: theme.palette.carbon[700],
},
}}
/>
);
};

export default PreviewBadge;
47 changes: 2 additions & 45 deletions src/components/Layout/Header/HeaderNavConfigData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,11 @@ import { NavConfig } from "./HeaderNavConfigType";
import { CLOUD_MODE_KEY } from "shared/useCloudPlan";
import { CloudPlan, TOCNamespace } from "shared/interface";
import OpenInNewIcon from "@mui/icons-material/OpenInNew";
import Chip from "@mui/material/Chip";
import { useTheme } from "@mui/material/styles";
import PreviewBadge from "components/Badge/PreviewBadge";

import TiDBCloudIcon from "media/icons/cloud-03.svg";
import TiDBIcon from "media/icons/layers-three-01.svg";

const PreviewBadge = (props: { label: string }) => {
const theme = useTheme();
return (
<Chip
label={props.label}
size="small"
variant="outlined"
sx={{
height: "20px",
fontSize: "12px",
fontWeight: 400,
borderRadius: "10px",
pointerEvents: "none",
"& .MuiChip-label": {
paddingLeft: "8px",
paddingRight: "8px",
lineHeight: "20px",
color: theme.palette.carbon[700],
},
}}
/>
);
};

const BetaTagBadge = (props: { label: string }) => (
<Chip
label={props.label}
variant="outlined"
size="small"
sx={{
flexShrink: 0,
textTransform: "uppercase",
pointerEvents: "none",
fontSize: "10px",
height: "20px",
borderColor: "#c0e1f1",
color: "#2d9cd2",
fontWeight: 500,
}}
/>
);

/**
* Default navigation configuration
*/
Expand Down Expand Up @@ -167,7 +124,7 @@ const getDefaultNavConfig = (
leftNavLabel: (
<>
{t("navbar.tidbForAI")}
<BetaTagBadge label={t("navbar.badge.beta")} />
<PreviewBadge label={t("navbar.badge.preview")} />
</>
),
to: "/ai",
Expand Down
32 changes: 25 additions & 7 deletions src/components/Layout/LeftNav/LeftNavTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
import Divider from "@mui/material/Divider";
import { useTheme } from "@mui/material/styles";
import { useTranslation } from "gatsby-plugin-react-i18next";

import { RepoNavLink, RepoNav } from "shared/interface";
import LinkComponent from "components/Link";
import PreviewBadge from "components/Badge/PreviewBadge";
import { scrollToElementIfInView } from "shared/utils";
import { alpha, Chip } from "@mui/material";

Expand Down Expand Up @@ -190,6 +192,8 @@ export default function ControlledTreeView(props: {
});

const theme = useTheme();
const { t } = useTranslation();
const previewBadgeLabel = t("navbar.badge.preview");
const [disableTransition, setDisableTransition] = React.useState(false);
const previousUrlRef = React.useRef<string | null>(null);

Expand Down Expand Up @@ -313,7 +317,7 @@ export default function ControlledTreeView(props: {
) : (
<Box sx={{ flexShrink: 0 }} width={16} height={16} />
)}
{generateItemLabel(item)}
{generateItemLabel(item, previewBadgeLabel)}
</Stack>
);
};
Expand Down Expand Up @@ -389,10 +393,22 @@ export default function ControlledTreeView(props: {
);
}

const generateItemLabel = ({ content: contents, tag }: RepoNavLink) => {
const tagQuery = new URLSearchParams(tag?.query);
const tagColor = tagQuery.get("color");
const tagColor02 = tagColor && alpha(tagColor, 0.2);
const generateItemLabel = (
{ content: contents, tag }: RepoNavLink,
previewBadgeLabel: string
) => {
const normalizedTagValue = tag?.value?.trim().toUpperCase();
const isPreviewTag = normalizedTagValue === "PREVIEW";
let tagColor: string | null = null;
let tagColor02: string | null = null;

// PREVIEW intentionally ignores source color overrides to match shared badges.
if (tag && !isPreviewTag) {
const tagQuery = new URLSearchParams(tag.query ?? "");
tagColor = tagQuery.get("color");
tagColor02 = tagColor ? alpha(tagColor, 0.2) : null;
}
Comment thread
qiancai marked this conversation as resolved.

return (
<Stack sx={{ width: "100%" }} direction="row" gap="4px">
<Box
Expand Down Expand Up @@ -422,7 +438,9 @@ const generateItemLabel = ({ content: contents, tag }: RepoNavLink) => {
);
})}
</Box>
{tag && (
{tag && isPreviewTag ? (
<PreviewBadge label={previewBadgeLabel} />
) : tag ? (
<Chip
label={tag.value}
variant="outlined"
Expand All @@ -438,7 +456,7 @@ const generateItemLabel = ({ content: contents, tag }: RepoNavLink) => {
fontWeight: 500,
}}
/>
)}
) : null}
</Stack>
);
};