myst-cli
Version:
Command line tools for MyST
53 lines (52 loc) • 2.09 kB
JavaScript
import { mystParse } from 'myst-parser';
import { buttonRole } from 'myst-ext-button';
import { cardDirective } from 'myst-ext-card';
import { gridDirectives } from 'myst-ext-grid';
import { proofDirective } from 'myst-ext-proof';
import { exerciseDirectives } from 'myst-ext-exercise';
import { tabDirectives } from 'myst-ext-tabs';
import { VFile } from 'vfile';
import { logMessagesFromVFile } from '../utils/logging.js';
import { selectCurrentProjectConfig } from '../store/selectors.js';
export function getMystParserOptions(session, opts) {
// Get parser settings from project config
// Right now this is only project level, but in the future we will allow page level settings.
const parserOptions = selectCurrentProjectConfig(session.store.getState())?.settings?.parser;
// Configure math extensions based on parser settings
let mathExtension = true;
if (parserOptions?.dollarmath === false) {
// If dollarmath is explicitly disabled, configure math to only enable amsmath
mathExtension = { dollarmath: false, amsmath: true };
}
return {
markdownit: { linkify: true },
directives: [
cardDirective,
...gridDirectives,
proofDirective,
...exerciseDirectives,
...tabDirectives,
...(session.plugins?.directives ?? []),
],
extensions: {
frontmatter: !opts?.ignoreFrontmatter,
math: mathExtension,
},
roles: [buttonRole, ...(session.plugins?.roles ?? [])],
};
}
/**
* Parse MyST content using the full suite of built-in directives, roles, and plugins
*
* @param session session with logging
* @param content Markdown content to parse
* @param file path to file containing content
*/
export function parseMyst(session, content, file, opts) {
const vfile = new VFile();
vfile.path = file;
const parserOptions = getMystParserOptions(session, opts);
const parsed = mystParse(content, { ...parserOptions, vfile });
logMessagesFromVFile(session, vfile);
return parsed;
}