@lexho111/plainblog 0.2.7 → 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, {
@@ -150,11 +174,11 @@ export default class Blog {
150
174
  const dbTitle = await this.#databaseModel.getBlogTitle();
151
175
  const dbArticles = await this.#databaseModel.findAll();
152
176
  //console.log(`articles: ${JSON.stringify(dbarticles)}`)
153
- this.reloadScriptsStylesOnGET = true;
177
+ this.reloadScriptsStylesOnGET = false;
154
178
  if(this.reloadScriptsStylesOnGET) console.log("reload scripts and styles on GET-Request");
155
179
  let title = "";
156
- if (this.title.length > 0) title = this.title; // use blog title if set
157
- 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
158
182
  const responseData = { title: title, articles: dbArticles };
159
183
  this.#applyBlogData(responseData);
160
184
  }
@@ -301,7 +325,7 @@ export default class Blog {
301
325
  /** Populates the blog's title and articles from a data object. */
302
326
  #applyBlogData(data) {
303
327
  this.articles = []; // Clear existing articles before loading new ones
304
- this.setTitle(data.title);
328
+ this.title = data.title;
305
329
  // Assuming the API returns an array of objects with title and content
306
330
  if (data.articles && Array.isArray(data.articles)) {
307
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,7 +19,8 @@ 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>
@@ -30,7 +36,7 @@ export function formatHTML(data, script, style) {
30
36
  ${data.login}
31
37
  </nav>
32
38
  <h1>${data.title}</h1>
33
- <div style="width: 500px;">
39
+ <div style="max-width: 500px; width: 100%;">
34
40
  ${form}
35
41
  <section class="grid">
36
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.7",
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",