1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<script>
// Adds tags to documentation pages for common Bevy traits like `Component` or `Resource`.
// This makes it easier to see at a glance what types are used for.
//
// This extension is passed to `rustdoc` using the `--html-after-content` flag.
// Traits that we want to show as tags.
// Order determines sort order of items in listings.
const bevyTraits = [
'Plugin',
'PluginGroup',
'Component',
'Resource',
'Asset',
'Event',
'Message',
'ScheduleLabel',
'SystemSet',
'SystemParam',
'Relationship',
'RelationshipTarget'
];
// Find all traits that are implemented by the current type.
const implementedBevyTraits = findImplementedBevyTraits(document);
// If we found any implemented traits, add them as tags to the top of the page.
if (implementedBevyTraits.size > 0) {
// Create a container for the tags.
const heading = document.body.querySelector(".main-heading h1");
const tagContainer = document.createElement('div');
tagContainer.className = 'bevy-tag-container';
heading.appendChild(tagContainer);
// Check if an implemented trait has a `type Mutability = Immutable` associated type.
// This is used to determine if a `Component` is immutable or not.
// TODO: Ideally we should just check the associated types of the `Component` trait,
// but the docs.rs layout makes it tricky to do so in a robust way.
const associatedTypeHeader = document.querySelectorAll(".trait-impl.associatedtype .code-header");
const isImmutable = [...associatedTypeHeader].some(el => el.innerText.includes('type Mutability = Immutable'));
// Create a tag for each implemented trait.
for (let [tagName, href] of implementedBevyTraits) {
if (tagName == 'Component' & isImmutable) {
tagName = 'Immutable Component';
}
// Create the tag and append it to the container.
tagContainer.appendChild(createBevyTag(tagName, href));
}
}
function findImplementedBevyTraits(doc) {
// Traits that are implemented by the current type.
// The key is the trait name, and the value is the href to the trait's documentation.
const implementedTraits = new Map();
// Find all trait implementation headers.
const allTraitHeaders = doc.body.querySelectorAll(
'#trait-implementations-list .impl .code-header, #blanket-implementations-list .impl .code-header'
);
for (const header of allTraitHeaders) {
// We can extract the trait name by removing any generics and splitting the string by spaces.
// This results in ['impl', 'TraitName', 'for', 'TypeName'].
const traitName = removeGenerics(header.innerText).split(' ')[1].trim();
// Find the link to the trait if the anchor element exists.
// Otherwise, the trait is just in plain text.
const traitLinkEl = [...header.children].find(el => el.getAttribute('href')?.includes(`trait.${traitName}.html`));
const href = traitLinkEl?.getAttribute('href');
implementedTraits.set(traitName, href);
}
const implementedBevyTraits = new Map(
[...implementedTraits].filter(([traitName, _]) => bevyTraits.find((x) => x == traitName))
);
return implementedBevyTraits;
}
// Helper function to remove generics from a string of Rust code.
// For example, 'Vec<T>' would become 'Vec'.
function removeGenerics(str) {
// Remove the innermost generics.
const newStr = str.replace(/<([^<>])*>/g, '');
// If there are still generics, perform the removal again recursively.
if (newStr !== str) {
return removeGenerics(newStr);
}
// No more generics to remove.
return newStr;
}
// Helper function to create a tag element with the given name and href,
// if available.
function createBevyTag(tagName, href) {
const el = document.createElement('a');
const kebabCaseName = tagName.toLowerCase().replace(' ', '-');
if (href) {
el.setAttribute('href', href);
}
el.innerText = tagName;
el.className = `bevy-tag ${kebabCaseName}-tag`;
return el;
}
</script>
<style>
.bevy-tag-container {
padding: 0.5rem 0;
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.bevy-tag {
display: flex;
align-items: center;
width: fit-content;
height: 1.5rem;
padding: 0 0.5rem;
border-radius: 0.75rem;
font-size: 1rem;
font-weight: normal;
color: white;
}
.bevy-tag {
background-color: var(--tag-color);
}
.component-tag,
.immutable-component-tag {
--tag-color: oklch(50% 27% 80);
}
.resource-tag {
--tag-color: oklch(50% 27% 110);
}
.asset-tag {
--tag-color: oklch(50% 27% 0);
}
.event-tag {
--tag-color: oklch(50% 27% 310);
}
.message-tag {
--tag-color: oklch(50% 27% 190);
}
.plugin-tag,
.plugingroup-tag {
--tag-color: oklch(50% 27% 50);
}
.schedulelabel-tag,
.systemset-tag {
--tag-color: oklch(50% 27% 270);
}
.systemparam-tag {
--tag-color: oklch(50% 27% 240);
}
.relationship-tag,
.relationshiptarget-tag {
--tag-color: oklch(50% 27% 150);
}
</style>