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:
@@ -19,7 +19,7 @@ styling current links where applicable.
|
||||
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;
|
||||
@@ -37,7 +37,7 @@ In Tailwindcss or similar CSS frameworks, you can apply styles to elements with
|
||||
the `aria-current` attribute using bracket notation in your class definitions.
|
||||
For Tailwindcss, use the syntax:
|
||||
|
||||
```tsx
|
||||
```tsx components/Menu.tsx
|
||||
function Menu() {
|
||||
return (
|
||||
<a href="/foo" class="aria-[current]:text-green-600">
|
||||
|
||||
@@ -14,12 +14,12 @@ To get started with daisyUI, make sure you have Tailwind CSS enabled in your
|
||||
Fresh project, then install daisyUI and update your configuration.
|
||||
|
||||
1. Run `deno i -D npm:daisyui@latest` to install daisyUI
|
||||
2. add daisyui configuration in `./static/styles.css`:
|
||||
2. Add daisyUI configuration in `./static/styles.css`:
|
||||
|
||||
```diff
|
||||
@import "tailwindcss";
|
||||
+ @plugin "daisyui";
|
||||
```
|
||||
```diff static/styles.css
|
||||
@import "tailwindcss";
|
||||
+ @plugin "daisyui";
|
||||
```
|
||||
|
||||
Now you're ready to use daisyUI.
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ can transform markdown to html.
|
||||
1. Run `deno install jsr:@deno/gfm`
|
||||
2. Create a markdown file like `content.md`:
|
||||
|
||||
```md
|
||||
```md path/to/content.md
|
||||
## some heading
|
||||
|
||||
and some interesting text here
|
||||
@@ -24,7 +24,7 @@ and some interesting text here
|
||||
|
||||
4. Add a route that renders that file
|
||||
|
||||
```tsx
|
||||
```tsx main.ts
|
||||
import { CSS, render as renderMarkdown } from "@deno/gfm";
|
||||
|
||||
const CONTENT = `## some heading
|
||||
|
||||
@@ -16,7 +16,7 @@ Use this guide to migrate a Fresh 1.x app to Fresh 2.
|
||||
Most changes can be applied automatically with the update script. Start the
|
||||
update by running it in your project directory:
|
||||
|
||||
```sh
|
||||
```sh Terminal
|
||||
deno run -Ar jsr:@fresh/update
|
||||
```
|
||||
|
||||
@@ -29,12 +29,13 @@ Configuring Fresh doesn't require a dedicated config file anymore. You can
|
||||
delete the `fresh.config.ts` file. The `fresh.gen.ts` manifest file isn't needed
|
||||
anymore either.
|
||||
|
||||
```diff
|
||||
routes/
|
||||
dev.ts
|
||||
- fresh.gen.ts
|
||||
- fresh.config.ts
|
||||
main.ts
|
||||
```diff Project structure
|
||||
<project root>
|
||||
├── routes/
|
||||
├── dev.ts
|
||||
- ├── fresh.gen.ts
|
||||
- ├── fresh.config.ts
|
||||
└── main.ts
|
||||
```
|
||||
|
||||
Fresh 2 takes great care in ensuring that code that's only needed during
|
||||
@@ -50,7 +51,7 @@ plugins like [tailwindcss](https://tailwindcss.com/).
|
||||
|
||||
The full `dev.ts` file for newly generated Fresh 2 projects looks like this:
|
||||
|
||||
```ts
|
||||
```ts dev.ts
|
||||
import { Builder } from "fresh/dev";
|
||||
import { tailwind } from "@fresh/plugin-tailwind";
|
||||
|
||||
@@ -93,12 +94,12 @@ export const app = new App()
|
||||
Both the `_500.tsx` and `_404.tsx` template have been unified into a single
|
||||
`_error.tsx` template.
|
||||
|
||||
```diff
|
||||
routes/
|
||||
- ├── _404.tsx
|
||||
- ├── _500.tsx
|
||||
+ ├── _error.tsx
|
||||
└── ...
|
||||
```diff Project structure
|
||||
└── <root>/routes/
|
||||
- ├── _404.tsx
|
||||
- ├── _500.tsx
|
||||
+ ├── _error.tsx
|
||||
└── ...
|
||||
```
|
||||
|
||||
Inside the `_error.tsx` template you can show different content based on errors
|
||||
@@ -129,8 +130,7 @@ machinery in the background to work.
|
||||
|
||||
Instead, passing head-related data is best done via `ctx.state`
|
||||
|
||||
```tsx
|
||||
// about.tsx
|
||||
```tsx routes/about.tsx
|
||||
export const handler = {
|
||||
GET(ctx) {
|
||||
// Set a route specific data in a handler
|
||||
@@ -138,8 +138,9 @@ export const handler = {
|
||||
return page();
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
// Render that in _app.tsx
|
||||
```tsx routes/_app.tsx
|
||||
export default function AppWrapper(ctx: Context) {
|
||||
return (
|
||||
<html lang="en">
|
||||
@@ -169,7 +170,7 @@ The handling trailing slashes has been extracted to an optional middleware that
|
||||
you can add if needed. This middleware can be used to ensure that URLs always
|
||||
have a trailing slash at the end or that they will never have one.
|
||||
|
||||
```diff
|
||||
```diff main.ts
|
||||
- import { App, staticFiles } from "fresh";
|
||||
+ import { App, staticFiles, trailingSlashes } from "fresh";
|
||||
|
||||
@@ -190,14 +191,14 @@ Middleware, handler and route component signatures have been unified to all look
|
||||
the same. Instead of receiving two arguments, they receive one. The `Request`
|
||||
object is stored on the context object as `ctx.req`.
|
||||
|
||||
```diff
|
||||
```diff middleware.ts
|
||||
- const middleware = (req, ctx) => new Response("ok");
|
||||
+ const middleware = (ctx) => new Response("ok");
|
||||
```
|
||||
|
||||
Same is true for handlers:
|
||||
|
||||
```diff
|
||||
```diff route/page.tsx
|
||||
export const handler = {
|
||||
- GET(req, ctx) {
|
||||
+ GET(ctx) {
|
||||
@@ -208,7 +209,7 @@ Same is true for handlers:
|
||||
|
||||
...and async route components:
|
||||
|
||||
```diff
|
||||
```diff routes/my-page.tsx
|
||||
- export default async function MyPage(req: Request, ctx: RouteContext) {
|
||||
+ export default async function MyPage(props: PageProps) {
|
||||
const value = await loadFooValue();
|
||||
@@ -240,7 +241,7 @@ re-use existing objects internally as a minor performance optimization.
|
||||
The `createHandler` function was often used to launch Fresh for tests. This can
|
||||
be now done via the [`Builder`](/docs/canary/concepts/builder).
|
||||
|
||||
```ts
|
||||
```ts main.test.ts
|
||||
// Best to do this once instead of for every test case for
|
||||
// performance reasons.
|
||||
const builder = new Builder();
|
||||
|
||||
@@ -10,13 +10,13 @@ situations.
|
||||
To address this you can render raw HTML via Preact's `dangerouslySetInnerHTML`
|
||||
prop:
|
||||
|
||||
```tsx
|
||||
```tsx routes/dynamic-html.tsx
|
||||
<div dangerouslySetInnerHTML={{ __html: "<h1>This is raw HTML</h1>" }} />;
|
||||
```
|
||||
|
||||
This will output:
|
||||
|
||||
```html
|
||||
```html Response body
|
||||
<div>
|
||||
<h1>This is raw HTML</h1>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user