diff --git a/packages/jats-to-myst/src/transforms/figureCaptions.spec.ts b/packages/jats-to-myst/src/transforms/figureCaptions.spec.ts new file mode 100644 index 000000000..8341230ed --- /dev/null +++ b/packages/jats-to-myst/src/transforms/figureCaptions.spec.ts @@ -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); + }); +}); diff --git a/packages/jats-to-myst/src/transforms/figureCaptions.ts b/packages/jats-to-myst/src/transforms/figureCaptions.ts new file mode 100644 index 000000000..4c8804a25 --- /dev/null +++ b/packages/jats-to-myst/src/transforms/figureCaptions.ts @@ -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 & { 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); +}; diff --git a/packages/jats-to-myst/src/transforms/index.ts b/packages/jats-to-myst/src/transforms/index.ts index e8a5fa2f7..3f010edc5 100644 --- a/packages/jats-to-myst/src/transforms/index.ts +++ b/packages/jats-to-myst/src/transforms/index.ts @@ -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> =