Added image resizing

This commit is contained in:
2025-02-04 21:13:47 +01:00
parent 3e5daff525
commit 5f92f8f8fb
6 changed files with 221 additions and 11 deletions

View File

@@ -6,11 +6,14 @@ import {
} from "./toolchain-helpers.js";
import { toHtml } from "./markdown-parser.js";
import { IPluginBuilder, MenuManifest, Plugin } from "./typings.js";
import { path } from "@ffmpeg-installer/ffmpeg";
import ffmpeg from "fluent-ffmpeg";
type MarkdownLoaderManifestOptions = {
notesRootPath: string;
noteFileName: string;
assetsDirectoryName: string;
imageWidth?: number;
};
type MarkdownLoaderManifest = {
@@ -51,13 +54,27 @@ const createMarkdownLoaderManifest = ({
const copyAssetsToOutputDirectory = (
manifest: MarkdownLoaderManifest,
outputDirectory: string
outputDirectory: string,
options: MarkdownLoaderOptions
) => {
manifest.forEach((m) =>
m.assetFiles.forEach((asset) =>
copyFile(asset.path, `${outputDirectory}/${m.name}_${asset.name}`)
)
);
ffmpeg.setFfmpegPath(path);
manifest.forEach((m) => {
return m.assetFiles.forEach((asset) => {
const isImage = asset.path.match(/\.(png|jpe?g|gif|webp)$/);
if (isImage) {
console.log("Processing image", asset.path);
ffmpeg(asset.path)
.output(`${outputDirectory}/${m.name}_${asset.name}`)
.size(`${options.imageWidth ?? 200}x?`)
.run();
} else {
return copyFile(
asset.path,
`${outputDirectory}/${m.name}_${asset.name}`
);
}
});
});
};
const writeMarkdownAsHtmlToOutputDirectory = (
@@ -85,6 +102,7 @@ type MarkdownLoaderOptions = {
noteFileName: string;
noteHtmlTemplatePath: string;
markdownHtmlReplacementTag: string;
imageWidth?: number;
};
export class MarkdownLoader implements IPluginBuilder {
options: MarkdownLoaderOptions;
@@ -115,7 +133,8 @@ export class MarkdownLoader implements IPluginBuilder {
);
copyAssetsToOutputDirectory(
markdownManifest,
builderContext.outputDirectory
builderContext.outputDirectory,
this.options
);
};
}