Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungWhat is Programming And How i can Enjoy it?(24.09.2026 um 11:54 Uhr)
Sichere ProgrammierungYou Don't Need Adobe Commerce Cloud to Survive Black Friday(24.09.2026 um 11:55 Uhr)
Malware / Trojaner / VirenBeyond Lazarus: Organization of DPRK cyber capabilities(24.09.2026 um 11:59 Uhr)
Malware / Trojaner / VirenBeyond Lazarus: Organization of DPRK Cyber Capabilities(24.09.2026 um 11:59 Uhr)
Malware / Trojaner / VirenThe fake worker threat and the rise of human infiltration(24.09.2026 um 11:59 Uhr)
Malware / Trojaner / VirenPolinRider Spreads Through Compromised GitHub Accounts and Packagist(24.09.2026 um 11:59 Uhr)
Malware / Trojaner / VirenWeaselBiscuit Strips BeaverTail and OtterCookie Down to Essentials(24.09.2026 um 11:59 Uhr)
Sichere ProgrammierungWhat is Programming And How i can Enjoy it?(24.09.2026 um 11:54 Uhr)
Sichere ProgrammierungYou Don't Need Adobe Commerce Cloud to Survive Black Friday(24.09.2026 um 11:55 Uhr)
Malware / Trojaner / VirenBeyond Lazarus: Organization of DPRK cyber capabilities(24.09.2026 um 11:59 Uhr)
Malware / Trojaner / VirenBeyond Lazarus: Organization of DPRK Cyber Capabilities(24.09.2026 um 11:59 Uhr)
Malware / Trojaner / VirenThe fake worker threat and the rise of human infiltration(24.09.2026 um 11:59 Uhr)
Malware / Trojaner / VirenPolinRider Spreads Through Compromised GitHub Accounts and Packagist(24.09.2026 um 11:59 Uhr)
Malware / Trojaner / VirenWeaselBiscuit Strips BeaverTail and OtterCookie Down to Essentials(24.09.2026 um 11:59 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Build a Docusaurus-like Site with FastAPI: Step 4 - Parsing Frontmatter

In the previous article, we added syntax highlighting for our Markdown code blocks. But you might have noticed that the document page title ({{ page_title }}) is still hardcoded in the main.py route function ("page_title": "Hello,…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Cover



In the previous article, we added syntax highlighting for our Markdown code blocks.



But you might have noticed that the document page title ({{ page_title }}) is still hardcoded in the main.py route function ("page_title": "Hello, Markdown!"). This is very inflexible. Does this mean we have to modify the code every time we add a new document?



A documentation site needs to be flexible, allowing for articles to be added or removed at any time. The article's metadata—like its title, author, and date—should be defined within the Markdown file itself, just like the content.



In this article, we will introduce Frontmatter (a common specification for defining metadata at the top of a Markdown file) and enable FastAPI to parse it, allowing us to load metadata dynamically.






Step 1: Install the Frontmatter Parsing Library



We will use python-frontmatter to separate and parse the Frontmatter and the Markdown content from our files.



Install it with the following command:




pip install python-frontmatter









Step 2: Add Frontmatter to Our Markdown Document



Next, let's modify the docs/hello.md file to add Frontmatter at the very top.



A Frontmatter block is enclosed by triple dashes (---).



Update docs/hello.md:




---
title: Hello, Frontmatter!
author: FastAPI Developer
date: 2025-11-09
---

... (Rest of the Markdown content)






We've defined three metadata fields here: title, author, and date. You can add more fields as needed. The title field will ultimately be used as the article's page_title.






Step 3: Modify main.py to Parse Frontmatter



Now, we'll modify the get_hello_doc route function to use the frontmatter library to load the file, instead of a simple open().read().



The frontmatter.load() function will parse the file into two parts:




  1. post.metadata: A dictionary containing all the Frontmatter data (e.g., {'title': 'Hello, Frontmatter!', ...}).

  2. post.content: A string containing only the main body of the Markdown content.



Open main.py and modify it as follows:




# main.py
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
import markdown
from fastapi.staticfiles import StaticFiles
import frontmatter # 1. Import the frontmatter library

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")

templates = Jinja2Templates(directory="templates")


# --- Home route (unchanged) ---
@app.get("/", response_class=HTMLResponse)
async def root(request: Request):
context = {
"request": request,
"page_title": "Hello, Jinja2!" # (Kept original Chinese for consistency with image, but "Hello, Jinja2!" is the English equivalent)
}
return templates.TemplateResponse("index.html", context)


# --- 2. Modify the document route ---
@app.get("/docs/hello", response_class=HTMLResponse)
async def get_hello_doc(request: Request):
"""
Reads, parses (including Frontmatter), and renders the hello.md document
"""
md_file_path = "docs/hello.md"

try:
# 3. Use frontmatter.load to read and parse the file
post = frontmatter.load(md_file_path)
except FileNotFoundError:
return HTMLResponse(content="<h1>404 - Document Not Found</h1>", status_code=404)
except Exception as e:
# Add general error handling in case of malformed YAML
return HTMLResponse(content=f"<h1>500 - Parse Error: {e}</h1>", status_code=500)

# 4. Extract metadata and content
metadata = post.metadata
md_content = post.content # This is the pure Markdown content

# 5. Convert only the Markdown content
extensions = ['fenced_code', 'codehilite']
html_content = markdown.markdown(md_content, extensions=extensions)

# 6. Dynamically get the page_title from the metadata
# Use .get() to avoid crashing if the 'title' key is missing
page_title = metadata.get('title', 'Untitled Document')

context = {
"request": request,
"page_title": page_title, # Replaced the hardcoded value
"content": html_content
}

return templates.TemplateResponse("doc.html", context)









Step 4: Run and Test



Run uvicorn main:app --reload to start the server.



Now, visit http://127.0.0.1:8000/docs/hello again.



You will see that the browser tab title and the <h1> tag on the page are no longer "Hello, Markdown!" but have been replaced with "Hello, Frontmatter!", which we just defined in the hello.md file's Frontmatter.






Conclusion and Next Steps



By introducing Frontmatter and the logic to parse it, our articles are now completely decoupled from the site itself.



Besides Markdown, a website also contains static files like images. How can we get FastAPI to deploy these static files correctly so they can be accessed online?



In the next article, we will solve this problem: handling static assets (like images) referenced in Markdown files, allowing them to be correctly deployed by FastAPI and displayed on the final webpage.






Other



After building your site, you might want to deploy it online for others to see. But most cloud platforms are expensive, and it's not worth paying a high price for a practice project like this.



Is there a more economical way to deploy? You can try Leapcell. It supports deploying multiple languages like Python, Node.js, Go, and Rust, and offers a generous free tier every month, allowing you to deploy up to 20 projects without spending a dime.



Leapcell






Follow us on X: @LeapcellHQ






Read other articles in this series



Related Posts:



SOC Incident Playbook: Remote Code Execution (RCE) Defense
title: Detect Exploitation - Build a Docusaurus-like Site with FastAPI: Step 4 - Parsing Frontmatter
id: 3084967c-41ce-4ff4-a48f-aaabe1789c85
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-24
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-24"
        description = "YARA Signature for "
    strings:
        $str = "Build a Docusaurus-like Site w" ascii wide
    condition:
        any of them
}
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich Build a Docusaurus-like Site with FastAP.... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Build a Docusaurus-like Site with FastAPI: Step 4 - Parsing Frontmatter

Thematisch verwandte Begriffe: Build, Docusauruslike, Site, with · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY Kritische Sicherheitsmeldung
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel TTP ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick