Skip to content

Commit

Permalink
🦁 Fix ROR naming (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored May 10, 2024
1 parent 9e311c3 commit b86d621
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-sloths-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-cli": patch
---

Fix ROR linking and transform ordering bug
2 changes: 1 addition & 1 deletion packages/myst-cli/src/process/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ export async function postProcessMdast(
new StaticFileTransformer(session, file), // Links static files and internally linked files
];
resolveLinksAndCitationsTransform(mdast, { state, transformers });
await transformLinkedRORs(session, vfile, mdast, file);
linksTransform(mdast, state.vfile as VFile, {
transformers,
selector: LINKS_SELECTOR,
});
await transformLinkedRORs(session, vfile, mdast, file);
resolveReferencesTransform(mdast, state.vfile as VFile, { state, transformers });
await transformMystXRefs(session, vfile, mdast, frontmatter);
await embedTransform(session, mdast, file, dependencies, state);
Expand Down
25 changes: 9 additions & 16 deletions packages/myst-cli/src/transforms/ror.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import fs from 'node:fs';
import { join } from 'node:path';
import type { Link } from 'myst-spec';
import type { GenericNode, GenericParent } from 'myst-common';
import { RuleId, plural, fileError, toText } from 'myst-common';
import { selectAll } from 'unist-util-select';
import { computeHash, tic } from 'myst-cli-utils';
import type { VFile } from 'vfile';
import type { ISession } from '../session/types.js';
import { loadFromCache, writeToCache } from '../session/cache.js';

const ROR_MAX_AGE = 30; // in days

type RORResponse = {
id: string;
Expand All @@ -19,11 +20,8 @@ type RORResponse = {
* @param session: CLI session
* @param ror: normalized ROR ID
*/
function rorFromCacheFile(session: ISession, ror: string) {
const filename = `ror-${computeHash(ror)}.json`;
const cacheFolder = join(session.buildPath(), 'cache');
if (!fs.existsSync(cacheFolder)) fs.mkdirSync(cacheFolder, { recursive: true });
return join(cacheFolder, filename);
function rorCacheFilename(ror: string) {
return `ror-${computeHash(ror)}.json`;
}

/**
Expand Down Expand Up @@ -67,13 +65,10 @@ export async function resolveROR(
if (!ror) return undefined;

// Cache ROR resolution as JSON
const cachePath = rorFromCacheFile(session, ror);
const filename = rorCacheFilename(ror);

if (fs.existsSync(cachePath)) {
const cached = fs.readFileSync(cachePath).toString();
session.log.debug(`Loaded cached ROR response for https://ror.org/${ror}`);
return JSON.parse(cached);
}
const cached = loadFromCache(session, filename, { maxAge: ROR_MAX_AGE });
if (cached) return JSON.parse(cached);
const toc = tic();
let data;
try {
Expand All @@ -93,8 +88,7 @@ export async function resolveROR(
}

if (!data) return undefined;
session.log.debug(`Saving ROR JSON to cache ${cachePath}`);
fs.writeFileSync(cachePath, JSON.stringify(data));
writeToCache(session, filename, JSON.stringify(data));
return data as unknown as RORResponse;
}

Expand All @@ -118,7 +112,6 @@ export async function transformLinkedRORs(
if (!ror) return;
number += 1;
const rorData = await resolveROR(session, vfile, node, ror);
console.log(rorData);
if (rorData && toText(node.children) === ror) {
// If the link text is the ROR, update with a organization name
node.children = [{ type: 'text', value: rorData.name }];
Expand Down

0 comments on commit b86d621

Please sign in to comment.