Skip to content

Commit 23eb4e3

Browse files
committed
feat: allow giscus dynamatically update theme based on book theme
1 parent a5c5898 commit 23eb4e3

File tree

14 files changed

+87
-79
lines changed

14 files changed

+87
-79
lines changed

docs/book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ giscus.repo-id = "R_kgDOLCxX0Q"
6060
giscus.category = "General"
6161
giscus.category-id = "DIC_kwDOLCxX0c4CdGx-"
6262
giscus.reactions-enabled = "1"
63-
giscus.theme = "light"
63+
giscus.theme = "book"
6464
giscus.lang = "en"
6565
giscus.loading = "eager"

docs/src/more-apps.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ In this section, I will show you how to add more apps to this preprocessor.
66

77
You may have some other apps that preprocessor doesn't support yet. However, it's very easy to add a new app based on project custom template engine.
88

9-
What we need to do is put a new app template in the `templates` folder. The template file name should be the app name ended with `.html`.
9+
What we need to do is put a new app template in the **templates** folder. The template file name should be the app name ended with **.html**.
1010

11-
For example we want to add a new app called `youtube`, then we could create a `youtube.html` under `templates` folder.
11+
For example we want to add a new app called **youtube**, then we could create a **youtube.html** under **templates** folder.
1212

1313
We know that we can use an iframe to embed a youtube video. Template file could be like this:
1414

@@ -25,9 +25,9 @@ We know that we can use an iframe to embed a youtube video. Template file could
2525

2626
> 💥Attention
2727
>
28-
> You can even add `style` and `js` content to the template file. But the `style` and `js` content should be put in the `style` and `js` blocks.
28+
> You can even add **style** and **js** content to the template file. But the style and js content should be put in the style and js **blocks**.
2929
30-
However, we want video `id` and `loading` strategy to be dynamic and loading strategy has default `lazy` value. So we can replace them with placeholders like this:
30+
However, we want video **id** and **loading** strategy to be dynamic and loading strategy has default **lazy** value. So we can replace them with placeholders like this:
3131

3232
```html
3333
<iframe
@@ -40,19 +40,19 @@ However, we want video `id` and `loading` strategy to be dynamic and loading str
4040
></iframe>
4141
```
4242

43-
The placeholder syntax is similar to a **function call** in programming languages, which is the combination of function `name` and `argument` wrapped by `{%` and `%}`.
43+
The placeholder syntax is similar to a **function call** in programming languages, which is the combination of function **name** and **argument** wrapped by **{%** and **%}**.
4444

4545
**Function name**
4646

47-
The preprocessor reconginzes two function names, `raw` and `markdown`. Which `raw` will keep inner value as it is, `markdown` will treat the inner value as markdown content and render it to be html.
47+
The preprocessor reconginzes two function names, **raw** and **markdown**. Which raw will keep inner value as it is, markdown will treat the inner value as markdown content and render it to be html.
4848

4949
**Function argument**
5050

51-
The inner value is key follwed by a default value in the form of `key=default_value`. If the key is not provided, the default value will be used.
51+
The inner value is key follwed by a default value in the form of **key=default_value**. If the key is not provided, the default value will be used.
5252

53-
So `raw(id)` means the placeholder will be replaced by the value of `id` key and id is not optional because it doesn't have a default value.
53+
So **raw(id)** means the placeholder will be replaced by the value of **id** key and id is not optional because it doesn't have a default value.
5454

55-
`raw(loading=lazy)` means the placeholder will be replaced by the value of `loading` key. If user doesn't provide the value, the default value `lazy` will be used. And means loading is optional.
55+
**raw(loading=lazy)** means the placeholder will be replaced by the value of **loading** key. If user doesn't provide the value, the default value **lazy** will be used. And means loading is optional.
5656

5757
## Build the project
5858

docs/src/third-party/bilibili.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
## Options
66

7-
| Option | Description | Required | Default |
8-
| :------ | :----------- | :------- | :------ |
9-
| id | Video ID | Yes | - - |
10-
| loading | Loading type | No | lazy |
7+
| Option | Description | Required | Default |
8+
| :------ | :------------------------------ | :------- | :------ |
9+
| id | Video ID | Yes | - - |
10+
| loading | Supports **lazy** and **eager** | No | lazy |
1111

1212
## Example
1313

docs/src/third-party/codepen.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
## Options
66

7-
| Option | Description | Required | Default |
8-
| :------ | :------------ | :------- | :------ |
9-
| user | username | Yes | - - |
10-
| slug | Project slug | Yes | - - |
11-
| height | Iframe height | No | 600 |
12-
| theme | Iframe theme | No | dark |
13-
| loading | Loading type | No | lazy |
7+
| Option | Description | Required | Default |
8+
| :------ | :------------------------------ | :------- | :------ |
9+
| user | username | Yes | - - |
10+
| slug | Project slug | Yes | - - |
11+
| height | Iframe height | No | 600 |
12+
| theme | Iframe theme | No | dark |
13+
| loading | Supports **lazy** and **eager** | No | lazy |
1414

