Skip to content

Commit ca2f1e3

Browse files
LeoMcAfiji-flo
andauthored
feat(macros): add InteractiveExample macro (#84)
* feat(macros): add InteractiveExample macro * handle interactive-example code-example class * add name argument to macro * render height optional and minor refactor --------- Co-authored-by: Florian Dieminger <[email protected]>
1 parent 3ce09de commit ca2f1e3

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

crates/rari-doc/src/html/rewriter.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ pub fn post_process_html<T: PageLike>(
124124
let class = el.get_attribute("class");
125125
let class = class.as_deref().unwrap_or_default();
126126
let is_hidden = class.split_ascii_whitespace().any(|c| c == "hidden");
127+
let is_interactive_example = class
128+
.split_ascii_whitespace()
129+
.any(|c| c.starts_with("interactive-example"));
127130
let name = class
128131
.split_ascii_whitespace()
129132
.skip_while(|s| *s != "brush:")
@@ -134,7 +137,7 @@ pub fn post_process_html<T: PageLike>(
134137
el.prepend("<code>", ContentType::Html);
135138
el.append("</code>", ContentType::Html);
136139
}
137-
if is_hidden {
140+
if is_hidden || is_interactive_example {
138141
el.before(r#"<div class="code-example">"#, ContentType::Html);
139142
el.after("</div>", ContentType::Html);
140143
} else if !name.is_empty() && name != "plain" {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use html_escape::encode_double_quoted_attribute;
2+
use rari_templ_func::rari_f;
3+
use rari_utils::concat_strs;
4+
5+
use crate::error::DocError;
6+
use crate::helpers::l10n::l10n_json_data;
7+
use crate::templ::api::RariApi;
8+
9+
/// Adds an <interactive-example> element to the content
10+
///
11+
/// Parameters:
12+
/// $0 - Name of interactive example
13+
/// $1 - Optional custom height class to set on interactive-example element
14+
///
15+
/// Example call {{InteractiveExample("JavaScript Demo: Array.from()", "taller")}}
16+
#[rari_f]
17+
pub fn interactive_example(name: String, height: Option<String>) -> Result<String, DocError> {
18+
let title = l10n_json_data("Template", "interactive_example_cta", env.locale)?;
19+
let id = RariApi::anchorize(title);
20+
21+
let height = height
22+
.map(|height| {
23+
concat_strs!(
24+
r#" height="#,
25+
&encode_double_quoted_attribute(&height).as_ref(),
26+
r#"""#
27+
)
28+
})
29+
.unwrap_or_default();
30+
Ok(concat_strs!(
31+
r#"<h2 id=""#,
32+
&id,
33+
r#"">"#,
34+
title,
35+
"</h2>\n",
36+
r#"<interactive-example name=""#,
37+
encode_double_quoted_attribute(&name).as_ref(),
38+
r#"""#,
39+
&height,
40+
r#"></interactive-example>"#
41+
))
42+
}

crates/rari-doc/src/templ/templs/embeds/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ pub mod embed_gh_live_sample;
22
pub mod embed_interactive_example;
33
pub mod embed_live_sample;
44
pub mod embed_youtube;
5+
pub mod interactive_example;
56
pub mod jsfiddle_embed;
67
pub mod live_sample_link;

crates/rari-doc/src/templ/templs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub fn invoke(
103103
"embedinteractiveexample" => {
104104
embeds::embed_interactive_example::embed_interactive_example_any
105105
}
106+
"interactiveexample" => embeds::interactive_example::interactive_example_any,
106107
"embedghlivesample" => embeds::embed_gh_live_sample::embed_gh_live_sample_any,
107108
"embedlivesample" => embeds::embed_live_sample::embed_live_sample_any,
108109
"embedyoutube" => embeds::embed_youtube::embed_youtube_any,

0 commit comments

Comments
 (0)