-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🏋️♀️ Jats-to-myst transform to lift figure caption titles
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/jats-to-myst/src/transforms/figureCaptions.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters