mikel-press 0.30.0 → 0.30.1

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.
Files changed (3) hide show
  1. package/README.md +14 -0
  2. package/index.js +23 -12
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -98,6 +98,7 @@ Each HTML file processed by **mikel-press** will be handled by the mikel templat
98
98
  | Variable | Description |
99
99
  |----------|-------------|
100
100
  | `site.pages` | A list containing all pages processed by **mikel-press**. |
101
+ | `site.assets` | A list containing all assets files loaded by the `AssetsPlugin`. |
101
102
  | `site.data` | An object containing all data items loaded by `DataPlugin`. |
102
103
  | `site.partials` | A list containing all partials files loaded by the `PartialsPlugin`. |
103
104
  | `site.layouts` | A list containing all layout files loaded by the `LayoutsPlugin`. |
@@ -110,9 +111,22 @@ Each HTML file processed by **mikel-press** will be handled by the mikel templat
110
111
  | `page.content` | The raw content of the page before begin processed by **mikel**. |
111
112
  | `page.title` | The title of the page. |
112
113
  | `page.path` | The path to the page. Example: `about/index.html`. |
114
+ | `page.dir` | The directory of the page. Example: `about`. |
115
+ | `page.name` | The name of the page without extension. Example: `index`. |
116
+ | `page.ext` | The file extension of the page. Example: `.html`. |
113
117
  | `page.url` | The path to the page including the leading `/`. Example: `/about/index.html`. |
114
118
  | `page.attributes` | An object containing all the frontmatter variables in the page processed by `FrontmatterPlugin`. |
115
119
 
120
+ #### Asset variables
121
+
122
+ | Variable | Description |
123
+ |----------|-------------|
124
+ | `asset.path` | The path to the asset file. Example: `images/logo.png`. |
125
+ | `asset.dir` | The directory of the asset file. Example: `images`. |
126
+ | `asset.name` | The name of the asset file without extension. Example: `logo`. |
127
+ | `asset.ext` | The file extension of the asset file. Example: `.png`. |
128
+ | `asset.url` | The path to the asset file including the leading `/`. Example: `/images/logo.png`. |
129
+
116
130
  ## Plugins
117
131
 
118
132
  **mikel-press** relies on plugins to handle file reading, transformation, and rendering. The following plugins are built-in:
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as path from "node:path";
3
+ import * as crypto from "node:crypto";
3
4
 
4
5
  // @description internal method to get the first node that ends with the provided string
5
6
  const getNodeFromSource = (nodes = [], endingStr = "") => {
@@ -162,9 +163,17 @@ press.watchContext = (context, options = {}) => {
162
163
 
163
164
  // @description general utilities
164
165
  press.utils = {
165
- // @description normalize a path
166
- normalizePath: (rawPath) => {
167
- return path.normalize("/" + rawPath);
166
+ // @description generate the md5 hash of the given content
167
+ // @param {String} content - content to hash
168
+ // @returns {String} md5 hash
169
+ md5: content => {
170
+ return crypto.createHash("md5").update(content).digest("hex");
171
+ },
172
+ // @description generate a random identifier
173
+ // @param {Number} length - length of the identifier
174
+ // @returns {String} random identifier
175
+ randomId: (length = 20) => {
176
+ return crypto.randomBytes(Math.ceil(length / 2)).toString("hex").slice(0, length);
168
177
  },
169
178
  // @description read a file from disk
170
179
  // @param {String} file path to the file to read
@@ -268,7 +277,6 @@ press.SourcePlugin = (options = {}) => {
268
277
  source: path.join(folder, file),
269
278
  label: options.label || press.LABEL_PAGE,
270
279
  path: path.join(options?.basePath || ".", file),
271
- url: press.utils.normalizePath(path.join(options?.basePath || ".", file)),
272
280
  };
273
281
  });
274
282
  },
@@ -342,10 +350,7 @@ press.FrontmatterPlugin = () => {
342
350
  node.content = result.body || "";
343
351
  node.attributes = result.attributes || {};
344
352
  node.title = node.attributes?.title || node.path;
345
- if (node.attributes.permalink) {
346
- node.path = node.attributes.permalink;
347
- node.url = press.utils.normalizePath(node.path);
348
- }
353
+ node.path = node.attributes?.permalink || node.path;
349
354
  }
350
355
  },
351
356
  };
@@ -393,6 +398,15 @@ press.ContentPagePlugin = (siteData = {}) => {
393
398
  }
394
399
  });
395
400
  }
401
+ // 5. fix pages and assets variables
402
+ [...siteData.pages, ...siteData.assets].forEach(node => {
403
+ return Object.assign(node, {
404
+ ext: path.extname(node.path),
405
+ name: path.basename(node.path, path.extname(node.path)),
406
+ dir: path.dirname(node.path),
407
+ url: path.normalize(path.join("/", node.path)),
408
+ });
409
+ });
396
410
  },
397
411
  transform: (context, node) => {
398
412
  if (node.label === press.LABEL_PAGE && typeof node.content === "string") {
@@ -419,11 +433,9 @@ press.CopyAssetsPlugin = (options = {}) => ({
419
433
  return item.from && fs.existsSync(path.resolve(item.from));
420
434
  });
421
435
  return filesToCopy.map(item => {
422
- const filePath = path.join(options?.basePath || ".", item.to || path.basename(item.from));
423
436
  return {
424
437
  source: path.resolve(item.from),
425
- path: filePath,
426
- url: press.utils.normalizePath(filePath),
438
+ path: path.join(options?.basePath || ".", item.to || path.basename(item.from)),
427
439
  label: options?.label || press.LABEL_ASSET,
428
440
  };
429
441
  });
@@ -437,7 +449,6 @@ press.RedirectsPlugin = (options = {}) => ({
437
449
  return (options.redirects || []).map(redirection => ({
438
450
  source: redirection.from,
439
451
  path: path.join(options?.basePath || ".", redirection.from),
440
- url: press.utils.normalizePath(path.join(options?.basePath || ".", redirection.from)),
441
452
  label: press.LABEL_ASSET,
442
453
  content: generateRedirectHTML(redirection.to),
443
454
  }));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mikel-press",
3
3
  "description": "A tiny and fast static site generator based on mikel templating",
4
- "version": "0.30.0",
4
+ "version": "0.30.1",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {