Skip to content
Merged
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: 5 additions & 1 deletion app/components/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export default function Index({
}: {
groupedReferences: Record<string, Record<string, ReferencePage[]>>;
}) {
const pathname = usePathname();
// trailingSlash: true makes this "/x/", but the hrefs compared below have no slash
const pathname = usePathname().replace(/\/+$/, '');
const activeItemRef = useRef<HTMLAnchorElement>(null);
const containerRef = useRef<HTMLElement>(null);

Expand Down Expand Up @@ -114,6 +115,7 @@ export default function Index({
<Link
href={`/docs/reference/${typeSection.type}`}
ref={isTypeActive ? activeItemRef : null}
aria-current={isTypeActive ? 'page' : undefined}
className={`flex-1 px-3 py-3 rounded-lg text-sm font-semibold uppercase transition-all duration-200 ${
isTypeActive
? "bg-blue-500/20 text-blue-300 border border-blue-500/30"
Expand Down Expand Up @@ -147,6 +149,7 @@ export default function Index({
<Link
href={`/docs/reference/${typeSection.type}/${categorySection.category}`}
ref={isCategoryActive ? activeItemRef : null}
aria-current={isCategoryActive ? 'page' : undefined}
className={`flex-1 px-3 py-3 rounded-lg text-sm transition-all duration-200 ${
isCategoryActive
? "bg-blue-500/20 text-blue-300 border border-blue-500/30"
Expand All @@ -170,6 +173,7 @@ export default function Index({
<Link
href={itemPath}
ref={isItemActive ? activeItemRef : null}
aria-current={isItemActive ? 'page' : undefined}
className={`flex-1 px-3 py-3 rounded-lg text-sm transition-all duration-200 ${
isItemActive
? "bg-blue-500/20 text-blue-300 border border-blue-500/30"
Expand Down
17 changes: 15 additions & 2 deletions app/components/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ interface MarkdownProps {
sourcePath: string;
}

// GFM table cells can't hold newlines, so docs break lines with <br>; render that one tag, not raw HTML.
function remarkBrTags() {
return (tree: any) => {
const visit = (node: any) => node.children?.forEach((child: any, i: number) => {
if (child.type === 'html' && /^<br\s*\/?>$/i.test(child.value)) node.children[i] = { type: 'break' };
else visit(child);
});
visit(tree);
};
}

const remarkPlugins = [remarkGfm, remarkBrTags];

export default function Markdown({ content, sourcePath }: MarkdownProps) {
const processedContent = useMemo(() => {
// Split content by H2 headings to group sections
Expand All @@ -29,7 +42,7 @@ export default function Markdown({ content, sourcePath }: MarkdownProps) {
elements.push(
<div key={`intro-${index}`} className="mb-8">
<ReactMarkdown
remarkPlugins={[remarkGfm]}
remarkPlugins={remarkPlugins}
components={getMarkdownComponents(sourcePath)}
>
{section}
Expand All @@ -49,7 +62,7 @@ export default function Markdown({ content, sourcePath }: MarkdownProps) {
{title}
</h2>
<ReactMarkdown
remarkPlugins={[remarkGfm]}
remarkPlugins={remarkPlugins}
components={getMarkdownComponents(sourcePath)}
>
{sectionContent}
Expand Down
37 changes: 35 additions & 2 deletions app/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { useEffect, useRef, useState, type ReactNode } from "react";
import Image from "next/image";
import Link from "next/link";
import { usePathname } from "next/navigation";
// The Jekyll layout renders the same file, so the blog and announcement pages show this nav too
import nav from "../../blogs/_data/nav.json";
import { withBasePath } from "../services/sitePath";
Expand Down Expand Up @@ -96,14 +97,45 @@ function ExternalIcon() {
);
}

// The banner above the sticky nav varies in height, so fit the open menu to the screen space actually left
function fitToViewport(menu: HTMLDivElement | null) {
if (!menu) return;
const fit = () => {
menu.style.maxHeight = `${window.innerHeight - menu.getBoundingClientRect().top}px`;
};
fit();
window.addEventListener("resize", fit);
return () => window.removeEventListener("resize", fit);
}

export default function Navbar() {
const pathname = usePathname();
const [isMenuOpen, setIsMenuOpen] = useState(false);
const [menuPathname, setMenuPathname] = useState(pathname);
const [openGroup, setOpenGroup] = useState<string | null>(null);
const desktopNav = useRef<HTMLDivElement>(null);
const menuButton = useRef<HTMLButtonElement>(null);

const closeMenu = () => setIsMenuOpen(false);
const closeGroup = () => setOpenGroup(null);

// Close on any navigation, back/forward included; resetting during render avoids a frame of open menu on the new page
if (menuPathname !== pathname) {
setMenuPathname(pathname);
setIsMenuOpen(false);
}

useEffect(() => {
if (!isMenuOpen) return;
const closeOnEscape = (event: KeyboardEvent) => {
if (event.key !== "Escape") return;
setIsMenuOpen(false);
menuButton.current?.focus();
};
document.addEventListener("keydown", closeOnEscape);
return () => document.removeEventListener("keydown", closeOnEscape);
}, [isMenuOpen]);

useEffect(() => {
if (!openGroup) return;
// Safari never focuses a clicked button, so blur alone cannot close the panel
Expand All @@ -127,7 +159,7 @@ export default function Navbar() {
<nav className="sticky top-0 z-50 border-b border-neutral-700 bg-neutral-900/95 shadow-sm backdrop-blur">
<div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
<div className="flex items-center justify-between gap-4 py-3 md:h-16 md:py-0">
<Link href="/" className="flex shrink-0 items-center gap-2 text-xl font-bold text-white sm:text-2xl">
<Link href="/" onClick={closeMenu} className="flex shrink-0 items-center gap-2 text-xl font-bold text-white sm:text-2xl">
<Image
src={withBasePath("/images/DocumentDB Logo - background removed.png")}
alt="DocumentDB Logo"
Expand Down Expand Up @@ -215,6 +247,7 @@ export default function Navbar() {
</div>

<button
ref={menuButton}
type="button"
className="inline-flex h-10 w-10 items-center justify-center rounded-md border border-neutral-700 text-gray-200 transition-colors hover:border-blue-400 hover:text-white focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-400 lg:hidden"
aria-expanded={isMenuOpen}
Expand All @@ -235,7 +268,7 @@ export default function Navbar() {
</div>

{isMenuOpen ? (
<div id="mobile-navigation" className="border-t border-neutral-800 py-3 lg:hidden">
<div id="mobile-navigation" ref={fitToViewport} className="overflow-y-auto border-t border-neutral-800 py-3 lg:hidden">
<div className="flex flex-col gap-1">
{links.map((item) =>
"items" in item ? (
Expand Down
12 changes: 6 additions & 6 deletions app/docs/reference/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export default function ReferenceLayout({
}>) {
const groupedReferences = getReferencesGroupedByTypeAndCategory();

// Phones get no sidebar, so the page scrolls rather than a 100vh pane half under the navbar
// overflow-clip, not hidden: hidden makes a scroll container and the sidebar would stop sticking
return (
<div className="min-h-screen bg-neutral-900 relative overflow-hidden md:max-h-screen">
<div className="min-h-screen bg-neutral-900 relative overflow-clip">
{/* Background elements */}
<div className="absolute inset-0 bg-gradient-to-br from-neutral-900 via-neutral-800 to-black"></div>
<div className="absolute inset-0 opacity-5">
Expand All @@ -29,9 +29,9 @@ export default function ReferenceLayout({
style={{ animationDelay: "1.5s" }}
></div>
</div>
<div className="relative flex md:h-screen">
{/* Sidebar (desktop only; mobile gets the disclosure below) */}
<div className="hidden w-80 bg-neutral-800/50 backdrop-blur-sm border-r border-neutral-700/50 md:flex flex-col h-full">
<div className="relative flex">
{/* Sidebar (desktop only; mobile gets the disclosure below), held under the 4rem navbar */}
<div className="hidden w-80 bg-neutral-800/50 backdrop-blur-sm border-r border-neutral-700/50 md:flex flex-col md:sticky md:top-16 md:h-[calc(100dvh-4rem)]">
<div className="p-6 border-b border-neutral-700/50 flex-shrink-0">
<Link href="/docs" className="text-blue-400 hover:text-blue-300 text-sm mb-4 flex items-center transition-colors">
<svg
Expand All @@ -53,7 +53,7 @@ export default function ReferenceLayout({
</div>
<Index groupedReferences={groupedReferences} />
</div>
<article className="min-w-0 flex-1 p-4 sm:p-8 md:h-full md:overflow-y-auto">
<article className="min-w-0 flex-1 p-4 sm:p-8">
<div className="max-w-4xl">
{/* Mobile reference navigation */}
<details className="mb-6 rounded-lg border border-neutral-700/50 bg-neutral-800/50 md:hidden">
Expand Down
9 changes: 9 additions & 0 deletions tests/markdownBasePath.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ describe('Markdown heading anchors', () => {
}
});
});

describe('Markdown table cells', () => {
it('turns <br> into a line break but keeps other HTML escaped', async () => {
const html = await render('| a | b |\n| --- | --- |\n| x <br> y <br/> z | <b>bold</b> |');
expect(html.match(/<br\/>/g)).toHaveLength(2);
expect(html).not.toContain('&lt;br');
expect(html).toContain('&lt;b&gt;bold&lt;/b&gt;');
});
});
27 changes: 27 additions & 0 deletions tests/referenceIndex.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it, vi } from 'vitest';
import { renderToStaticMarkup } from 'react-dom/server';
import { createElement } from 'react';

const pathname = vi.hoisted(() => ({ value: '' }));
vi.mock('next/navigation', () => ({ usePathname: () => pathname.value }));

import Index from '../app/components/Index';

const item = (name: string) => ({ name, description: '', reference: `operators/accumulators/${name}`, type: 'operators', category: 'accumulators' });
const groupedReferences = { operators: { accumulators: [item('$avg'), item('$sum')] } };

function currentLinks(path: string) {
pathname.value = path;
const html = renderToStaticMarkup(createElement(Index, { groupedReferences }));
return [...html.matchAll(/<a[^>]*aria-current="page"[^>]*>([^<]*)<\/a>/g)].map((match) => match[1]);
}

describe('Reference sidebar current page', () => {
// trailingSlash: true serves every page at "/x/"; the static render sees "%24", the browser "$"
it('marks exactly the current entry', () => {
expect(currentLinks('/docs/reference/operators/accumulators/%24avg/')).toEqual(['$avg']);
expect(currentLinks('/docs/reference/operators/accumulators/$avg/')).toEqual(['$avg']);
expect(currentLinks('/docs/reference/operators/accumulators/')).toEqual(['Accumulators']);
expect(currentLinks('/docs/reference/operators/')).toEqual(['Operators']);
});
});
Loading