Skip to content

Commit 993876d

Browse files
committed
Add codeblocks to docs (filename is slightly broken)
1 parent 49ba74f commit 993876d

5 files changed

Lines changed: 378 additions & 5 deletions

File tree

app/docs/content/about/docs-page-example.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
import { Heading, SubHeading, Text } from "@/components/docs_elements";
1+
import { Code, Heading, SubHeading, Text } from "@/components/docs_elements";
2+
3+
const codeExample = `
4+
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
5+
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
6+
import io.github.javabee.Robot;
7+
8+
@TeleOp(name="Example OpMode")
9+
public class ExampleOpMode extends LinearOpMode {
10+
private Robot robot;
11+
12+
@Override
13+
public void runOpMode() throws InterruptedException {
14+
robot = new Robot(this);
15+
16+
telemetry.addLine("Initialized!");
17+
waitForStart();
18+
while (opModeIsActive()) {
19+
robot.update();
20+
}
21+
}
22+
}`;
223

324
export default function DocsPageExample() {
425
return (
@@ -10,20 +31,26 @@ export default function DocsPageExample() {
1031
Praesent orci ipsum, tristique quis erat ut, porta fringilla felis. Suspendisse turpis nisi, maximus a
1132
semper at, efficitur vel orci.
1233
</Text>
13-
<Heading>Use a Heading for big ideas</Heading>
34+
35+
<Heading>This is a Heading</Heading>
1436
<Text>
1537
Suspendisse consequat orci eu tincidunt pulvinar. Quisque vel libero arcu. Donec arcu metus, eleifend non
1638
tincidunt nec, molestie at purus. Nam porttitor est vitae elit varius tincidunt. Aenean tristique nisi at
1739
sollicitudin ultrices. Sed nisl quam, accumsan vel leo vitae, ullamcorper bibendum ipsum. Interdum et
1840
malesuada fames ac ante ipsum primis in faucibus. Integer eget accumsan ipsum. Praesent vel nulla lacus. In
1941
mollis ipsum nec diam faucibus aliquet.
2042
</Text>
21-
<SubHeading>Use a Subheading for smaller sections</SubHeading>
43+
44+
<SubHeading>This is a SubHeading</SubHeading>
2245
<Text>
2346
Curabitur a nunc ac nisl efficitur efficitur. Donec eget ligula sed enim efficitur convallis. Donec
2447
convallis, enim in efficitur efficitur, ipsum nisl efficitur nunc, nec efficitur nunc nisl nec nisi.
2548
Donec convallis, enim in efficitur efficitur, ipsum nisl efficitur nunc, nec efficitur nunc nisl nec nisi.
2649
</Text>
50+
51+
<Heading>Codeblocks</Heading>
52+
<Text>Here is some code for something:</Text>
53+
<Code code={codeExample} language="java" filename="ExampleOpMode.java" />
2754
</div>
2855
);
2956
}

app/docs/content/about/getting-started.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Heading, SubHeading, Text, List } from "@/components/docs_elements";
22
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
33
import { Button } from "@/components/ui/button";
44
import { CodeXml, Hammer, BookOpenText } from "lucide-react";
5-
import { Separator } from "@/components/ui/separator";
65

76
const suitePartsListContent = [
87
{

components/docs_elements.tsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
* These components help maintain consistent styling across the documentation pages.
44
*/
55

6+
// For using Java syntax highlighting in code blocks
7+
import Prism from "prismjs";
8+
import "prismjs/components/prism-java";
9+
import { CodeBlock } from 'react-code-block';
10+
import { themes } from 'prism-react-renderer';
11+
import { useCopyToClipboard } from 'react-use';
12+
import { Button } from "@/components/ui/button";
13+
14+
import { useTheme } from "next-themes";
15+
616
/**
717
* Heading component for main section titles. Uses an h2 tag internally.
818
* @param children The content to be displayed inside the heading.
@@ -67,4 +77,41 @@ function List({ordered, content}: ListProps) {
6777
);
6878
}
6979

70-
export { Heading, SubHeading, Text, List };
80+
interface CodeProps {
81+
code: string;
82+
language: string;
83+
filename: string;
84+
}
85+
86+
function Code({ code, language, filename }: CodeProps) {
87+
const { theme } = useTheme();
88+
const [state, copyToClipboard] = useCopyToClipboard();
89+
90+
const copyCode = () => {
91+
// Logic to copy `code`
92+
copyToClipboard(code);
93+
};
94+
95+
return (
96+
<div className="relative">
97+
<CodeBlock code={code} language={language} prism={Prism} theme={theme === 'dark' ? themes.vsDark : themes.vsLight}>
98+
{/* Filename */}
99+
<div className="text-sm text-gray-400 px-6 py-4">{filename}</div>
100+
<CodeBlock.Code className="bg-card p-4 rounded-lg shadow-lg ring-1 ring-foreground/10">
101+
<div className="table-row">
102+
<CodeBlock.LineNumber className="table-cell pr-5 text-sm text-gray-500 text-right select-none" />
103+
<CodeBlock.LineContent className="table-cell">
104+
<CodeBlock.Token />
105+
</CodeBlock.LineContent>
106+
</div>
107+
</CodeBlock.Code>
108+
109+
<Button className="absolute top-2 right-2 text-sm font-semibold" onClick={copyCode}>
110+
{state.value ? 'Copied!' : 'Copy code'}
111+
</Button>
112+
</CodeBlock>
113+
</div>
114+
);
115+
}
116+
117+
export { Heading, SubHeading, Text, List, Code };

0 commit comments

Comments
 (0)