fix(docs): improve and increase consistency for code blocks (#3163)

Follow-up from #3162.

This PR does various tweaks, mostly related to code blocks.

- Adds missing titles to code blocks, and updates titles to be more
consistent
- Adds filenames to most ` ```tsx` blocks with e.g. `main.ts`, `dev.ts`
or a suitable example (e.g. `routes/greeting.tsx`) based on the code
- Adds filenames to ` ```diff` blocks if there's a relevant file, to
make the icon e.g. TS over Diff icon
  - Code blocks with Fresh code (e.g. interfaces) uses ` ```tsx fresh 🍋`
- Changes most ` ```sh` blocks with example terminal commands to ` ```sh
Terminal`
- Updates ` ```txt Project Structure` or ` ```diff Project Structure`
blocks to be more consistent
- Changes `sh` or `txt` to `txt-files` which improves highlighting and
makes icon consistent
- Renames `<project dir>` to `<project root>` to match builder `root`
option terminology
- Adds `<project root>` root folder to most/all overview folder
overviews
  - Makes the structure blocks more consistently displayed
- Tried to make some "bare" examples more complete

## Comparison

Here's a small curated set of before/after changes.

### Deployment

- `deno` highlighted in terminal commands
- `.gitignore` syntax highlighting works without using `sh` as language

<img width="1641" height="1009" alt="image"
src="https://github.com/user-attachments/assets/170cf833-1f6a-4b0d-8d2d-a081bc28a967"
/>

### File routing

Updated icon, comments same as before, but root and hierarchy have new
colors (`operator`/`function`).

<img width="1647" height="588" alt="image"
src="https://github.com/user-attachments/assets/f28db51b-4bee-45e4-be1c-e137a6d3ae3d"
/>
This commit is contained in:
Christian Svensson
2025-08-01 09:07:42 +02:00
committed by GitHub
parent 1cecb372a6
commit a2de5154ec
57 changed files with 280 additions and 214 deletions
+9 -9
View File
@@ -8,18 +8,18 @@ aria-current attribute when rendering links that match the current URL. This
attribute is recognized by assistive technologies and clearly indicates the
current page within a set of pages.
- aria-current="page" - Added to links with an exact path match, enhancing
- `aria-current="page"` - Added to links with an exact path match, enhancing
accessibility by indicating the current page to assistive technologies.
As we aim to improve accessibility, we encourage the use of aria-current for
As we aim to improve accessibility, we encourage the use of `aria-current` for
styling current links where applicable.
## Styling with CSS
The aria-current attribute is easily styled with CSS using attribute selectors,
providing a native way to visually differentiate the active link.
The `aria-current` attribute is easily styled with CSS using attribute
selectors, providing a native way to visually differentiate the active link.
```css
```css static/styles.css
/* Give links pointing to the current page a green color */
a[aria-current="page"] {
color: green;
@@ -38,7 +38,7 @@ elements with the aria-current attribute using bracket notation in your class
definitions. However, the specific syntax varies slightly between Tailwind and
Twind. For Tailwind, use the syntax:
```tsx
```tsx component/Menu.tsx
function Menu() {
return (
<a href="/foo" class="aria-[current]:text-green-600">
@@ -50,7 +50,7 @@ function Menu() {
For Twind, the syntax is:
```tsx
```tsx component/Menu.tsx
function Menu() {
return (
<a href="/foo" class="[aria-current]:text-green-600">
@@ -65,7 +65,7 @@ function Menu() {
The original twind plugin (`import twindPlugin from "$fresh/plugins/twind.ts";`)
supports the above style:
```tsx
```tsx routes/page.tsx
class="[aria-current='page']:text-green-600"
```
@@ -74,6 +74,6 @@ class="[aria-current='page']:text-green-600"
The new twind plugin (`import twindPlugin from "$fresh/plugins/twindv1.ts";`)
requires a slightly different syntax (note the position of the left bracket):
```tsx
```tsx routes/page.tsx
class="aria-[current='page']:text-green-600"
```
+4 -4
View File
@@ -6,8 +6,8 @@ description: |
When you initialize a project with `deno run -A -r https://fresh.deno.dev`,
you'll end up with a project like the following:
```txt Project Structure
.
```txt-files Project Structure
<project root>
├── README.md
├── components
│   └── Button.tsx
@@ -53,8 +53,8 @@ Here's what the diff of `deno.json` looks like:
The resulting file structure looks like this:
```txt Project Structure
.
```txt-files Project Structure
<project root>
├── README.md
├── deno.json
└── src
@@ -29,7 +29,7 @@ rendering might not be feasible.
> [warn]: Proper typing might not be easily available, so we might need to
> define our own types or not use types at all.
```ts
```ts context.ts
export const leafletContext = createContext<typeof Leaflet | null>(null);
```
@@ -46,7 +46,7 @@ state, and this value will be handled/shared with our other components.
> script and css may cause issues. Leaflet, for instance, will throw errors if
> we try to load it again.
```tsx
```tsx context.ts
function LeafletProvider(props: { children: ComponentChildren }) {
if (!IS_BROWSER) {
return (
@@ -85,7 +85,7 @@ cases where the context has not loaded values yet is a good practice as well, in
this way we can have a smooth integration and manipulation of client-side data
and logic on our server-side code.
```tsx
```tsx component/Map.tsx
function MapComponent() {
const leaf = useContext(leafletContext);
if (!leaf) return <p>Context not ready. Component placeholder</p>;
@@ -105,7 +105,7 @@ to demonstrate a simple usage. In real cases, it's usually better to add the
Provider directly to our Page and then use Components that depend on that
provider inside it.
```tsx
```tsx islands/MapIsland.tsx
export default function MapIsland() {
return (
<LeafletProvider>
@@ -117,7 +117,7 @@ export default function MapIsland() {
## Full code:
```tsx MapIsland.tsx
```tsx islands/MapIsland.tsx
import * as Leaflet from "https://esm.sh/v135/@types/leaflet@1.9.4/index.d.ts";
import { IS_BROWSER } from "$fresh/runtime.ts";
import { useContext, useEffect } from "preact/hooks";
+18 -17
View File
@@ -19,12 +19,13 @@ In this example we'll be creating a small API that uses
Our project structure will look like this (in addition to the rest of the Fresh
code from a new project):
```txt Project Structure
routes
└── api
└── users
├── [id].ts
└── index.ts
```txt-files Project Structure
<project root>
└── routes
└── api
└── users
├── [id].ts
└── index.ts
```
In each section about a method, only the relevant handler will be shown. The
@@ -50,7 +51,7 @@ Test this with Postman (or your favorite client) with a URL like
`http://localhost:8000/api/users` and a method of `POST`. Make sure to have a
payload like:
```json
```json Request body
{
"id": "2",
"name": "TestUserName"
@@ -59,7 +60,7 @@ payload like:
You should receive the same thing back:
```json
```json Response body
{ "id": "2", "name": "TestUserName" }
```
@@ -82,7 +83,7 @@ export const handler: Handlers<User | null> = {
Let's practice retrieving our user! A `GET` request to
`http://localhost:8000/api/users/2` should return:
```json
```json Response body
{ "id": "2", "name": "TestUserName" }
```
@@ -116,7 +117,7 @@ export const handler: Handlers<User | null> = {
Time to change their name. We'll now `PUT` a request to
`http://localhost:8000/api/users/2` like:
```json
```json Request body
{
"id": "2",
"name": "New Name"
@@ -125,14 +126,14 @@ Time to change their name. We'll now `PUT` a request to
We should receive:
```json
```json Response body
{ "id": "2", "name": "New Name" }
```
If, on the other hand, we chose to implement this as a `PATCH` operation, the
request would just involve the changed property like this:
```json
```json Response body
{
"name": "New Name"
}
@@ -161,7 +162,7 @@ export const handler: Handlers<User | null> = {
Try sending `DELETE` to `http://localhost:8000/api/users/2` without a body.
We'll get back:
```txt
```txt Response body
user 2 deleted
```
@@ -174,9 +175,9 @@ request checks for complex CORS use cases. See more on the
## Full File Reference
<details>
<summary>[id].ts</summary>
<summary><code>[id].ts</code></summary>
```ts
```ts routes/api/users/[id].ts
import { Handlers } from "$fresh/server.ts";
type User = {
@@ -218,9 +219,9 @@ export const handler: Handlers<User | null> = {
</details>
<details>
<summary>index.ts</summary>
<summary><code>index.ts</code></summary>
```ts
```ts routes/api/users/index.ts
import { Handlers } from "$fresh/server.ts";
type User = {
+10 -10
View File
@@ -35,7 +35,7 @@ Now if we hit the server with a request like
`http://localhost:8000/x/bestModule@1.33.7/asdf`, then logging the params will
show the following:
```
```txt Console output
{
module: "bestModule",
version: "1.33.7",
@@ -66,18 +66,18 @@ Values are available via `params.resource` and `params.id`.
Here are some example URLs that match this:
- /api/db/bar/1
- /api/db/jobs/1
- /api/db/job/1
- /api/db/job
- /api/db/jobs
- /api/db/bar
- `/api/db/bar/1`
- `/api/db/jobs/1`
- `/api/db/job/1`
- `/api/db/job`
- `/api/db/jobs`
- `/api/db/bar`
Here are some that don't:
- /api/db/other/123
- /api/db/jobs/abc
- /api/db
- `/api/db/other/123`
- `/api/db/jobs/abc`
- `/api/db`
## Regex
+5 -5
View File
@@ -86,7 +86,7 @@ have access to the complicated initialization step by calling
When you run `deno task start` you should see the following output:
```
```txt Terminal output
Task start deno run -A --watch=static/,routes/ dev.ts
Watcher Process started.
i'm logged during initialization, and not during handling!
@@ -98,7 +98,7 @@ The manifest has been generated for 6 routes and 1 islands.
Going to `http://localhost:8000/` should produce:
```
```txt Terminal output
i'm logged during a request!
Context { complicatedStartupValue: 42 }
```
@@ -107,7 +107,7 @@ Context { complicatedStartupValue: 42 }
When you run `deno task build` you should see:
```
```txt Terminal output
Task build deno run -A dev.ts build
i'm logged during initialization, and not during handling!
The manifest has been generated for 6 routes and 1 islands.
@@ -121,7 +121,7 @@ initialization occurred.
Finally when you run `deno task preview` you should see:
```
```txt Terminal output
Task preview deno run -A main.ts
i'm logged during initialization, and not during handling!
Using snapshot found at /Users/reed/code/temp/1763/_fresh
@@ -132,7 +132,7 @@ Using snapshot found at /Users/reed/code/temp/1763/_fresh
Going to `http://localhost:8000/` should produce:
```
```txt Terminal output
i'm logged during a request!
Context { complicatedStartupValue: 42 }
```
@@ -121,9 +121,9 @@ That's it! Now you can use Tailwind CSS in your project.
> apply. In other words, to use dynamic classes, you need to ensure that they
> are present in the final CSS file.
>
> | Twind | Tailwind CSS |
> | --------------------------------- | ------------------------------------------------------------ |
> | `<a class={`link-${color}`}></a>` | `<a class={color === 'blue' ?`link-blue`:`link-green`}></a>` |
> | Twind | Tailwind CSS |
> | ----------------------------------- | -------------------------------------------------------------- |
> | ``<a class={`link-${color}`}></a>`` | ``<a class={color === 'blue' ?`link-blue`:`link-green`}></a>`` |
## Frequently Asked Questions (FAQ)
+8 -8
View File
@@ -43,30 +43,30 @@ You might end up with duplicate tags, when multiple `<Head />` components are
rendered on the same page. This can happen when you render `<Head />` in a route
and another `<Head />` in another component for example.
```tsx
// routes/page-a.tsx
```tsx routes/page-a.tsx
<Head>
<meta name="og:title" content="This is a title" />
</Head>
</Head>;
```
// components/MyTitle.tsx
```tsx components/MyTitle.tsx
<Head>
<meta name="og:title" content="Other title" />
</Head>
</Head>;
```
To ensure that the tag is not duplicated, Fresh supports setting the `key` prop.
By giving matching elements the same `key` prop, only the last one will be
rendered.
```diff
// routes/page-a.tsx
```diff routes/page-a.tsx
<Head>
- <meta name="og:title" content="This is a title" />
+ <meta name="og:title" content="This is a title" key="title" />
</Head>
```
// components/MyTitle.tsx
```diff components/MyTitle.tsx
<Head>
- <meta name="og:title" content="Other title" />
+ <meta name="og:title" content="Other title" key="title" />
+1 -1
View File
@@ -92,7 +92,7 @@ description: testFromText
You'll also need to import the `Github Flavored Markdown` module:
```sh
```sh Terminal
deno add jsr:@deno/gfm
```
+3 -3
View File
@@ -22,7 +22,7 @@ you trust.
Suppose we need to add some microdata markup to a page. The following will
result in **escaped characters, and will not work**:
```tsx
```tsx components/json-ld.tsx
const json = `
{
"@context": "http://schema.org",
@@ -40,7 +40,7 @@ export default function JsonLd() {
Instead, we can use `dangerouslySetInnerHTML`:
```tsx
```tsx components/json-ld.tsx
export default function JsonLd() {
return (
<script
@@ -57,7 +57,7 @@ Syntax highlighters parse strings into HTML tags, allowing them to be
individually styled with CSS. We can build a simple Preact syntax highlighter
like so:
```tsx
```tsx components/code.tsx
import Prism from "https://esm.sh/prismjs@1.29.0";
interface Props {
@@ -36,7 +36,7 @@ export default function Counter(props: CounterProps) {
Note how `useSignal` is within the `Counter` component. Then if we instantiate
some counters like this...
```tsx
```tsx routes/index.tsx
<Counter start={3} />
<Counter start={4} />
```
@@ -179,7 +179,7 @@ function CartItem(props: CartItemProps) {
Now we can add the islands to our site by doing the following:
```tsx
```tsx routes/cart.tsx
<AddToCart product="Lemon" />
<AddToCart product="Lime" />
<Cart />
+6 -6
View File
@@ -25,7 +25,7 @@ Fresh's CSP implementation supports the following
<details>
<summary>directives</summary>
```ts
```ts fresh 🍋
export interface ContentSecurityPolicyDirectives {
// Fetch directives
/**
@@ -175,7 +175,7 @@ export default function Home(req: Request, ctx: RouteContext) {
We can hit `http://localhost:8000/noCSP` and we should see the following:
```txt
```txt Response body
This page doesn't use CSP at all. Styles will be applied.
```
@@ -212,7 +212,7 @@ export const config: RouteConfig = {
We can hit `http://localhost:8000/incorrectCSP` and we should see the following:
```txt
```txt Response body
This page violates our configured CSP. Styles won't be applied.
```
@@ -247,7 +247,7 @@ export const config: RouteConfig = {
We can hit `http://localhost:8000/correctCSP` and we should see the following:
```txt
```txt Response body
This page adheres to our configured CSP. Styles will be applied.
```
@@ -282,7 +282,7 @@ export default function Home(req: Request, ctx: RouteContext) {
We can hit `http://localhost:8000/cspNoRouteConfig` and we should see the
following:
```txt
```txt Response body
This page violates our configured CSP. But we don't have a RouteConfig enabled, so Fresh doesn't know to use the CSP. Styles will be applied.
```
@@ -342,7 +342,7 @@ export const handler = {
We can hit `http://localhost:8000/incorrectCSPwithReport` and we should see the
following:
```txt
```txt Response body
This page violates our configured CSP. But we're using "reportOnly". Styles will be applied.
```
+1 -6
View File
@@ -36,12 +36,7 @@ export default defineConfig({
Let's bump that up to v1:
```diff
diff --git a/fresh.config.ts b/fresh.config.ts
index 548e16a..e00d557 100644
--- a/fresh.config.ts
+++ b/fresh.config.ts
@@ -1,5 +1,5 @@
```diff fresh.config.ts
import { defineConfig } from "$fresh/server.ts";
-import twindPlugin from "$fresh/plugins/twind.ts";
+import twindPlugin from "$fresh/plugins/twindv1.ts";
+2 -2
View File
@@ -93,7 +93,7 @@ ok | 1 passed (3 steps) | 0 failed (236ms)
This function is typed as follows:
```ts
```ts fresh 🍋
export async function createHandler(
manifest: Manifest,
config: FreshConfig = {},
@@ -107,7 +107,7 @@ free to provide your own bag of options.
[`FreshConfig`](https://deno.land/x/fresh/server.ts?s=FreshConfig) is declared
as follows:
```ts
```ts fresh 🍋
export interface FreshConfig {
build?: {
outDir?: string;