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