@lexho111/plainblog 0.7.0 → 0.7.2

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/AssetManager.js CHANGED
@@ -15,7 +15,7 @@ export default class AssetManager {
15
15
  this.reloadStylesOnGET = false;
16
16
  this.stylesheetPath = null;
17
17
  this.compilestyle = false;
18
- this.styles = "body { font-family: Arial; }";
18
+ this.styles = "";
19
19
  this.compiledStyles = "";
20
20
  this.stylesHash = "";
21
21
  this.scriptsHash = "";
@@ -32,60 +32,7 @@ export default class AssetManager {
32
32
  await this.processStylesheets(this.stylesheetPath);
33
33
  }
34
34
  if (!this.stylesheetPath) {
35
- // compile and merge hardcoded styles in "this.styles" with "src/styles.css"
36
- const srcStylePath = path.join(__dirname, "src", "styles.css");
37
- const publicStylePath = path.join(this.publicDir, "styles.min.css");
38
-
39
- let publicHash = null;
40
- let srcStyles = "";
41
-
42
- await Promise.all([
43
- fs.promises
44
- .readFile(publicStylePath, "utf8")
45
- .then((publicCSS) => {
46
- const match = publicCSS.match(
47
- /\/\* source-hash: ([a-f0-9]{64}) \*\//,
48
- );
49
- if (match) publicHash = match[1];
50
- })
51
- .catch((err) => {
52
- if (err.code !== "ENOENT") console.error(err);
53
- }),
54
- fs.promises
55
- .readFile(srcStylePath, "utf8")
56
- .then((content) => {
57
- srcStyles = content;
58
- })
59
- .catch((err) => {
60
- if (err.code !== "ENOENT") console.error(err);
61
- }),
62
- ]);
63
-
64
- const combinedStyles = this.styles + srcStyles;
65
- const srcHash = crypto
66
- .createHash("sha256")
67
- .update(combinedStyles)
68
- .digest("hex");
69
-
70
- if (srcHash !== publicHash && this.compilestyle) {
71
- debug("Styles have changed. Recompiling in worker...");
72
- const finalStyles = await this.runWorker("mergeStyles", [
73
- this.styles,
74
- srcStyles,
75
- ]);
76
-
77
- try {
78
- await fs.promises.mkdir(path.dirname(publicStylePath), {
79
- recursive: true,
80
- });
81
- await fs.promises.writeFile(
82
- publicStylePath,
83
- finalStyles + `\n/* source-hash: ${srcHash} */`,
84
- );
85
- } catch (err) {
86
- console.error("Failed to write styles to public folder:", err);
87
- }
88
- }
35
+ await this.processDefaultStyles();
89
36
  }
90
37
 
91
38
  // Process Scripts
@@ -94,13 +41,76 @@ export default class AssetManager {
94
41
  }
95
42
 
96
43
  async reload() {
44
+ debug("reload");
97
45
  if (this.stylesheetPath != null && this.compilestyle) {
98
46
  await this.processStylesheets(this.stylesheetPath);
99
47
  }
48
+ if (!this.stylesheetPath) {
49
+ await this.processDefaultStyles();
50
+ }
100
51
  const srcScriptPath = path.join(__dirname, "src", "fetchData.js");
101
52
  await this.processScripts(srcScriptPath);
102
53
  }
103
54
 
55
+ async processDefaultStyles() {
56
+ // compile and merge hardcoded styles in "this.styles" with "src/styles.css"
57
+ const srcStylePath = path.join(__dirname, "src", "styles.css");
58
+ const publicStylePath = path.join(this.publicDir, "styles.min.css");
59
+
60
+ let publicHash = null;
61
+ let srcStyles = "";
62
+
63
+ await Promise.all([
64
+ fs.promises
65
+ .readFile(publicStylePath, "utf8")
66
+ .then((publicCSS) => {
67
+ const match = publicCSS.match(
68
+ /\/\* source-hash: ([a-f0-9]{64}) \*\//,
69
+ );
70
+ if (match) publicHash = match[1];
71
+ })
72
+ .catch((err) => {
73
+ if (err.code !== "ENOENT" && !err.message.includes("ENOENT"))
74
+ console.error(err);
75
+ }),
76
+ fs.promises
77
+ .readFile(srcStylePath, "utf8")
78
+ .then((content) => {
79
+ srcStyles = content;
80
+ })
81
+ .catch((err) => {
82
+ if (err.code !== "ENOENT" && !err.message.includes("ENOENT"))
83
+ console.error(err);
84
+ }),
85
+ ]);
86
+
87
+ const combinedStyles = srcStyles + "\n" + this.styles;
88
+ const srcHash = crypto
89
+ .createHash("sha256")
90
+ .update(combinedStyles)
91
+ .digest("hex");
92
+
93
+ if (srcHash !== publicHash) {
94
+ debug("Styles have changed. Recompiling in worker...");
95
+ const finalStyles = await this.runWorker("mergeStyles", [
96
+ srcStyles,
97
+ this.styles,
98
+ ]);
99
+
100
+ try {
101
+ await fs.promises.mkdir(path.dirname(publicStylePath), {
102
+ recursive: true,
103
+ });
104
+ await fs.promises.writeFile(
105
+ publicStylePath,
106
+ finalStyles + `\n/* source-hash: ${srcHash} */`,
107
+ );
108
+ } catch (err) {
109
+ console.error("Failed to write styles to public folder:", err);
110
+ }
111
+ }
112
+ }
113
+
104
114
  async processStylesheets(files) {
105
115
  debug("process stylesheets");
106
116
  const fileList = Array.isArray(files) ? files : [files];
package/Blog.js CHANGED
@@ -60,12 +60,10 @@ export default class Blog {
60
60
  title: this.#title,
61
61
  articles: this.#articles.getAllArticles(),
62
62
  server: serverInfo,
63
- compiledStyles: this.assetManager.compiledStyles,
63
+ assetManager: this.assetManager,
64
64
  sessions: this.sessions,
65
65
  database: this.database,
66
66
  password: this.#password,
67
- styles: this.assetManager.styles,
68
- reloadStylesOnGET: this.assetManager.reloadStylesOnGET,
69
67
  };
70
68
 
71
69
  return JSON.parse(JSON.stringify(json)); // make json read-only
@@ -100,14 +98,6 @@ export default class Blog {
100
98
  this.#password = password;
101
99
  }
102
100
 
103
- /**
104
- * Appends CSS rules to the \<style\>-tag.
105
- * @param {string} style - A string containing CSS rules.
106
- */
107
- setStyle(style) {
108
- this.assetManager.setStyle(style);
109
- }
110
-
111
101
  set title(t) {
112
102
  this.#title = t;
113
103
  }
@@ -129,11 +119,17 @@ export default class Blog {
129
119
  }
130
120
 
131
121
  /**
132
- * Appends CSS rules to the \<style\>-tag.
122
+ * Sets the CSS rules for the blog.
133
123
  * @param {string} style - A string containing CSS rules.
134
124
  */
135
125
  set style(style) {
136
- this.assetManager.setStyle(style);
126
+ debug("set style");
127
+ this.assetManager.styles = style;
128
+ this.assetManager.compilestyle = true;
129
+ }
130
+
131
+ get style() {
132
+ return this.assetManager.styles;
137
133
  }
138
134
 
139
135
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lexho111/plainblog",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "A tool for creating and serving a minimalist, single-page blog.",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/postinstall.js CHANGED
@@ -59,7 +59,7 @@ async function main() {
59
59
  await fs.promises.access(destFile);
60
60
  // If it exists, we do nothing to avoid overwriting user files.
61
61
  } catch (error) {
62
- if (error.code === "ENOENT") {
62
+ if (error.code === "ENOENT" || error.message.includes("ENOENT")) {
63
63
  // File doesn't exist in the destination, so copy it.
64
64
  try {
65
65
  await fs.promises.copyFile(sourceFile, destFile);
@@ -1,2 +1,2 @@
1
- body{font-family:Arial,sans-serif}h1{color:#333}:root{--black:#111;--clearwhite:#fefefe;--white:#eee;--darkgray:rgba(54,54,54,.5);--text-primary:var(--black);--text-secondary:var(--clearwhite);--max-width:600px}body,html{margin:0;overflow-x:hidden}html{font-size:clamp(.85rem,1vw + .5rem,1.1rem)}body{background:var(--white_darker);color:var(--text-primary);font-family:Arial}nav{align-items:center;background:#ebebeb;box-sizing:border-box;display:flex;margin-top:10px;max-width:var(--max-width);padding:5px}#header{max-width:var(--max-width)}button,form input,form textarea{box-sizing:border-box;padding:5px 10px}button{padding:10px;width:-moz-fit-content;width:fit-content}img{max-width:100%}input,textarea{border:1px solid #ababab}input,input:focus,textarea,textarea:focus{box-shadow:6px 6px 1px 1px rgba(50,50,50,.2)}input:focus,textarea:focus{background-color:var(--clearwhite);border:2px solid #3b40c1;outline:none}.form_element{display:block;font-family:monospace;font-size:103%;margin-bottom:10px;padding:4px}.wide{width:100%}.password{width:250px}.new_title{height:35px;padding:10px}.new_content{height:300px;padding:10px;resize:none}#createNew{max-width:var(--max-width);width:100%}#search{border:1px solid var(--text-primary);box-shadow:none;font-size:1rem;margin-left:auto;padding:.4rem 1rem}hr{margin:40px 0}.articles,hr{max-width:var(--max-width)}.articles{border:0 solid #000;display:grid;gap:.25rem;grid-template-columns:1fr}.articles article{border:2px solid #a9a9a9;border-radius:4px;margin-bottom:10px;min-width:0;overflow-wrap:break-word;padding:.4rem}.articles article h2{color:#353535;margin-bottom:5px}.articles article .datetime{color:#757575;margin:0}.articles article p{margin-bottom:0;margin-top:10px}article a,article a:visited,h1{color:#696969}h2{border:0 solid #000;margin-top:0}nav a{color:#3b40c1;font-size:20px;text-decoration:underline}nav a:visited{color:#3b40c1;text-decoration-color:#3b40c1}.loginform{margin-left:25px}#wrapper{margin-left:0;max-width:1200px;padding:0 20px}#wrapper,.buttons{box-sizing:border-box;width:100%}.buttons{align-items:center;border:0 solid #000;display:flex;gap:5px;height:25px;list-style:none;margin:0 0 16px;padding:15px 15px 15px 0}.btn{border:none;border:1px solid var(--text-primary);border-radius:0;box-shadow:3px 2px 2px var(--darkgray);color:var(--text-primary);cursor:pointer;font-size:1rem;font-weight:500;padding:.4rem 1rem;text-decoration:none;width:-moz-fit-content;width:fit-content}.btn:hover{background-color:#fff;border:2px solid var(--black);color:#000}.light{background:var(--clearwhite);color:var(--black)}.light:hover{background:var(--black);color:var(--clearwhite)}.login{font-weight:600}.hide-image{display:none}.edit{background-color:blue}.delete,.edit{color:var(--clearwhite)}.delete{background-color:red}@media screen and (min-width:1000px){#wrapper,body{font-size:.75rem;margin:0 auto;max-width:1400px;padding:0 40px;width:90%}.articles article p,.form_element{font-size:105%}}
2
- /* source-hash: ee7408c8775382654848bb837c557736056d12e9f7e0bf6f59898ac573716f14 */
1
+ :root{--black:#111;--clearwhite:#fefefe;--white:#eee;--darkgray:rgba(54,54,54,.5);--text-primary:var(--black);--text-secondary:var(--clearwhite);--max-width:600px}body,html{margin:0;overflow-x:hidden}html{font-size:clamp(.85rem,1vw + .5rem,1.1rem)}body{background:var(--white_darker);color:var(--text-primary);font-family:Arial}nav{align-items:center;background:#ebebeb;box-sizing:border-box;display:flex;margin-top:10px;max-width:var(--max-width);padding:5px}#header{max-width:var(--max-width)}button,form input,form textarea{box-sizing:border-box;padding:5px 10px}button{padding:10px;width:-moz-fit-content;width:fit-content}img{max-width:100%}input,textarea{border:1px solid #ababab}input,input:focus,textarea,textarea:focus{box-shadow:6px 6px 1px 1px rgba(50,50,50,.2)}input:focus,textarea:focus{background-color:var(--clearwhite);border:2px solid #3b40c1;outline:none}.form_element{display:block;font-family:monospace;font-size:103%;margin-bottom:10px;padding:4px}.wide{width:100%}.password{width:250px}.new_title{height:35px;padding:10px}.new_content{height:300px;padding:10px;resize:none}#createNew{max-width:var(--max-width);width:100%}#search{border:1px solid var(--text-primary);box-shadow:none;font-size:1rem;margin-left:auto;padding:.4rem 1rem}hr{margin:40px 0}.articles,hr{max-width:var(--max-width)}.articles{border:0 solid #000;display:grid;gap:.25rem;grid-template-columns:1fr}.articles article{border:2px solid #a9a9a9;border-radius:4px;margin-bottom:10px;min-width:0;overflow-wrap:break-word;padding:.4rem}.articles article h2{color:#353535;margin-bottom:5px}.articles article .datetime{color:#757575;margin:0}.articles article p{margin-bottom:0;margin-top:10px}article a,article a:visited,h1{color:#696969}h2{border:0 solid #000;margin-top:0}nav a{color:#3b40c1;font-size:20px;text-decoration:underline}nav a:visited{color:#3b40c1;text-decoration-color:#3b40c1}.loginform{margin-left:25px}#wrapper{margin-left:0;max-width:1200px;padding:0 20px}#wrapper,.buttons{box-sizing:border-box;width:100%}.buttons{align-items:center;border:0 solid #000;display:flex;gap:5px;height:25px;list-style:none;margin:0 0 16px;padding:15px 15px 15px 0}.btn{border:none;border:1px solid var(--text-primary);border-radius:0;box-shadow:3px 2px 2px var(--darkgray);color:var(--text-primary);cursor:pointer;font-size:1rem;font-weight:500;padding:.4rem 1rem;text-decoration:none;width:-moz-fit-content;width:fit-content}.btn:hover{background-color:#fff;border:2px solid var(--black);color:#000}.light{background:var(--clearwhite);color:var(--black)}.light:hover{background:var(--black);color:var(--clearwhite)}.login{font-weight:600}.hide-image{display:none}.edit{background-color:blue}.delete,.edit{color:var(--clearwhite)}.delete{background-color:red}@media screen and (min-width:1000px){#wrapper,body{font-size:.75rem;margin:0 auto;max-width:1400px;padding:0 40px;width:90%}.articles article p,.form_element{font-size:105%}}
2
+ /* source-hash: 322c19466bcee8ad0e0b817e72b3cc495e1ebbf84398d7c346ab1fc8ec65f0b9 */
@@ -1 +0,0 @@
1
- {"id":1,"title":"Test","content":"Content","createdAt":1735689600000}