Skip to content

Commit 7b3a208

Browse files
committed
feat: add additional information to final app rendering
1 parent 087aa40 commit 7b3a208

11 files changed

Lines changed: 203 additions & 26 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mdbook-embedify"
3-
version = "0.2.17"
3+
version = "0.2.18"
44
edition = "2021"
55
license = "MIT"
66
readme = "README.md"

docs/assets/js/patch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function appendVersion() {
22
const shield = document.createElement("span");
3-
shield.textContent = "0.2.17";
3+
shield.textContent = "0.2.18";
44
document.querySelector(".menu-title").appendChild(shield);
55
}
66

docs/book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ footer.enable = true
4949
footer.message = "Copyright © 2025 • Created with ❤️ by [MR-Addict](https://github.com/MR-Addict)"
5050

5151
announcement-banner.enable = true
52-
announcement-banner.id = "0.2.17"
52+
announcement-banner.id = "0.2.18"
5353
announcement-banner.message = "*New version [0.2.17](https://github.com/MR-Addict/mdbook-embedify/releases/tag/0.2.17) relased*"
5454

5555
giscus.enable = true

docs/src/development/template.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,19 @@ Let's create a **canvas** app - a simple drawable canvas component.
5353

5454
**Step 1: Create the template file** (`assets/templates/canvas.html`)
5555

56-
**Basic HTML structure with styling:**
56+
**Basic HTML:**
5757

5858
```html
5959
<div class="canvas-container">
6060
<canvas height="400"></canvas>
6161
</div>
62-
{% embed include file="assets/templates/canvas.html" range="4-15" type="raw" %}
6362
```
6463

65-
**Add interactive JavaScript:**
64+
**Add some styles:**
65+
66+
{% embed include file="assets/templates/canvas.html" range="4-15" %}
67+
68+
**Add interactive javaScript:**
6669

6770
{% embed include file="assets/templates/canvas.html" range="16-" %}
6871

docs/src/local/announcement-banner.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Announcement banner allows you put important messages at the top of the page. It
1212
## Example
1313

1414
```text
15-
{% embed-ignore announcement-banner id="0.2.17" message="*New version [0.2.17](https://github.com/MR-Addict/mdbook-embedify/releases/tag/0.2.17) relased*" %}
15+
{% embed-ignore announcement-banner id="0.2.18" message="*New version [0.2.17](https://github.com/MR-Addict/mdbook-embedify/releases/tag/0.2.17) relased*" %}
1616
```
1717

1818
This book's announcement banner is enabled, you can see it at the top of this page.

src/assets/scripts/include.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use crate::parser;
44
use mdbook::preprocess::PreprocessorContext;
55
use std::fs;
66

