Replies: 1 comment 4 replies
-
import type { DocAttributes } from './docs.model'
import { injectContent, injectContentFiles, MarkdownComponent } from '@analogjs/content'
import { Location } from '@angular/common'
import { Component, inject } from '@angular/core'
import { toSignal } from '@angular/core/rxjs-interop'
import { map, of } from 'rxjs'
export function injectDocs<Attributes extends Record<string, any>>(folder: string, slug: string) {
const contentFiles = injectContentFiles<Attributes>(contentFile => contentFile.filename.includes(`/src/content/${folder}`))
const contentFile = contentFiles.find((contentFile) => {
return contentFile.filename.includes(`${folder}/${slug}`)
})
if (!contentFile) {
return of(null)
}
const parts = contentFile.filename.split('/').slice(3)
const customFilename = parts.join('/').split('.md').join('')
return injectContent<Attributes>({
customFilename,
}).pipe(
map((doc) => {
return {
...doc,
slug: doc.slug.replace('/index', ''),
}
}),
)
}
@Component({
selector: 'app-docs-slug',
imports: [MarkdownComponent],
template: `
@if (doc()) {
<h2>{{ doc()?.attributes?.title }}</h2>
<analog-markdown [content]="doc()?.content"></analog-markdown>
} @else {
<p>Documento no encontrado.</p>
}
`,
})
export default class DocsSlugPage {
private readonly location = inject(Location)
private readonly currentPath = this.location.path().replace(/^\//, '')
private readonly pathParts = this.currentPath.split('/')
private readonly slug = this.pathParts.slice(1).join('/')
protected readonly doc = toSignal(
injectDocs<DocAttributes>('docs', this.slug),
{ initialValue: null },
)
} |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
adrian-ub
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
content files
src/pages/docs/[...slug].page.ts
route try access
/docs (docs/index.md)
/docs/installation/vite (docs/installation/vite.md)
Beta Was this translation helpful? Give feedback.
All reactions