Przeglądaj źródła

Meta support

* Add href and other meta for pages
* Clean resources state
* Add basic debug output
Craig Fletcher 5 miesięcy temu
rodzic
commit
615ea13928
4 zmienionych plików z 59 dodań i 23 usunięć
  1. 4 0
      src/index.js
  2. 11 1
      src/lib.js
  3. 36 22
      src/processors.js
  4. 8 0
      src/util.js

+ 4 - 0
src/index.js

@@ -10,10 +10,14 @@ const taskRunner = tasks.reduce(
   async (metaPromise, task) => {
     const meta = await metaPromise;
     console.log("Processing task:", task.name)
+    if (meta.opts.debug) {
+      console.log(task.name, "in meta", JSON.stringify(meta, null, 2));
+    }
     const processedPaths = await processFiles(task, meta)
     console.log(
       `Processed ${processedPaths.length} paths for task ${task.name}`,
     )
+
     return {
       ...meta,
       resources: {

+ 11 - 1
src/lib.js

@@ -42,6 +42,10 @@ export async function processFiles(config, meta) {
           config.outputFileExtension
         )
       );
+      if (meta.opts.debug) {
+        console.log("in filePath", filePath);
+        console.log("out fileOutputPath", fileOutputPath);
+      }
       const fileOutputDir = path.dirname(fileOutputPath);
       if (!fs.existsSync(fileOutputDir)) {
         fs.mkdirSync(fileOutputDir, { recursive: true });
@@ -50,12 +54,18 @@ export async function processFiles(config, meta) {
       const { result, detail, written } = await config.processor(
         filePath,
         meta,
-        fileOutputDir
+        fileOutputDir,
+        fileOutputPath
       );
       if (!written) {
         await writeFile(fileOutputPath, result);
       }
 
+      if (meta.opts.debug) {
+        console.log(filePath, "out detail", detail);
+        console.log(filePath, "out result", result);
+      }
+
       return {
         detail,
         path: written ? result : fileOutputPath.replace(meta.opts.baseDir, "/")

+ 36 - 22
src/processors.js

@@ -1,5 +1,10 @@
 import * as sass from "sass";
-import { firstFound, stripFileExtension, getCleanPath } from "./util.js";
+import {
+  firstFound,
+  stripFileExtension,
+  getCleanPath,
+  getHref
+} from "./util.js";
 import fs from "fs/promises";
 import handlebars from "handlebars";
 import { marked } from "marked";
@@ -32,20 +37,20 @@ function createMarkdownRenderer(meta) {
           const hrefWithoutExt = stripFileExtension(href);
           const attrs = [`alt="${text}"`];
 
-          const foundSrcSet = meta.resources.images.find(({ path }) => {
-            return stripFileExtension(path[0]) === hrefWithoutExt;
+          const foundSrcSet = meta.resources.images.find(({ detail }) => {
+            return detail.imageRef === href;
           });
 
           if (foundSrcSet) {
-            const srcSetString = foundSrcSet.path[1]
+            const srcSetString = foundSrcSet.detail.srcSet
               .map(src => src.join(" "))
               .join(", ");
-            const defaultSrc = foundSrcSet.path[1][0][0];
+            const defaultSrc = foundSrcSet.detail.srcSet[0][0];
             attrs.push(`src="${defaultSrc}"`);
             attrs.push(`srcset="${srcSetString}"`);
             attrs.push(`sizes="(min-width: 800px) 40vw, 100vw"`);
             attrs.push(
-              `style="aspect-ratio: ${foundSrcSet.path[2].aspectRatio}"`
+              `style="aspect-ratio: ${foundSrcSet.detail.aspectRatio}"`
             );
           } else {
             attrs.push(`src="${href}"`);
@@ -61,10 +66,16 @@ function createMarkdownRenderer(meta) {
     });
 }
 
-export async function renderMarkdownWithTemplate(filePath, meta) {
+export async function renderMarkdownWithTemplate(
+  filePath,
+  meta,
+  fileOutputDir,
+  fileOutputPath
+) {
   const content = await fs.readFile(filePath, "utf8");
   const { data, content: markdown } = matter(content);
   const templateName = data.template || meta.opts.defaultTemplate;
+  const href = getHref(fileOutputPath, meta);
 
   if (!templateCache.has(templateName)) {
     const templatePath = firstFound(
@@ -81,8 +92,10 @@ export async function renderMarkdownWithTemplate(filePath, meta) {
   const html = template({
     ...data,
     ...meta,
+    href,
     content: renderer(markdown)
   });
+  console.log("====>", data, meta, href);
   const minifiedHtml = await minify(html, {
     collapseWhitespace: true,
     removeComments: true,
@@ -92,7 +105,7 @@ export async function renderMarkdownWithTemplate(filePath, meta) {
     minifyJS: true
   });
   return {
-    detail: data,
+    detail: { ...data, href },
     result: minifiedHtml
   };
 }
@@ -115,7 +128,7 @@ export async function copy(filePath) {
   return { result: fileContent };
 }
 
-export async function optimiseImage(filePath, meta, fileOutputPath) {
+export async function optimiseImage(filePath, meta, fileOutputDir) {
   const sourceExtension = path.extname(filePath);
   const outputExtension = ".webp";
   const base = path.basename(filePath, sourceExtension);
@@ -130,11 +143,11 @@ export async function optimiseImage(filePath, meta, fileOutputPath) {
 
   const aspectRatio = width / height;
 
-  const srcset = await Promise.all(
+  const srcSet = await Promise.all(
     imageSizes.map(async size => {
       const sizeNum = parseInt(size.replace("w", ""), 10);
       const outputFile = path.join(
-        fileOutputPath,
+        fileOutputDir,
         `${base}-${sizeNum}${outputExtension}`
       );
 
@@ -151,15 +164,16 @@ export async function optimiseImage(filePath, meta, fileOutputPath) {
   const imageRef = getCleanPath(path.join(filePath), meta);
 
   return {
-    result: [imageRef, srcset, { aspectRatio }],
+    result: srcSet.map(src => src[0]),
+    detail: { imageRef, srcSet, aspectRatio },
     written: true
   };
 }
 
-export async function generateFavicons(filePath, meta, fileOutputPath) {
+export async function generateFavicons(filePath, meta, fileOutputDir) {
   // Configuration for favicons package
   const configuration = {
-    path: getCleanPath(fileOutputPath, meta), // Path for overriding default icons path
+    path: getCleanPath(fileOutputDir, meta), // Path for overriding default icons path
     appName: meta.opts.site?.name || "Website",
     appShortName: meta.opts.site?.shortName || "Site",
     appDescription: meta.opts.site?.description || "",
@@ -194,7 +208,7 @@ export async function generateFavicons(filePath, meta, fileOutputPath) {
     // Write all generated images to disk
     await Promise.all(
       response.images.map(async image => {
-        const outputPath = path.join(fileOutputPath, image.name);
+        const outputPath = path.join(fileOutputDir, image.name);
         await fs.writeFile(outputPath, image.contents);
       })
     );
@@ -202,7 +216,7 @@ export async function generateFavicons(filePath, meta, fileOutputPath) {
     // Write all generated files (manifests, etc.) to disk
     await Promise.all(
       response.files.map(async file => {
-        const outputPath = path.join(fileOutputPath, file.name);
+        const outputPath = path.join(fileOutputDir, file.name);
         await fs.writeFile(outputPath, file.contents);
       })
     );
@@ -211,14 +225,14 @@ export async function generateFavicons(filePath, meta, fileOutputPath) {
     const htmlMeta = response.html.join("\n    ");
     return {
       detail: { htmlMeta },
-      result: {
-        images: response.images.map(img =>
-          getCleanPath(path.join(fileOutputPath, img.name), meta)
+      result: [
+        ...response.images.map(img =>
+          getCleanPath(path.join(fileOutputDir, img.name), meta)
         ),
-        files: response.files.map(file =>
-          getCleanPath(path.join(fileOutputPath, file.name), meta)
+        ...response.files.map(file =>
+          getCleanPath(path.join(fileOutputDir, file.name), meta)
         )
-      },
+      ],
       written: true
     };
   } catch (error) {

+ 8 - 0
src/util.js

@@ -48,3 +48,11 @@ export function stripFileExtension(filePath) {
 export function getCleanPath(filePath, meta) {
   return filePath.replace(meta.opts.runDir, "").replace(meta.opts.baseDir, "/");
 }
+
+export function getHref(filePath, meta) {
+  const route = getCleanPath(filePath, meta);
+  if (route.includes("index.html")) {
+    return route.replace("index.html", "");
+  }
+  return route.replace(".html", "");
+}