Skip to content

Fix invalid HTML on an Announcement banner without a CTA. #3251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions .changeset/healthy-chicken-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gitbook-v2": patch
"gitbook": patch
---

Fix invalid HTML on an Announcement banner without a CTA.
3 changes: 2 additions & 1 deletion packages/gitbook-v2/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
const resolve = ADAPTIVE_CONTENT_HOSTS.includes(siteRequestURL.hostname)
? resolvePublishedContentByUrl
: getPublishedContentByURL;

const siteURLData = await throwIfDataError(
resolve({
url: siteRequestURL.toString(),
Expand Down Expand Up @@ -290,7 +291,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
return writeResponseCookies(response, cookies);
};

// For https://preview/<siteURL> requests,
// For https://preview/<siteId> requests,
if (siteRequestURL.hostname === 'preview') {
return serveWithQueryAPIToken(
// We scope the API token to the site ID.
Expand Down
71 changes: 48 additions & 23 deletions packages/gitbook/src/components/Announcement/AnnouncementBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,18 @@ export function AnnouncementBanner(props: {
}) {
const { announcement, contentRef } = props;

const hasLink = announcement.link && contentRef?.href;
const hasLink = contentRef?.href;
const closeable = announcement.style !== 'danger';

const Tag = hasLink ? Link : 'div';
const style = BANNER_STYLES[announcement.style];

return (
<div id="announcement-banner" className="theme-bold:bg-header-background pt-4 pb-2">
<div className="scroll-nojump">
<div className={tcls('relative', CONTAINER_STYLE)}>
<Tag
href={contentRef?.href ?? ''}
className={tcls(
'flex w-full items-start justify-center overflow-hidden rounded-md straight-corners:rounded-none px-4 py-3 text-neutral-strong text-sm theme-bold:ring-1 theme-gradient:ring-1 ring-inset transition-colors',
style.container,
closeable && 'pr-12',
hasLink && style.hover
)}
insights={
announcement.link
? {
type: 'link_click',
link: {
target: announcement.link.to,
position: SiteInsightsLinkPosition.Announcement,
},
}
: undefined
}
<AnnouncementBannerParent
announcement={announcement}
closeable={closeable}
contentRef={contentRef}
>
<Icon
icon={style.icon as IconName}
Expand Down Expand Up @@ -75,7 +58,7 @@ export function AnnouncementBanner(props: {
</div>
) : null}
</div>
</Tag>
</AnnouncementBannerParent>
{closeable ? (
<button
className={`absolute top-0 right-4 mt-2 mr-2 rounded straight-corners:rounded-none p-1.5 transition-all hover:ring-1 sm:right-6 md:right-8 ${style.close}`}
Expand All @@ -91,6 +74,48 @@ export function AnnouncementBanner(props: {
);
}

/**
* Render the appropriate parent for the announcement banner depending on the presence of a link.
*/
function AnnouncementBannerParent(props: {
announcement: CustomizationAnnouncement;
children: React.ReactNode;
closeable: boolean;
contentRef: ResolvedContentRef | null;
}) {
const { announcement, contentRef, closeable, children } = props;
const style = BANNER_STYLES[announcement.style];

const classNames = [
'flex w-full items-start justify-center overflow-hidden rounded-md straight-corners:rounded-none px-4 py-3 text-neutral-strong text-sm theme-bold:ring-1 theme-gradient:ring-1 ring-inset transition-colors',
style.container,
];

if (contentRef?.href) {
return (
<Link
href={contentRef.href}
className={tcls(classNames, closeable && 'pr-12', style.hover)}
insights={
announcement.link
? {
type: 'link_click',
link: {
target: announcement.link.to,
position: SiteInsightsLinkPosition.Announcement,
},
}
: undefined
}
>
{children}
</Link>
);
}

return <div className={tcls(classNames)}>{children}</div>;
}

/**
* Dismiss the announcement banner and store the dismissal state in local storage.
* @see AnnouncementScript
Expand Down