-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
236 lines (204 loc) · 5.46 KB
/
test.ts
File metadata and controls
236 lines (204 loc) · 5.46 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { markdownToHtml, getMarkdownMetadata } from "./src/index";
import { writeFileSync, truncateSync, readFileSync } from "fs";
import { join } from "path";
//! Command
// npx ts-node test.ts
// Define the paths for the Markdown file and the HTML output file
const markdownFilePath = join(__dirname, "notes/example-html-blog-partial.md");
// const markdownFilePath = join(__dirname, "notes/used-car-questions.md");
// const markdownFilePath = join(__dirname, "notes/example-html-blog.md");
// const markdownFilePath = join(__dirname, "notes/example-html-blog.md");
// const markdownFilePath = join(__dirname, "notes/double-slash-meta-test.md");
// const markdownFilePath = join(__dirname, "notes/example-markdown-blog-post.md");
// const markdownFilePath = join(__dirname, "notes/example-js-blog.md");
// const markdownFilePath = join(__dirname, "notes/regex-in-python.md");
// const markdownFilePath = join(__dirname, "notes/example-meta-html-blog.md");
// const markdownFilePath = join(__dirname, "notes/example-py-blog.md");
// const markdownFilePath = join(__dirname, "notes/rust-markdown-test.md");
const htmlFilePath = join(__dirname, "test.html");
// Read the Markdown file
let markdown: string;
try {
markdown = readFileSync(markdownFilePath, "utf8");
} catch (error) {
console.error("Error reading Markdown file:", error);
process.exit(1); // Exit the process with an error code
}
try {
const metadata = getMarkdownMetadata(markdown);
console.dir({ metadata });
} catch (err) {
console.error("Error processing markdown metadata:", err);
}
const opts = { addCopyToClipboard: true, interactiveCheckboxes: false };
// Convert Markdown to HTML
const htmlContent = markdownToHtml(markdown, opts);
// const htmlContent = markdownToHtml(markdown, true);
// console.log(htmlContent);
// process.exit();
// Basic HTML template
const htmlTemplate = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Markdown Test</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0.5rem;
margin-left: 1rem;
line-height: 1.6;
color: #333;
background-color: #f9f9f9;;
}
img {
max-width: 80%;
margin: auto;
display: block;
}
h1, h2, h3, h4 {
color: #444;
margin-bottom: 0.2rem;
border-bottom: 2px solid #ddd;
padding-bottom: 5px;
}
h1 { font-size: 1.5em; }
h2 { font-size: 1.3em; }
h3 { font-size: 1.2em; }
h4 { font-size: 1em; }
h1, h2 {
margin-top: 2rem;
}
p {
margin: 0.1rem 0;
}
ul, ol {
margin: 0.2rem 0;
padding-left: 40px;
}
li {
margin-bottom: 10px;
}
.md-inline-code {
background: #eaeaea;
color: #d63384;
padding: 2px 4px;
border-radius: 4px;
font-family: 'Courier New', Courier, monospace;
font-size: 0.9em;
}
pre {
background: #2d2d2d;
padding: 1rem;
border-radius: 8px;
color: #f8f8f2;
overflow-x: auto;
font-size: 0.9em;
}
.md-code {
background: #2d2d2d;
color: #f8f8f2;
padding: 5px 8px;
border-radius: 4px;
display: block;
white-space: pre-wrap;
font-family: 'Courier New', Courier, monospace;
}
.md-code-container {
margin-bottom: 0.2rem;
position: relative;
}
.md-code-container button {
position: absolute;
top: 10px;
right: 10px;
background-color: #5a5a5a;
color: #f8f8f2;
border: none;
padding: 5px 10px;
border-radius: 5px;
cursor: pointer;
font-size: 0.85em;
}
.md-code-container button:hover {
background-color: #444;
}
.md-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
font-size: 0.95em;
background-color: #fff;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.md-table th,
.md-table td {
border: 1px solid #ddd;
padding: 12px 15px;
text-align: left;
}
.md-table th {
background-color: #f4f4f4;
color: #333;
font-weight: bold;
}
.md-table tbody tr:nth-child(even) {
background-color: #f9f9f9;
}
.md-table tbody tr:hover {
background-color: #f1f1f1;
}
.md-line {
/* For the '---' markdown horizontal decorative lines */
border-top: 2px #909090 solid ;
margin: 1em 0;
}
.md-checkbox {
list-style: none;
margin-left: 0;
padding-left: 0;
display: flex;
align-items: center;
}
.md-checkbox input[type="checkbox"] {
margin-right: 8px;
accent-color: #39FF14;
}
.md-checkbox input[type="checkbox"]:checked + span {
text-decoration: line-through;
color: #888;
}
/* Syntax highlighting */
.md-keyword { color: #66d9ef; font-weight: bold; }
.md-string { color: #a6e22e; }
.md-number { color: #ae81ff; }
.md-comment { color: #909090; font-style: italic; }
.md-special { color: #FF69B4; font-style: bold; }
.md-class { color: #39FF14; font-style: bold; }
.md-decorator { color: magenta; font-style: normal; }
.md-regex { color: red; font-style: normal; }
.md-call-method { color: yellow; font-style: bold; }
</style>
</head>
<body>
${htmlContent}
</body>
</html>
`;
// Truncate the file if it already exists
try {
truncateSync(htmlFilePath);
} catch (error) {
console.error("Error truncating file:", error);
}
// Write the HTML content to the file
try {
writeFileSync(htmlFilePath, htmlTemplate, "utf8");
// console.log("\nresult =>");
// console.log(htmlTemplate);
// console.log(`\nHTML written to ${htmlFilePath}`);
} catch (error) {
console.error("Error writing to file:", error);
}