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:
@@ -22,7 +22,7 @@ pre-process or generate CSS files, for example.
|
||||
|
||||
To have Fresh optimize all the assets, run one of the following commands:
|
||||
|
||||
```sh
|
||||
```sh Terminal
|
||||
# As a task in newer Fresh projects
|
||||
deno task build
|
||||
# or invoke it manually
|
||||
|
||||
@@ -8,8 +8,8 @@ deployed to any system or platform that can run a Deno based web server.
|
||||
|
||||
Here are instructions for specific providers / systems:
|
||||
|
||||
- [Deno Deploy](#deno-deploy)
|
||||
- [Docker](#docker)
|
||||
- [Deno Deploy](#-deno-deploy)
|
||||
- [Docker](#-docker)
|
||||
|
||||
## Deno Deploy
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ export default function BlogpostPage({ data }) {
|
||||
This can also be achieved by throwing an error, if you're uninterested in
|
||||
passing specific data to your 404 page:
|
||||
|
||||
```tsx
|
||||
```tsx routes/missing-page.tsx
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
|
||||
@@ -131,7 +131,7 @@ enough information to put the islands where they are supposed to go without
|
||||
requiring hydration for the static children of interactive islands. No
|
||||
Javascript is sent to the client when no interactivity is needed.
|
||||
|
||||
```html
|
||||
```html Response body
|
||||
<!--frsh-myisland_default:default:0-->
|
||||
<div>
|
||||
Counter is at 0.
|
||||
|
||||
@@ -7,7 +7,8 @@ A layout is defined in a `_layout.tsx` file in any sub directory (at any level)
|
||||
under the `routes/` folder. It must contain a default export that is a regular
|
||||
Preact component. Only one such layout is allowed per sub directory.
|
||||
|
||||
```txt Project structure
|
||||
```txt-files Project structure
|
||||
<project root>
|
||||
└── routes
|
||||
├── sub
|
||||
│ ├── page.tsx
|
||||
@@ -64,7 +65,7 @@ To make it a little quicker to write async layouts, Fresh ships with a
|
||||
`defineLayout` helper which automatically infers the correct types for the
|
||||
function arguments.
|
||||
|
||||
```tsx
|
||||
```tsx routes/greet/_layout.tsx
|
||||
import { defineLayout } from "$fresh/server.ts";
|
||||
|
||||
export default defineLayout(async (req, ctx) => {
|
||||
@@ -85,8 +86,8 @@ Sometimes you want to opt out of the layout inheritance mechanism for a
|
||||
particular route. This can be done via route configuration. Picture a directory
|
||||
structure like this:
|
||||
|
||||
```txt Project structure
|
||||
└── routes
|
||||
```txt-files Project structure
|
||||
└── <root>/routes
|
||||
├── sub
|
||||
│ ├── _layout_.tsx
|
||||
│ ├── special.tsx # should not inherit layouts
|
||||
|
||||
@@ -50,8 +50,8 @@ specific first).
|
||||
|
||||
For example, take a project with the following routes:
|
||||
|
||||
```txt Project Structure
|
||||
└── routes
|
||||
```txt-files Project Structure
|
||||
└── <root>/routes
|
||||
├── _middleware.ts
|
||||
├── index.ts
|
||||
└── admin
|
||||
@@ -118,7 +118,7 @@ the value of `acme` in your middleware.
|
||||
To set the stage for this section, let's focus on the part of `FreshContext`
|
||||
that looks like this:
|
||||
|
||||
```ts
|
||||
```ts fresh 🍋
|
||||
export interface FreshContext<State = Record<string, unknown>> {
|
||||
...
|
||||
next: () => Promise<Response>;
|
||||
@@ -135,7 +135,7 @@ export interface FreshContext<State = Record<string, unknown>> {
|
||||
|
||||
and `router.DestinationKind` is defined like this:
|
||||
|
||||
```ts
|
||||
```ts fresh 🍋
|
||||
export type DestinationKind = "internal" | "static" | "route" | "notFound";
|
||||
```
|
||||
|
||||
@@ -203,7 +203,7 @@ is only supposed to deal with routes.
|
||||
|
||||
If you want to redirect a request from a middleware, you can do so by returning:
|
||||
|
||||
```ts
|
||||
```ts routes/_middleware.ts
|
||||
export function handler(req: Request): Response {
|
||||
return Response.redirect("https://example.com", 307);
|
||||
}
|
||||
@@ -212,7 +212,7 @@ export function handler(req: Request): Response {
|
||||
`307` stands for temporary redirect. You can also use `301` for permanent
|
||||
redirect. You can also redirect to a relative path by doing:
|
||||
|
||||
```ts
|
||||
```ts routes/_middleware.ts
|
||||
export function handler(req: Request): Response {
|
||||
return new Response("", {
|
||||
status: 307,
|
||||
|
||||
@@ -62,7 +62,7 @@ picks out the relevant parts of the response. We can optimize this pattern
|
||||
further by only rendering the parts we need, instead of always rendering the
|
||||
full page. This is done by adding the `f-partial` attribute to a link.
|
||||
|
||||
```diff
|
||||
```diff routes/_app.tsx
|
||||
- <a href="/docs/routes">Routes</a>
|
||||
+ <a href="/docs/routes" f-partial="/partials/docs/routes">Routes</a>
|
||||
```
|
||||
@@ -180,7 +180,7 @@ achieved by adding the `mode` prop to a `Partial` component.
|
||||
Personally, we’ve found that the `append` mode is really useful when you have an
|
||||
UI which displays log messages or similar list-like data.
|
||||
|
||||
```tsx
|
||||
```tsx routes/log.tsx
|
||||
export default function LogView() {
|
||||
const lines = getNewLogLines();
|
||||
|
||||
@@ -203,7 +203,7 @@ If you want to exempt a particular element from triggering a partial request
|
||||
like on a particular link, form or button, you can opt out of it by setting
|
||||
`f-client-nav={false}` on the element or one of the ancestor elements.
|
||||
|
||||
```tsx
|
||||
```tsx routes/index.tsx
|
||||
<body f-client-nav>
|
||||
{/* This will cause a partial navigation */}
|
||||
<a href="/docs/page1">With partials</a>
|
||||
|
||||
@@ -40,7 +40,7 @@ A Fresh plugin is just a JavaScript object that conforms to the
|
||||
required property of a plugin is its name. Names must only contain the
|
||||
characters `a`-`z`, and `_`.
|
||||
|
||||
```ts
|
||||
```ts my-plugin.ts
|
||||
import { Plugin } from "$fresh/server.ts";
|
||||
|
||||
const plugin: Plugin = {
|
||||
|
||||
@@ -172,7 +172,7 @@ To make it a little quicker to write async routes, Fresh ships with a
|
||||
`defineRoute` helper which automatically infers the correct types for the
|
||||
function arguments.
|
||||
|
||||
```tsx
|
||||
```tsx routes/hello.tsx
|
||||
import { defineRoute } from "$fresh/server.ts";
|
||||
|
||||
export default defineRoute(async (req, ctx) => {
|
||||
|
||||
@@ -61,7 +61,7 @@ suggested by the URL segment.
|
||||
|
||||
Let's illustrate that with an example:
|
||||
|
||||
```txt
|
||||
```txt Example page layout
|
||||
/about -> layout A
|
||||
/career -> layout A
|
||||
/archive -> layout B
|
||||
@@ -71,7 +71,8 @@ Let's illustrate that with an example:
|
||||
Without any way to group routes this is a problem because every route segment
|
||||
can only have one `_layout` file.
|
||||
|
||||
```txt Project structure
|
||||
```txt-files Project structure
|
||||
<project root>
|
||||
└── routes
|
||||
├── _layout.tsx # applies to all routes here :(
|
||||
├── about.tsx
|
||||
@@ -85,8 +86,8 @@ a name that is wrapped in parentheses. For example `(info)` would be considered
|
||||
a route group and so would `(marketing)`. This enables us to group related
|
||||
routes in a folder and use a different `_layout` file for each group.
|
||||
|
||||
```txt Project structure
|
||||
└── routes
|
||||
```txt-files Project structure
|
||||
└── <root>/routes
|
||||
├── (marketing)
|
||||
│ ├── _layout.tsx # only applies to about.tsx and career.tsx
|
||||
│ ├── about.tsx
|
||||
@@ -101,8 +102,8 @@ routes in a folder and use a different `_layout` file for each group.
|
||||
> URL. Such scenarios will lead to ambiguity as to which route file should be
|
||||
> picked.
|
||||
>
|
||||
> ```txt Project structure
|
||||
> └── routes
|
||||
> ```txt-files Project structure
|
||||
> └── <root>/routes
|
||||
> ├── (group-1)
|
||||
> │ └── about.tsx # Bad: Maps to same `/about` url
|
||||
> └── (group-2)
|
||||
@@ -128,8 +129,8 @@ to house these.
|
||||
The one special name is `(_islands)` which tells Fresh to treat all files in
|
||||
that folder as an island.
|
||||
|
||||
```txt Project structure
|
||||
└── routes
|
||||
```txt-files Project structure
|
||||
└── <root>/routes
|
||||
├── (marketing)
|
||||
│ ├── _layout.tsx
|
||||
│ ├── about.tsx
|
||||
|
||||
@@ -18,7 +18,7 @@ things get interesting.
|
||||
[`FreshConfig`](https://deno.land/x/fresh/server.ts?s=FreshConfig) looks like
|
||||
this:
|
||||
|
||||
```ts
|
||||
```ts fresh 🍋
|
||||
export interface FreshConfig {
|
||||
build?: {
|
||||
/**
|
||||
@@ -44,7 +44,7 @@ export interface FreshConfig {
|
||||
|
||||
And for completeness here are the remaining two types:
|
||||
|
||||
```ts
|
||||
```ts fresh 🍋
|
||||
export type RenderFunction = (
|
||||
ctx: RenderContext,
|
||||
render: InnerRenderFunction,
|
||||
@@ -81,7 +81,7 @@ export interface RouterOptions {
|
||||
As the comment suggests, this can be used to configure where generated files are
|
||||
written:
|
||||
|
||||
```tsx
|
||||
```tsx dev.ts
|
||||
await dev(import.meta.url, "./main.ts", {
|
||||
build: {
|
||||
outDir: Deno.env.get("FRESH_TEST_OUTDIR") ?? undefined,
|
||||
@@ -93,7 +93,7 @@ await dev(import.meta.url, "./main.ts", {
|
||||
|
||||
This should be a valid ES Build target.
|
||||
|
||||
```tsx
|
||||
```tsx dev.ts
|
||||
await dev(import.meta.url, "./main.ts", {
|
||||
build: {
|
||||
target: "es2015",
|
||||
|
||||
@@ -46,6 +46,10 @@ and `<source>` HTML tags. These will automatically use "locked" paths if Fresh
|
||||
deems it safe to do so. You can always opt out of this behaviour per tag, by
|
||||
adding the `data-fresh-disable-lock` attribute.
|
||||
|
||||
```tsx
|
||||
```tsx routes/user.tsx
|
||||
{/* Locked URL source */}
|
||||
<img src="/user.png" />;
|
||||
|
||||
{/* Preserve URL source and disable lock */}
|
||||
<img src="/user.png" data-fresh-disable-lock />;
|
||||
```
|
||||
|
||||
@@ -97,7 +97,7 @@ the recommended way to use JSX in Fresh projects. Instead, starting with version
|
||||
1.1.0, Fresh projects should use the automatic JSX transform that requires no
|
||||
JSX pragma or preact import.
|
||||
|
||||
```diff
|
||||
```diff routes/hello-world.tsx
|
||||
- /** @jsx h */
|
||||
- import { h } from "preact";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user