docs: add "Rendering markdown" example (#1307)
This commit is contained in:
@@ -17,3 +17,4 @@ like to see here, please open
|
||||
- [Dealing with CORS](./examples/dealing-with-cors)
|
||||
- [Creating a CRUD API](./examples/creating-a-crud-api)
|
||||
- [Handling complex routes](./examples/handling-complex-routes)
|
||||
- [Rendering markdown](./examples/rendering-markdown)
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
---
|
||||
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
|
||||
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/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
|
||||
---
|
||||
description: testFromText
|
||||
---
|
||||
|
||||
# Really Big Text
|
||||
|
||||
**bold**
|
||||
```
|
||||
|
||||
You'll also need to import the `Github Flavored Markdown` module:
|
||||
|
||||
```json
|
||||
"$gfm": "https://deno.land/x/gfm@0.2.3/mod.ts",
|
||||
```
|
||||
|
||||
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.
|
||||
+2
-1
@@ -50,7 +50,8 @@
|
||||
["using-fresh-canary-version", "Using Fresh canary version"],
|
||||
["dealing-with-cors", "Dealing with CORS"],
|
||||
["creating-a-crud-api", "Creating a CRUD API"],
|
||||
["handling-complex-routes", "Handling complex routes"]
|
||||
["handling-complex-routes", "Handling complex routes"],
|
||||
["rendering-markdown", "Rendering markdown"]
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user