Skip to content

Commit 40af6e1

Browse files
authored
docs: added example in docs of using a resolver for metatags for markdown component (#1014) (#1082)
1 parent 329b7e8 commit 40af6e1

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

apps/docs-app/docs/features/routing/content.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,62 @@ export default class BlogPostComponent {
161161
}
162162
```
163163

164+
### Using A Resolver For Metatags
165+
166+
In your route configuration, you can use the `RouteMeta` object to resolve meta tags for a route. This is done by assigning the `postMetaResolver` function to the `meta` property.
167+
168+
Below is an example of using a `postMetaResolver` function that fetches the meta tags for a post. This function returns an array of meta tags.
169+
170+
```ts
171+
export const postMetaResolver: ResolveFn<MetaTag[]> = (route) => {
172+
const postAttributes = injectActivePostAttributes(route);
173+
174+
return [
175+
{
176+
name: 'description',
177+
content: postAttributes.description,
178+
},
179+
{
180+
name: 'author',
181+
content: 'Analog Team',
182+
},
183+
{
184+
property: 'og:title',
185+
content: postAttributes.title,
186+
},
187+
{
188+
property: 'og:description',
189+
content: postAttributes.description,
190+
},
191+
{
192+
property: 'og:image',
193+
content: postAttributes.coverImage,
194+
},
195+
];
196+
};
197+
```
198+
199+
The meta tags can be done asynchronously also. Assign the `postMetaResolver` function to the `meta` property.
200+
201+
```ts
202+
export const routeMeta: RouteMeta = {
203+
title: postTitleResolver,
204+
meta: postMetaResolver,
205+
};
206+
```
207+
208+
The resolved meta tags can also be accessed in the component using the `ActivatedRoute` service.
209+
210+
```ts
211+
export default class BlogPostComponent {
212+
readonly route = inject(ActivatedRoute);
213+
readonly metaTags$ = this.route.data.pipe(map(data => data['meta']));
214+
215+
// In the template
216+
<my-component [metaTags]="metaTags$ | async"></my-component>
217+
}
218+
```
219+
164220
### Enabling support for Mermaid
165221

166222
Analog's markdown component supports [Mermaid](https://mermaid.js.org/). To enable support by the `MarkdownComponent` define a dynamic import for `loadMermaid` in `withMarkdownRenderer()`.

0 commit comments

Comments
 (0)