azoxjs 0.1.0 → 0.3.0

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/core/routes.js CHANGED
@@ -5,6 +5,11 @@
5
5
  // pages/blog/index.azox → /blog → blog/index.html
6
6
  // pages/blog/first.azox → /blog/first → blog/first/index.html
7
7
  //
8
+ // A segment in brackets is a parameter, and the file is a template
9
+ // rather than a route of its own:
10
+ //
11
+ // pages/blog/[slug].azox → one page per entry the file declares
12
+ //
8
13
  // Emitting a directory with an index.html means clean URLs work on
9
14
  // any static host without rewrite rules, since serving index.html
10
15
  // for a directory is universal behaviour.
@@ -43,6 +48,38 @@ function walk(dir, pagesDir) {
43
48
  return found;
44
49
  }
45
50
 
51
+ // A bracketed segment names a parameter: [slug] matches one segment
52
+ // and binds it to `slug`.
53
+ const PARAM_SEGMENT = /^\[([A-Za-z_$][\w$]*)\]$/;
54
+
55
+ export function paramNames(segments) {
56
+ return segments.map((segment) => segment.match(PARAM_SEGMENT)?.[1]).filter(Boolean);
57
+ }
58
+
59
+ // Fills a template's bracketed segments from a set of parameter
60
+ // values, producing the concrete route that will be written.
61
+ export function resolveRoute(route, values) {
62
+ const segments = route.templateSegments.map((segment) => {
63
+ const name = segment.match(PARAM_SEGMENT)?.[1];
64
+ if (!name) return segment;
65
+ return String(values[name]);
66
+ });
67
+
68
+ const url = segments.length ? `/${segments.join('/')}` : '/';
69
+ const outputDir = segments.join('/');
70
+
71
+ return {
72
+ ...route,
73
+ params: values,
74
+ isTemplate: false,
75
+ name: segments.join('/'),
76
+ url,
77
+ htmlPath: outputDir ? `${outputDir}/index.html` : 'index.html',
78
+ assetPrefix: '../'.repeat(segments.length) || './',
79
+ outputDir,
80
+ };
81
+ }
82
+
46
83
  function describeRoute(pagesDir, sourcePath) {
47
84
  const relativePath = relative(pagesDir, sourcePath);
48
85
  const segments = relativePath.slice(0, -PAGE_EXTENSION.length).split(sep);
@@ -54,6 +91,8 @@ function describeRoute(pagesDir, sourcePath) {
54
91
  const url = routeSegments.length ? `/${routeSegments.join('/')}` : '/';
55
92
  const outputDir = routeSegments.join('/');
56
93
 
94
+ const params = paramNames(routeSegments);
95
+
57
96
  return {
58
97
  // The name used on the command line: `azox compile --page=blog/first`
59
98
  name: segments.join('/'),
@@ -64,6 +103,11 @@ function describeRoute(pagesDir, sourcePath) {
64
103
  // back out to reach the shared runtime at the build root.
65
104
  assetPrefix: '../'.repeat(routeSegments.length) || './',
66
105
  outputDir,
106
+ // A template is not a page: it stands in for however many the
107
+ // file declares, and is never written at this url.
108
+ isTemplate: params.length > 0,
109
+ paramNames: params,
110
+ templateSegments: routeSegments,
67
111
  };
68
112
  }
69
113
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azoxjs",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Azox Framework — The Sound of Future Web",
5
5
  "type": "module",
6
6
  "license": "MIT",