Skip to content

Commit bfff95a

Browse files
mmckyclaude
andcommitted
Address review: single provider resolution + figure template for kind-less captions
- Resolve the providing page state once per container instead of two linear searches across states (target lookup + numbering selection) - Containers without a kind are figures (kindFromNode); their caption label now honours numbering.figure.template instead of a hard-coded default Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 7c22539 commit bfff95a

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

packages/myst-transforms/src/enumerate.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,21 @@ describe('code numbering (#47)', () => {
12861286
expect(captionParagraph.children[0].type).toBe('captionNumber');
12871287
expect(toText(captionParagraph.children[0])).toBe('Listing 1.1:');
12881288
});
1289+
test('kind-less containers honour the figure template', () => {
1290+
const tree = u('root', [
1291+
u('container', { identifier: 'fig-1' }, [
1292+
u('caption', [u('paragraph', [u('text', 'My figure')])]),
1293+
]),
1294+
]);
1295+
const state = new ReferenceState('main.md', {
1296+
frontmatter: { numbering: { all: { enabled: true }, figure: { template: 'Fig. %s' } } },
1297+
vfile: new VFile(),
1298+
});
1299+
enumerateTargetsTransform(tree, { state });
1300+
addContainerCaptionNumbersTransform(tree, new VFile(), { state });
1301+
const captionParagraph = (tree as any).children[0].children[0].children[0];
1302+
expect(toText(captionParagraph.children[0])).toBe('Fig. 1:');
1303+
});
12891304
test('caption noun falls back to the default without a template', () => {
12901305
const tree = u('root', [
12911306
u('container', { kind: 'code', identifier: 'list-1' }, [

packages/myst-transforms/src/enumerate.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,10 +1033,12 @@ export const enumerateTargetsPlugin: Plugin<[StateOptions], GenericParent, Gener
10331033
function getCaptionLabel(kind?: Container['kind'], subcontainer?: boolean, numbering?: Numbering) {
10341034
if (subcontainer && (kind === 'equation' || kind === 'subequation')) return `(%s)`;
10351035
if (subcontainer) return `({subEnumerator})`;
1036-
if (!kind) return 'Figure %s:';
10371036
// The caption noun follows the configured template, matching what
1038-
// cross-references render (e.g. `numbering.code.template: "Listing %s"`)
1039-
const template = numbering?.[kind]?.template ?? getDefaultNumberedReferenceTemplate(kind);
1037+
// cross-references render (e.g. `numbering.code.template: "Listing %s"`);
1038+
// containers without a kind are figures (matching kindFromNode)
1039+
const effectiveKind = kind || 'figure';
1040+
const template =
1041+
numbering?.[effectiveKind]?.template ?? getDefaultNumberedReferenceTemplate(effectiveKind);
10401042
return `${template}:`;
10411043
}
10421044

@@ -1056,7 +1058,11 @@ export function addContainerCaptionNumbersTransform(
10561058
containers
10571059
.filter((container: Container) => container.enumerator)
10581060
.forEach((container: Container) => {
1059-
const target = opts.state.getTarget(container.identifier)?.node;
1061+
// Resolve the providing page state once: it serves both the target
1062+
// lookup and the numbering selection below. Single-page states do not
1063+
// resolve without a page argument but carry the numbering directly.
1064+
const stateProvider = opts.state.resolveStateProvider(container.identifier);
1065+
const target = (stateProvider ?? opts.state).getTarget(container.identifier)?.node;
10601066
if (!target?.enumerator) return;
10611067
// Only look for direct caption children
10621068
let para = select(
@@ -1080,11 +1086,8 @@ export function addContainerCaptionNumbersTransform(
10801086
html_id: (container as any).html_id,
10811087
enumerator: target.enumerator,
10821088
};
1083-
// Page-level resolvers need the identifier's page; a single-page
1084-
// ReferenceState carries the numbering directly
10851089
const numbering =
1086-
opts.state.resolveStateProvider(container.identifier)?.numbering ??
1087-
(opts.state as { numbering?: Numbering }).numbering;
1090+
stateProvider?.numbering ?? (opts.state as { numbering?: Numbering }).numbering;
10881091
fillReferenceEnumerators(
10891092
file,
10901093
captionNumber,

0 commit comments

Comments
 (0)