Skip to content

Commit

Permalink
🔧 Fix single file export references by building project files (#358)
Browse files Browse the repository at this point in the history
* 🔧 Fix single file export references by building project files
* Add apt-get update to github action
* 🔧 Update embed directive test
* 🔧 myst-to-jats dependencies and tests
  • Loading branch information
fwkoch authored Apr 24, 2023
1 parent 1ce88e9 commit ff43d9c
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-moles-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-cli': patch
---

Fix crosslinked content for single file exports
6 changes: 6 additions & 0 deletions .changeset/lucky-moles-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'myst-directives': patch
'myst-parser': patch
---

Remove identifier from embed node
5 changes: 5 additions & 0 deletions .changeset/poor-toes-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-tex': patch
---

Remove {name} template from getting printed into single article exports
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
with:
fetch-depth: 2
submodules: recursive
- run: sudo apt-get update
- run: sudo apt-get install -y libxml2-utils
- name: Setup Node.js environment
uses: actions/setup-node@v2
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-cli/src/build/utils/collectExportOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function prepareExportOptions(
exportOptions.forEach((exp) => {
// If no files are specified, use the sourceFile for article
if (!exp.article && SOURCE_EXTENSIONS.includes(path.extname(sourceFile))) {
exp.article = sourceFile;
exp.article = path.resolve(sourceFile);
}
// Also validate that sub_articles exist
if (exp.sub_articles) {
Expand Down
18 changes: 13 additions & 5 deletions packages/myst-cli/src/build/utils/getFileContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ export async function getFileContent(
const toc = tic();
files = files.map((file) => resolve(file));
projectPath = projectPath ?? resolve('.');
const { project } = await loadProject(session, projectPath);
const { project, pages } = await loadProject(session, projectPath);
const projectFiles = pages.map((page) => page.file);
const allFiles = [...new Set([...files, ...projectFiles])];
await Promise.all([
// Load all citations (.bib)
...project.bibliography.map((path) => loadFile(session, path, '.bib')),
// Load all content (.md and .ipynb)
...files.map((file) => loadFile(session, file, undefined, { minifyMaxCharacters: 0 })),
...allFiles.map((file) => loadFile(session, file, undefined, { minifyMaxCharacters: 0 })),
// Load up all the intersphinx references
loadIntersphinx(session, { projectPath }) as Promise<any>,
]);
Expand All @@ -55,7 +57,7 @@ export async function getFileContent(
// extraTransforms.push(...opts.extraTransforms);
// }
await Promise.all(
files.map(async (file) => {
allFiles.map(async (file) => {
await transformMdast(session, {
file,
imageWriteFolder,
Expand All @@ -68,7 +70,7 @@ export async function getFileContent(
);
const pageReferenceStates = selectPageReferenceStates(
session,
files.map((file) => {
allFiles.map((file) => {
return { file };
}),
);
Expand All @@ -82,6 +84,12 @@ export async function getFileContent(
return selectedFile;
}),
);
session.log.info(toc(`📚 Built ${files.length} pages for export from ${projectPath} in %s.`));
session.log.info(
toc(
`📚 Built ${allFiles.length} pages for export (including ${
allFiles.length - files.length
} dependencies) from ${projectPath} in %s.`,
),
);
return selectedFiles;
}
3 changes: 1 addition & 2 deletions packages/myst-directives/src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ export const embedDirective: DirectiveSpec = {
},
},
run(data: DirectiveData): GenericNode[] {
const { label, identifier } = normalizeLabel(data.options?.label as string) || {};
const { label } = normalizeLabel(data.options?.label as string) || {};
return [
{
type: 'embed',
label,
identifier,
'remove-input': data.options?.['remove-input'],
'remove-output': data.options?.['remove-output'],
},
Expand Down
1 change: 0 additions & 1 deletion packages/myst-parser/tests/directives/embed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ cases:
children:
- type: embed
label: hi
identifier: hi
remove-output: true
2 changes: 1 addition & 1 deletion packages/myst-to-jats/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
verbose: true,
testEnvironment: 'node',
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!(vfile|formdata-polyfill|chalk|fetch-blob|vfile-message|unified|bail|trough|zwitch|unist-|hast-|html-|rehype-|mdast-|micromark-|trim-|web-namespaces|property-information|space-separated-tokens|comma-separated-tokens|get-port|stringify-entities|character-entities-html4|ccount))',
'<rootDir>/node_modules/(?!(vfile|formdata-polyfill|chalk|fetch-blob|vfile-message|unified|bail|trough|zwitch|unist-|hast-|html-|rehype-|mdast-|micromark-|trim-|web-namespaces|property-information|space-separated-tokens|comma-separated-tokens|get-port|stringify-entities|character-entities-html4|ccount|is-plain-obj|nanoid))',
],
testPathIgnorePatterns: ['/node_modules/', '/.yalc/', '/dist/'],
};
5 changes: 5 additions & 0 deletions packages/myst-to-jats/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@
"myst-frontmatter": "^0.0.12",
"myst-spec": "^0.0.4",
"myst-spec-ext": "^0.0.10",
"unified": "^10.1.2",
"unist-util-select": "^4.0.3",
"vfile-reporter": "^7.0.4",
"xml-js": "^1.6.11"
},
"devDependencies": {
"@types/jest": "^28.1.6",
"@types/js-yaml": "^4.0.5",
"@types/mdast": "^3.0.11",
"jest": "^28.1.3",
"js-yaml": "^4.1.0",
"myst-cli-utils": "^0.0.11",
"npm-run-all": "^4.1.5",
"prettier": "^2.6.1",
"rimraf": "^3.0.2",
Expand Down
29 changes: 24 additions & 5 deletions packages/myst-to-jats/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import yaml from 'js-yaml';
import { VFile } from 'vfile';
import mystToJats, { writeJats } from '../src';

const TEST_DTD = false;

type TestFile = {
cases: TestCase[];
};
Expand Down Expand Up @@ -39,6 +41,19 @@ const testLogger = new Session({
},
});

function addHeader(data: string) {
return `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD with MathML3 v1.3 20210610//EN" "http://jats.nlm.nih.gov/publishing/1.3/JATS-archivearticle1-3-mathml3.dtd">
<article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ali="http://www.niso.org/schemas/ali/1.0/" dtd-version="1.3" xml:lang="en">
<front>
<article-meta/>
</front>
<body>
${data}
</body>
</article>`;
}

async function writeValidateDelete(data: string) {
const testXml = path.join(__dirname, 'test.xml');
fs.writeFileSync(testXml, data);
Expand All @@ -49,11 +64,12 @@ async function writeValidateDelete(data: string) {

describe('Basic JATS body', () => {
const cases = loadCases('basic.yml');
test.each(cases.map((c): [string, TestCase] => [c.title, c]))('%s', (_, { tree, jats }) => {
test.each(cases.map((c): [string, TestCase] => [c.title, c]))('%s', async (_, { tree, jats }) => {
const pipe = unified().use(mystToJats);
pipe.runSync(tree as any);
const vfile = pipe.stringify(tree as any);
expect(vfile.result).toEqual(jats);
if (TEST_DTD) expect(await writeValidateDelete(addHeader(vfile.result as string))).toBeTruthy();
});
});

Expand All @@ -69,7 +85,7 @@ describe('JATS full article', () => {
pipe.runSync(tree as any);
const vfile = pipe.stringify(tree as any);
expect(vfile.result).toEqual(jats);
expect(await writeValidateDelete(vfile.result as string)).toBeTruthy();
if (TEST_DTD) expect(await writeValidateDelete(vfile.result as string)).toBeTruthy();
},
);
});
Expand All @@ -86,7 +102,7 @@ describe('JATS full article with bibliography', () => {
pipe.runSync(tree as any);
const vfile = pipe.stringify(tree as any);
expect(vfile.result).toEqual(jats);
expect(await writeValidateDelete(vfile.result as string)).toBeTruthy();
if (TEST_DTD) expect(await writeValidateDelete(vfile.result as string)).toBeTruthy();
},
);
});
Expand All @@ -106,7 +122,7 @@ describe('JATS multi-article', () => {
},
);
expect(vfile.result).toEqual(jats);
expect(await writeValidateDelete(vfile.result as string)).toBeTruthy();
if (TEST_DTD) expect(await writeValidateDelete(vfile.result as string)).toBeTruthy();
},
);
});
Expand All @@ -115,7 +131,7 @@ describe('JATS SI units', () => {
const cases = loadCases('siunit.yml');
test.each(cases.map((c): [string, TestCase] => [c.title, c]))(
'%s',
(_, { tree, jats, frontmatter, citations }) => {
async (_, { tree, jats, frontmatter, citations }) => {
const vfile = writeJats(
new VFile(),
{ mdast: tree as any, frontmatter, citations },
Expand All @@ -124,6 +140,9 @@ describe('JATS SI units', () => {
},
);
expect(vfile.result).toEqual(jats);
if (TEST_DTD) {
expect(await writeValidateDelete(addHeader(vfile.result as string))).toBeTruthy();
}
},
);
});
3 changes: 2 additions & 1 deletion packages/myst-to-tex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ const handlers: Record<string, Handler> = {
captionNumber: () => undefined,
crossReference(node, state) {
// Look up reference and add the text
const text = (node.template ?? toText(node))?.replace(/\s/g, '~') || '%s';
const usedTemplate = node.template?.includes('%s') ? node.template : undefined;
const text = (usedTemplate ?? toText(node))?.replace(/\s/g, '~') || '%s';
const id = node.label;
state.write(text.replace(/%s/g, `\\ref{${id}}`));
},
Expand Down

0 comments on commit ff43d9c

Please sign in to comment.