Skip to content

Commit 47175c6

Browse files
committed
finding the source of truth in MDX configs and page components
1 parent 5af8b24 commit 47175c6

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

src/components/MDX/MDXComponents.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ function Illustration({
315315
return (
316316
<div className="relative group before:absolute before:-inset-y-16 before:inset-x-0 my-16 mx-0 2xl:mx-auto max-w-4xl 2xl:max-w-6xl">
317317
<figure className="my-8 flex justify-center">
318-
<Image
318+
<img
319319
src={src}
320320
alt={alt}
321321
style={{maxHeight: 300}}
@@ -351,7 +351,7 @@ function IllustrationBlock({
351351
const images = imageInfos.map((info, index) => (
352352
<figure key={index}>
353353
<div className="bg-white rounded-lg p-4 flex-1 flex xl:p-6 justify-center items-center my-4">
354-
<Image
354+
<img
355355
className="text-primary"
356356
src={info.src}
357357
alt={info.alt}
@@ -487,9 +487,7 @@ function YouTubeIframe(props: any) {
487487

488488
function Image(props: any) {
489489
const {alt, ...rest} = props;
490-
return (
491-
<Image alt={alt} className="max-w-[calc(min(700px,100%))]" {...rest} />
492-
);
490+
return <img alt={alt} className="max-w-[calc(min(700px,100%))]" {...rest} />;
493491
}
494492

495493
export const MDXComponents = {

src/components/PageHeading.tsx

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ import Tag from 'components/Tag';
1414
import {H1} from './MDX/Heading';
1515
import type {RouteTag, RouteItem} from './Layout/getRouteMeta';
1616
import * as React from 'react';
17+
import {useState, useEffect} from 'react';
18+
import {useRouter} from 'next/router';
1719
import {IconCanary} from './Icon/IconCanary';
1820
import {IconExperimental} from './Icon/IconExperimental';
21+
import {IconCopy} from './Icon/IconCopy';
22+
import {Button} from './Button';
1923

2024
interface PageHeadingProps {
2125
title: string;
@@ -27,18 +31,67 @@ interface PageHeadingProps {
2731
breadcrumbs: RouteItem[];
2832
}
2933

34+
function CopyAsMarkdownButton() {
35+
const {asPath} = useRouter();
36+
const [copied, setCopied] = useState(false);
37+
38+
useEffect(() => {
39+
if (!copied) return;
40+
const timer = setTimeout(() => setCopied(false), 2000);
41+
return () => clearTimeout(timer);
42+
}, [copied]);
43+
44+
async function fetchPageBlob() {
45+
const cleanPath = asPath.split(/[?#]/)[0];
46+
const res = await fetch(cleanPath + '.md');
47+
if (!res.ok) throw new Error('Failed to fetch');
48+
const text = await res.text();
49+
return new Blob([text], {type: 'text/plain'});
50+
}
51+
52+
async function handleCopy() {
53+
try {
54+
await navigator.clipboard.write([
55+
// Don't wait for the blob, or Safari will refuse clipboard access
56+
new ClipboardItem({'text/plain': fetchPageBlob()}),
57+
]);
58+
setCopied(true);
59+
} catch {
60+
// Silently fail
61+
}
62+
}
63+
64+
return (
65+
<Button onClick={handleCopy} className="text-sm py-1 px-3">
66+
<IconCopy className="w-3.5 h-3.5 me-1.5" />
67+
{copied ? (
68+
'Copied!'
69+
) : (
70+
<>
71+
<span className="hidden sm:inline">Copy page</span>
72+
<span className="sm:hidden">Copy</span>
73+
</>
74+
)}
75+
</Button>
76+
);
77+
}
78+
3079
function PageHeading({
3180
title,
3281
status,
3382
version,
3483
tags = [],
3584
breadcrumbs,
3685
}: PageHeadingProps) {
37-
console.log('version', version);
3886
return (
3987
<div className="px-5 sm:px-12 pt-3.5">
4088
<div className="max-w-4xl ms-0 2xl:mx-auto">
41-
{breadcrumbs ? <Breadcrumbs breadcrumbs={breadcrumbs} /> : null}
89+
<div className="flex justify-between items-start">
90+
<div className="flex-1">
91+
{breadcrumbs ? <Breadcrumbs breadcrumbs={breadcrumbs} /> : null}
92+
</div>
93+
<CopyAsMarkdownButton />
94+
</div>
4295
<H1 className="mt-0 text-primary dark:text-primary-dark -mx-.5 break-words">
4396
{title}
4497
{version === 'canary' && (

src/sidebarReference.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,4 +575,4 @@
575575
]
576576
}
577577
]
578-
}
578+
}

0 commit comments

Comments
 (0)