Skip to content

Commit 1468878

Browse files
committed
feat(tmpl): css_ref
1 parent 18146d1 commit 1468878

File tree

10 files changed

+417
-26
lines changed

10 files changed

+417
-26
lines changed

Diff for: Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: crates/rari-doc/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ imagesize = "0.13"
3636
svg_metadata = "0.5"
3737
itertools = "0.13"
3838
memoize = "0.4"
39+
concat-in-place = "1"
40+
indexmap = "2"
3941

4042
rari-utils = { path = "../rari-utils" }
4143
rari-types = { path = "../rari-types" }

Diff for: crates/rari-doc/src/helpers/css_info.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::borrow::Cow;
2-
use std::collections::HashMap;
32
use std::fmt::Write;
43
use std::sync::OnceLock;
54

5+
use indexmap::IndexMap;
66
use itertools::Itertools;
77
use rari_l10n::l10n_json_data;
88
use rari_types::globals::data_dir;
@@ -20,9 +20,13 @@ use crate::templ::templs::links::cssxref::cssxref_internal;
2020
// mdn/data is deprecated so we do a least effort integration here.
2121
#[derive(Debug, Default)]
2222
pub struct MDNDataFiles {
23-
pub css_properties: HashMap<String, Value>,
24-
pub css_at_rules: HashMap<String, Value>,
25-
pub css_l10n: HashMap<String, Value>,
23+
pub css_properties: IndexMap<String, Value>,
24+
pub css_at_rules: IndexMap<String, Value>,
25+
pub css_types: IndexMap<String, Value>,
26+
pub css_l10n: IndexMap<String, Value>,
27+
pub css_syntaxes: IndexMap<String, Value>,
28+
pub css_seclectors: IndexMap<String, Value>,
29+
pub css_units: IndexMap<String, Value>,
2630
}
2731

2832
impl MDNDataFiles {
@@ -37,6 +41,18 @@ impl MDNDataFiles {
3741
css_l10n: serde_json::from_str(&read_to_string(
3842
data_dir().join("mdn-data/package/l10n/css.json"),
3943
)?)?,
44+
css_types: serde_json::from_str(&read_to_string(
45+
data_dir().join("mdn-data/package/css/types.json"),
46+
)?)?,
47+
css_syntaxes: serde_json::from_str(&read_to_string(
48+
data_dir().join("mdn-data/package/css/syntaxes.json"),
49+
)?)?,
50+
css_seclectors: serde_json::from_str(&read_to_string(
51+
data_dir().join("mdn-data/package/css/selectors.json"),
52+
)?)?,
53+
css_units: serde_json::from_str(&read_to_string(
54+
data_dir().join("mdn-data/package/css/units.json"),
55+
)?)?,
4056
})
4157
}
4258
}

Diff for: crates/rari-doc/src/html/links.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22

3+
use rari_md::anchor::anchorize;
34
use rari_types::fm_types::FeatureStatus;
45
use rari_types::locale::Locale;
56
use tracing::warn;
@@ -28,7 +29,7 @@ pub fn render_internal_link(
2829
out.push_str(url);
2930
if let Some(anchor) = anchor {
3031
out.push('#');
31-
out.push_str(anchor);
32+
out.push_str(&anchorize(anchor));
3233
}
3334
if let Some(title) = title {
3435
out.push_str("\" title=\"");
@@ -136,9 +137,17 @@ pub fn render_link_via_page(
136137

137138
out.push_str("<a href=\"");
138139
let content = match content {
139-
Some(content) => &html_escape::encode_safe(content),
140-
None if url.starts_with('/') => &url[url.rfind('/').unwrap_or(0)..],
141-
_ => &html_escape::encode_safe(&url),
140+
Some(content) => {
141+
let decoded_content = html_escape::decode_html_entities(content);
142+
let encoded_content = html_escape::encode_safe(&decoded_content);
143+
if content != encoded_content {
144+
Cow::Owned(encoded_content.into_owned())
145+
} else {
146+
Cow::Borrowed(content)
147+
}
148+
}
149+
None if url.starts_with('/') => Cow::Borrowed(&url[url.rfind('/').unwrap_or(0)..]),
150+
_ => html_escape::encode_safe(&url),
142151
};
143152
out.push_str(&url);
144153
if let Some(title) = title {
@@ -149,7 +158,7 @@ pub fn render_link_via_page(
149158
if code {
150159
out.push_str("<code>");
151160
}
152-
out.push_str(content);
161+
out.push_str(&content);
153162
if code {
154163
out.push_str("</code>");
155164
}

Diff for: crates/rari-doc/src/html/modifier.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,27 @@ pub fn add_missing_ids(html: &mut Html) -> Result<(), DocError> {
5656
el.text().collect::<String>()
5757
};
5858
let mut id = anchorize(&text);
59-
if ids.contains(id.as_str()) {
59+
if ids.contains(id.as_ref()) {
6060
let (prefix, mut count) = if let Some((prefix, counter)) = id.rsplit_once('_') {
6161
if counter.chars().all(|c| c.is_ascii_digit()) {
6262
let count = counter.parse::<i64>().unwrap_or_default() + 1;
6363
(prefix, count)
6464
} else {
65-
(id.as_str(), 2)
65+
(id.as_ref(), 2)
6666
}
6767
} else {
68-
(id.as_str(), 2)
68+
(id.as_ref(), 2)
6969
};
7070
let mut new_id = format!("{prefix}_{count}");
7171
while ids.contains(new_id.as_str()) && count < 666 {
7272
count += 1;
7373
new_id = format!("{prefix}_{count}");
7474
}
75-
id = new_id;
75+
id = Cow::Owned(new_id);
7676
}
77-
78-
ids.insert(Cow::Owned(id.clone()));
79-
(el.id(), id)
77+
let id_ = id.to_string();
78+
ids.insert(Cow::Owned(id.into_owned()));
79+
(el.id(), id_)
8080
})
8181
.collect::<Vec<_>>();
8282
for (el_id, id) in subs {

Diff for: crates/rari-doc/src/templ/api.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use percent_encoding::utf8_percent_encode;
24
use rari_md::anchor::anchorize;
35
use rari_types::globals::{deny_warnings, settings};
@@ -11,7 +13,7 @@ use crate::redirects::resolve_redirect;
1113

1214
pub struct RariApi {}
1315
impl RariApi {
14-
pub fn anchorize(content: &str) -> String {
16+
pub fn anchorize(content: &str) -> Cow<'_, str> {
1517
anchorize(content)
1618
}
1719

0 commit comments

Comments
 (0)