myst-cli
Version:
Command line tools for MyST
75 lines (74 loc) • 3.21 kB
JavaScript
import path from 'node:path';
import yaml from 'js-yaml';
import { tic, writeFileToFolder } from 'myst-cli-utils';
import { extractPart, fileWarn, toText } from 'myst-common';
import { filterKeys } from 'simple-validators';
import { VFile } from 'vfile';
import { CFF_KEYS, frontmatterToCFF } from 'cffjs';
import { selectors } from '../store/index.js';
import { logMessagesFromVFile } from '../utils/logging.js';
import { KNOWN_IMAGE_EXTENSIONS } from '../utils/resolveExtension.js';
import { cleanOutput } from './utils/cleanOutput.js';
import { getFileContent } from './utils/getFileContent.js';
import { resolveFrontmatterParts } from '../utils/resolveFrontmatterParts.js';
function exportOptionsToCFF(exportOptions) {
// Handle overlap of key "format" between CFF and export
const exportForCFF = { ...exportOptions, format: undefined };
if (exportForCFF['cff-format']) {
exportForCFF.format = exportForCFF['cff-format'];
}
return filterKeys(exportForCFF, CFF_KEYS);
}
export async function runCffExport(session, sourceFile, exportOptions, opts) {
var _a;
const toc = tic();
const { output, articles } = exportOptions;
const { clean, projectPath, extraLinkTransformers } = opts !== null && opts !== void 0 ? opts : {};
const article = articles[0];
const state = session.store.getState();
let frontmatter;
let abstract;
if (projectPath && selectors.selectLocalConfigFile(state, projectPath) === sourceFile) {
// Process the project only, without any files
await getFileContent(session, [], {
projectPath,
imageExtensions: KNOWN_IMAGE_EXTENSIONS,
extraLinkTransformers,
});
frontmatter = selectors.selectLocalProjectConfig(state, projectPath);
const { abstract: frontmatterAbstract } = (_a = resolveFrontmatterParts(session, frontmatter)) !== null && _a !== void 0 ? _a : {};
if (frontmatterAbstract) {
abstract = toText(frontmatterAbstract.mdast);
}
}
else if (article.file) {
const [content] = await getFileContent(session, [article.file], {
projectPath,
imageExtensions: KNOWN_IMAGE_EXTENSIONS,
extraLinkTransformers,
});
frontmatter = content.frontmatter;
const abstractMdast = extractPart(content.mdast, 'abstract', {
frontmatterParts: resolveFrontmatterParts(session, frontmatter),
});
if (abstractMdast)
abstract = toText(abstractMdast);
}
if (!frontmatter)
return { tempFolders: [] };
if (clean)
cleanOutput(session, output);
const vfile = new VFile();
vfile.path = output;
if (path.basename(output) !== 'CITATION.cff') {
fileWarn(vfile, `Invalid Citation File Format filename ${path.basename(output)} - CFF requires filename 'CITATION.cff'`);
}
const cff = {
...frontmatterToCFF(frontmatter, abstract),
...exportOptionsToCFF(exportOptions),
};
logMessagesFromVFile(session, vfile);
session.log.info(toc(`📑 Exported CFF in %s, copying to ${output}`));
writeFileToFolder(output, yaml.dump(cff));
return { tempFolders: [] };
}