-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdownParser.js
68 lines (58 loc) · 1.99 KB
/
markdownParser.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const express = require("express");
const { marked } = require("marked");
const fs = require("fs");
const path = require("path");
const ejs = require("ejs");
const matter = require("gray-matter");
marked.setOptions({
breaks: true,
gfm: true,
smartLists: true,
headerIds: false,
xhtml: false,
});
const parseMarkdown = (markdownText) => {
const { content, data } = matter(markdownText); // Extract content and frontmatter
const htmlContent = marked(content);
return { htmlContent, metadata: data };
};
const transformImageMarkdown = (htmlContent) => {
const regex = /!\[(.*?)\]\(\.\.\/img\/(.*?)\/(.*?)\)/g;
return htmlContent.replace(regex, (match, altText, folder, filename) => {
return `<center>
<figure>
<img src="${filename}"
loading="lazy" alt="${altText}" width="80%" />
<figcaption>
Animation of rod movement
</figcaption>
</figure>
</center>`;
});
};
const app = express();
const PORT = 3000;
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
fs.readdir("./posts", (err, files) => {
files.forEach((file) => {
const name = file.split(".")[0];
const filePath = path.join(__dirname, "posts", file);
const fileContents = fs.readFileSync(filePath, "utf8");
const { htmlContent, metadata } = parseMarkdown(fileContents); // Get HTML and metadata
console.log(metadata); // For debugging, check the extracted metadata
htmlContent = transformImageMarkdown(htmlContent);
app.get(`/${name}`, (req, res) => {
res.render("post", {
title: metadata.title || name,
date: metadata.date,
author: metadata.author,
content: htmlContent,
});
});
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});