Skip to content

Commit

Permalink
refactor: change announment banner API
Browse files Browse the repository at this point in the history
  • Loading branch information
MR-Addict committed Jan 15, 2024
1 parent 927caa8 commit 9494819
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/page.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
cp example/assets/bin/* /usr/local/bin && chmod u+x /usr/local/bin/mdbook*
- name: Build book
run: mdbook build example
run: sed -i 54d example/book.toml && mdbook build example

- uses: peaceiris/actions-gh-pages@v3
with:
Expand Down
7 changes: 4 additions & 3 deletions example/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mathjax-support = true
copy-fonts = true
no-section-label = false
git-repository-url = "https://github.com/mr-addict/mdbook-embedify"
edit-url-template = "https://github.com/mr-addict/mdbook-embedify/edit/main/{path}"
edit-url-template = "https://github.com/mr-addict/mdbook-embedify/edit/main/example/{path}"
additional-css = ["assets/css/patch.css"]

[output.html.print]
Expand Down Expand Up @@ -51,9 +51,10 @@ heading-split-level = 3
copy-js = true

[preprocessor.embedify]
command = "target/release/mdbook-embedify.exe"
scroll-to-top.enable = true

announcement-banner.enable = true
announcement-banner.name = "0.2.2"
announcement-banner.style = "default"
announcement-banner.id = "0.2.2"
announcement-banner.theme = "default"
announcement-banner.message = "*Version **0.2.2** now has relased, check it out [here](https://github.com/MR-Addict/mdbook-embedify/releases/tag/0.2.2).*"
4 changes: 2 additions & 2 deletions example/src/global-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Below is a full list of apps that support global configuration:
scroll-to-top.enable = true

announcement-banner.enable = true
announcement-banner.name = "0.2.2"
announcement-banner.style = "default"
announcement-banner.id = "0.2.2"
announcement-banner.theme = "default"
announcement-banner.message = "*Version **0.2.2** now has relased, check it out [here](https://github.com/MR-Addict/mdbook-embedify/releases/tag/0.2.2).*"
```

Expand Down
2 changes: 1 addition & 1 deletion example/src/ignore-embeds.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ignore Embeds

Sometimes you may want preprocessors to ignore some embeds.
Sometimes you may want preprocessor to ignore some embeds.

You can do it by wrapping content that you want to ignore with below two comments:

Expand Down
12 changes: 6 additions & 6 deletions example/src/local/announcement-banner.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

| Option | Description | Required | Default |
| :------ | :----------------------------------------------------------- | :------- | :------ |
| name | Announcement name | Yes | - - |
| id | Announcement id | Yes | - - |
| message | Announcement message, supports markdown syntax | Yes | - - |
| style | Supports style: **default**, **ocean**, **forest**, **lava** | No | default |
| theme | Supports theme: **default**, **ocean**, **forest**, **lava** | No | default |

## Example

<!-- embed ignore begin -->

```text
{% embed announcement-banner name="0.2.2" style="default" message="*Version **0.2.2** now has relased, check it out [here](https://github.com/MR-Addict/mdbook-embedify/releases/tag/0.2.2).*" %}
{% embed announcement-banner id="0.2.2" theme="default" message="*Version **0.2.2** now has relased, check it out [here](https://github.com/MR-Addict/mdbook-embedify/releases/tag/0.2.2).*" %}
```

<!-- embed ignore end -->
Expand All @@ -23,9 +23,9 @@ However, you may want to enable it for the whole book. You can do this by adding
```toml
[preprocessor.embedify]
announcement-banner.enable = true
announcement-banner.name = "0.2.2"
announcement-banner.style = "default"
announcement-banner.id = "0.2.2"
announcement-banner.theme = "default"
announcement-banner.message = "*Version **0.2.2** now has relased, check it out [here](https://github.com/MR-Addict/mdbook-embedify/releases/tag/0.2.2).*"
```

Note that announcement banner name must be **unique**, otherwise it won't be shown if there is another announcement banner with the same name when user closed it.
Note that announcement banner id must be **unique**, otherwise it won't be shown if there is another announcement banner with the same id when user closed it.
11 changes: 5 additions & 6 deletions src/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ impl Preprocessor for Embed {

let announcement_banner_enabled =
utils::get_config_bool(config, "announcement-banner.enable", false);
let announcement_banner_name =
utils::get_config_string(config, "announcement-banner.name", "");
let announcement_banner_id = utils::get_config_string(config, "announcement-banner.id", "");
let announcement_banner_theme =
utils::get_config_string(config, "announcement-banner.theme", "default");
let announcement_banner_message =
utils::get_config_string(config, "announcement-banner.message", "");
let announcement_banner_style =
utils::get_config_string(config, "announcement-banner.style", "default");

book.for_each_mut(|item| {
if let mdbook::book::BookItem::Chapter(chapter) = item {
Expand All @@ -39,8 +38,8 @@ impl Preprocessor for Embed {
}
if announcement_banner_enabled {
chapter.content.push_str(&format!(
"\n{{% embed announcement-banner name=\"{}\" message=\"{}\" style=\"{}\" %}}\n",
announcement_banner_name, announcement_banner_message, announcement_banner_style
"\n{{% embed announcement-banner id=\"{}\" message=\"{}\" theme=\"{}\" %}}\n",
announcement_banner_id, announcement_banner_message, announcement_banner_theme
));
}
chapter.content = utils::render_embeds(chapter.content.clone());
Expand Down
14 changes: 7 additions & 7 deletions templates/announcement-banner.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
--site-announcement-bar-stripe-color2: #d1d5db;
}

.announcement-banner[data-style="ocean"] {
.announcement-banner[data-theme="ocean"] {
--site-announcement-bar-stripe-color1: #86b2f9;
--site-announcement-bar-stripe-color2: #7298ea;
}

.announcement-banner[data-style="forest"] {
.announcement-banner[data-theme="forest"] {
--site-announcement-bar-stripe-color1: #97f5d6;
--site-announcement-bar-stripe-color2: #6de0bf;
}

.announcement-banner[data-style="lava"] {
.announcement-banner[data-theme="lava"] {
--site-announcement-bar-stripe-color1: #fea3a3;
--site-announcement-bar-stripe-color2: #e57e7e;
}
Expand Down Expand Up @@ -62,17 +62,17 @@
}
</style>

<div class="announcement-banner" data-style="{% style|default %}" data-name="{% name %}">
<div class="announcement-banner" data-theme="{% theme|default %}" data-id="{% id %}">
{% message %} <button type="button">X</button>
</div>

<script>
const banner = document.querySelector(".announcement-banner");
const name = banner.getAttribute("data-name");
const id = banner.getAttribute("data-id");
const message = banner.querySelector("p").textContent;
const localData = JSON.parse(localStorage.getItem("mdbook-announcement-banner"));

if (localData && localData.hide === true && localData.name === name) banner.remove();
if (localData && localData.hide === true && localData.id === id) banner.remove();
else {
// move banner before page class element slibling
const page = document.querySelector(".page");
Expand All @@ -81,7 +81,7 @@
// add event listener to close button
banner.querySelector("button").addEventListener("click", () => {
banner.remove();
localStorage.setItem("mdbook-announcement-banner", JSON.stringify({ name, hide: true, message }));
localStorage.setItem("mdbook-announcement-banner", JSON.stringify({ id, hide: true, message }));
});
}
</script>

0 comments on commit 9494819

Please sign in to comment.