|
| 1 | +# Layout y markdown |
| 2 | + |
| 3 | +Ya tenemos el layout definido en... casí todas las ventanas, nos queda cunado pinchamos en un post, aquí podríamos: |
| 4 | + |
| 5 | +- Usar el componente `Markdown` de Astro y encastrarlo todo en un layout. |
| 6 | +- Definie un layou para el markdown. |
| 7 | + |
| 8 | +# Manos a la obra |
| 9 | + |
| 10 | +Vamos a crear un layout para el markdown, para ello vamos a crear un nuevo archivo en `src/layouts` llamado `markdown-post.astro`: |
| 11 | + |
| 12 | +Miramos que propiedades trae el frontmatter de cada post, ponemos cabecera y soltamos un `slot` para el markdown. |
| 13 | + |
| 14 | +_./src/layouts/markdown-post.astro_ |
| 15 | + |
| 16 | +```astro |
| 17 | +--- |
| 18 | +interface Frontmatter { |
| 19 | + layout: string; |
| 20 | + title: string; |
| 21 | + pubDate: string; |
| 22 | + description: string; |
| 23 | + author: string; |
| 24 | + image: { |
| 25 | + url: string; |
| 26 | + alt: string; |
| 27 | + }; |
| 28 | + tags: string[]; |
| 29 | +} |
| 30 | +
|
| 31 | +const { frontmatter } = Astro.props as { frontmatter: Frontmatter }; |
| 32 | +--- |
| 33 | +
|
| 34 | +<h1>{frontmatter.title}</h1> |
| 35 | +<p> Written by {frontmatter.author}</p> |
| 36 | +<slot /> |
| 37 | +``` |
| 38 | + |
| 39 | +Ya tenemos el layout, ahora vamos a decirle a cada post que lo use: |
| 40 | + |
| 41 | +_./src/pages/posts/post-1.md_ |
| 42 | + |
| 43 | +```diff |
| 44 | +--- |
| 45 | ++ layout: ../../layouts/markdown-post.astro |
| 46 | +title: "Mi primer post" |
| 47 | +pubDate: 2024-11-20 |
| 48 | +description: "Mi primer post" |
| 49 | +author: "Lemoncoder" |
| 50 | +image: |
| 51 | + url: "https://docs.astro.build/assets/rose.webp" |
| 52 | + alt: "The Astro logo on a dark background with a pink glow." |
| 53 | +tags: ["astro", "blogging", "hola mundo"] |
| 54 | +--- |
| 55 | + |
| 56 | +# Mi primer post |
| 57 | +``` |
| 58 | + |
| 59 | +_./src/pages/posts/post-2.md_ |
| 60 | + |
| 61 | +```diff |
| 62 | +--- |
| 63 | ++ layout: ../../layouts/markdown-post.astro |
| 64 | +title: Mi Segundo Post |
| 65 | +author: Lemoncoder |
| 66 | +description: "Segundo post para el ejemplo" |
| 67 | +image: |
| 68 | + url: "https://docs.astro.build/assets/arc.webp" |
| 69 | + alt: "The Astro logo on a dark background with a purple gradient arc." |
| 70 | +pubDate: 2024-11-23 |
| 71 | +tags: ["astro", "blogging", "learning in public", "successes"] |
| 72 | +--- |
| 73 | + |
| 74 | +- # Mi primer post |
| 75 | + |
| 76 | +- Publicado el : 2024-11-20 |
| 77 | +``` |
| 78 | + |
| 79 | +_./src/pages/posts/post-3.md_ |
| 80 | + |
| 81 | +```diff |
| 82 | +--- |
| 83 | ++ layout: ../../layouts/markdown-post.astro |
| 84 | +title: Mi tercer Post |
| 85 | +author: Lemoncoder |
| 86 | +description: "Tercer post para el ejemplo" |
| 87 | +image: |
| 88 | +url: "https://docs.astro.build/assets/arc.webp" |
| 89 | +alt: "The Astro logo on a dark background with a purple gradient arc." |
| 90 | +pubDate: 2024-11-25 |
| 91 | +tags: ["astro", "blogging", "learning in public", "successes"] |
| 92 | +--- |
| 93 | +``` |
| 94 | + |
| 95 | +Y ahora podemos ver como cada post utiliza el layout. |
| 96 | + |
| 97 | +Bueno, no esta mal, pero echamos de menos la estructura principal (la que definimos en el otro layout), ¿Qué podemos hacer? Componer layouts :). |
| 98 | + |
| 99 | +Me voy a _markdown-post.astro_ utilizo el layout base. |
| 100 | + |
| 101 | +_./src/layouts/markdown-post.astro_ |
| 102 | + |
| 103 | +```diff |
| 104 | +--- |
| 105 | ++ import BaseLayout from "./base.astro"; |
| 106 | + |
| 107 | +interface Frontmatter { |
| 108 | + layout: string; |
| 109 | + title: string; |
| 110 | + pubDate: string; |
| 111 | + description: string; |
| 112 | + author: string; |
| 113 | + image: { |
| 114 | + url: string; |
| 115 | + alt: string; |
| 116 | + }; |
| 117 | + tags: string[]; |
| 118 | +} |
| 119 | + |
| 120 | +const { frontmatter } = Astro.props as { frontmatter: Frontmatter }; |
| 121 | +--- |
| 122 | + |
| 123 | ++ <BaseLayout pageTitle={frontmatter.title}> |
| 124 | +- <h1>{frontmatter.title}</h1> |
| 125 | + <p>Written by {frontmatter.author}</p> |
| 126 | + <slot /> |
| 127 | ++ </BaseLayout> |
| 128 | +``` |
0 commit comments