aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/MarkdownRenderer.jsx
blob: ca1da7591fb4fff32a6fbd649704353cc7a34a58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React, { useEffect, useState } from "react";
import { marked } from "marked";

const MarkdownRenderer = () => {
    const [content, setContent] = useState("");
    const [error, setError] = useState(null);
    const [file, setFile] = useState(null);

    useEffect(() => {
        const params = new URLSearchParams(window.location.search);
        const fileParam = params.get("q");

        if (!fileParam) {
            setError("No file specified");
            return;
        }

        setFile(fileParam);

        const fetchMarkdown = async () => {
            try {
                const response = await fetch(`/md/${fileParam}.md`);
                if (!response.ok) {
                    throw new Error(`Could not fetch ${fileParam}.md`);
                }
                const text = await response.text();
                setContent(marked(text));
            } catch (err) {
                setError(err.message);
            }
        };

        fetchMarkdown();
    }, []);

    if (error) {
        return <div style={{ color: "red" }}>{error}</div>;
    }

    if (!file) {
        return <div>Loading...</div>;
    }
    return <div dangerouslySetInnerHTML={{ __html: content }} />;
};

export default MarkdownRenderer;
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage