Skip to content

Commit

Permalink
docs: update README and optimise docs experience
Browse files Browse the repository at this point in the history
  • Loading branch information
MR-Addict committed Jan 10, 2024
1 parent 781b7d4 commit 0167b8f
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ keywords = ["mdbook", "embed", "plugin"]
authors = ["MR-Addict <MR-Addict@qq.com>"]
repository = "https://github.com/MR-Addict/mdbook-embedify"
documentation = "https://mr-addict.github.io/mdbook-embedify"
description = "A rust based mdbook preprocessor plugin that allows you to embed apps to your book"
description = "A rust based mdbook preprocessor plugin that allows you to embed apps to your book, like youtube, codepen and some other apps"

[dependencies]
clap = "4.4.13"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Crates.io](https://img.shields.io/crates/v/mdbook-embedify) ![Crates.io](https://img.shields.io/crates/l/mdbook-embedify)

This is a [mdbook](https://rust-lang.github.io/mdBook) preprocessor plugin that allows you to embed apps to your book.
This is a [mdbook](https://rust-lang.github.io/mdBook) preprocessor plugin that allows you to embed apps to your book, like youtube, codepen and some other apps.

## 1. Installation

Expand Down Expand Up @@ -34,6 +34,6 @@ Then you can use `embed` macro to embed an app. The syntax is like this:

## 3. Examples

![example](docs/example.png)
![example](docs/assets/images/example.png)

You can see a live demo and more detailed documentation [here](https://mr-addict.github.io/mdbook-embedify).
30 changes: 30 additions & 0 deletions docs/assets/css/examples.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.examples {
display: flex;
flex-direction: column;
gap: 2rem;
}

.app-container {
display: flex;
flex-direction: column;
gap: 0.5rem;
}

.window {
width: 100%;
height: 100%;
aspect-ratio: 16/9;
border-radius: 0.375rem;
border: none;
background: black;
}

.app button {
outline: none;
border: none;
color: white;
background: var(--gray-900);
border-radius: 0.25rem;
padding: 0.5rem 1rem;
cursor: pointer;
}
42 changes: 42 additions & 0 deletions docs/assets/css/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

a {
color: blue;
}

:root {
--gray-100: #f3f4f6;
--gray-200: #f3f4f6;
--gray-300: #d1d5db;
--gray-400: #d1d5db;
--gray-500: #6b7280;
--gray-600: #4b5563;
--gray-700: #374151;
--gray-800: #1f2937;
--gray-900: #111827;
}

body {
font-family: monospace;
font-size: 16px;

min-height: 100vh;
min-height: 100avh;

display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;

padding: 1rem;
}

@media (max-width: 600px) {
body {
padding: 0.5rem;
}
}
47 changes: 0 additions & 47 deletions docs/index.css → docs/assets/css/paragraph.css
Original file line number Diff line number Diff line change
@@ -1,40 +1,3 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

a {
color: blue;
}

:root {
--gray-100: #f3f4f6;
--gray-200: #f3f4f6;
--gray-300: #d1d5db;
--gray-400: #d1d5db;
--gray-500: #6b7280;
--gray-600: #4b5563;
--gray-700: #374151;
--gray-800: #1f2937;
--gray-900: #111827;
}

body {
font-family: monospace;
font-size: 16px;

min-height: 100vh;
min-height: 100avh;

display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;

padding: 1rem;
}

:is(h1, h2, h3, h4, h5, h6) {
font-weight: bolder;
color: var(--gray-800);
Expand Down Expand Up @@ -104,13 +67,3 @@ pre {
.red {
color: red;
}

iframe:not(:last-of-type) {
margin-bottom: 2rem;
}

@media (max-width: 600px) {
body {
padding: 0.5rem;
}
}
File renamed without changes
File renamed without changes
50 changes: 50 additions & 0 deletions docs/assets/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const apps = [
{
name: "youtube",
style: "aspect-ratio: 16/9",
url: "https://www.youtube.com/embed/EngW7tLk6R8",
},
{
name: "bilibili",
style: "aspect-ratio: 16/9",
url: "https://player.bilibili.com/player.html?bvid=BV1uT4y1P7CX&danmaku=0",
},
{
name: "codesandbox",
style: "aspect-ratio: 16/9",
url: "https://codesandbox.io/embed/ke8wx?&theme=dark",
},
{
name: "stackblitz",
style: "aspect-ratio: 16/9",
url: "https://stackblitz.com/edit/vitejs-vite-y8mdxg?embed=1&theme=dark&view=preview",
},
{
name: "codepen",
style: "height: 600px;",
url: "https://codepen.io/MR-Addict/embed/NWBOqKw?default-tab=result&theme-id=dark",
},
];

const buttons = document.querySelectorAll(".app button");
buttons.forEach((button) => {
button.addEventListener("click", () => {
const container = button.parentNode;

if (container.getAttribute("data-third-part") === null) container.querySelector("div").style = "";
else {
// get app
const app = apps.find((app) => app.name === container.getAttribute("data-app"));
// create iframe
const iframe = document.createElement("iframe");
iframe.src = app.url;
iframe.allowFullscreen = true;
iframe.style = app.style;
iframe.classList.add("window");
// append iframe
container.appendChild(iframe);
}
// remove button
button.remove();
});
});
119 changes: 63 additions & 56 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="favicon.svg" type="image/svg+xml" />
<link rel="stylesheet" href="index.css" />
<title>Mdbook Embed Preprocessor</title>
<link rel="icon" href="assets/images/favicon.svg" type="image/svg+xml" />
<link rel="stylesheet" href="assets/css/index.css" />
<link rel="stylesheet" href="assets/css/paragraph.css" />
<link rel="stylesheet" href="assets/css/examples.css" />
<title>Mdbook Embedify</title>
</head>
<body>
<main>
<section>
<h1>Mdbook Embed Preprocessor</h1>
<h1>Mdbook Embedify</h1>

<p>
This is a <a href="https://rust-lang.github.io/mdBook" target="_blank">mdbook</a> preprocessor plugin that
allows you embed apps to your book. If you find any problems while using it or have any other suggestions,
welcmoe to open an <a href="https://github.com/MR-Addict/mdbook-embedify/issues" target="_blank">issue</a>.
allows you embed apps to your book, like youtube, codepen and some other apps. If you find any problems while
using it or have any other suggestions, welcmoe to open an
<a href="https://github.com/MR-Addict/mdbook-embedify/issues" target="_blank">issue</a>.
</p>
</section>

Expand All @@ -41,13 +44,15 @@ <h2>2. Usage</h2>
<pre><code>{% embed app options[] %}</code></pre>

<p>
<strong>options</strong> are key-value based array seperated by space and its value must be wrapped by quotes.
For example:
<strong>options</strong> are key-value based array seperated by space and its value must be wrapped by
<strong>quotes</strong>. For example:
</p>

<pre><code>{% embed codepen user="MR-Addict" slug="NWBOqKw" height="600" theme="dark" %}</code></pre>

<p>Below is the list of supported apps and their options(<strong class="red">*</strong> means required):</p>
<p>
Below is the full list of supported apps and their options(<strong class="red">*</strong> means required):
</p>

<table class="table">
<thead>
Expand Down Expand Up @@ -88,76 +93,78 @@ <h2>2. Usage</h2>
<section>
<h2>3. Examples</h2>

<h3>Gist</h3>
<div class="examples">
<div class="app-container">
<h3>Gist</h3>

<pre><code>{% embed gist id="76cf171d1bdd7da41d4ca96b908eb57a" %}</code></pre>
<pre><code>{% embed gist id="76cf171d1bdd7da41d4ca96b908eb57a" %}</code></pre>

<script src="https://gist.github.com/MR-Addict/76cf171d1bdd7da41d4ca96b908eb57a.js"></script>
<div class="app" data-app="gist">
<button>Preview</button>
<div style="display: none">
<script src="https://gist.github.com/MR-Addict/76cf171d1bdd7da41d4ca96b908eb57a.js"></script>
</div>
</div>
</div>

<h3>Youtube</h3>
<div class="app-container">
<h3>Youtube</h3>

<pre><code>{% embed youtube id="EngW7tLk6R8" %}</code></pre>
<pre><code>{% embed youtube id="EngW7tLk6R8" %}</code></pre>

<iframe
allowfullscreen
src="https://www.youtube.com/embed/EngW7tLk6R8"
style="width: 100%; height: 100%; border: none; aspect-ratio: 16/9; border-radius: 0.375rem"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
></iframe>
<div class="app" data-third-part data-app="youtube">
<button>Preview</button>
</div>
</div>

<h3>Bilibili</h3>
<div class="app-container">
<h3>Bilibili</h3>

<pre><code>{% embed bilibili id="BV1uT4y1P7CX" danmaku="0" %}</code></pre>
<pre><code>{% embed bilibili id="BV1uT4y1P7CX" danmaku="0" %}</code></pre>

<iframe
allowfullscreen
name="Bilibili"
src="https://player.bilibili.com/player.html?bvid=BV1uT4y1P7CX&danmaku=0"
style="width: 100%; height: 100%; border: none; aspect-ratio: 16/9; border-radius: 0.375rem"
></iframe>
<div class="app" data-third-part data-app="bilibili">
<button>Preview</button>
</div>
</div>

<h3>Codesandbox</h3>
<div class="app-container">
<h3>Codesandbox</h3>

<pre><code>{% embed codesandbox id="ke8wx" theme="dark" %}</code></pre>
<pre><code>{% embed codesandbox id="ke8wx" theme="dark" %}</code></pre>

<iframe
loading="lazy"
name="Sandbox"
src="https://codesandbox.io/embed/ke8wx?&theme=light"
style="width: 100%; height: 100%; border: solid 1px #e5e7eb; aspect-ratio: 16/9; border-radius: 0.375rem"
></iframe>
<div class="app" data-third-part data-app="codesandbox">
<button>Preview</button>
</div>
</div>

<h3>Stackblitz</h3>
<div class="app-container">
<h3>Stackblitz</h3>

<pre><code>{% embed stackblitz id="vitejs-vite-y8mdxg" theme="light" %}</code></pre>
<pre><code>{% embed stackblitz id="vitejs-vite-y8mdxg" theme="dark" %}</code></pre>

<iframe
loading="lazy"
name="Stackblitz"
src="https://stackblitz.com/edit/vitejs-vite-y8mdxg?embed=1&theme=light&view=preview"
style="width: 100%; height: 100%; border: none; aspect-ratio: 16/9; border-radius: 0.375rem"
></iframe>
</section>
<div class="app" data-third-part data-app="stackblitz">
<button>Preview</button>
</div>
</div>

<section>
<h3>Codepen</h3>
<div class="app-container">
<h3>Codepen</h3>

<pre><code>{% embed codepen user="MR-Addict" slug="NWBOqKw" height="600" theme="dark" %}</code></pre>
<pre><code>{% embed codepen user="MR-Addict" slug="NWBOqKw" height="600" theme="dark" %}</code></pre>

<iframe
height="600"
title="Codepen"
allowfullscreen
allowtransparency
style="width: 100%; border: none; border-radius: 0.375rem"
src="https://codepen.io/MR-Addict/embed/NWBOqKw?default-tab=result&theme-id=dark"
></iframe>
<div class="app" data-third-part data-app="codepen">
<button>Preview</button>
</div>
</div>
</div>
</section>
</main>

<footer>
Made by <a href="https://github.com/MR-Addict" target="_blank">MR-Addict</a> | Licence
<a href="https://opensource.org/license/mit" target="_blank">MIT</a>
</footer>

<script src="assets/js/index.js"></script>
</body>
</html>
3 changes: 2 additions & 1 deletion src/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl Preprocessor for Embed {
let options_str = caps.get(2).map_or("", |m| m.as_str());

// render the template
let template = utils::render_template(app, &utils::parse_options(options_str));
let options = utils::parse_options(options_str);
let template = utils::render_template(app, &options);

// replace the content
chapter.content = re.replace_all(&content as &str, template).to_string()
Expand Down
2 changes: 1 addition & 1 deletion templates/bilibili.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<iframe
allowfullscreen
loading="lazy"
name="Bilibili"
name="bilibili"
src="https://player.bilibili.com/player.html?bvid={id}&danmaku=0"
style="width: 100%; height: 100%; border: none; aspect-ratio: 16/9; border-radius: 0.375rem"
></iframe>
Loading

0 comments on commit 0167b8f

Please sign in to comment.