@lexho111/plainblog 0.2.6 → 0.2.8

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/Blog.js CHANGED
@@ -22,6 +22,8 @@ export default class Blog {
22
22
  this.styles = "";
23
23
  this.filename = null;
24
24
  this.#server = null;
25
+ this.#password = "admin";
26
+ this.styles = "body { font-family: Arial; }";
25
27
 
26
28
  this.database = {
27
29
  type: "sqlite",
@@ -37,8 +39,7 @@ export default class Blog {
37
39
 
38
40
  this.loadScripts();
39
41
 
40
- if(this.styles.length == 0) { // no style override specified via index.js
41
- this.styles = "body { font-family: Arial; }"; // apply default style
42
+ if(this.styles == "body { font-family: Arial; }") { // no style override specified via index.js
42
43
  this.loadStyles();
43
44
  }
44
45
  //console.log(styles)
@@ -62,7 +63,7 @@ export default class Blog {
62
63
  const __filename = fileURLToPath(import.meta.url);
63
64
  const __dirname = path.dirname(__filename);
64
65
  try {
65
- this.styles = fs.readFileSync(
66
+ this.styles += fs.readFileSync(
66
67
  path.join(__dirname, "styles.min.css"), "utf-8");
67
68
  } catch(err) {
68
69
  console.error("no styles.min.css file found")
@@ -71,21 +72,44 @@ export default class Blog {
71
72
 
72
73
  // Private fields
73
74
  #server = null;
75
+ #password = null;
74
76
  #databaseModel;
75
77
  #isExternalAPI = false;
76
78
  #apiUrl = "";
79
+ #title = "";
77
80
 
78
81
  setTitle(title) {
79
- this.title = title;
82
+ this.#title = title;
80
83
  }
81
84
 
82
- addArticle(article) {
83
- this.articles.push(article);
85
+ setPassword(password) {
86
+ this.#password = password;
84
87
  }
85
88
 
86
89
  setStyle(style) {
87
- this.styles = style;
90
+ this.styles += style;
91
+ }
92
+
93
+ set title(t) {
94
+ this.#title = t;
95
+ }
96
+
97
+ get title() {
98
+ return this.#title;
99
+ }
100
+
101
+ set password(x) {
102
+ this.#password = x;
88
103
  }
104
+
105
+ set style(style) {
106
+ this.styles += style;
107
+ }
108
+
109
+ addArticle(article) {
110
+ this.articles.push(article);
111
+ }
112
+
89
113
  isAuthenticated(req) {
90
114
  if (!req.headers.cookie) return false;
91
115
  const params = new URLSearchParams(req.headers.cookie.replace(/; /g, "&"));
@@ -103,7 +127,7 @@ export default class Blog {
103
127
  const body = await firstValueFrom(body$);
104
128
  const params = new URLSearchParams(body);
105
129
 
106
- if (params.get("password") === "admin") {
130
+ if (params.get("password") === this.#password) {
107
131
  const id = crypto.randomUUID();
108
132
  this.sessions.add(id);
109
133
  res.writeHead(303, {
@@ -113,9 +137,12 @@ export default class Blog {
113
137
  res.end();
114
138
  } else {
115
139
  res.writeHead(401, { "Content-Type": "text/html" });
116
- res.end(`<style>${this.styles}</style><h1>Unauthorized</h1><p>Please enter the password.<form method="POST">
117
- <input type="password" name="password" placeholder="Password" />
118
- <button style="margin: 2px;">Login</button></form>`);
140
+ res.end(`<html><head><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>${this.styles}</style></head>
141
+ <body>
142
+ <h1>Unauthorized</h1><p>Please enter the password.<form method="POST">
143
+ <input type="password" name="password" placeholder="Password" />
144
+ <button style="margin: 2px;">Login</button></form>
145
+ </body></html>`);
119
146
  }
120
147
  }
121
148
 
@@ -147,11 +174,11 @@ export default class Blog {
147
174
  const dbTitle = await this.#databaseModel.getBlogTitle();
148
175
  const dbArticles = await this.#databaseModel.findAll();
149
176
  //console.log(`articles: ${JSON.stringify(dbarticles)}`)
150
- this.reloadScriptsStylesOnGET = true;
177
+ this.reloadScriptsStylesOnGET = false;
151
178
  if(this.reloadScriptsStylesOnGET) console.log("reload scripts and styles on GET-Request");
152
179
  let title = "";
153
- if (this.title.length > 0) title = this.title; // use blog title if set
154
- else title = dbTitle;
180
+ if (this.#title != null && this.#title.length > 0) title = this.#title; // use blog title if set
181
+ else title = dbTitle; // use title from the database
155
182
  const responseData = { title: title, articles: dbArticles };
156
183
  this.#applyBlogData(responseData);
157
184
  }
@@ -214,9 +241,12 @@ export default class Blog {
214
241
  if (req.url === "/login") {
215
242
  if (req.method === "GET") {
216
243
  res.writeHead(200, { "Content-Type": "text/html" });
217
- res.end(`<style>${this.styles}</style><h1>Login</h1><form method="POST">
218
- <input type="password" name="password" placeholder="Password" />
219
- <button style="margin: 2px;">Login</button></form>`);
244
+ res.end(`<html><head><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>${this.styles}</style></head>
245
+ <body>
246
+ <h1>Login</h1><form method="POST">
247
+ <input type="password" name="password" placeholder="Password" />
248
+ <button style="margin: 2px;">Login</button></form>
249
+ </body></html>`);
220
250
  return;
221
251
  } else if (req.method === "POST") {
222
252
  await this.handleLogin(req, res);
@@ -295,7 +325,7 @@ export default class Blog {
295
325
  /** Populates the blog's title and articles from a data object. */
296
326
  #applyBlogData(data) {
297
327
  this.articles = []; // Clear existing articles before loading new ones
298
- this.setTitle(data.title);
328
+ this.title = data.title;
299
329
  // Assuming the API returns an array of objects with title and content
300
330
  if (data.articles && Array.isArray(data.articles)) {
301
331
  for (const articleData of data.articles) {
package/Formatter.js CHANGED
@@ -5,7 +5,12 @@ export function formatHTML(data, script, style) {
5
5
  const button = "";
6
6
  let form1 = "";
7
7
  if (data.loggedin) {
8
- form1 = `<form action="/" method="POST">
8
+ form1 = `<!DOCTYPE html>
9
+ <html lang="de">
10
+ <head>
11
+ <meta charset="UTF-8">
12
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
13
+ <form action="/" method="POST">
9
14
  <h3>Add a New Article</h3>
10
15
  <input type="text" id="title" name="title" placeholder="Article Title" required style="display: block; width: 300px; margin-bottom: 10px;">
11
16
  <textarea id="content" name="content" placeholder="Article Content" required style="display: block; width: 300px; height: 100px; margin-bottom: 10px;"></textarea>
@@ -14,13 +19,15 @@ export function formatHTML(data, script, style) {
14
19
  ${script}
15
20
  </script>
16
21
  </form>
17
- <hr>`;
22
+ <hr>
23
+ </body></html>`;
18
24
  }
19
25
  const form = form1;
20
26
  return `<!DOCTYPE html>
21
27
  <html lang="de">
22
28
  <head>
23
29
  <meta charset="UTF-8">
30
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
24
31
  <title>${data.title}</title>
25
32
  <style>${style}</style>
26
33
  </head>
@@ -29,7 +36,7 @@ export function formatHTML(data, script, style) {
29
36
  ${data.login}
30
37
  </nav>
31
38
  <h1>${data.title}</h1>
32
- <div style="width: 500px;">
39
+ <div style="max-width: 500px; width: 100%;">
33
40
  ${form}
34
41
  <section class="grid">
35
42
  ${data.articles.map((article) => article.toHTML()).join("")}
package/README.md CHANGED
@@ -14,8 +14,9 @@ npm install @lexho111/plainblog
14
14
  import Blog from "@lexho111/plainblog";
15
15
 
16
16
  const blog = new Blog();
17
- blog.setTitle("My Blog");
18
- blog.setStyle("body { font-family: Arial, sans-serif; } h1 { color: #333; }");
17
+ blog.title = "My Blog";
18
+ blog.style = "body { font-family: Arial, sans-serif; } h1 { color: #333; }";
19
+ blog.password = "mypassword"
19
20
 
20
21
  blog.startServer(8080);
21
22
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lexho111/plainblog",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "A tool for creating and serving a minimalist, single-page blog.",
5
5
  "main": "index.js",
6
6
  "type": "module",