1515
## Example
1616

docs/src/third-party/codesandbox.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
## Options
66

7-
| Option | Description | Required | Default |
8-
| :------ | :----------- | :------- | :------ |
9-
| id | Project ID | Yes | - - |
10-
| theme | Iframe theme | No | dark |
11-
| loading | Loading type | No | lazy |
7+
| Option | Description | Required | Default |
8+
| :------ | :------------------------------ | :------- | :------ |
9+
| id | Project ID | Yes | - - |
10+
| theme | Iframe theme | No | dark |
11+
| loading | Supports **lazy** and **eager** | No | lazy |
1212

1313
## Example
1414

docs/src/third-party/giscus.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44

55
## Options
66

7-
| Option | Description | Required | Default |
8-
| :---------------- | :--------------- | :------- | :------ |
9-
| repo | Repository | Yes | - - |
10-
| repo-id | Repository ID | Yes | - - |
11-
| category | Category | Yes | - - |
12-
| category-id | Category ID | Yes | - - |
13-
| reactions-enabled | Enable reactions | No | 1 |
14-
| theme | Theme | No | light |
15-
| lang | Language | No | en |
16-
| loading | Loading type | No | lazy |
7+
| Option | Description | Required | Default |
8+
| :---------------- | :--------------------------------------- | :------- | :------ |
9+
| repo | Repository | Yes | - - |
10+
| repo-id | Repository ID | Yes | - - |
11+
| category | Category | Yes | - - |
12+
| category-id | Category ID | Yes | - - |
13+
| reactions-enabled | Enable reactions | No | 1 |
14+
| theme | Supports **book**,**light** and **dark** | No | book |
15+
| lang | Language | No | en |
16+
| loading | Supports **lazy** and **eager** | No | lazy |
1717

1818
## Example
1919

2020
<!-- embed ignore begin -->
2121

2222
```text
23-
{% embed giscus repo="MR-Addict/mdbook-embedify" repo-id="R_kgDOLCxX0Q" category="General" category-id="DIC_kwDOLCxX0c4CdGx-" theme="light" loading="eager" %}
23+
{% embed giscus repo="MR-Addict/mdbook-embedify" repo-id="R_kgDOLCxX0Q" category="General" category-id="DIC_kwDOLCxX0c4CdGx-" theme="book" loading="eager" %}
2424
```
2525

