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];
};

17
src/converters/pandoc.ts Normal file
View file

@ -0,0 +1,17 @@
import { exec } from "node:child_process";
export const properties = {
from: [
"md", "html", "docx", "pdf", "tex", "txt", "bibtex", "biblatex", "commonmark", "commonmark_x", "creole", "csljson", "csv", "tsv", "docbook", "dokuwiki", "endnotexml", "epub", "fb2", "gfm", "haddock", "ipynb", "jats", "jira", "json", "latex", "markdown", "markdown_mmd", "markdown_phpextra", "markdown_strict", "mediawiki", "man", "muse", "native", "odt", "opml", "org", "ris", "rtf", "rst", "t2t", "textile", "tikiwiki", "twiki", "vimwiki"
],
to: [
"asciidoc", "asciidoctor", "beamer", "bibtex", "biblatex", "commonmark", "commonmark_x", "context", "csljson", "docbook", "docbook4", "docbook5", "docx", "dokuwiki", "epub", "epub3", "epub2", "fb2", "gfm", "haddock", "html", "html5", "html4", "icml", "ipynb", "jats_archiving", "jats_articleauthoring", "jats_publishing", "jats", "jira", "json", "latex", "man", "markdown", "markdown_mmd", "markdown_phpextra", "markdown_strict", "markua", "mediawiki", "ms", "muse", "native", "odt", "opml", "opendocument", "org", "pdf", "plain", "pptx", "rst", "rtf", "texinfo", "textile", "slideous", "slidy", "dzslides", "revealjs", "s5", "tei", "xwiki", "zimwiki"
]
};
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export function convert(filePath: string, fileType: string, convertTo: string, targetPath: string, options?: any) {
return exec(
`pandoc ${filePath} -f ${fileType} -t ${convertTo} -o ${targetPath}`,
);
}