Skip to content

Commit 4a7e16b

Browse files
Use <caption> for table captions instead of <figcaption> (#706)
Use <caption> for tables instead of <figcaption>. Tables with captions will no longer be embedded in a <figure> with a <figcaption>. Instead, the <table> will simply have a <caption> as its first element when needed. Co-authored-by: Marina Aísa <[email protected]>
1 parent d90bfef commit 4a7e16b

File tree

7 files changed

+169
-111
lines changed

7 files changed

+169
-111
lines changed

src/components/ContentNode.vue

+36-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import CodeVoice from './ContentNode/CodeVoice.vue';
1717
import DictionaryExample from './ContentNode/DictionaryExample.vue';
1818
import EndpointExample from './ContentNode/EndpointExample.vue';
1919
import Figure from './ContentNode/Figure.vue';
20-
import FigureCaption from './ContentNode/FigureCaption.vue';
20+
import Caption from './ContentNode/Caption.vue';
2121
import InlineImage from './ContentNode/InlineImage.vue';
2222
import Reference from './ContentNode/Reference.vue';
2323
import Table from './ContentNode/Table.vue';
@@ -31,6 +31,8 @@ import TaskList from './ContentNode/TaskList.vue';
3131
import LinksBlock from './ContentNode/LinksBlock.vue';
3232
import DeviceFrame from './ContentNode/DeviceFrame.vue';
3333

34+
const { CaptionPosition, CaptionTag } = Caption.constants;
35+
3436
export const BlockType = {
3537
aside: 'aside',
3638
codeListing: 'codeListing',
@@ -229,9 +231,16 @@ function renderNode(createElement, references) {
229231
const figureContent = [renderChildren([node])];
230232
if ((title && abstract.length) || abstract.length) {
231233
// if there is a `title`, it should be above, otherwise below
232-
figureContent.splice(title ? 0 : 1, 0,
233-
createElement(FigureCaption, {
234-
props: { title, centered: !title },
234+
const position = title ? CaptionPosition.leading : CaptionPosition.trailing;
235+
const index = position === CaptionPosition.trailing ? 1 : 0;
236+
const tag = CaptionTag.figcaption;
237+
figureContent.splice(index, 0,
238+
createElement(Caption, {
239+
props: {
240+
title,
241+
position,
242+
tag,
243+
},
235244
}, renderChildren(abstract)));
236245
}
237246
return createElement(Figure, { props: { anchor } }, figureContent);
@@ -297,18 +306,37 @@ function renderNode(createElement, references) {
297306
renderChildren(node.inlineContent)
298307
));
299308
}
300-
case BlockType.table:
301-
if (node.metadata && node.metadata.anchor) {
302-
return renderFigure(node);
309+
case BlockType.table: {
310+
const children = renderTableChildren(
311+
node.rows, node.header, node.extendedData, node.alignments,
312+
);
313+
314+
if (node.metadata && node.metadata.abstract) {
315+
const { title } = node.metadata;
316+
const position = title ? CaptionPosition.leading : CaptionPosition.trailing;
317+
const tag = CaptionTag.caption;
318+
children.unshift(createElement(Caption, {
319+
props: {
320+
title,
321+
position,
322+
tag,
323+
},
324+
}, (
325+
renderChildren(node.metadata.abstract)
326+
)));
303327
}
304328

305329
return createElement(Table, {
330+
attrs: {
331+
id: node.metadata && node.metadata.anchor,
332+
},
306333
props: {
307334
spanned: !!node.extendedData,
308335
},
309336
}, (
310-
renderTableChildren(node.rows, node.header, node.extendedData, node.alignments)
337+
children
311338
));
339+
}
312340
case BlockType.termList:
313341
return createElement('dl', {}, node.items.map(({ term, definition }) => [
314342
createElement('dt', {}, (
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<!--
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
-->
10+
11+
<template>
12+
<component class="caption" :class="{ trailing }" :is="tag">
13+
<template v-if="title">
14+
<strong>{{ title }}</strong>&nbsp;<slot />
15+
</template>
16+
<template v-else>
17+
<slot />
18+
</template>
19+
</component>
20+
</template>
21+
22+
<script>
23+
const CaptionTag = {
24+
caption: 'caption',
25+
figcaption: 'figcaption',
26+
};
27+
28+
const CaptionPosition = {
29+
leading: 'leading', // before element and left aligned
30+
trailing: 'trailing', // after element and center aligned
31+
};
32+
33+
export default {
34+
name: 'Caption',
35+
constants: {
36+
CaptionPosition,
37+
CaptionTag,
38+
},
39+
props: {
40+
title: {
41+
type: String,
42+
required: false,
43+
},
44+
tag: {
45+
type: String,
46+
required: true,
47+
validator: v => Object.hasOwnProperty.call(CaptionTag, v),
48+
},
49+
position: {
50+
type: String,
51+
default: () => CaptionPosition.leading,
52+
validator: v => Object.hasOwnProperty.call(CaptionPosition, v),
53+
},
54+
},
55+
computed: {
56+
trailing: ({ position }) => position === CaptionPosition.trailing,
57+
},
58+
};
59+
</script>
60+
61+
<style scoped lang="scss">
62+
@import 'docc-render/styles/_core.scss';
63+
64+
.caption {
65+
@include font-styles(documentation-caption);
66+
margin: 0 0 var(--spacing-stacked-margin-large) 0;
67+
68+
&.trailing {
69+
margin: var(--spacing-stacked-margin-large) 0 0 0;
70+
text-align: center;
71+
}
72+
}
73+
74+
// for <caption> specifically since it must be the first element in a <table>
75+
// even when displayed at the bottom using this property
76+
caption.trailing {
77+
caption-side: bottom;
78+
}
79+
80+
/deep/ p {
81+
display: inline-block;
82+
}
83+
</style>

src/components/ContentNode/Figure.vue

-6
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,3 @@ export default {
2323
},
2424
};
2525
</script>
26-
27-
<style scoped lang="scss">
28-
/deep/ figcaption + * {
29-
margin-top: 1rem;
30-
}
31-
</style>

src/components/ContentNode/FigureCaption.vue

-56
This file was deleted.

src/styles/core/typography/_font-styles.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ $font-styles: (
153153
documentation-code-listing-number: (
154154
large: 12_18_normal_compact_mono,
155155
),
156-
documentation-figcaption: (
156+
documentation-caption: (
157157
large: 14_21,
158158
),
159159
documentation-declaration-link: (

0 commit comments

Comments
 (0)