@static-pages/core 7.0.0-alpha.1 → 7.0.0-alpha.3

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;
@@ -7,7 +7,6 @@ exports.createReader = void 0;
7
7
  const picomatch_1 = __importDefault(require("picomatch"));
8
8
  const autoparse_js_1 = require("./autoparse.js");
9
9
  const helpers_js_1 = require("./helpers.js");
10
- const node_path_1 = require("node:path");
11
10
  const node_fs_1 = __importDefault(require("node:fs"));
12
11
  async function* createReader({ fs = node_fs_1.default, cwd = 'pages', pattern, ignore, parse = autoparse_js_1.autoparse, onError = (error) => { throw error; }, } = {}) {
13
12
  if (!(0, helpers_js_1.isFilesystem)(fs))
@@ -25,13 +24,23 @@ async function* createReader({ fs = node_fs_1.default, cwd = 'pages', pattern, i
25
24
  if (typeof onError !== 'function')
26
25
  throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(onError)}' at 'onError' property.`);
27
26
  let filenames = await new Promise((resolve, reject) => {
28
- fs.readdir(cwd, { encoding: 'utf8', recursive: true, withFileTypes: true }, (err, files) => {
27
+ fs.readdir(cwd, { recursive: true, withFileTypes: false, encoding: 'utf8' }, (err, entries) => {
29
28
  if (err)
30
- reject(err);
31
- else
32
- resolve(files
33
- .filter(x => x.isFile())
34
- .map(x => (0, node_path_1.join)((0, node_path_1.relative)(cwd, x.path), x.name)));
29
+ return reject(err);
30
+ let filtered = [];
31
+ let processed = 0;
32
+ for (const entry of entries) {
33
+ fs.stat(cwd + '/' + entry, (err, stats) => {
34
+ if (err)
35
+ return reject(err);
36
+ if (stats.isFile()) {
37
+ filtered.push(entry);
38
+ }
39
+ if (++processed === entries.length) {
40
+ return resolve(filtered);
41
+ }
42
+ });
43
+ }
35
44
  });
36
45
  });
37
46
  if (typeof pattern !== 'undefined' || typeof ignore !== 'undefined') {
@@ -47,7 +56,7 @@ async function* createReader({ fs = node_fs_1.default, cwd = 'pages', pattern, i
47
56
  for (const filename of filenames) {
48
57
  try {
49
58
  const content = await new Promise((resolve, reject) => {
50
- fs.readFile((0, node_path_1.join)(cwd, filename), (err, data) => {
59
+ fs.readFile(cwd + '/' + filename, (err, data) => {
51
60
  if (err)
52
61
  reject(err);
53
62
  else
@@ -25,8 +25,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.createWriter = void 0;
27
27
  const helpers_js_1 = require("./helpers.js");
28
- const node_path_1 = require("node:path");
29
28
  const nodeFs = __importStar(require("node:fs"));
29
+ const node_path_1 = require("node:path");
30
30
  const defaultNamer = (data) => {
31
31
  if (!!data && typeof data === 'object' && 'url' in data && typeof data.url === 'string') {
32
32
  return data.url.concat('.html');
package/cjs/helpers.d.ts CHANGED
@@ -1,26 +1,14 @@
1
1
  export type MaybePromise<T> = T | Promise<T>;
2
- interface Stats {
3
- isFile(): boolean;
4
- isDirectory(): boolean;
5
- }
6
- interface Dirent {
7
- name: string;
8
- path: string;
9
- isFile(): boolean;
10
- isDirectory(): boolean;
11
- }
12
2
  export interface Filesystem {
3
+ stat(path: string | URL, callback: (err: Error | null, stats: {
4
+ isFile(): boolean;
5
+ isDirectory(): boolean;
6
+ }) => void): void;
13
7
  readdir(path: string | URL, options: {
14
8
  encoding: 'utf8';
15
9
  withFileTypes: false;
16
10
  recursive: boolean;
17
11
  }, callback: (err: Error | null, files: string[]) => void): void;
18
- readdir(path: string | URL, options: {
19
- encoding: 'utf8';
20
- withFileTypes: true;
21
- recursive: boolean;
22
- }, callback: (err: Error | null, files: Dirent[]) => void): void;
23
- stat(path: string | URL, callback: (err: Error | null, stats: Stats) => void): void;
24
12
  mkdir(path: string | URL, options: {
25
13
  recursive: true;
26
14
  }, callback: (err: Error | null, path?: string) => void): void;
@@ -31,4 +19,3 @@ export declare const isFilesystem: (x: unknown) => x is Filesystem;
31
19
  export declare const isIterable: <T>(x: unknown) => x is Iterable<T>;
32
20
  export declare const isAsyncIterable: <T>(x: unknown) => x is AsyncIterable<T>;
33
21
  export declare const getType: (x: unknown) => string;
34
- export {};
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
  }
@@ -1,7 +1,6 @@
1
1
  import picomatch from 'picomatch';
2
2
  import { autoparse } from './autoparse.js';
3
3
  import { getType, isIterable, isAsyncIterable, isFilesystem } from './helpers.js';
4
- import { join, relative } from 'node:path';
5
4
  import nodeFs from 'node:fs';
6
5
  export async function* createReader({ fs = nodeFs, cwd = 'pages', pattern, ignore, parse = autoparse, onError = (error) => { throw error; }, } = {}) {
7
6
  if (!isFilesystem(fs))
@@ -19,13 +18,23 @@ export async function* createReader({ fs = nodeFs, cwd = 'pages', pattern, ignor
19
18
  if (typeof onError !== 'function')
20
19
  throw new TypeError(`Expected 'function', recieved '${getType(onError)}' at 'onError' property.`);
21
20
  let filenames = await new Promise((resolve, reject) => {
22
- fs.readdir(cwd, { encoding: 'utf8', recursive: true, withFileTypes: true }, (err, files) => {
21
+ fs.readdir(cwd, { recursive: true, withFileTypes: false, encoding: 'utf8' }, (err, entries) => {
23
22
  if (err)
24
- reject(err);
25
- else
26
- resolve(files
27
- .filter(x => x.isFile())
28
- .map(x => join(relative(cwd, x.path), x.name)));
23
+ return reject(err);
24
+ let filtered = [];
25
+ let processed = 0;
26
+ for (const entry of entries) {
27
+ fs.stat(cwd + '/' + entry, (err, stats) => {
28
+ if (err)
29
+ return reject(err);
30
+ if (stats.isFile()) {
31
+ filtered.push(entry);
32
+ }
33
+ if (++processed === entries.length) {
34
+ return resolve(filtered);
35
+ }
36
+ });
37
+ }
29
38
  });
30
39
  });
31
40
  if (typeof pattern !== 'undefined' || typeof ignore !== 'undefined') {
@@ -41,7 +50,7 @@ export async function* createReader({ fs = nodeFs, cwd = 'pages', pattern, ignor
41
50
  for (const filename of filenames) {
42
51
  try {
43
52
  const content = await new Promise((resolve, reject) => {
44
- fs.readFile(join(cwd, filename), (err, data) => {
53
+ fs.readFile(cwd + '/' + filename, (err, data) => {
45
54
  if (err)
46
55
  reject(err);
47
56
  else
@@ -1,6 +1,6 @@
1
1
  import { getType, isIterable, isAsyncIterable, isFilesystem } from './helpers.js';
2
- import { dirname } from 'node:path';
3
2
  import * as nodeFs from 'node:fs';
3
+ import { dirname } from 'node:path';
4
4
  const defaultNamer = (data) => {
5
5
  if (!!data && typeof data === 'object' && 'url' in data && typeof data.url === 'string') {
6
6
  return data.url.concat('.html');
package/esm/helpers.d.ts CHANGED
@@ -1,26 +1,14 @@
1
1
  export type MaybePromise<T> = T | Promise<T>;
2
- interface Stats {
3
- isFile(): boolean;
4
- isDirectory(): boolean;
5
- }
6
- interface Dirent {
7
- name: string;
8
- path: string;
9
- isFile(): boolean;
10
- isDirectory(): boolean;
11
- }
12
2
  export interface Filesystem {
3
+ stat(path: string | URL, callback: (err: Error | null, stats: {
4
+ isFile(): boolean;
5
+ isDirectory(): boolean;
6
+ }) => void): void;
13
7
  readdir(path: string | URL, options: {
14
8
  encoding: 'utf8';
15
9
  withFileTypes: false;
16
10
  recursive: boolean;
17
11
  }, callback: (err: Error | null, files: string[]) => void): void;
18
- readdir(path: string | URL, options: {
19
- encoding: 'utf8';
20
- withFileTypes: true;
21
- recursive: boolean;
22
- }, callback: (err: Error | null, files: Dirent[]) => void): void;
23
- stat(path: string | URL, callback: (err: Error | null, stats: Stats) => void): void;
24
12
  mkdir(path: string | URL, options: {
25
13
  recursive: true;
26
14
  }, callback: (err: Error | null, path?: string) => void): void;
@@ -31,4 +19,3 @@ export declare const isFilesystem: (x: unknown) => x is Filesystem;
31
19
  export declare const isIterable: <T>(x: unknown) => x is Iterable<T>;
32
20
  export declare const isAsyncIterable: <T>(x: unknown) => x is AsyncIterable<T>;
33
21
  export declare const getType: (x: unknown) => string;
34
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@static-pages/core",
3
- "version": "7.0.0-alpha.1",
3
+ "version": "7.0.0-alpha.3",
4
4
  "description": "General purpose static pages renderer.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.js",