2626
<!-- embed ignore end -->
@@ -36,7 +36,7 @@ giscus.repo-id = "R_kgDOLCxX0Q"
3636
giscus.category = "General"
3737
giscus.category-id = "DIC_kwDOLCxX0c4CdGx-"
3838
giscus.reactions-enabled = "1"
39-
giscus.theme = "light"
39+
giscus.theme = "book"
4040
giscus.lang = "en"
4141
giscus.loading = "eager"
4242
```
@@ -50,15 +50,15 @@ For exampe:
5050
**node.js installed**
5151

5252
```sh
53-
npx serve book
53+
npx serve book -p 3000
5454
```
5555

5656
Which will serve your book at [http://localhost:3000](http://localhost:3000).
5757

5858
**python installed**
5959

6060
```sh
61-
python -m http.server --directory book
61+
python -m http.server --directory book 8080
6262
```
6363

6464
Which will serve your book at [http://localhost:8080](http://localhost:8080).

docs/src/third-party/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Third party apps are apps that are hosted on third party sites. Below are all supported third party apps and its detailed options.
44

55
- [Gist](gist.md)
6+
- [Vimeo](vimeo.md)
67
- [Giscus](giscus.md)
78
- [Youtube](youtube.md)
89
- [Codepen](codepen.md)

docs/src/third-party/stackblitz.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
## Options
66

7-
| Option | Description | Required | Default |
8-
| :------ | :----------- | :------- | :------ |
9-
| id | Project ID | Yes | - - |
10-
| theme | Iframe theme | No | dark |
11-
| loading | Loading type | No | lazy |
7+
| Option | Description | Required | Default |
8+
| :------ | :------------------------------ | :------- | :------ |
9+
| id | Project ID | Yes | - - |
10+
| theme | Iframe theme | No | dark |
11+
| loading | Supports **lazy** and **eager** | No | lazy |
1212

1313
## Example
1414

docs/src/third-party/vimeo.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Vimeo is a video hosting platform that allows you to upload and share videos.
44

55
## Options
66

7-
| Option | Description | Required | Default |
8-
| :------ | :----------- | :------- | :------ |
9-
| id | Video ID | Yes | - - |
10-
| loading | Loading type | No | lazy |
7+
| Option | Description | Required | Default |
8+
| :------ | :------------------------------ | :------- | :------ |
9+
| id | Video ID | Yes | - - |
10+
| loading | Supports **lazy** and **eager** | No | lazy |
1111

1212
## Example
1313

docs/src/third-party/youtube.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
## Options
66

7-
| Option | Description | Required | Default |
8-
| :------ | :----------- | :------- | :------ |
9-
| id | Video ID | Yes | - - |
10-
| loading | Loading type | No | lazy |
7+
| Option | Description | Required | Default |
8+
| :------ | :------------------------------ | :------- | :------ |
9+
| id | Video ID | Yes | - - |
10+
| loading | Supports **lazy** and **eager** | No | lazy |
1111

1212
## Example
1313

templates/announcement-banner.html

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,25 @@
8686
background-color: transparent;
8787
}
8888
</style>
89-
9089
<div style="display: none" data-id="{% raw(id) %}" class="announcement-banner" data-theme="{% raw(theme=default) %}">
9190
{% markdown(message) %}
9291
<button type="button">X</button>
9392
</div>
94-
9593
<script>
96-
const banner = document.querySelector(".announcement-banner");
97-
const id = banner.getAttribute("data-id");
98-
const message = banner.querySelector("p").textContent;
99-
const localData = JSON.parse(localStorage.getItem("mdbook-announcement-banner"));
100-
101-
if (!localData || localData.id !== id || localData.hide !== true) {
102-
// update banner display style to be flex
103-
banner.style.display = "flex";
104-
105-
// move banner before page class element slibling
106-
const page = document.querySelector(".page");
107-
page.parentNode.insertBefore(banner, page);
108-
109-
// add event listener to close button
110-
banner.querySelector("button").addEventListener("click", () => {
111-
banner.remove();
112-
localStorage.setItem("mdbook-announcement-banner", JSON.stringify({ id, hide: true, message }));
113-
});
114-
}
94+
(() => {
95+
const banner = document.querySelector(".announcement-banner");
96+
const id = banner.getAttribute("data-id");
97+
const message = banner.querySelector("p").textContent;
98+
const localData = JSON.parse(localStorage.getItem("mdbook-announcement-banner"));
99+
100+
if (!localData || localData.id !== id || localData.hide !== true) {
101+
banner.style.display = "flex";
102+
const page = document.querySelector(".page");
103+
page.parentNode.insertBefore(banner, page);
104+
banner.querySelector("button").addEventListener("click", () => {
105+
banner.remove();
106+
localStorage.setItem("mdbook-announcement-banner", JSON.stringify({ id, hide: true, message }));
107+
});
108+
}
109+
})();
115110
</script>

templates/footer.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@
1212
margin: 0;
1313
}
1414
</style>
15-
1615
<footer>{% markdown(message) %}</footer>

templates/giscus.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
margin-top: 6rem;
44
}
55
</style>
6-
76
<script
87
src="{% raw(src=https://giscus.app/client.js) %}"
98
data-repo="{% raw(repo) %}"
@@ -15,9 +14,25 @@
1514
data-reactions-enabled="{% raw(reactions-enabled=1) %}"
1615
data-emit-metadata="0"
1716
data-input-position="bottom"
18-
data-theme="{% raw(theme=light) %}"
17+
data-theme="{% raw(theme=book) %}"
1918
data-lang="{% raw(lang=en) %}"
2019
data-loading="{% raw(loading=lazy) %}"
2120
crossorigin="anonymous"
2221
async
2322
></script>
23+
<script>
24+
(() => {
25+
const giscusScript = document.querySelector("script[data-repo][data-repo-id]");
26+
if (giscusScript?.getAttribute("data-theme") !== "book") return;
27+
const mapTheme = (theme) => (theme === "light" || theme === "rust" ? "light" : "dark");
28+
const bookTheme = localStorage.getItem("mdbook-theme") || html.getAttribute("class");
29+
giscusScript.setAttribute("data-theme", mapTheme(bookTheme));
30+
document.querySelectorAll("button[role='menuitem'].theme").forEach((btn) => {
31+
btn.addEventListener("click", (event) => {
32+
theme = mapTheme(event.target.id);
33+
const iframe = document.querySelector("iframe.giscus-frame");
34+
if (iframe) iframe.contentWindow.postMessage({ giscus: { setConfig: { theme } } }, "*");
35+
});
36+
});
37+
})();
38+
</script>

templates/scroll-to-top.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@
3131
}
3232
}
3333
</style>
34-
3534
<button type="button" aria-label="scroll-to-top" class="scroll-to-top hidden" onclick="scrollToTop()">
3635
<i class="fa fa-angle-up"></i>
3736
</button>
38-
3937
<script>
4038
const scrollToTop = () => window.scroll({ top: 0, behavior: "smooth" });
4139
window.addEventListener("scroll", () => {

0 commit comments

Comments
 (0)