serve-my-md 1.1.7 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +61 -44
- package/package.json +2 -2
- package/web/public/og-image.png +0 -0
package/bin/index.js
CHANGED
|
@@ -12,12 +12,12 @@ var Logger = class {
|
|
|
12
12
|
|
|
13
13
|
// cli/src/shared.ts
|
|
14
14
|
import MarkdownIt from "markdown-it";
|
|
15
|
-
import
|
|
15
|
+
import path3 from "path";
|
|
16
16
|
import Prism from "prismjs";
|
|
17
17
|
|
|
18
18
|
// cli/src/core/index.ts
|
|
19
|
-
import
|
|
20
|
-
import
|
|
19
|
+
import fs3 from "fs/promises";
|
|
20
|
+
import path2 from "path";
|
|
21
21
|
import { minimatch } from "minimatch";
|
|
22
22
|
|
|
23
23
|
// cli/src/utils/index.ts
|
|
@@ -114,6 +114,8 @@ var config_default = {
|
|
|
114
114
|
|
|
115
115
|
// cli/src/lib/commander.ts
|
|
116
116
|
import { Command } from "commander";
|
|
117
|
+
import path from "path";
|
|
118
|
+
import fs2 from "fs";
|
|
117
119
|
var program = new Command();
|
|
118
120
|
program.name(config_default.name).description(config_default.description).version(config_default.version);
|
|
119
121
|
program.option("-d, --directory <path>", "Directory to scan for markdown files", ".");
|
|
@@ -130,6 +132,11 @@ if (options.interactive || options.directory === void 0) {
|
|
|
130
132
|
});
|
|
131
133
|
options.directory = res.trim();
|
|
132
134
|
}
|
|
135
|
+
options.directory = path.resolve(options.directory);
|
|
136
|
+
fs2.existsSync(options.directory) || (() => {
|
|
137
|
+
console.error("Directory does not exist: " + options.directory);
|
|
138
|
+
process.exit(1);
|
|
139
|
+
})();
|
|
133
140
|
|
|
134
141
|
// cli/src/core/index.ts
|
|
135
142
|
import { readdirSync } from "fs";
|
|
@@ -143,7 +150,7 @@ var constants_default = {
|
|
|
143
150
|
var STATIC_TEMP_CONTENT_PREFIX = constants_default.STATIC_TEMP_CONTENT_PREFIX;
|
|
144
151
|
async function readConfig(filepath) {
|
|
145
152
|
try {
|
|
146
|
-
const data = JSON.parse(await
|
|
153
|
+
const data = JSON.parse(await fs3.readFile(filepath, "utf-8"));
|
|
147
154
|
return data;
|
|
148
155
|
} catch (err) {
|
|
149
156
|
Logger.log(
|
|
@@ -166,7 +173,7 @@ async function parseSmmIgnore(filePath) {
|
|
|
166
173
|
return ignored;
|
|
167
174
|
};
|
|
168
175
|
var shouldIgnore2 = shouldIgnore3;
|
|
169
|
-
const raw = await
|
|
176
|
+
const raw = await fs3.readFile(filePath, "utf8");
|
|
170
177
|
const rules = raw.split(/\r?\n/).map((line) => line.trim()).filter((line) => line !== "" && !line.startsWith("#")).map((line) => {
|
|
171
178
|
const negated = line.startsWith("!");
|
|
172
179
|
const pattern = negated ? line.slice(1) : line;
|
|
@@ -185,11 +192,11 @@ async function parseSmmIgnore(filePath) {
|
|
|
185
192
|
}
|
|
186
193
|
}
|
|
187
194
|
async function getMarkdownFiles(baseUrl, pairChildren) {
|
|
188
|
-
const files = await
|
|
195
|
+
const files = await fs3.readdir(baseUrl, { withFileTypes: true });
|
|
189
196
|
const routeTree = pairChildren || [];
|
|
190
197
|
const promises = [];
|
|
191
198
|
for (const file of files) {
|
|
192
|
-
const filePath =
|
|
199
|
+
const filePath = path2.join(baseUrl, file.name);
|
|
193
200
|
if (shouldIgnore(filePath.slice(options.directory.length)) || filePath.slice(options.directory.length) === finalConfig.publicPath)
|
|
194
201
|
continue;
|
|
195
202
|
if (file.isDirectory()) {
|
|
@@ -247,16 +254,16 @@ function getPath(filepath) {
|
|
|
247
254
|
return slugify(transformedPath).split("/").filter((s) => !(s.startsWith("(") && s.endsWith(")"))).join("/") || "/";
|
|
248
255
|
}
|
|
249
256
|
async function parseMD(filepath) {
|
|
250
|
-
const
|
|
257
|
+
const path5 = getPath(filepath);
|
|
251
258
|
return {
|
|
252
|
-
path:
|
|
253
|
-
content: mdParser.render(await
|
|
259
|
+
path: path5,
|
|
260
|
+
content: mdParser.render(await fs3.readFile(filepath, "utf-8"))
|
|
254
261
|
};
|
|
255
262
|
}
|
|
256
263
|
async function generateHtml(distDir, routeContent) {
|
|
257
264
|
try {
|
|
258
|
-
let htmlTemplate = await
|
|
259
|
-
|
|
265
|
+
let htmlTemplate = await fs3.readFile(
|
|
266
|
+
path2.join(import.meta.dirname, "..", "index.html"),
|
|
260
267
|
"utf-8"
|
|
261
268
|
);
|
|
262
269
|
const commentStart = htmlTemplate.indexOf("<!--");
|
|
@@ -268,20 +275,20 @@ async function generateHtml(distDir, routeContent) {
|
|
|
268
275
|
""
|
|
269
276
|
);
|
|
270
277
|
if (distDir) {
|
|
271
|
-
const files = readdirSync(
|
|
278
|
+
const files = readdirSync(path2.join(distDir, "assets"));
|
|
272
279
|
const cssFile = files.find((file) => file.endsWith(".css"));
|
|
273
280
|
const jsFile = files.find((file) => file.endsWith(".js"));
|
|
274
|
-
const prefix = distDir.slice(
|
|
281
|
+
const prefix = distDir.slice(path2.join(import.meta.dirname, options.directory).length);
|
|
275
282
|
htmlTemplate = htmlTemplate.replace(`<script type="module" src="/src/main.tsx"></script>`, "");
|
|
276
283
|
if (cssFile && jsFile) {
|
|
277
284
|
htmlTemplate = htmlTemplate.replace(
|
|
278
285
|
"{{distAssets}}",
|
|
279
|
-
`<link rel="stylesheet" href="${
|
|
286
|
+
`<link rel="stylesheet" href="${path2.join(
|
|
280
287
|
prefix,
|
|
281
288
|
"assets",
|
|
282
289
|
cssFile
|
|
283
290
|
)}" />
|
|
284
|
-
<script type="module" src="${
|
|
291
|
+
<script type="module" src="${path2.join(
|
|
285
292
|
prefix,
|
|
286
293
|
"assets",
|
|
287
294
|
jsFile
|
|
@@ -315,19 +322,19 @@ async function buildDistRoutesFromRouteTree(routeTree, groupedRoutes, distPath,
|
|
|
315
322
|
prefix
|
|
316
323
|
);
|
|
317
324
|
} else {
|
|
318
|
-
const distRoutePath =
|
|
319
|
-
await
|
|
325
|
+
const distRoutePath = path2.join(distPath, prefix, node.pathSegment.replace("/", "")) + (node.pathSegment === "" ? "/index.html" : ".html");
|
|
326
|
+
await fs3.mkdir(path2.dirname(distRoutePath), { recursive: true });
|
|
320
327
|
const html = await generateHtml(
|
|
321
328
|
distPath,
|
|
322
|
-
groupedRoutes[
|
|
329
|
+
groupedRoutes[path2.posix.join(prefix, node.pathSegment)]?.[0]?.content
|
|
323
330
|
);
|
|
324
|
-
await
|
|
331
|
+
await fs3.writeFile(distRoutePath, html, "utf-8");
|
|
325
332
|
if (node.children) {
|
|
326
333
|
await buildDistRoutesFromRouteTree(
|
|
327
334
|
node.children,
|
|
328
335
|
groupedRoutes,
|
|
329
336
|
distPath,
|
|
330
|
-
|
|
337
|
+
path2.join(prefix, node.pathSegment)
|
|
331
338
|
);
|
|
332
339
|
}
|
|
333
340
|
}
|
|
@@ -362,11 +369,11 @@ import MarkdownItFootNote from "markdown-it-footnote";
|
|
|
362
369
|
import MarkdownItTasks from "markdown-it-task-lists";
|
|
363
370
|
import loadLanguages from "prismjs/components/index.js";
|
|
364
371
|
var { shouldIgnore } = await parseSmmIgnore(
|
|
365
|
-
|
|
372
|
+
path3.join(options.directory, config_default.defaultIgnorePath)
|
|
366
373
|
);
|
|
367
374
|
var finalConfig = {
|
|
368
375
|
...smm_config_default,
|
|
369
|
-
...await readConfig(
|
|
376
|
+
...await readConfig(path3.join(options.directory, config_default.defaultConfigPath))
|
|
370
377
|
};
|
|
371
378
|
var md = new MarkdownIt({
|
|
372
379
|
...finalConfig.markdownItOptions,
|
|
@@ -383,10 +390,10 @@ var mdParser = md;
|
|
|
383
390
|
|
|
384
391
|
// cli/src/core/build.ts
|
|
385
392
|
import { cp, readdir as readdir2, rm, writeFile } from "fs/promises";
|
|
386
|
-
import
|
|
393
|
+
import path4, { resolve } from "path";
|
|
387
394
|
import { fileURLToPath } from "url";
|
|
388
395
|
import { build as viteBuild } from "vite";
|
|
389
|
-
import { mkdirSync } from "fs";
|
|
396
|
+
import { mkdirSync, existsSync } from "fs";
|
|
390
397
|
var DIST_DIRNAME = finalConfig.outDir || "dist";
|
|
391
398
|
var WEB_DIRNAME = "web";
|
|
392
399
|
var PUBLIC_DIRNAME = "public";
|
|
@@ -398,7 +405,7 @@ async function build(options2) {
|
|
|
398
405
|
const parsePromises = [];
|
|
399
406
|
Logger.log("Processing routes...");
|
|
400
407
|
for (const file of makeRoutesOfNestedPathsRaw(routeTree)) {
|
|
401
|
-
parsePromises.push(parseMD(
|
|
408
|
+
parsePromises.push(parseMD(path4.join(options2.directory, file)));
|
|
402
409
|
}
|
|
403
410
|
cleanNestedPaths(routeTree);
|
|
404
411
|
const groupedRoutes = Object.groupBy(
|
|
@@ -406,7 +413,13 @@ async function build(options2) {
|
|
|
406
413
|
(route) => route.path
|
|
407
414
|
);
|
|
408
415
|
const routes = makeRoutesOfNestedPaths(routeTree).reduce(
|
|
409
|
-
(acc, pth) => [
|
|
416
|
+
(acc, pth) => [
|
|
417
|
+
...acc,
|
|
418
|
+
...(groupedRoutes[pth] ?? []).map((r) => ({
|
|
419
|
+
...r,
|
|
420
|
+
path: path4.join(finalConfig.baseRoute || "/", r.path)
|
|
421
|
+
}))
|
|
422
|
+
],
|
|
410
423
|
[]
|
|
411
424
|
);
|
|
412
425
|
const out = {
|
|
@@ -429,37 +442,41 @@ async function build(options2) {
|
|
|
429
442
|
routes.forEach((o) => {
|
|
430
443
|
Logger.log(o.path);
|
|
431
444
|
});
|
|
432
|
-
const __dirname =
|
|
433
|
-
const webDir =
|
|
434
|
-
const distDir =
|
|
435
|
-
mkdirSync(
|
|
445
|
+
const __dirname = path4.dirname(fileURLToPath(import.meta.url));
|
|
446
|
+
const webDir = path4.join(__dirname, "..", WEB_DIRNAME);
|
|
447
|
+
const distDir = path4.join(webDir, DIST_DIRNAME);
|
|
448
|
+
mkdirSync(path4.join(webDir, "src", ".generated"), { recursive: true });
|
|
436
449
|
await writeFile(
|
|
437
|
-
|
|
450
|
+
path4.join(webDir, "src", ".generated", "output.json"),
|
|
438
451
|
JSON.stringify(out)
|
|
439
452
|
);
|
|
440
453
|
await writeFile(
|
|
441
|
-
|
|
454
|
+
path4.join(webDir, "src", ".generated", "paths.json"),
|
|
442
455
|
JSON.stringify(routeTree)
|
|
443
456
|
);
|
|
444
457
|
Logger.log("\nParsed MDs");
|
|
445
|
-
await writeFile(
|
|
458
|
+
await writeFile(path4.join(webDir, "index.html"), await generateHtml());
|
|
446
459
|
Logger.log("Generated HTML from template");
|
|
447
460
|
if (!skipBuild) {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
461
|
+
if (existsSync(path4.join(webDir, PUBLIC_DIRNAME))) {
|
|
462
|
+
const entries = await readdir2(path4.join(webDir, PUBLIC_DIRNAME));
|
|
463
|
+
for (const entry of entries) {
|
|
464
|
+
await rm(path4.join(webDir, PUBLIC_DIRNAME, entry), {
|
|
465
|
+
recursive: true,
|
|
466
|
+
force: true
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
} else {
|
|
470
|
+
mkdirSync(path4.join(webDir, PUBLIC_DIRNAME));
|
|
454
471
|
}
|
|
455
472
|
if (finalConfig.publicPath) {
|
|
456
473
|
if (await FileOrDirectoryExists(
|
|
457
|
-
|
|
474
|
+
path4.join(options2.directory, finalConfig.publicPath)
|
|
458
475
|
)) {
|
|
459
476
|
Logger.log(`Copying public assets from ${finalConfig.publicPath}...`);
|
|
460
477
|
await cp(
|
|
461
|
-
|
|
462
|
-
|
|
478
|
+
path4.join(options2.directory, finalConfig.publicPath),
|
|
479
|
+
path4.join(webDir, PUBLIC_DIRNAME),
|
|
463
480
|
{ recursive: true }
|
|
464
481
|
);
|
|
465
482
|
} else {
|
|
@@ -471,7 +488,7 @@ async function build(options2) {
|
|
|
471
488
|
configFile: resolve(webDir, "vite.config.ts")
|
|
472
489
|
});
|
|
473
490
|
await buildDistRoutesFromRouteTree(routeTree, groupedRoutes, distDir);
|
|
474
|
-
const targetDist =
|
|
491
|
+
const targetDist = path4.join(options2.directory, DIST_DIRNAME);
|
|
475
492
|
await rm(targetDist, { recursive: true }).catch(() => {
|
|
476
493
|
});
|
|
477
494
|
Logger.log("Built the app, copying results...");
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "serve-my-md",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Generate beautiful SEO-friendly static websites from Markdown files.",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/AshishAntil07/serve-my-md.git"
|
|
9
9
|
},
|
|
10
|
-
"homepage": "https://
|
|
10
|
+
"homepage": "https://serve-my-md.ashishantil.dev",
|
|
11
11
|
"author": "AshishAntil07 <ashishantil.antil07@gmail.com>",
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"keywords": [
|
package/web/public/og-image.png
DELETED
|
Binary file
|