@static-pages/core 7.0.0-alpha.0 → 7.0.0-alpha.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/cjs/autoparse.js CHANGED
@@ -7,18 +7,31 @@ exports.autoparse = void 0;
7
7
  const yaml_1 = require("yaml");
8
8
  const gray_matter_1 = __importDefault(require("gray-matter"));
9
9
  function autoparse(content, filename) {
10
- const extension = filename.split('.').pop().toLowerCase();
10
+ const dot = filename.lastIndexOf('.');
11
+ if (dot < 1) {
12
+ throw new Error(`Could not parse document without an extension.`);
13
+ }
14
+ const extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
15
+ let doc;
11
16
  switch (extension) {
12
17
  case 'json':
13
- return JSON.parse(content.toString());
18
+ doc = JSON.parse(content.toString());
19
+ break;
14
20
  case 'yaml':
15
21
  case 'yml':
16
- return (0, yaml_1.parse)(content.toString());
22
+ doc = (0, yaml_1.parse)(content.toString());
23
+ break;
17
24
  case 'md':
18
25
  case 'markdown':
19
26
  const { data, content: markdownContent } = (0, gray_matter_1.default)(content.toString());
20
- return { ...data, content: markdownContent };
27
+ doc = { ...data, content: markdownContent };
28
+ break;
29
+ default:
30
+ throw new Error(`Could not parse document with '${extension}' extension.`);
31
+ }
32
+ if (doc && typeof doc === 'object' && !('url' in doc)) {
33
+ doc.url = filename.substring(0, dot).replace(/\\/g, '/');
21
34
  }
22
- throw new Error(`Could not parse document with '${extension}' extension.`);
35
+ return doc;
23
36
  }
24
37
  exports.autoparse = autoparse;
@@ -47,7 +47,7 @@ async function* createReader({ fs = node_fs_1.default, cwd = 'pages', pattern, i
47
47
  for (const filename of filenames) {
48
48
  try {
49
49
  const content = await new Promise((resolve, reject) => {
50
- fs.readFile((0, node_path_1.join)(cwd, filename), null, (err, data) => {
50
+ fs.readFile((0, node_path_1.join)(cwd, filename), (err, data) => {
51
51
  if (err)
52
52
  reject(err);
53
53
  else
package/cjs/helpers.d.ts CHANGED
@@ -20,14 +20,11 @@ export interface Filesystem {
20
20
  withFileTypes: true;
21
21
  recursive: boolean;
22
22
  }, callback: (err: Error | null, files: Dirent[]) => void): void;
23
- readFile(path: string | URL, options: {
24
- encoding: 'utf8';
25
- }, callback: (err: Error | null, data: string) => void): void;
26
- readFile(path: string | URL, options: null, callback: (err: Error | null, data: Uint8Array) => void): void;
27
23
  stat(path: string | URL, callback: (err: Error | null, stats: Stats) => void): void;
28
24
  mkdir(path: string | URL, options: {
29
25
  recursive: true;
30
26
  }, callback: (err: Error | null, path?: string) => void): void;
27
+ readFile(path: string | URL, callback: (err: Error | null, data: Uint8Array) => void): void;
31
28
  writeFile(path: string | URL, data: string | Uint8Array, callback: (err: Error | null) => void): void;
32
29
  }
33
30
  export declare const isFilesystem: (x: unknown) => x is Filesystem;
package/esm/autoparse.js CHANGED
@@ -1,17 +1,30 @@
1
1
  import { parse as parseYAML } from 'yaml';
2
2
  import parseMarkdown from 'gray-matter';
3
3
  export function autoparse(content, filename) {
4
- const extension = filename.split('.').pop().toLowerCase();
4
+ const dot = filename.lastIndexOf('.');
5
+ if (dot < 1) {
6
+ throw new Error(`Could not parse document without an extension.`);
7
+ }
8
+ const extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
9
+ let doc;
5
10
  switch (extension) {
6
11
  case 'json':
7
- return JSON.parse(content.toString());
12
+ doc = JSON.parse(content.toString());
13
+ break;
8
14
  case 'yaml':
9
15
  case 'yml':
10
- return parseYAML(content.toString());
16
+ doc = parseYAML(content.toString());
17
+ break;
11
18
  case 'md':
12
19
  case 'markdown':
13
20
  const { data, content: markdownContent } = parseMarkdown(content.toString());
14
- return { ...data, content: markdownContent };
21
+ doc = { ...data, content: markdownContent };
22
+ break;
23
+ default:
24
+ throw new Error(`Could not parse document with '${extension}' extension.`);
25
+ }
26
+ if (doc && typeof doc === 'object' && !('url' in doc)) {
27
+ doc.url = filename.substring(0, dot).replace(/\\/g, '/');
15
28
  }
16
- throw new Error(`Could not parse document with '${extension}' extension.`);
29
+ return doc;
17
30
  }
@@ -41,7 +41,7 @@ export async function* createReader({ fs = nodeFs, cwd = 'pages', pattern, ignor
41
41
  for (const filename of filenames) {
42
42
  try {
43
43
  const content = await new Promise((resolve, reject) => {
44
- fs.readFile(join(cwd, filename), null, (err, data) => {
44
+ fs.readFile(join(cwd, filename), (err, data) => {
45
45
  if (err)
46
46
  reject(err);
47
47
  else
package/esm/helpers.d.ts CHANGED
@@ -20,14 +20,11 @@ export interface Filesystem {
20
20
  withFileTypes: true;
21
21
  recursive: boolean;
22
22
  }, callback: (err: Error | null, files: Dirent[]) => void): void;
23
- readFile(path: string | URL, options: {
24
- encoding: 'utf8';
25
- }, callback: (err: Error | null, data: string) => void): void;
26
- readFile(path: string | URL, options: null, callback: (err: Error | null, data: Uint8Array) => void): void;
27
23
  stat(path: string | URL, callback: (err: Error | null, stats: Stats) => void): void;
28
24
  mkdir(path: string | URL, options: {
29
25
  recursive: true;
30
26
  }, callback: (err: Error | null, path?: string) => void): void;
27
+ readFile(path: string | URL, callback: (err: Error | null, data: Uint8Array) => void): void;
31
28
  writeFile(path: string | URL, data: string | Uint8Array, callback: (err: Error | null) => void): void;
32
29
  }
33
30
  export declare const isFilesystem: (x: unknown) => x is Filesystem;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@static-pages/core",
3
- "version": "7.0.0-alpha.0",
3
+ "version": "7.0.0-alpha.2",
4
4
  "description": "General purpose static pages renderer.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.js",