Skip to content

Commit 7821ca6

Browse files
author
Dobromir Hristov
authored
Add support for the @Image directive (#404)
fixes rdar://97715120, rdar://97739628 * refactor: allow wrapping inline-image in a Figure, if there is an abstract, but not anchor. use the `metadata.title` to determine if a figure caption should be above/below the content
1 parent 916bb58 commit 7821ca6

File tree

6 files changed

+109
-13
lines changed

6 files changed

+109
-13
lines changed

src/components/ContentNode.vue

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,20 @@ function renderNode(createElement, references) {
185185
186186
const renderFigure = ({
187187
metadata: {
188-
abstract,
188+
abstract = [],
189189
anchor,
190190
title,
191191
},
192192
...node
193-
}) => createElement(Figure, { props: { anchor } }, [
194-
...(title && abstract && abstract.length ? [
195-
createElement(FigureCaption, { props: { title } }, (
196-
renderChildren(abstract)
197-
)),
198-
] : []),
199-
renderChildren([node]),
200-
]);
193+
}) => {
194+
const figureContent = [renderChildren([node])];
195+
if ((title && abstract.length) || abstract.length) {
196+
// if there is a `title`, it should be above, otherwise below
197+
figureContent.splice(title ? 0 : 1, 0,
198+
createElement(FigureCaption, { props: { title } }, renderChildren(abstract)));
199+
}
200+
return createElement(Figure, { props: { anchor } }, figureContent);
201+
};
201202
202203
return function render(node) {
203204
switch (node.type) {
@@ -284,7 +285,7 @@ function renderNode(createElement, references) {
284285
renderChildren(node.inlineContent)
285286
));
286287
case InlineType.image: {
287-
if (node.metadata && node.metadata.anchor) {
288+
if (node.metadata && (node.metadata.anchor || node.metadata.abstract)) {
288289
return renderFigure(node);
289290
}
290291

src/components/ContentNode/Figure.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
props: {
1919
anchor: {
2020
type: String,
21-
required: true,
21+
required: false,
2222
},
2323
},
2424
};

src/components/ContentNode/FigureCaption.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<template>
1212
<figcaption class="caption">
13-
<strong>{{ title }}</strong>&nbsp;<slot />
13+
<strong v-if="title">{{ title }}</strong>&nbsp;<slot />
1414
</figcaption>
1515
</template>
1616

@@ -20,7 +20,7 @@ export default {
2020
props: {
2121
title: {
2222
type: String,
23-
required: true,
23+
required: false,
2424
},
2525
},
2626
};

tests/unit/components/ContentNode.spec.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,83 @@ describe('ContentNode', () => {
431431
expect(caption.props('title')).toBe(metadata.title);
432432
expect(caption.text()).toContain('blah');
433433
});
434+
435+
it('renders a `Figure`/`FigureCaption` without an anchor, with text under the image', () => {
436+
const metadata = {
437+
abstract: [{
438+
type: 'paragraph',
439+
inlineContent: [{ type: 'text', text: 'blah' }],
440+
}],
441+
};
442+
const wrapper = mountWithItem({
443+
type: 'image',
444+
identifier: 'figure1.png',
445+
metadata,
446+
}, references);
447+
448+
const figure = wrapper.find(Figure);
449+
expect(figure.exists()).toBe(true);
450+
expect(figure.props('anchor')).toBeFalsy();
451+
expect(figure.contains(InlineImage)).toBe(true);
452+
453+
const caption = wrapper.find(FigureCaption);
454+
expect(caption.exists()).toBe(true);
455+
expect(caption.contains('p')).toBe(true);
456+
expect(caption.props('title')).toBeFalsy();
457+
expect(caption.text()).toContain('blah');
458+
// assert figurerecaption is below the image
459+
expect(figure.html()).toMatchInlineSnapshot(`
460+
<figure-stub>
461+
<inlineimage-stub alt="" variants="[object Object],[object Object]"></inlineimage-stub>
462+
<figurecaption-stub>
463+
<p>blah</p>
464+
</figurecaption-stub>
465+
</figure-stub>
466+
`);
467+
});
468+
469+
it('renders a `FigureCaption` before the image, if it has a title', () => {
470+
const metadata = {
471+
title: 'foo',
472+
abstract: [{
473+
type: 'paragraph',
474+
inlineContent: [{ type: 'text', text: 'blah' }],
475+
}],
476+
};
477+
const wrapper = mountWithItem({
478+
type: 'image',
479+
identifier: 'figure1.png',
480+
metadata,
481+
}, references);
482+
expect(wrapper.find(Figure).html()).toMatchInlineSnapshot(`
483+
<figure-stub>
484+
<figurecaption-stub title="foo">
485+
<p>blah</p>
486+
</figurecaption-stub>
487+
<inlineimage-stub alt="" variants="[object Object],[object Object]"></inlineimage-stub>
488+
</figure-stub>
489+
`);
490+
});
491+
492+
it('renders no `FigureCaption`, if there is a `title`, but no `abstract`', () => {
493+
const metadata = {
494+
postTitle: true,
495+
title: 'Foo',
496+
anchor: 'foo-figure',
497+
};
498+
const wrapper = mountWithItem({
499+
type: 'image',
500+
identifier: 'figure1.png',
501+
metadata,
502+
}, references);
503+
504+
const figure = wrapper.find(Figure);
505+
expect(figure.exists()).toBe(true);
506+
expect(figure.props('anchor')).toBe('foo-figure');
507+
expect(figure.contains(InlineImage)).toBe(true);
508+
509+
expect(wrapper.find(FigureCaption).exists()).toBe(false);
510+
});
434511
});
435512

436513
describe('with type="link"', () => {

tests/unit/components/ContentNode/Figure.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,15 @@ describe('Figure', () => {
2525
expect(p.exists()).toBe(true);
2626
expect(p.text()).toBe('blah');
2727
});
28+
29+
it('renders a <figure> without an id, just slot content', () => {
30+
const wrapper = shallowMount(Figure, { slots });
31+
32+
expect(wrapper.is('figure')).toBe(true);
33+
expect(wrapper.attributes('id')).toBeFalsy();
34+
35+
const p = wrapper.find('p');
36+
expect(p.exists()).toBe(true);
37+
expect(p.text()).toBe('blah');
38+
});
2839
});

tests/unit/components/ContentNode/FigureCaption.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ describe('FigureCaption', () => {
2020
expect(wrapper.is('figcaption')).toBe(true);
2121
expect(wrapper.text()).toMatch(/Figure 1\sBlah/);
2222
});
23+
it('renders a <figcaption> with slot content only', () => {
24+
const slots = { default: '<p>Blah</p>' };
25+
const wrapper = shallowMount(FigureCaption, { slots });
26+
27+
expect(wrapper.is('figcaption')).toBe(true);
28+
expect(wrapper.text()).toBe('Blah');
29+
});
2330
});

0 commit comments

Comments
 (0)