Files
sweet/docs/latest/examples/rendering-markdown.md
T
Christian Svensson c9b43ea32b feat(docs): add file/language type icons to code blocks (#3162)
I originally wanted to make it clearer the distinction between "files"
and e.g. `` ```tsx foo.tsx `` compared to `` ```sh Terminal `` or ``
```txt Project structure``. I originally started just trying to add a 📝
icon or something. But instead I ended showing file types of all
markdown languages.

I took the file type icons for the SVG's from
https://github.com/vscode-icons/vscode-icons, which has a MIT license.

I think the icons make it a even more clear what the language of the
code snippet is.

I have a follow-up PR for improving code block titles and making
examples more consistent in #3163.


## Comparisons

<details>

<summary><strong>Open &mdash; Before | After</strong></summary>


### TS + Shell icons - Hover tooltips and alt text

<img width="1504" height="639" alt="image"
src="https://github.com/user-attachments/assets/636838a3-f38c-4048-a536-52c1cdd6b6d4"
/>


### TS + Shell icons 2

<img width="1511" height="761" alt="image"
src="https://github.com/user-attachments/assets/2ad6d25c-3995-433a-ad93-162830ca55a1"
/>

### Shell + Dockerfile icons

<img width="1500" height="780" alt="image"
src="https://github.com/user-attachments/assets/88010749-d0d2-4498-9eb2-9bd971e48365"
/>

### Nested blocks + Git icon

<img width="1513" height="634" alt="image"
src="https://github.com/user-attachments/assets/400c27f8-d0b4-40bd-801d-611e1e8a8e36"
/>

</details>
2025-07-31 23:48:00 +02:00

101 lines
2.3 KiB
Markdown

---
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:
1. the markdown is coming from a remote source
2. the markdown is defined in a string
3. the markdown is on a file
The following file uses
[dynamic routing](https://fresh.deno.dev/docs/getting-started/dynamic-routes) to
handle the three cases. It's assumed this file is called `[slug].tsx`:
```ts routes/[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:
```md text.md
---
description: testFromText
---
# Really Big Text
**bold**
```
You'll also need to import the `Github Flavored Markdown` module:
```sh
deno add jsr:@deno/gfm
```
Andy has a helpful [post](https://deno.com/blog/build-a-blog-with-fresh) on the
Deno Blog which goes into a slightly more realistic example.