myst-cli
Version:
Command line tools for MyST
110 lines (109 loc) • 5.49 kB
JavaScript
import { getFrontmatter } from 'myst-transforms';
import { validateExportsList, fillPageFrontmatter, simplifyLicenses, unnestKernelSpec, validatePageFrontmatter, } from 'myst-frontmatter';
import { fileError, fileWarn, normalizeLabel, RuleId } from 'myst-common';
import { VFile } from 'vfile';
import { selectors, watch } from './store/index.js';
import { logMessagesFromVFile } from './utils/logging.js';
import { castSession } from './session/cache.js';
export function frontmatterValidationOpts(vfile, opts) {
var _a;
return {
property: (_a = opts === null || opts === void 0 ? void 0 : opts.property) !== null && _a !== void 0 ? _a : 'frontmatter',
file: vfile.path,
messages: {},
errorLogFn: (message) => {
var _a;
fileError(vfile, message, { ruleId: (_a = opts === null || opts === void 0 ? void 0 : opts.ruleId) !== null && _a !== void 0 ? _a : RuleId.validPageFrontmatter });
},
warningLogFn: (message) => {
var _a;
fileWarn(vfile, message, { ruleId: (_a = opts === null || opts === void 0 ? void 0 : opts.ruleId) !== null && _a !== void 0 ? _a : RuleId.validPageFrontmatter });
},
};
}
/**
* Get page frontmatter from mdast tree
*
* @param session
* @param tree - mdast tree already loaded
* @param vfile - vfile used for logging
* @param preFrontmatter - incoming frontmatter for the page that is not from the project or in the tree
* @param keepTitleNode - do not remove leading H1 even if it is lifted as title
*/
export function getPageFrontmatter(session, tree, vfile, preFrontmatter, keepTitleNode) {
var _a;
const { frontmatter: rawPageFrontmatter, identifiers } = getFrontmatter(vfile, tree, {
propagateTargets: true,
preFrontmatter,
keepTitleNode,
});
unnestKernelSpec(rawPageFrontmatter);
const pageFrontmatter = validatePageFrontmatter(rawPageFrontmatter, frontmatterValidationOpts(vfile));
if (pageFrontmatter.label) {
const { identifier } = (_a = normalizeLabel(pageFrontmatter.label)) !== null && _a !== void 0 ? _a : {};
if (identifier)
identifiers.push(identifier);
}
logMessagesFromVFile(session, vfile);
return { frontmatter: pageFrontmatter, identifiers };
}
export function processPageFrontmatter(session, pageFrontmatter, validationOpts, path) {
var _a, _b, _c, _d, _e, _f;
const cache = castSession(session);
const state = session.store.getState();
const siteFrontmatter = (_a = selectors.selectCurrentSiteConfig(state)) !== null && _a !== void 0 ? _a : {};
const projectFrontmatter = path ? (_b = selectors.selectLocalProjectConfig(state, path)) !== null && _b !== void 0 ? _b : {} : {};
const frontmatter = fillPageFrontmatter(pageFrontmatter, projectFrontmatter, validationOpts);
const siteTemplate = cache.$siteTemplate;
if (siteTemplate) {
const siteOptions = siteTemplate.validateOptions((_c = pageFrontmatter.site) !== null && _c !== void 0 ? _c : {}, path, {
// The property is different on the page vs the myst.yml
property: 'site',
// Passing in the log files ensures this isn't prefixed with `myst.yml`.
warningLogFn: session.log.warn,
errorLogFn: session.log.error,
});
if (siteOptions && Object.keys(siteOptions).length > 0)
frontmatter.site = siteOptions;
}
else {
// The options are still there, they are just not validated
session.log.debug(`Site template not available to validate site frontmatter in ${path}`);
}
if (((_d = siteFrontmatter === null || siteFrontmatter === void 0 ? void 0 : siteFrontmatter.options) === null || _d === void 0 ? void 0 : _d.hide_authors) || ((_f = (_e = siteFrontmatter === null || siteFrontmatter === void 0 ? void 0 : siteFrontmatter.options) === null || _e === void 0 ? void 0 : _e.design) === null || _f === void 0 ? void 0 : _f.hide_authors)) {
delete frontmatter.authors;
}
return frontmatter;
}
export function prepareToWrite(frontmatter) {
if (!frontmatter.license)
return { ...frontmatter };
return { ...frontmatter, license: simplifyLicenses(frontmatter.license) };
}
export function getExportListFromRawFrontmatter(session, rawFrontmatter, file) {
var _a;
const vfile = new VFile();
vfile.path = file;
const exports = validateExportsList((_a = rawFrontmatter === null || rawFrontmatter === void 0 ? void 0 : rawFrontmatter.exports) !== null && _a !== void 0 ? _a : rawFrontmatter === null || rawFrontmatter === void 0 ? void 0 : rawFrontmatter.export, frontmatterValidationOpts(vfile, {
property: 'exports',
ruleId: RuleId.validFrontmatterExportList,
}));
logMessagesFromVFile(session, vfile);
return exports !== null && exports !== void 0 ? exports : [];
}
export function updateFileInfoFromFrontmatter(session, file, frontmatter, url, dataUrl) {
session.store.dispatch(watch.actions.updateFileInfo({
path: file,
title: frontmatter.title,
short_title: frontmatter.short_title,
description: frontmatter.description,
date: frontmatter.date,
thumbnail: frontmatter.thumbnail,
thumbnailOptimized: frontmatter.thumbnailOptimized,
banner: frontmatter.banner,
bannerOptimized: frontmatter.bannerOptimized,
tags: frontmatter.tags,
url,
dataUrl,
}));
}