-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaff.mjs
More file actions
202 lines (175 loc) · 5.65 KB
/
staff.mjs
File metadata and controls
202 lines (175 loc) · 5.65 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import fs from 'fs';
import yaml from 'js-yaml';
// This stuff is terrible and should be in a mini library, sorry.
function text(value) {
return { type: 'text', value };
}
function link(value, url) {
return { type: 'link', url, children: [text(value)] };
}
function paragraph(children) {
return { type: 'paragraph', children };
}
function strong(value) {
return { type: 'strong', children: [text(value)] };
}
function emphasis(value) {
return { type: 'emphasis', children: [text(value)] };
}
function image(src, alt) {
return { type: 'image', url: src, alt };
}
function heading(level, value, children = null) {
if (children) {
return { type: 'heading', depth: level, children };
}
return { type: 'heading', depth: level, children: [text(value)] };
}
function list(items, ordered = false) {
return {
type: ordered ? 'list' : 'list',
ordered,
children: items.map(item => ({ type: 'listItem', children: [paragraph([text(item)])] }))
};
}
function container(children, style = {}) {
return { type: 'container', children, style };
}
// Helper function to create a person's card with image and info
function createPersonCard(person) {
const personInfoChildren = [];
// Name heading with optional website link and pronouns
const nameChildren = [];
if (person.website) {
nameChildren.push(link(person.name, person.website));
} else {
nameChildren.push(text(person.name));
}
personInfoChildren.push(heading(3, '', nameChildren));
// Pronouns
if (person.pronouns) {
personInfoChildren.push(paragraph([text(person.pronouns)]));
}
// Email
if (person.email) {
personInfoChildren.push(paragraph([link(person.email, `mailto:${person.email}`)]));
}
// Office Hours
if (person['office-hours'] && person['office-hours'].length > 0) {
personInfoChildren.push(paragraph([strong('Office Hours:')]));
// Process office hours with structured format
const processedHours = person['office-hours'].map(hours => {
if (typeof hours === 'object' && hours.when) {
// Structured format: { when, where, link }
const hoursChildren = [];
// Add the time
hoursChildren.push(strong(hours.when));
// Add location if provided
if (hours.where) {
hoursChildren.push(text(', '));
hoursChildren.push(text(hours.where));
}
// Add link if provided
if (hours.link) {
hoursChildren.push(text(' ('));
hoursChildren.push(link('Join', hours.link));
hoursChildren.push(text(')'));
}
return { type: 'listItem', children: [paragraph(hoursChildren)] };
} else if (typeof hours === 'string') {
// Legacy string format - keep for backward compatibility
return { type: 'listItem', children: [paragraph([text(hours)])] };
}
return { type: 'listItem', children: [paragraph([text('Invalid office hours format')])] };
});
personInfoChildren.push({
type: 'list',
ordered: false,
children: processedHours
});
}
// About Me
if (person['about-me']) {
personInfoChildren.push(paragraph([strong('About Me: '), text(person['about-me'])]));
}
// Create the layout with image on left and info on right
if (person.photo) {
// Create a container with flexbox layout
return {
type: 'div',
class: 'staff-person-card',
children: [
// Image on the left
{
type: 'div',
class: 'staff-person-photo',
children: [
image(person.photo, `Photo of ${person.name}`)
]
},
// Info on the right
{
type: 'div',
class: 'staff-person-info',
children: personInfoChildren
}
]
};
} else {
// If no photo, just use the info without flex layout
return {
type: 'div',
class: 'staff-person-card-no-photo',
children: personInfoChildren
};
}
}
const staffDirective = {
name: 'staff',
doc: 'Staff directive presents a listing of staff information based on a YAML file',
arg: { type: String },
options: {},
run(data) {
const staffList = yaml.load(fs.readFileSync(data.arg).toString());
// Group staff by role
const staffByRole = {};
staffList.forEach(person => {
const role = person.role || 'Staff';
if (!staffByRole[role]) {
staffByRole[role] = [];
}
staffByRole[role].push(person);
});
// Define role order and pluralization
const roleOrder = ['Instructor', 'Teaching Assistant', 'Tutor'];
const rolePlurals = {
'Instructor': 'Instructors',
'Teaching Assistant': 'Teaching Assistants',
'Tutor': 'Tutors'
};
const children = [];
// Process each role group
roleOrder.forEach(role => {
if (staffByRole[role] && staffByRole[role].length > 0) {
const roleHeading = staffByRole[role].length === 1 ? role : (rolePlurals[role] || role + 's');
children.push(heading(2, roleHeading));
staffByRole[role].forEach(person => {
children.push(createPersonCard(person));
});
}
});
// Handle any remaining roles not in the predefined order
Object.keys(staffByRole).forEach(role => {
if (!roleOrder.includes(role)) {
const roleHeading = staffByRole[role].length === 1 ? role : (rolePlurals[role] || role + 's');
children.push(heading(2, roleHeading));
staffByRole[role].forEach(person => {
children.push(createPersonCard(person));
});
}
});
return children;
},
};
const plugin = { name: 'Staff Directive', directives: [staffDirective] };
export default plugin;