@rettangoli/vt 0.0.2-rc2 → 0.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rettangoli/vt",
3
- "version": "0.0.2-rc2",
3
+ "version": "0.0.3",
4
4
  "description": "Rettangoli Visual Testing",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -53,19 +53,28 @@ async function main(options) {
53
53
 
54
54
  // Check for local templates first, fallback to library templates
55
55
  const localTemplatesPath = join(vtPath, "templates");
56
+
56
57
  const defaultTemplatePath = existsSync(join(localTemplatesPath, "default.html"))
57
- ? join(localTemplatesPath, "default.html")
58
- : join(libraryTemplatesPath, "default.html");
59
-
58
+ ? join(localTemplatesPath, "default.html")
59
+ : join(libraryTemplatesPath, "default.html");
60
+
61
+ // Resolve index template path
60
62
  const indexTemplatePath = existsSync(join(localTemplatesPath, "index.html"))
61
- ? join(localTemplatesPath, "index.html")
62
- : join(libraryTemplatesPath, "index.html");
63
+ ? join(localTemplatesPath, "index.html")
64
+ : join(libraryTemplatesPath, "index.html");
65
+
66
+ // Build template configuration for per-file/section templates
67
+ const templateConfig = {
68
+ defaultTemplate: defaultTemplatePath,
69
+ vtPath: vtPath,
70
+ };
63
71
 
64
72
  // Generate HTML files
65
73
  const generatedFiles = await generateHtml(
66
74
  specsPath,
67
75
  defaultTemplatePath,
68
- candidatePath
76
+ candidatePath,
77
+ templateConfig
69
78
  );
70
79
 
71
80
  // Generate overview page with all files
@@ -83,7 +83,7 @@
83
83
  <iframe
84
84
  loading="lazy"
85
85
  width="100%"
86
- src="/candidate/{{ file.path }}"
86
+ src="/candidate/{{ file.path | remove_ext }}.html"
87
87
  frameborder="0"
88
88
  style="
89
89
  width: 1080px;
package/src/common.js CHANGED
@@ -47,6 +47,12 @@ engine.registerFilter("slug", (value) => {
47
47
  return value.toLowerCase().replace(/\s+/g, "-");
48
48
  });
49
49
 
50
+ // Add custom filter to remove file extension
51
+ engine.registerFilter("remove_ext", (value) => {
52
+ if (typeof value !== "string") return "";
53
+ return value.replace(/\.[^/.]+$/, "");
54
+ });
55
+
50
56
  /**
51
57
  * Get all files from a directory recursively
52
58
  */
@@ -102,24 +108,16 @@ function ensureDirectoryExists(dirPath) {
102
108
  mkdirSync(absolutePath, { recursive: true });
103
109
  }
104
110
  }
105
-
106
111
  /**
107
112
  * Main function to generate HTML files from specs
108
113
  */
109
- async function generateHtml(specsDir, templatePath, outputDir) {
114
+ async function generateHtml(specsDir, templatePath, outputDir, templateConfig) {
110
115
  try {
111
- // Initialize LiquidJS engine
112
-
113
- // Read template
114
- const templateContent = readFileSync(templatePath, "utf8");
115
-
116
- // Ensure output directory exists
116
+ const defaultTemplateContent = readFileSync(templatePath, "utf8");
117
117
  ensureDirectoryExists(outputDir);
118
118
 
119
- // Get all files from specs directory
120
119
  const allFiles = getAllFiles(specsDir);
121
120
 
122
- // Process each file
123
121
  const processedFiles = [];
124
122
  for (const filePath of allFiles) {
125
123
  const fileContent = readFileSync(filePath, "utf8");
@@ -130,31 +128,25 @@ async function generateHtml(specsDir, templatePath, outputDir) {
130
128
  theme: "slack-dark",
131
129
  });
132
130
 
133
- // Parse YAML frontmatter
134
131
  let frontMatterObj = null;
135
132
  if (frontMatter) {
136
- try {
137
- frontMatterObj = loadYaml(frontMatter);
138
- } catch (e) {
139
- console.error(`Error parsing frontmatter in ${filePath}:`, e.message);
140
- }
133
+ frontMatterObj = loadYaml(frontMatter);
141
134
  }
142
135
 
136
+ const relativePath = path.relative(specsDir, filePath);
137
+
138
+ let templateToUse = defaultTemplateContent;
139
+ const resolvedTemplatePath = frontMatterObj.template ?
140
+ join(templateConfig.vtPath, "templates", frontMatterObj.template) :
141
+ templateConfig.defaultTemplate;
142
+ templateToUse = readFileSync(resolvedTemplatePath, "utf8");
143
+
143
144
  // Render template
144
- let renderedContent = "";
145
- try {
146
- renderedContent = engine.parseAndRenderSync(templateContent, {
147
- content: content,
148
- frontMatter: frontMatterObj || {},
149
- });
150
- } catch (error) {
151
- console.error(`Error rendering template for ${filePath}:`, error);
152
- renderedContent = `<html><body><h1>Error rendering template</h1><p>${error.message}</p><pre>${content}</pre></body></html>`;
153
- }
145
+ const renderedContent = engine.parseAndRenderSync(templateToUse, {
146
+ content: content,
147
+ frontMatter: frontMatterObj || {},
148
+ });
154
149
 
155
- // Get relative path from specs directory
156
- const relativePath = path.relative(specsDir, filePath);
157
-
158
150
  // Save file
159
151
  const outputPath = join(outputDir, convertToHtmlExtension(relativePath));
160
152
  ensureDirectoryExists(dirname(outputPath));
@@ -174,8 +166,7 @@ async function generateHtml(specsDir, templatePath, outputDir) {
174
166
  console.log(`Successfully generated ${processedFiles.length} files`);
175
167
  return processedFiles;
176
168
  } catch (error) {
177
- console.error("Error generating HTML:", error);
178
- throw error;
169
+ throw new Error("Error generating HTML:", error);
179
170
  }
180
171
  }
181
172