7-
fn wrap_content_in_code_block(content: &str, language: &str, lang_detected: &str) -> String {
8-
// check if the language is markdown, if so, wrapped by backticks
9-
if lang_detected == "markdown" {
7+
fn wrap_content_in_code_block(content: String, language: String, lang_detected: String) -> String {
8+
let backticks = if lang_detected == "markdown" {
109
// Count the maximum number of consecutive backticks in the content
1110
let max_backticks = content
1211
.lines()
@@ -22,12 +21,12 @@ fn wrap_content_in_code_block(content: &str, language: &str, lang_detected: &str
2221
.unwrap_or(2) // Default to 2 if no backticks found
2322
+ 1; // Add 1 to ensure we have enough backticks
2423

25-
let backticks = "`".repeat(max_backticks);
26-
27-
format!("{}{}\n{}\n{}", backticks, language, content, backticks)
24+
"`".repeat(max_backticks)
2825
} else {
29-
format!("```{}\n{}\n```", language, content)
30-
}
26+
"```".to_string()
27+
};
28+
29+
format!("{}{}\n{}\n{}", backticks, language, content, backticks)
3130
}
3231

3332
fn parse_include_range(input: String, max_line: usize) -> (usize, usize) {
@@ -73,12 +72,16 @@ pub fn include_script(
7372
options: Vec<parser::EmbedAppOption>,
7473
) -> Result<String, String> {
7574
// get the file path from the options
76-
let file_path = parser::get_option("file", options.clone());
77-
if file_path.is_none() {
75+
let file_path_option = parser::get_option("file", options.clone());
76+
if file_path_option.is_none() {
7877
return Err("Option file is required".to_string());
7978
}
80-
let file_path = ctx.root.join(&file_path.unwrap().value);
81-
let file_path = file_path.to_string_lossy().to_string();
79+
80+
let file_path = ctx
81+
.root
82+
.join(file_path_option.unwrap().value)
83+
.to_string_lossy()
84+
.to_string();
8285

8386
// read the file content
8487
if !std::path::Path::new(&file_path).exists() {
@@ -117,9 +120,5 @@ pub fn include_script(
117120
_ => lang_detected.clone(),
118121
};
119122

120-
Ok(wrap_content_in_code_block(
121-
&content,
122-
&language,
123-
&lang_detected,
124-
))
123+
Ok(wrap_content_in_code_block(content, language, lang_detected))
125124
}

src/embed.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,26 @@ fn render_embeds(ctx: &PreprocessorContext, chapter: Chapter, content: String) -
184184
}
185185

186186
// unwrap the result
187-
rendered.unwrap().unwrap()
187+
// Convert app.options to JSON string for data attribute
188+
let options_string = {
189+
let mut json_parts = Vec::new();
190+
for option in &app.options {
191+
json_parts.push(format!(
192+
"data-option-{}=\"{}\"",
193+
option.name,
194+
option.value
195+
));
196+
}
197+
format!("{}", json_parts.join(" "))
198+
};
199+
200+
format!(
201+
"<!-- {} -->\n\n<div data-embedify data-app=\"{}\" {} style=\"display:none\"></div>\n\n{}",
202+
input,
203+
app.name,
204+
options_string,
205+
rendered.unwrap().unwrap()
206+
)
188207
})
189208
.to_string();
190209

tests/test-book/expected/app-include.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This page tests the include app functionality for including source files with va
88

99
Include a Python file with automatic language detection:
1010

11+
<!-- {% embed include file="src/samples/hello.py" %} -->
12+
13+
<div data-embedify data-app="include" data-option-file="src/samples/hello.py" style="display:none"></div>
14+
1115
```python
1216
#!/usr/bin/env python3
1317

@@ -31,6 +35,10 @@ if __name__ == "__main__":
3135

3236
Include a JavaScript file with automatic language detection:
3337

38+
<!-- {% embed include file="src/samples/fibonacci.js" %} -->
39+
40+
<div data-embedify data-app="include" data-option-file="src/samples/fibonacci.js" style="display:none"></div>
41+
3442
```javascript
3543
function fibonacci(n) {
3644
if (n <= 1) {
@@ -54,6 +62,10 @@ console.log(generateSequence(10));
5462

5563
Include a JSON file with automatic language detection:
5664

65+
<!-- {% embed include file="src/samples/package.json" %} -->
66+
67+
<div data-embedify data-app="include" data-option-file="src/samples/package.json" style="display:none"></div>
68+
5769
```json
5870
{
5971
"name": "test-project",
@@ -82,6 +94,10 @@ Include a JSON file with automatic language detection:
8294

8395
Include only the first 5 lines of the Python file:
8496

97+
<!-- {% embed include file="src/samples/hello.py" range="-5" %} -->
98+
99+
<div data-embedify data-app="include" data-option-file="src/samples/hello.py" data-option-range="-5" style="display:none"></div>
100+
85101
```python
86102
#!/usr/bin/env python3
87103

@@ -94,6 +110,10 @@ def greet(name):
94110

95111
Include lines 3 through 8 of the JavaScript file:
96112

113+
<!-- {% embed include file="src/samples/fibonacci.js" range="3-8" %} -->
114+
115+
<div data-embedify data-app="include" data-option-file="src/samples/fibonacci.js" data-option-range="3-8" style="display:none"></div>
116+
97117
```javascript
98118
return n;
99119
}
@@ -107,6 +127,10 @@ function generateSequence(length) {
107127
108128
Include from line 10 to the end of the JavaScript file:
109129
130+
<!-- {% embed include file="src/samples/fibonacci.js" range="10-" %} -->
131+
132+
<div data-embedify data-app="include" data-option-file="src/samples/fibonacci.js" data-option-range="10-" style="display:none"></div>
133+
110134
```javascript
111135
for (let i = 0; i < length; i++) {
112136
sequence.push(fibonacci(i));
@@ -121,6 +145,10 @@ console.log(generateSequence(10));
121145

122146
Include only line 1 of the Python file:
123147

148+
<!-- {% embed include file="src/samples/hello.py" range="1-1" %} -->
149+
150+
<div data-embedify data-app="include" data-option-file="src/samples/hello.py" data-option-range="1-1" style="display:none"></div>
151+
124152
```python
125153
#!/usr/bin/env python3
126154
```
@@ -131,6 +159,10 @@ Include only line 1 of the Python file:
131159

132160
Include Python file but override language to text:
133161

162+
<!-- {% embed include file="src/samples/hello.py" lang="text" %} -->
163+
164+
<div data-embedify data-app="include" data-option-file="src/samples/hello.py" data-option-lang="text" style="display:none"></div>
165+
134166
```text
135167
#!/usr/bin/env python3
136168
@@ -154,6 +186,10 @@ if __name__ == "__main__":
154186

155187
Include JSON file but override language to JavaScript:
156188

189+
<!-- {% embed include file="src/samples/package.json" lang="javascript" %} -->
190+
191+
<div data-embedify data-app="include" data-option-file="src/samples/package.json" data-option-lang="javascript" style="display:none"></div>
192+
157193
```javascript
158194
{
159195
"name": "test-project",
@@ -182,6 +218,10 @@ Include JSON file but override language to JavaScript:
182218

183219
Include markdown file as raw content (not wrapped in code block):
184220

221+
<!-- {% embed include file="src/samples/config.md" type="raw" %} -->
222+
223+
<div data-embedify data-app="include" data-option-file="src/samples/config.md" data-option-type="raw" style="display:none"></div>
224+
185225
# Sample Configuration
186226

187227
This is a sample configuration file for testing include functionality.
@@ -208,6 +248,10 @@ This is a sample configuration file for testing include functionality.
208248

209249
Include lines 3-6 of Python file as raw content:
210250

251+
<!-- {% embed include file="src/samples/hello.py" range="3-6" type="raw" %} -->
252+
253+
<div data-embedify data-app="include" data-option-file="src/samples/hello.py" data-option-range="3-6" data-option-type="raw" style="display:none"></div>
254+
211255

212256
def greet(name):
213257
"""Greet someone with a hello message."""
@@ -219,6 +263,10 @@ def greet(name):
219263

220264
Include lines 1-3 of JavaScript file with Python syntax highlighting:
221265

266+
<!-- {% embed include file="src/samples/fibonacci.js" range="1-3" lang="python" %} -->
267+
268+
<div data-embedify data-app="include" data-option-file="src/samples/fibonacci.js" data-option-range="1-3" data-option-lang="python" style="display:none"></div>
269+
222270
```python
223271
function fibonacci(n) {
224272
if (n <= 1) {
@@ -229,6 +277,10 @@ function fibonacci(n) {
229277

230278
Include lines 2-4 of JSON file as raw content:
231279

280+
<!-- {% embed include file="src/samples/package.json" range="2-4" type="raw" %} -->
281+
282+
<div data-embedify data-app="include" data-option-file="src/samples/package.json" data-option-range="2-4" data-option-type="raw" style="display:none"></div>
283+
232284
"name": "test-project",
233285
"version": "1.0.0",
234286
"description": "A test project for include functionality",
@@ -237,6 +289,10 @@ Include lines 2-4 of JSON file as raw content:
237289

238290
Include lines 5-10 of Python file as raw content with language override:
239291

292+
<!-- {% embed include file="src/samples/hello.py" range="5-10" lang="bash" type="raw" %} -->
293+
294+
<div data-embedify data-app="include" data-option-file="src/samples/hello.py" data-option-range="5-10" data-option-lang="bash" data-option-type="raw" style="display:none"></div>
295+
240296
"""Greet someone with a hello message."""
241297
return f"Hello, {name}!"
242298

@@ -250,6 +306,10 @@ def main():
250306

251307
Test empty range defaults to full file:
252308

309+
<!-- {% embed include file="src/samples/hello.py" range="" %} -->
310+
311+
<div data-embedify data-app="include" data-option-file="src/samples/hello.py" data-option-range="" style="display:none"></div>
312+
253313
```python
254314
#!/usr/bin/env python3
255315

@@ -273,6 +333,10 @@ if __name__ == "__main__":
273333

274334
Test invalid range defaults to full file:
275335

336+
<!-- {% embed include file="src/samples/hello.py" range="invalid-range" %} -->
337+
338+
<div data-embedify data-app="include" data-option-file="src/samples/hello.py" data-option-range="invalid-range" style="display:none"></div>
339+
276340
```python
277341
#!/usr/bin/env python3
278342

@@ -296,6 +360,10 @@ if __name__ == "__main__":
296360

297361
Test range that goes beyond file length:
298362

363+
<!-- {% embed include file="src/samples/hello.py" range="1-1000" %} -->
364+
365+
<div data-embedify data-app="include" data-option-file="src/samples/hello.py" data-option-range="1-1000" style="display:none"></div>
366+
299367
```python
300368
#!/usr/bin/env python3
301369

0 commit comments

Comments
 (0)