Skip to content

Commit

Permalink
Stage 9: Code block styling & MDX plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Jihillestad committed Jul 24, 2024
1 parent fe16de1 commit ebc3982
Show file tree
Hide file tree
Showing 7 changed files with 445 additions and 18 deletions.
1 change: 1 addition & 0 deletions app/blog/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { posts } from "#site/content";
import { MDXContent } from "@/components/mdx-components";
import { notFound } from "next/navigation";
import "@/styles/mdx.css";

interface PostPageProps {
params: {
Expand Down
2 changes: 1 addition & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function RootLayout({
children: React.ReactNode;
}>) {
return (
<html lang="en">
<html lang="en" className="scroll-pt-[3.5rem]">
<body
className={cn(
"min-h-screen bg-background font-sans antialiased",
Expand Down
107 changes: 107 additions & 0 deletions content/blog/code-blocks.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: Lets see what we can do with rehype pretty code
description: Syntax highlighting, line numbers, line highlights, word highlights
date: 2024-03-04
tags: ["code", "rehype pretty", "mdx"]
published: true
---

[`rehype-pretty-code`](https://github.com/atomiks/rehype-pretty-code) is a Rehype plugin powered by the
[`shiki`](https://github.com/shikijs/shiki) syntax highlighter that provides beautiful code blocks for Markdown or MDX. It works on both the server at build-time (avoiding runtime syntax highlighting) and on the client for dynamic highlighting.

## Editor-Grade Highlighting

<span className="mix-blend-plus-lighter text-zinc-400/80">
Enjoy the accuracy and granularity of VS Code's syntax highlighting engine and
the popularity of its themes ecosystem — use any VS Code theme you want!
</span>

## Line Numbers and Line Highlighting

Draw attention to a particular line of code.

```js {4} showLineNumbers
import { useFloating } from "@floating-ui/react";

function MyComponent() {
const { refs, floatingStyles } = useFloating();

return (
<>
<div ref={refs.setReference} />
<div ref={refs.setFloating} style={floatingStyles} />
</>
);
}
```

## Word Highlighting

Draw attention to a particular word or series of characters.

```js /floatingStyles/
import { useFloating } from "@floating-ui/react";

function MyComponent() {
const { refs, floatingStyles } = useFloating();

return (
<>
<div ref={refs.setReference} />
<div ref={refs.setFloating} style={floatingStyles} />
</>
);
}
```

## ANSI Highlighting

```ansi
 vite v5.0.0 dev server running at:
> Local: http://localhost:3000/
> Network: use `--host` to expose
ready in 125ms.
8:38:02 PM [vite] hmr update /src/App.jsx
```

Inline ANSI: `> Local: http://localhost:3000/{:ansi}`

---

### Kitchen Sink Meta Strings

```js showLineNumbers {2-4} title="isEven.js" /console/ caption="Im a caption"
function isEven(number) {
if (number === 1) {
return false;
} else if (number === 2) {
return true;
} else if (number === 3) {
return false;
} else if (number === 4) {
return true;
} else if (number === 5) {
return false;
} else if (number === 6) {
return true;
} else if (number === 7) {
return false;
} else if (number === 8) {
return true;
} else if (number === 9) {
return false;
} else if (number === 10) {
return true;
} else {
return "Number is not between 1 and 10.";
}
}

// Example usage:
console.log(isEven(3)); // Should return false
console.log(isEven(4)); // Should return true
console.log(isEven(11)); // Should return "Number is not between 1 and 10."
```
Loading

0 comments on commit ebc3982

Please sign in to comment.