-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <me@fiji-flo.de>
- Loading branch information
Showing
4 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
crates/rari-doc/src/templ/templs/embeds/interactive_example.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use html_escape::encode_double_quoted_attribute; | ||
use rari_templ_func::rari_f; | ||
use rari_utils::concat_strs; | ||
|
||
use crate::error::DocError; | ||
use crate::helpers::l10n::l10n_json_data; | ||
use crate::templ::api::RariApi; | ||
|
||
/// Adds an <interactive-example> element to the content | ||
/// | ||
/// Parameters: | ||
/// $0 - Name of interactive example | ||
/// $1 - Optional custom height class to set on interactive-example element | ||
/// | ||
/// Example call {{InteractiveExample("JavaScript Demo: Array.from()", "taller")}} | ||
#[rari_f] | ||
pub fn interactive_example(name: String, height: Option<String>) -> Result<String, DocError> { | ||
let title = l10n_json_data("Template", "interactive_example_cta", env.locale)?; | ||
let id = RariApi::anchorize(title); | ||
|
||
let height = height | ||
.map(|height| { | ||
concat_strs!( | ||
r#" height="#, | ||
&encode_double_quoted_attribute(&height).as_ref(), | ||
r#"""# | ||
) | ||
}) | ||
.unwrap_or_default(); | ||
Ok(concat_strs!( | ||
r#"<h2 id=""#, | ||
&id, | ||
r#"">"#, | ||
title, | ||
"</h2>\n", | ||
r#"<interactive-example name=""#, | ||
encode_double_quoted_attribute(&name).as_ref(), | ||
r#"""#, | ||
&height, | ||
r#"></interactive-example>"# | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters