Skip to content

Commit 04e52eb

Browse files
authored
Render top-level "Attributes" primary content section when relevant (#797)
Resolves: rdar://123114458
1 parent ddc2bf0 commit 04e52eb

File tree

7 files changed

+144
-1
lines changed

7 files changed

+144
-1
lines changed

src/components/DocumentationTopic/PrimaryContent.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
</template>
2121

2222
<script>
23+
import Attributes
24+
from 'docc-render/components/DocumentationTopic/PrimaryContent/Attributes.vue';
2325
import PossibleValues
2426
from 'docc-render/components/DocumentationTopic/PrimaryContent/PossibleValues.vue';
2527
import RestEndpoint
@@ -37,6 +39,7 @@ import Mentions from './PrimaryContent/Mentions.vue';
3739
export default {
3840
name: 'PrimaryContent',
3941
components: {
42+
Attributes,
4043
ContentNode,
4144
Parameters,
4245
PropertyListKeyDetails,
@@ -70,6 +73,7 @@ export default {
7073
methods: {
7174
componentFor(section) {
7275
return {
76+
[SectionKind.attributes]: Attributes,
7377
[SectionKind.content]: ContentNode,
7478
[SectionKind.details]: PropertyListKeyDetails,
7579
[SectionKind.parameters]: Parameters,
@@ -86,6 +90,7 @@ export default {
8690
},
8791
propsFor(section) {
8892
const {
93+
attributes,
8994
bodyContentType,
9095
content,
9196
details,
@@ -99,6 +104,7 @@ export default {
99104
mentions,
100105
} = section;
101106
return {
107+
[SectionKind.attributes]: { attributes },
102108
[SectionKind.content]: { content },
103109
[SectionKind.details]: { details },
104110
[SectionKind.parameters]: { parameters },
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!--
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2024 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+
<section class="attributes">
13+
<LinkableHeading :anchor="section.anchor" :level="section.level">
14+
{{ $t(section.title) }}
15+
</LinkableHeading>
16+
<ParameterAttributes :attributes="attributes" />
17+
</section>
18+
</template>
19+
20+
<script>
21+
import { PrimaryContentSectionAnchors } from 'docc-render/constants/ContentSectionAnchors';
22+
import { SectionKind } from 'docc-render/constants/PrimaryContentSection';
23+
import LinkableHeading from 'docc-render/components/ContentNode/LinkableHeading.vue';
24+
import ParameterAttributes from 'docc-render/components/DocumentationTopic/PrimaryContent/ParameterAttributes.vue';
25+
26+
export default {
27+
name: 'Attributes',
28+
components: {
29+
LinkableHeading,
30+
ParameterAttributes,
31+
},
32+
props: {
33+
attributes: {
34+
type: Array,
35+
required: true,
36+
},
37+
},
38+
computed: {
39+
section: ({ sectionKind }) => PrimaryContentSectionAnchors[sectionKind],
40+
sectionKind: () => SectionKind.attributes,
41+
},
42+
};
43+
</script>
44+
45+
<style scoped lang="scss">
46+
@import '@/styles/_core.scss';
47+
48+
.parameter-attributes {
49+
margin-left: 1rem;
50+
}
51+
52+
:deep(.property-metadata) {
53+
color: currentColor;
54+
}
55+
</style>

src/constants/ContentSectionAnchors.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This source file is part of the Swift.org open source project
33
*
4-
* Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
4+
* Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
55
* Licensed under Apache License v2.0 with Runtime Library Exception
66
*
77
* See https://swift.org/LICENSE.txt for license information
@@ -34,6 +34,11 @@ export const MainContentSectionAnchors = {
3434
};
3535

3636
export const PrimaryContentSectionAnchors = {
37+
[SectionKind.attributes]: {
38+
title: 'sections.attributes',
39+
anchor: 'attributes',
40+
level: 2,
41+
},
3742
[SectionKind.details]: {
3843
title: 'sections.details',
3944
anchor: 'details',

src/constants/PrimaryContentSection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// eslint-disable-next-line import/prefer-default-export
1212
export const SectionKind = {
13+
attributes: 'attributes',
1314
content: 'content',
1415
declarations: 'declarations',
1516
mentions: 'mentions',

src/lang/locales/en-US.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
"close": "Close"
9696
},
9797
"sections": {
98+
"attributes": "Attributes",
9899
"title": "Section {number}",
99100
"on-this-page": "On this page",
100101
"topics": "Topics",

tests/unit/components/DocumentationTopic/PrimaryContent.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { shallowMount } from '@vue/test-utils';
1212
import PrimaryContent from 'docc-render/components/DocumentationTopic/PrimaryContent.vue';
1313

1414
const {
15+
Attributes,
1516
ContentNode,
1617
Parameters,
1718
PossibleValues,
@@ -23,6 +24,17 @@ const {
2324
} = PrimaryContent.components;
2425
const { SectionKind } = PrimaryContent.constants;
2526

27+
const attributesSection = {
28+
kind: SectionKind.attributes,
29+
attributes: [
30+
{
31+
kind: 'default',
32+
title: 'Default',
33+
value: 'foobar',
34+
},
35+
],
36+
};
37+
2638
const genericContentSection = {
2739
kind: SectionKind.content,
2840
content: [
@@ -139,6 +151,7 @@ const propsData = {
139151
conformance: { availbilityPrefix: [], constraints: [] },
140152
source: { url: 'foo.com' },
141153
sections: [
154+
attributesSection,
142155
detailsSection,
143156
genericContentSection,
144157
parametersSection,
@@ -174,6 +187,7 @@ describe('PrimaryContent', () => {
174187
});
175188
}
176189

190+
checkProps(Attributes, { attributes: attributesSection.attributes });
177191
checkProps(PropertyListKeyDetails, { details: detailsSection.details });
178192
checkProps(ContentNode, { content: genericContentSection.content, tag: 'div' });
179193
checkProps(Parameters, { parameters: parametersSection.parameters });
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* This source file is part of the Swift.org open source project
3+
*
4+
* Copyright (c) 2024 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+
import { shallowMount } from '@vue/test-utils';
11+
12+
import { PrimaryContentSectionAnchors } from 'docc-render/constants/ContentSectionAnchors';
13+
import { SectionKind } from 'docc-render/constants/PrimaryContentSection';
14+
import Attributes from 'docc-render/components/DocumentationTopic/PrimaryContent/Attributes.vue';
15+
import LinkableHeading from 'docc-render/components/ContentNode/LinkableHeading.vue';
16+
import ParameterAttributes from 'docc-render/components/DocumentationTopic/PrimaryContent/ParameterAttributes.vue';
17+
18+
const propsData = {
19+
attributes: [
20+
{
21+
kind: 'default',
22+
title: 'Default',
23+
value: 'foobar',
24+
},
25+
],
26+
};
27+
28+
const createWrapper = (opts = {}) => shallowMount(Attributes, {
29+
propsData,
30+
...opts,
31+
});
32+
33+
describe('Attributes', () => {
34+
it('renders a section', () => {
35+
const wrapper = createWrapper();
36+
const section = wrapper.find('section.attributes');
37+
expect(section.exists()).toBe(true);
38+
});
39+
40+
it('renders a linkable heading', () => {
41+
const wrapper = createWrapper();
42+
const heading = wrapper.find(LinkableHeading);
43+
44+
const {
45+
anchor,
46+
level,
47+
title,
48+
} = PrimaryContentSectionAnchors[SectionKind.attributes];
49+
expect(heading.exists()).toBe(true);
50+
expect(heading.props('anchor')).toBe(anchor);
51+
expect(heading.props('level')).toBe(level);
52+
expect(heading.text()).toBe(title);
53+
});
54+
55+
it('renders a `ParameterAttributes`', () => {
56+
const wrapper = createWrapper();
57+
const attrs = wrapper.find(ParameterAttributes);
58+
expect(attrs.exists()).toBe(true);
59+
expect(attrs.props('attributes')).toEqual(propsData.attributes);
60+
});
61+
});

0 commit comments

Comments
 (0)