Skip to content

Commit

Permalink
🏋️‍♀️ Jats-to-myst transform to lift figure caption titles
Browse files Browse the repository at this point in the history
  • Loading branch information
fwkoch committed Nov 16, 2023
1 parent 2fe15ec commit af0ff8d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
65 changes: 65 additions & 0 deletions packages/jats-to-myst/src/transforms/figureCaptions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, test } from 'vitest';
import { figCaptionTitleTransform } from './figureCaptions';

describe('figCaptionTitleTransform', () => {
test('figure caption title is moved up', () => {
const tree = {
type: 'root',
children: [
{
type: 'fig',
children: [
{
type: 'caption',
children: [
{
type: 'title',
children: [
{
type: 'text',
value: 'My Title',
},
],
},
{
type: 'text',
value: 'My content',
},
],
},
],
},
],
};
const result = {
type: 'root',
children: [
{
type: 'fig',
children: [
{
type: 'title',
children: [
{
type: 'text',
value: 'My Title',
},
],
},
{
type: 'caption',
children: [
{
type: 'text',
value: 'My content',
},
],
},
],
},
],
};
figCaptionTitleTransform(tree);
expect(tree).toEqual(result);
});
});
27 changes: 27 additions & 0 deletions packages/jats-to-myst/src/transforms/figureCaptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Plugin } from 'unified';
import type { Heading } from 'myst-spec';
import type { GenericParent } from 'myst-common';
import { copyNode } from 'myst-common';
import { select, selectAll } from 'unist-util-select';
import { remove } from 'unist-util-remove';
import { Tags } from 'jats-tags';

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

/**
* Move figure > caption > titles up to the figure
*/
export function figCaptionTitleTransform(tree: GenericParent) {
const figures = selectAll(Tags.fig, tree) as GenericParent[];
figures.forEach((figure) => {
const captionTitle = select('caption > title', figure);
if (!captionTitle) return;
figure.children = [copyNode(captionTitle), ...figure.children];
captionTitle.type = '__delete__';
});
remove(tree, '__delete__');
}

export const figCaptionTitlePlugin: Plugin<[], GenericParent, GenericParent> = () => (tree) => {
figCaptionTitleTransform(tree);
};
3 changes: 3 additions & 0 deletions packages/jats-to-myst/src/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import type { VFile } from 'vfile';
import { sectionTransform } from './sections.js';
import { typographyTransform } from './typography.js';
import { admonitionTransform } from './admonitions.js';
import { figCaptionTitleTransform } from './figureCaptions.js';

export { sectionTransform, sectionPlugin } from './sections.js';
export { typographyTransform, typographyPlugin } from './typography.js';
export { admonitionTransform, admonitionPlugin } from './admonitions.js';
export { figCaptionTitleTransform, figCaptionTitlePlugin } from './figureCaptions.js';

export function basicTransformations(tree: GenericParent, file: VFile) {
sectionTransform(tree);
typographyTransform(tree);
admonitionTransform(tree, file);
figCaptionTitleTransform(tree);
}

export const basicTransformationsPlugin: Plugin<[], GenericParent, GenericParent> =
Expand Down

0 comments on commit af0ff8d

Please sign in to comment.