Skip to content

Commit

Permalink
🔧 Tests/docs on jats-to-myst transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
fwkoch committed Nov 16, 2023
1 parent 01a0625 commit 2fe15ec
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 0 deletions.
104 changes: 104 additions & 0 deletions packages/jats-to-myst/src/transforms/admonitions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { VFile } from 'vfile';
import { describe, expect, test } from 'vitest';
import { admonitionTransform } from './admonitions';

describe('admonitionTransform', () => {
test('caption title is moved to admonition title', () => {
const vfile = new VFile();
const tree = {
type: 'root',
children: [
{
type: 'boxed-text',
children: [
{
type: 'caption',
children: [
{
type: 'title',
children: [
{
type: 'text',
value: 'My Title',
},
],
},
],
},
{
type: 'text',
value: 'My content',
},
],
},
],
};
const result = {
type: 'root',
children: [
{
type: 'boxed-text',
children: [
{
type: 'admonitionTitle',
children: [
{
type: 'text',
value: 'My Title',
},
],
},
{
type: 'text',
value: 'My content',
},
],
},
],
};
admonitionTransform(tree, vfile);
expect(tree).toEqual(result);
});
test('caption with no title is lost', () => {
const vfile = new VFile();
const tree = {
type: 'root',
children: [
{
type: 'boxed-text',
children: [
{
type: 'caption',
children: [
{
type: 'text',
value: 'My Title',
},
],
},
{
type: 'text',
value: 'My content',
},
],
},
],
};
const result = {
type: 'root',
children: [
{
type: 'boxed-text',
children: [
{
type: 'text',
value: 'My content',
},
],
},
],
};
admonitionTransform(tree, vfile);
expect(tree).toEqual(result);
});
});
7 changes: 7 additions & 0 deletions packages/jats-to-myst/src/transforms/admonitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import type { VFile } from 'vfile';

export type Section = Omit<Heading, 'type'> & { type: 'section' };

/**
* Convert boxed-text caption to admonitionTitle (with content from caption > title)
*
* If boxed-text caption does not have a title, warn.
*
* After converting, delete any remaining boxed-text captions.
*/
export function admonitionTransform(tree: GenericParent, file: VFile) {
const captions = selectAll(`${Tags.boxedText} > ${Tags.caption}`, tree) as GenericParent[];
captions.forEach((caption) => {
Expand Down
188 changes: 188 additions & 0 deletions packages/jats-to-myst/src/transforms/sections.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { describe, expect, test } from 'vitest';
import { sectionTransform } from './sections';

describe('sectionTransform', () => {
test('section title becomes heading with depth', () => {
const tree = {
type: 'root',
children: [
{
type: 'sec',
children: [
{
type: 'title',
children: [
{
type: 'text',
value: 'My Title',
},
],
},
{
type: 'text',
value: 'My content',
},
],
},
],
};
const result = {
type: 'root',
children: [
{
type: 'block',
children: [
{
type: 'heading',
depth: 1,
children: [
{
type: 'text',
value: 'My Title',
},
],
},
{
type: 'text',
value: 'My content',
},
],
},
],
};
sectionTransform(tree);
expect(tree).toEqual(result);
});
test('section without title remains', () => {
const tree = {
type: 'root',
children: [
{
type: 'sec',
children: [
{
type: 'text',
value: 'My content',
},
],
},
],
};
const result = {
type: 'root',
children: [
{
type: 'block',
children: [
{
type: 'text',
value: 'My content',
},
],
},
],
};
sectionTransform(tree);
expect(tree).toEqual(result);
});
test('nested sections flatten', () => {
const tree = {
type: 'root',
children: [
{
type: 'sec',
children: [
{
type: 'title',
children: [
{
type: 'text',
value: 'My Title',
},
],
},
{
type: 'text',
value: 'My content',
},
{
type: 'sec',
children: [
{
type: 'title',
children: [
{
type: 'text',
value: 'My Subitle',
},
],
},
{
type: 'sec',
children: [
{
type: 'title',
children: [
{
type: 'text',
value: 'Another title',
},
],
},
],
},
],
},
],
},
],
};
const result = {
type: 'root',
children: [
{
type: 'block',
children: [
{
type: 'heading',
depth: 1,
children: [
{
type: 'text',
value: 'My Title',
},
],
},
{
type: 'text',
value: 'My content',
},

{
type: 'heading',
depth: 2,
children: [
{
type: 'text',
value: 'My Subitle',
},
],
},
{
type: 'heading',
depth: 3,
children: [
{
type: 'text',
value: 'Another title',
},
],
},
],
},
],
};
sectionTransform(tree);
expect(tree).toEqual(result);
});
});
8 changes: 8 additions & 0 deletions packages/jats-to-myst/src/transforms/sections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ function recurseSections(tree: GenericNode, depth = 1): void {
});
}

/**
* Clean up nested sections
*
* - Identify sections with titles
* - Convert those titles to headings
* - Give headings depth value based on nesting
* - Flatten the sections
*/
export function sectionTransform(tree: GenericParent) {
recurseSections(tree);
const topSections = tree.children?.filter((n) => n.type === 'sec');
Expand Down

0 comments on commit 2fe15ec

Please sign in to comment.