start on pandoc

This commit is contained in:
C4illin 2024-05-19 23:51:27 +02:00
parent 391ef063f7
commit 13cc37d5a2
18 changed files with 782 additions and 408 deletions

View file

@ -1,39 +1,88 @@
import { properties, convert } from "./sharp";
import {
properties as propertiesImage,
convert as convertImage,
} from "./sharp";
import {
properties as propertiesPandoc,
convert as convertPandoc,
} from "./pandoc";
import { normalizeFiletype } from "../helpers/normalizeFiletype";
export async function mainConverter(
inputFilePath: string,
fileType: string,
convertTo: string,
targetPath: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
options?: any,
inputFilePath: string,
fileType: string,
convertTo: string,
targetPath: string,
options?: any,
) {
// Check if the fileType and convertTo are supported by the sharp converter
if (properties.from.includes(fileType) && properties.to.includes(convertTo)) {
// Use the sharp converter
try {
await convert(inputFilePath, fileType, convertTo, targetPath, options);
console.log(
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully.`,
);
} catch (error) {
console.error(
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo}.`,
error,
);
}
} else {
console.log(
`The sharp converter does not support converting from ${fileType} to ${convertTo}.`,
);
}
// Check if the fileType and convertTo are supported by the sharp converter
if (
propertiesImage.from.includes(fileType) &&
propertiesImage.to.includes(convertTo)
) {
// Use the sharp converter
try {
await convertImage(
inputFilePath,
fileType,
convertTo,
targetPath,
options,
);
console.log(
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully.`,
);
} catch (error) {
console.error(
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo}.`,
error,
);
}
}
// Check if the fileType and convertTo are supported by the pandoc converter
else if (
propertiesPandoc.from.includes(fileType) &&
propertiesPandoc.to.includes(convertTo)
) {
// Use the pandoc converter
try {
await convertPandoc(
inputFilePath,
fileType,
convertTo,
targetPath,
options,
);
console.log(
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully.`,
);
} catch (error) {
console.error(
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo}.`,
error,
);
}
} else {
console.log(
`Neither the sharp nor pandoc converter support converting from ${fileType} to ${convertTo}.`,
);
}
}
export function possibleConversions(fileType: string) {
// Check if the fileType is supported by the sharp converter
if (properties.from.includes(fileType)) {
return properties.to;
}
const possibleConversions: { [key: string]: string[] } = {};
return [];
for (const from of [...propertiesImage.from, ...propertiesPandoc.from]) {
possibleConversions[from] = [...propertiesImage.to, ...propertiesPandoc.to];
}
export const getPossibleConversions = (from: string): string[] => {
const fromClean = normalizeFiletype(from);
return possibleConversions[fromClean] || ([] as string[]);
};
export const getAllTargets = () => {
return [...propertiesImage.to, ...propertiesPandoc.to];
};