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" />
2.3 KiB
2.3 KiB
description
| description |
|---|
| How to render markdown on your Fresh site. |
What if you want to render some markdown on your site? There are a few possibilities:
- the markdown is coming from a remote source
- the markdown is defined in a string
- the markdown is on a file
The following file uses
dynamic routing to
handle the three cases. It's assumed this file is called [slug].tsx:
import { Handlers, PageProps } from "$fresh/server.ts";
import { extract } from "$std/front_matter/yaml.ts";
import { CSS, render } from "$gfm";
import { Head } from "$fresh/runtime.ts";
interface Page {
markdown: string;
data: Record<string, unknown>;
}
export const handler: Handlers<Page> = {
async GET(_req, ctx) {
let rawMarkdown = "";
if (ctx.params.slug === "remote") {
const resp = await fetch(
`https://raw.githubusercontent.com/denoland/fresh/main/docs/latest/introduction/index.md`,
);
if (resp.status !== 200) {
return ctx.render(undefined);
}
rawMarkdown = await resp.text();
} else if (ctx.params.slug === "string") {
rawMarkdown = `---
description: test
---
## big text
Look, it's working. _This is in italics._
`;
} else if (ctx.params.slug === "file") {
rawMarkdown = await Deno.readTextFile("text.md");
} else {
return ctx.render(undefined);
}
const { attrs, body } = extract(rawMarkdown);
return ctx.render({ markdown: body, data: attrs });
},
};
export default function MarkdownPage({ data }: PageProps<Page | null>) {
if (!data) {
return <h1>File not found.</h1>;
}
return (
<>
<Head>
<style dangerouslySetInnerHTML={{ __html: CSS }} />
</Head>
<main>
<div>{JSON.stringify(data.data)}</div>
<div
class="markdown-body"
dangerouslySetInnerHTML={{ __html: render(data?.markdown) }}
/>
</main>
</>
);
}
The contents of the text.md file are the following:
---
description: testFromText
---
# Really Big Text
**bold**
You'll also need to import the Github Flavored Markdown module:
deno add jsr:@deno/gfm
Andy has a helpful post on the Deno Blog which goes into a slightly more realistic example.