Skip to content

Commit 53f5565

Browse files
authored
Merge pull request #34 from ShadowDara/minisite-md
Minisite md
2 parents 7c74575 + 6b96956 commit 53f5565

27 files changed

Lines changed: 330 additions & 65 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ dist2/
1515
/crates/easy-git-ignore/target/
1616
/.samengine/tags.txt
1717
/crates/seg/target
18+
/minisite-md/index.html
19+
20+
.npmrc

packages/samengine-cli/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
src/
22
tsconfig.json
33
*.tgz
4+
.npmrc

packages/samengine-cli/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
{
22
"name": "samengine-cli",
3-
"version": "1.7.0",
4-
"description": "CLI which help with samengine",
3+
"version": "2.0.1",
4+
"description": "CLI Tools and other useful helper for samengine",
55
"scripts": {
66
"build": "tsc",
77
"pack": "node ../../scripts/clean.js && npm run build && npm pack && git push --follow-tags"
88
},
9+
"exports": {
10+
".": "./dist/index.js",
11+
"./csv": "./dist/csv/index.js"
12+
},
913
"bin": {
10-
"samengine-cli-linksaver": "dist/linksaver.js",
11-
"samengine-cli-buildinfos": "dist/buildinfos.js"
14+
"sam-cli": "dist/cli/index.js"
1215
},
1316
"author": "Shadowdara",
1417
"type": "module",
1518
"devDependencies": {
1619
"@types/node": "^25.5.2",
1720
"typescript": "^6.0.2"
1821
},
19-
"dependencies": {
20-
"chalk": "^5.6.2"
21-
},
2222
"license": "MIT",
2323
"repository": {
2424
"type": "git",
2525
"url": "https://github.com/shadowdara/samengine",
26-
"directory": "samengine-cli"
26+
"directory": "packages/samengine-cli"
2727
},
2828
"bugs": {
2929
"url": "https://github.com/shadowdara/samengine/issues"

packages/samengine-cli/src/buildinfos.ts renamed to packages/samengine-cli/src/cli/buildinfos.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import { readFileSync, writeFileSync, existsSync, readdirSync, statSync } from "fs";
77
import { join } from "path";
88
import os from "os";
9-
import chalk from 'chalk';
109
import { mkdir } from "fs/promises";
1110

1211
const CONFIG_FILE = ".samengine/buildinfos.json";
@@ -114,8 +113,7 @@ async function setup() {
114113

115114
// Help
116115
function help() {
117-
console.log(`
118-
${chalk.green("Buildinfos CLI by webgameengine")}
116+
console.log(`Buildinfos CLI by webgameengine
119117
120118
Commands:
121119
setup Create config file
@@ -125,8 +123,8 @@ Commands:
125123
}
126124

127125
// Main
128-
function main() {
129-
const args = process.argv.slice(2);
126+
export function main() {
127+
const args = process.argv.slice(3);
130128

131129
try {
132130
if (args[0] === "setup") {
@@ -153,6 +151,3 @@ function main() {
153151
process.exit(1);
154152
}
155153
}
156-
157-
// Start the Cli Tool
158-
main();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env node
2+
3+
import { parseMarkdown } from "../index.js";
4+
import fs from "fs/promises";
5+
import path from "path";
6+
7+
import { main as buildinfos} from "./buildinfos.js";
8+
import { main as minisite, newProj } from "./minisite.js";
9+
10+
async function convertMarkdown() {
11+
// const args = process.argv.slice(2); // alles nach node + script
12+
// falls du wirklich "nach dem 2. Argument" meinst:
13+
const args = process.argv.slice(3);
14+
15+
for (const arg of args) {
16+
const content = await fs.readFile(arg, "utf-8");
17+
const html = parseMarkdown(content);
18+
const filePath = path.join(arg + ".html");
19+
await fs.writeFile(filePath, html, "utf-8");
20+
}
21+
}
22+
23+
function help() {
24+
console.log(`samengine-cli
25+
26+
This package provides a cli tool for samengine:
27+
- markdown
28+
- buildinfos
29+
- minisite
30+
31+
and some usable functions:
32+
- a markdown parser
33+
- a csv parser
34+
- a jsonc parser
35+
`);
36+
}
37+
38+
// Main funciton
39+
async function main() {
40+
if (process.argv.length < 3) {
41+
help();
42+
process.exit(1);
43+
}
44+
45+
if (process.argv[2] == "markdown") {
46+
await convertMarkdown();
47+
}
48+
49+
if (process.argv[2] == "buildinfos") {
50+
buildinfos();
51+
}
52+
53+
if (process.argv[2] == "minisite") {
54+
// New minisite proj
55+
if (process.argv.length >= 4 && process.argv[3] == "new") {
56+
newProj();
57+
}
58+
59+
minisite();
60+
}
61+
}
62+
63+
await main();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { parseMarkdown, exportCompressedMarkdownCSS } from "../index.js";
4+
5+
const TEMPLATE_HTML = `<!doctype html><html lang="de"><head><meta charset="UTF-8"><title>MiniSite</title><style>__CSS__{}</style></head><body><main id="app" class="md-body"></main><script> const pages = __PAGES__; const app = document.getElementById("app"); function normalizeRoute(route) { route = route.trim(); route = route.replace(/^\/+/, ""); if ( route === "" || route === "/" || route === "index" ) { return "index"; } return route; } function render() { let route = location.hash.slice(1); route = normalizeRoute(route); const html = pages[route] ?? pages["404"] ?? \`<h1>404</h1><a href="#">Home</a>\`; app.innerHTML = html; } window.addEventListener("hashchange", render); window.addEventListener("DOMContentLoaded", render); </script></body></html>
6+
`;
7+
8+
export function newProj() {
9+
fs.writeFileSync("index.html", TEMPLATE_HTML);
10+
11+
console.log(`Create new Markdown files! Start with pages/index.md for the start page. Then run sam-cli minisite to compile it.!
12+
Have fun!
13+
`)
14+
}
15+
16+
export function main() {
17+
const pages = {};
18+
19+
function walk(dir) {
20+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
21+
const file = path.join(dir, entry.name);
22+
23+
if (entry.isDirectory()) {
24+
walk(file);
25+
continue;
26+
}
27+
28+
if (!entry.name.endsWith(".md"))
29+
continue;
30+
31+
const route = path
32+
.relative("pages", file)
33+
.replace(/\\/g, "/")
34+
.replace(/\.md$/, "");
35+
36+
const md = fs.readFileSync(file, "utf8");
37+
38+
// 👉 HIER passiert die Compilation
39+
const html = parseMarkdown(md);
40+
41+
pages[route] = html;
42+
}
43+
}
44+
45+
walk("pages");
46+
47+
const template = fs.readFileSync("template.html", "utf8");
48+
49+
const html = template.replace(
50+
"__PAGES__",
51+
JSON.stringify(pages)
52+
);
53+
54+
const html2 = html.replace(
55+
"__CSS__{}",
56+
exportCompressedMarkdownCSS()
57+
)
58+
59+
fs.writeFileSync("index.html", html2);
60+
61+
console.log("compiled", Object.keys(pages).length, "pages");
62+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!doctype html>
2+
<html lang="de">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>MiniSite</title>
6+
7+
<style>
8+
__CSS__ {}
9+
</style>
10+
</head>
11+
12+
<body>
13+
14+
<main id="app" class="md-body"></main>
15+
16+
<script>
17+
const pages = __PAGES__;
18+
const app = document.getElementById("app");
19+
20+
function normalizeRoute(route) {
21+
route = route.trim();
22+
23+
// entfernt führende /
24+
route = route.replace(/^\/+/, "");
25+
26+
// fallback auf index
27+
if (
28+
route === "" ||
29+
route === "/" ||
30+
route === "index"
31+
) {
32+
return "index";
33+
}
34+
35+
return route;
36+
}
37+
38+
function render() {
39+
let route = location.hash.slice(1);
40+
41+
route = normalizeRoute(route);
42+
43+
const html =
44+
pages[route] ??
45+
pages["404"] ??
46+
`<h1>404</h1><a href="#">Home</a>`;
47+
48+
app.innerHTML = html;
49+
}
50+
51+
window.addEventListener("hashchange", render);
52+
window.addEventListener("DOMContentLoaded", render);
53+
</script>
54+
55+
</body>
56+
</html>
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)