@curvenote/cli
Version:
CLI Client library for Curvenote
68 lines (67 loc) • 2.68 kB
JavaScript
import { castSession } from 'myst-cli';
import { fileWarn } from 'myst-common';
import { oxaLink, oxaLinkToId } from '@curvenote/blocks';
import { selectors } from '../store/index.js';
import { oxalink } from '../store/oxa/index.js';
/**
* Populate link node with rich oxa info
*/
export class OxaTransformer {
constructor(session) {
this.protocol = 'oxa';
this.session = session;
}
test(url) {
if (!url)
return false;
const oxa = oxaLinkToId(url);
return !!oxa;
}
transform(link, file) {
const urlSource = link.urlSource || link.url;
const oxa = oxaLinkToId(urlSource);
const key = oxaLink(oxa, false);
const store = this.session.store.getState();
const info = selectors.selectOxaLinkInformation(store, key);
const externalOxaUrl = oxa ? oxaLink(this.session.config.editorUrl, oxa.block) : null;
if (info) {
const url = info === null || info === void 0 ? void 0 : info.url;
if (url && url !== link.url) {
// the `internal` flag is picked up in the link renderer (prefetch!)
link.internal = true;
link.url = url;
// TODO: Link blocks!
// if (link.type === 'linkBlock') {
// // Any values already present on the block override link info
// link.title = link.title || info?.title || undefined;
// if (!link.children || link.children.length === 0) {
// link.children = [{ type: 'text', value: info?.description || '' }];
// }
// link.thumbnail = link.thumbnail || info?.thumbnail;
// }
}
}
else if (externalOxaUrl) {
fileWarn(file, `Replacing oxa link with external url: ${externalOxaUrl}`, { node: link });
link.url = externalOxaUrl;
}
else {
fileWarn(file, `Information for link not found: ${key}`, { node: link });
}
return true;
}
}
export async function transformOxalinkStore(session, opts) {
var _a;
const cache = castSession(session);
const mdastPost = (_a = cache.$getMdast(opts.file)) === null || _a === void 0 ? void 0 : _a.post;
const oxa = mdastPost === null || mdastPost === void 0 ? void 0 : mdastPost.frontmatter.oxa;
if (oxa) {
const url = opts.projectSlug ? `/${opts.projectSlug}/${mdastPost.slug}` : `/${mdastPost.slug}`;
session.store.dispatch(oxalink.actions.updateLinkInfo({
path: opts.file,
oxa: oxa,
url,
}));
}
}