Skip to content

Commit 3d70866

Browse files
Josh-Cenafiji-flo
andauthored
feat(sidebars/jsref): support temporal (#72)
* feat(sidebars/jsref): support temporal --------- Co-authored-by: Florian Dieminger <[email protected]>
1 parent c35af8e commit 3d70866

File tree

1 file changed

+55
-22
lines changed

1 file changed

+55
-22
lines changed

Diff for: crates/rari-doc/src/sidebars/jsref.rs

+55-22
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn sidebar(slug: &str, locale: Locale) -> Result<MetaSidebar, DocError> {
3232
}
3333
if !matches!(
3434
main_object.as_ref(),
35-
"Proxy" | "Atomics" | "Math" | "Intl" | "JSON" | "Reflect",
35+
"Proxy" | "Atomics" | "Math" | "Intl" | "JSON" | "Reflect" | "Temporal",
3636
) {
3737
// %Base% is the default inheritance when the class has no extends clause:
3838
// instances inherit from Object.prototype, and class inherits from Function.prototype
@@ -264,7 +264,7 @@ impl JSRefItem {
264264
}
265265
}
266266

267-
const ASYNC_GENERATOR: &[Cow<'static, str>] = &[Cow::Borrowed("AsyncGenerator")];
267+
const ASYNC_ITERATOR: &[Cow<'static, str>] = &[Cow::Borrowed("AsyncGenerator")];
268268
const FUNCTION: &[Cow<'static, str>] = &[
269269
Cow::Borrowed("AsyncFunction"),
270270
Cow::Borrowed("AsyncGeneratorFunction"),
@@ -275,6 +275,7 @@ const TYPED_ARRAY: &[Cow<'static, str>] = &[
275275
Cow::Borrowed("TypedArray"),
276276
Cow::Borrowed("BigInt64Array"),
277277
Cow::Borrowed("BigUint64Array"),
278+
Cow::Borrowed("Float16Array"),
278279
Cow::Borrowed("Float32Array"),
279280
Cow::Borrowed("Float64Array"),
280281
Cow::Borrowed("Int8Array"),
@@ -297,12 +298,18 @@ const ERROR: &[Cow<'static, str>] = &[
297298
Cow::Borrowed("URIError"),
298299
];
299300

301+
static INTL_SUBPAGES: LazyLock<Vec<Cow<'static, str>>> =
302+
LazyLock::new(|| namespace_subpages("Intl"));
303+
static TEMPORAL_SUBPAGES: LazyLock<Vec<Cow<'static, str>>> =
304+
LazyLock::new(|| namespace_subpages("Temporal"));
305+
300306
// Related pages
301-
fn get_group(main_obj: &str, inheritance: &[Cow<'_, str>]) -> Vec<Cow<'static, str>> {
302-
static GROUP_DATA: LazyLock<Vec<&[Cow<'static, str>]>> = LazyLock::new(|| {
307+
pub fn get_group(main_obj: &str, inheritance: &[Cow<'_, str>]) -> Vec<Cow<'static, str>> {
308+
static GROUP_DATA: LazyLock<Vec<&[Cow<'_, str>]>> = LazyLock::new(|| {
303309
vec![
304310
ERROR,
305311
&INTL_SUBPAGES,
312+
&TEMPORAL_SUBPAGES,
306313
&[
307314
Cow::Borrowed("Intl/Segmenter/segment/Segments"),
308315
Cow::Borrowed("Intl.Segmenter"),
@@ -312,7 +319,7 @@ fn get_group(main_obj: &str, inheritance: &[Cow<'_, str>]) -> Vec<Cow<'static, s
312319
]
313320
});
314321
for g in GROUP_DATA.iter() {
315-
if g.iter().any(|x| main_obj == x) {
322+
if g.contains(&Cow::Borrowed(main_obj)) {
316323
return g
317324
.iter()
318325
.filter(|x| !inheritance.contains(x))
@@ -325,7 +332,7 @@ fn get_group(main_obj: &str, inheritance: &[Cow<'_, str>]) -> Vec<Cow<'static, s
325332

326333
fn inheritance_data(obj: &str) -> Option<&str> {
327334
match obj {
328-
o if ASYNC_GENERATOR.iter().any(|x| x == o) => Some("AsyncIterator"),
335+
o if ASYNC_ITERATOR.iter().any(|x| x == o) => Some("AsyncIterator"),
329336
o if FUNCTION.iter().any(|x| x == o) => Some("Function"),
330337
o if ITERATOR.iter().any(|x| x == o) => Some("Iterator"),
331338
o if TYPED_ARRAY[1..].iter().any(|x| x == o) => Some("TypedArray"),
@@ -334,21 +341,31 @@ fn inheritance_data(obj: &str) -> Option<&str> {
334341
}
335342
}
336343

337-
static INTL_SUBPAGES: LazyLock<Vec<Cow<'static, str>>> = LazyLock::new(|| {
338-
once(Cow::Borrowed("Intl"))
344+
/// Intl and Temporal are big namespaces with many classes underneath it. The classes
345+
/// are shown as related pages.
346+
fn namespace_subpages(namespace: &str) -> Vec<Cow<'_, str>> {
347+
once(Cow::Borrowed(namespace))
339348
.chain(
340349
get_sub_pages(
341-
"/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl",
350+
&format!(
351+
"/en-US/docs/Web/JavaScript/Reference/Global_Objects/{}",
352+
namespace
353+
),
342354
Some(1),
343355
Default::default(),
344356
)
345357
.unwrap_or_default()
346358
.iter()
347-
.filter(|page| page.page_type() == PageType::JavascriptClass)
359+
.filter(|page| {
360+
matches!(
361+
page.page_type(),
362+
PageType::JavascriptClass | PageType::JavascriptNamespace
363+
)
364+
})
348365
.map(|page| Cow::Owned(slug_to_object_name(page.slug()).to_string())),
349366
)
350367
.collect()
351-
});
368+
}
352369

353370
fn slug_to_object_name(slug: &str) -> Cow<'_, str> {
354371
let sub_path = slug
@@ -360,18 +377,24 @@ fn slug_to_object_name(slug: &str) -> Cow<'_, str> {
360377
if sub_path.starts_with("Proxy/Proxy") {
361378
return "Proxy/handler".into();
362379
}
363-
if let Some(intl) = sub_path.strip_prefix("Intl/") {
364-
if intl
365-
.chars()
366-
.next()
367-
.map(|c| c.is_ascii_lowercase())
368-
.unwrap_or_default()
369-
{
370-
return "Intl".into();
380+
for namespace in &["Intl/", "Temporal/"] {
381+
if let Some(sub_sub_path) = sub_path.strip_prefix(namespace) {
382+
if sub_sub_path
383+
.chars()
384+
.next()
385+
.map(|c| c.is_ascii_lowercase())
386+
.unwrap_or_default()
387+
{
388+
return Cow::Borrowed(&namespace[..namespace.len() - 1]);
389+
}
390+
return Cow::Owned(
391+
sub_path[..sub_sub_path
392+
.find('/')
393+
.map(|i| i + namespace.len())
394+
.unwrap_or(sub_path.len())]
395+
.replace('/', "."),
396+
);
371397
}
372-
return Cow::Owned(
373-
sub_path[..intl.find('/').map(|i| i + 5).unwrap_or(sub_path.len())].replace('/', "."),
374-
);
375398
}
376399

377400
sub_path[..sub_path.find('/').unwrap_or(sub_path.len())].into()
@@ -397,6 +420,16 @@ mod test {
397420
),
398421
"Intl.DateTimeFormat"
399422
);
423+
assert_eq!(
424+
slug_to_object_name("Web/JavaScript/Reference/Global_Objects/Temporal/Now"),
425+
"Temporal.Now"
426+
);
427+
assert_eq!(
428+
slug_to_object_name(
429+
"Web/JavaScript/Reference/Global_Objects/Temporal/Now/plainDateISO"
430+
),
431+
"Temporal.Now"
432+
);
400433
assert_eq!(
401434
slug_to_object_name(
402435
"Web/JavaScript/Reference/Global_Objects/ArrayBuffer/maxByteLength"

0 commit comments

Comments
 (0)