@curvenote/cli
Version:
CLI Client library for Curvenote
86 lines (85 loc) • 3.48 kB
JavaScript
import { oxaLink } from '@curvenote/blocks';
import { affiliations, downloadAndSaveImage, selectors } from 'myst-cli';
import { PAGE_FRONTMATTER_KEYS, PROJECT_FRONTMATTER_KEYS, validatePageFrontmatterKeys, validateProjectFrontmatterKeys, } from 'myst-frontmatter';
import { filterKeys } from 'simple-validators';
import { dirname, join } from 'node:path';
export const THUMBNAILS_FOLDER = 'thumbnails';
export function saveAffiliations(session, project) {
session.store.dispatch(affiliations.actions.receive({
affiliations: project.affiliations || [],
}));
}
function resolveAffiliations(session, author) {
const { affiliations: authorAffiliations, ...rest } = author;
if (!authorAffiliations)
return { ...rest };
const state = session.store.getState();
const resolvedAffiliations = authorAffiliations
.map((id) => selectors.selectAffiliation(state, id))
.filter((text) => typeof text === 'string');
return { affiliations: resolvedAffiliations, ...rest };
}
export function projectFrontmatterFromDTO(session, project, opts) {
const apiFrontmatter = filterKeys(project, PROJECT_FRONTMATTER_KEYS);
if (apiFrontmatter.authors) {
apiFrontmatter.authors = apiFrontmatter.authors.map((author) => {
const resolvedAuthor = resolveAffiliations(session, author);
delete resolvedAuthor.id;
return resolvedAuthor;
});
}
if (project.licenses) {
// This will get validated below
apiFrontmatter.license = project.licenses;
}
const frontmatter = validateProjectFrontmatterKeys(apiFrontmatter, {
property: 'project',
suppressErrors: true,
suppressWarnings: true,
messages: {},
...opts,
});
delete frontmatter.affiliations;
return frontmatter;
}
export async function pageFrontmatterFromDTOAndThumbnail(session, filename, block, date, opts) {
const apiFrontmatter = pageFrontmatterFromDTO(session, block, date, opts);
if (block.links.thumbnail) {
const result = await downloadAndSaveImage(session, block.links.thumbnail, block.name || block.id.block, join(dirname(filename), THUMBNAILS_FOLDER));
if (result) {
apiFrontmatter.thumbnail = join(THUMBNAILS_FOLDER, result);
}
}
return apiFrontmatter;
}
export function pageFrontmatterFromDTO(session, block, date, opts) {
const apiFrontmatter = filterKeys(block, PAGE_FRONTMATTER_KEYS);
if (apiFrontmatter.authors) {
apiFrontmatter.authors = apiFrontmatter.authors.map((author) => {
const resolvedAuthor = resolveAffiliations(session, author);
delete resolvedAuthor.id;
return resolvedAuthor;
});
}
if (block.licenses) {
apiFrontmatter.license = block.licenses;
}
// TODO: Date needs to be in block frontmatter
if (block.date_modified) {
apiFrontmatter.date = (date || block.date_modified);
}
apiFrontmatter.oxa = oxaLink('', block.id);
const frontmatter = validatePageFrontmatterKeys(apiFrontmatter, {
property: 'page',
suppressErrors: true,
suppressWarnings: true,
messages: {},
...opts,
});
// in curvenote the article block tags are currently used to store the keywords
if (frontmatter.tags) {
frontmatter.keywords = frontmatter.tags?.map((t) => t);
}
delete frontmatter.affiliations;
return frontmatter;
}