Compare commits

..

29 Commits

Author SHA1 Message Date
Tom Milligan
b1707e8ba6 wip on pandoc parser 2025-10-22 13:41:08 +01:00
Tom Milligan
5714548254 setup code feature flags 2025-10-22 12:09:06 +01:00
Tom Milligan
11aed56c23 add pandoc crates and features 2025-10-22 12:05:36 +01:00
Tom Milligan
ea6c0a687a Merge pull request #229 from tommilligan/prep-1.20.0
chore: prep v1.20.0 release
2025-06-06 14:53:02 +01:00
Tom Milligan
50b9187ee5 chore: prep v1.20.0 release 2025-06-06 14:35:51 +01:00
Tom Milligan
fbd07ecaf0 Merge pull request #228 from tommilligan/print-collapsed-details
feat: show collapsed content in print view
2025-06-06 14:21:49 +01:00
Tom Milligan
37d03b872e feat: show collapsed content in print view 2025-06-06 12:58:11 +01:00
Tom Milligan
fa91fe3778 Merge pull request #227 from tommilligan/update-deps-2025-06
chore: update dependencies
2025-06-06 12:37:47 +01:00
Tom Milligan
d59dce3533 chore: update dependencies 2025-06-06 12:34:38 +01:00
Tom Milligan
21d86546ff Merge pull request #224 from tommilligan/fix-nested-list-content-indent
fix: do not double indent content in lists
2025-02-17 15:55:07 +00:00
Tom Milligan
79ee592f56 chore: prep v1.19.0 release 2025-02-17 15:42:27 +00:00
Tom Milligan
0d3eb11a2c fix: do not double indent content in lists 2025-02-17 15:38:33 +00:00
CoralPink
43579f902b Hide extra markers displayed in Safari. (#185)
* Hide extra markers displayed in `Safari`.

* [review] move webkit fix

---------

Co-authored-by: Tom Milligan <tom.milligan@uipath.com>
2024-08-17 06:19:01 +01:00
Tom Milligan
7ee3643dc3 Merge pull request #209 from tommilligan/deps-upgrade-2
chore: relax action constraints
2024-08-16 08:54:37 +01:00
Tom Milligan
c4bf2608e7 chore: relax action constraints 2024-08-16 08:50:25 +01:00
Tom Milligan
d4667a5f55 Merge pull request #208 from tommilligan/deps-upgrade
chore: upgrade rust, deps and actions
2024-08-16 08:39:23 +01:00
Tom Milligan
8751b3cd24 chore: upgrade github actions 2024-08-16 07:56:14 +01:00
Tom Milligan
516bbc6e7e chore: update rust lockfile 2024-08-16 07:48:11 +01:00
Tom Milligan
fc5c5eae39 chore: bump msrvc to 1.76.0 2024-08-16 07:46:50 +01:00
Tom Milligan
147a9bdb98 Merge pull request #199 from tommilligan/prep-1.18.0
chore: prep 1.18.0 release
2024-06-20 15:26:16 +01:00
Tom Milligan
540ea8e5e8 chore: prep 1.18.0 release 2024-06-20 14:43:12 +01:00
Tom Milligan
ae4bac21ba Merge pull request #198 from tommilligan/upgrade-deps
chore: upgrade deps
2024-06-20 10:53:11 +01:00
Tom Milligan
f0c4d3764e chore: upgrade deps 2024-06-20 10:42:01 +01:00
Tom Milligan
a942a8094e Merge pull request #197 from tommilligan/toml-serialize-error
fix: use up to date toml for serialization
2024-06-20 10:40:44 +01:00
Tom Milligan
d10a427b0f fix: use up to date toml for serialization 2024-06-20 10:05:48 +01:00
Tom Milligan
41a3b05094 Merge pull request #195 from tommilligan/aria-labelledby
feat: add aria attributes to block
2024-06-15 19:51:45 +01:00
Tom Milligan
d0f73baa3f Merge pull request #194 from tommilligan/readme-examples
readme: add more example projects
2024-06-15 13:46:16 +01:00
Tom Milligan
4c40418090 feat: add aria attributes to block 2024-06-15 13:43:58 +01:00
toastal
30c9ad2269 internal: refactor html rendering 2024-06-15 13:42:38 +01:00
25 changed files with 2524 additions and 779 deletions

View File

@@ -6,7 +6,7 @@ jobs:
# Fast test before we kick off all the other jobs
fast-test:
name: Fast test
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
steps:
- name: Checkout sources
uses: actions/checkout@v4
@@ -31,7 +31,7 @@ jobs:
detailed-test:
needs: fast-test
name: Test main target
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
steps:
- name: Checkout sources
uses: actions/checkout@v4
@@ -50,8 +50,10 @@ jobs:
with:
toolchain: stable
components: rustfmt, clippy
- name: Enable Corepack
run: corepack enable
- name: Install node toolchain
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "yarn"
@@ -70,16 +72,16 @@ jobs:
strategy:
matrix:
os:
- ubuntu-20.04
- ubuntu-24.04
rust:
- stable
- beta
- 1.74.0
- 1.82.0
experimental:
- false
include:
# Run a canary test on nightly that's allowed to fail
- os: ubuntu-20.04
- os: ubuntu-24.04
rust: nightly
experimental: true
# Test only stable on Windows, presume we'd get same result on other
@@ -89,7 +91,7 @@ jobs:
experimental: false
exclude:
# Don't bother retesting stable linux, we did it in the comprehensive test
- os: ubuntu-20.04
- os: ubuntu-24.04
rust: stable
experimental: false
runs-on: ${{ matrix.os }}

View File

@@ -25,7 +25,7 @@ jobs:
# binaries, so we use the same glibc version
#
# ref: https://github.com/rust-lang/mdBook/pull/1955
os: ubuntu-20.04
os: ubuntu-24.04
name: x86_64-unknown-linux-gnu.tar.gz
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
@@ -92,10 +92,12 @@ jobs:
tar czvf ../../stage/${{ env.CRATE_NAME }}-${{ steps.extract_tag.outputs.tag }}-${{ matrix.name }} ${{ env.CRATE_NAME }}
cd -
- name: Post Setup | Upload artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ env.CRATE_NAME }}-${{ steps.extract_tag.outputs.tag }}-${{ matrix.name }}
path: target/stage/*
# Idempotency: overwrite artefact by name if we're rerunning the deployment
overwrite: true
# Create GitHub release with Rust build targets and release notes
github_release:
@@ -109,7 +111,7 @@ jobs:
fetch-depth: 0
- name: Setup | Artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
- name: Setup | Extract version
shell: bash
@@ -120,7 +122,7 @@ jobs:
run: |
cat CHANGELOG.md | sed -n '/^## ${{ steps.extract_version.outputs.version }}$/,/^## /p' | sed '$d' > RELEASE.md
- name: Build | Publish
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
with:
files: ${{ env.CRATE_NAME }}-*/${{ env.CRATE_NAME }}-*
body_path: RELEASE.md
@@ -131,7 +133,7 @@ jobs:
publish:
name: Publish to crates.io
needs: github_release
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4

View File

@@ -11,7 +11,7 @@ permissions:
jobs:
publish:
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
@@ -39,7 +39,7 @@ jobs:
- name: Build book
run: ./scripts/build-book
- name: Push docs
uses: JamesIves/github-pages-deploy-action@v4.4.3
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages
folder: book/book

View File

@@ -1,5 +1,33 @@
# Changelog
## v1.20.0
### Changed
- MSRV (minimum supported rust version) is now 1.82.0 ([#227](https://github.com/tommilligan/mdbook-admonish/pull/227))
- Collapsible blocks now show content in print view ([#228](https://github.com/tommilligan/mdbook-admonish/pull/228)). Thanks to [@igor-petruk](https://github.com/igor-petruk) for raising this issue.
## v1.19.0
### Changed
- MSRV (minimum supported rust version) is now 1.76.0 ([#208](https://github.com/tommilligan/mdbook-admonish/pull/208))
### Fixed
- Fixed blocks not rendering correctly in indented list items. Thanks to [@JorelAli](https://github.com/JorelAli) for the bug report! ([#224](https://github.com/tommilligan/mdbook-admonish/pull/224))
## v1.18.0
### Changed
- Add ARIA attributes to generated blocks. Thanks to [@toastal](https://github.com/toastal) for suggesting this feature! ([#195](https://github.com/tommilligan/mdbook-admonish/pull/195))
- Note: This subtly alters the emitted HTML, and could cause additional styles applied to blocks to break. Native `mdbook-admonish` styles are not affected.
### Fixed
- Fixed some valid configurations producing TOML serialization errors. Thanks to [@DianaNites](https://github.com/DianaNites) for reporting this! ([#197](https://github.com/tommilligan/mdbook-admonish/pull/197))
## v1.17.1
### Fixed

1560
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,8 @@
[package]
name = "mdbook-admonish"
version = "1.17.1"
version = "1.20.0"
edition = "2021"
rust-version = "1.74.0"
rust-version = "1.76.0"
authors = ["Tom Milligan <code@tommilligan.net>"]
description = "A preprocessor for mdbook to add Material Design admonishments."
@@ -26,34 +26,34 @@ name = "mdbook_admonish"
path = "src/lib.rs"
[dependencies]
anyhow = "1.0.75"
anyhow = "1.0.86"
# Note: clap 4.4 increases MSRV to 1.70.0 (2023-06-01)
# To use MSRV supported dependencies, install using the lockfile with
# `cargo install mdbook-admonish --locked`
clap = { version = "4.3", default-features = false, features = ["std", "derive"], optional = true }
clap = { version = "4.5", default-features = false, features = ["std", "derive"], optional = true }
env_logger = { version = "0.11", default-features = false, optional = true }
log = "0.4.20"
mdbook = "0.4.35"
once_cell = "1.18.0"
log = "0.4.21"
mdbook = "0.4.40"
once_cell = "1.19.0"
pandoc = { version = "0.8", optional = true }
pandoc_ast = { version = "0.8", optional = true }
path-slash = "0.2.1"
pulldown-cmark = "0.11"
regex = "1.9.6"
semver = "1.0.19"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
# Peer dependency of mdbook
# The version of toml that mdbook uses internally (and uses in it's public api)
# Only used for compatilibilty with the mdbook public api
toml_mdbook = { package = "toml", version = "0.5.11" }
toml = "0.8.1"
toml_edit = { version = "0.22.13", optional = true }
pulldown-cmark = { version = "0.13", optional = true }
regex = "1.10.5"
semver = "1.0.23"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
toml = "0.8.14"
toml_edit = { version = "0.22.14", optional = true }
hex_color = { version = "3.0.0", features = ["serde"] }
[dev-dependencies]
pretty_assertions = "1.4.0"
[features]
default = ["cli", "cli-install"]
default = ["cli", "cli-install", "parser-markdown"]
parser-markdown = ["pulldown-cmark"]
parser-pandoc = ["pandoc", "pandoc_ast"]
# Enable the command line binary
cli = ["clap", "env_logger"]

View File

@@ -10,7 +10,7 @@ git-repository-url = "https://github.com/tommilligan/mdbook-admonish"
[preprocessor.admonish]
command = "mdbook-admonish"
assets_version = "3.0.2" # do not edit: managed by `mdbook-admonish install`
assets_version = "3.1.0" # do not edit: managed by `mdbook-admonish install`
[preprocessor.admonish.directive.custom]
expensive = { icon = "./money-bag.svg", color = "#24ab38" }

View File

@@ -1,3 +1,5 @@
/node_modules
*.css
*.css.map
.yarn/
.pnp*

View File

@@ -15,5 +15,6 @@
},
"devDependencies": {
"prettier": "^3.0.3"
}
},
"packageManager": "yarn@4.8.1+sha512.bc946f2a022d7a1a38adfc15b36a66a3807a67629789496c3714dd1703d2e6c6b1c69ff9ec3b43141ac7a1dd853b7685638eb0074300386a59c18df351ef8ff6"
}

View File

@@ -92,8 +92,9 @@ $admonitions: (
0 0.2rem 1rem rgba(0, 0, 0, 0.05),
0 0 0.1rem rgba(0, 0, 0, 0.1);
// [print]: Omit shadow as it may lead to rendering errors
// During print mode in browser, pdf renderer
@media print {
// Omit shadow as it may lead to rendering errors
box-shadow: none;
}
@@ -200,6 +201,14 @@ a.admonition-anchor-link {
}
}
// During print mode in browser, pdf renderer
@media print {
// Expand collapsed sections to show details
details.admonition::details-content {
display: contents;
}
}
summary.admonition-title {
details.admonition > &::after {
position: absolute;
@@ -222,6 +231,12 @@ summary.admonition-title {
details[open].admonition > &::after {
transform: rotate(90deg);
}
// Hide details marker for Safari that loves to show it anyway
// ref: https://github.com/tommilligan/mdbook-admonish/pull/185
&::-webkit-details-marker {
display: none;
}
}
:root {

File diff suppressed because it is too large Load Diff

View File

@@ -9,7 +9,7 @@ title = "mdbook-admonish-integration"
[preprocessor.admonish]
command = "mdbook-admonish"
assets_version = "3.0.2" # do not edit: managed by `mdbook-admonish install`
assets_version = "3.1.0" # do not edit: managed by `mdbook-admonish install`
after = ["links"]
[[preprocessor.admonish.custom]]

View File

@@ -9,7 +9,7 @@ title = "mdbook-admonish-integration"
[preprocessor.admonish]
command = "mdbook-admonish"
assets_version = "3.0.2" # do not edit: managed by `mdbook-admonish install`
assets_version = "3.1.0" # do not edit: managed by `mdbook-admonish install`
after = ["links"]
[[preprocessor.admonish.custom]]

View File

@@ -1,92 +1,108 @@
<h1 id="chapter-1"><a class="header" href="#chapter-1">Chapter 1</a></h1>
<div id="admonition-what-is-this" class="admonition admonish-abstract">
<div id="admonition-what-is-this" class="admonition admonish-abstract" role="note" aria-labelledby="admonition-what-is-this-title">
<div class="admonition-title">
<div id="admonition-what-is-this-title">
<p>What <i>is</i> this?</p>
<p><a class="admonition-anchor-link" href="#admonition-what-is-this"></a></p>
</div>
<a class="admonition-anchor-link" href="#admonition-what-is-this"></a>
</div>
<div>
<p>This book acts as an integration test for <code>mdbook-admonish</code>.</p>
<p>It verifies that <code>mdbook</code> post-processes our generated HTML in the way we expect.</p>
</div>
</div>
<div id="admonition-note" class="admonition admonish-note">
<div id="admonition-note" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-title">
<div class="admonition-title">
<div id="admonition-note-title">
<p>Note</p>
<p><a class="admonition-anchor-link" href="#admonition-note"></a></p>
</div>
<a class="admonition-anchor-link" href="#admonition-note"></a>
</div>
<div>
<p>Simples</p>
</div>
</div>
<div id="admonition-frog" class="admonition admonish-frog">
<div id="admonition-frog" class="admonition admonish-frog" role="note" aria-labelledby="admonition-frog-title">
<div class="admonition-title">
<div id="admonition-frog-title">
<p>Frog</p>
<p><a class="admonition-anchor-link" href="#admonition-frog"></a></p>
</div>
<a class="admonition-anchor-link" href="#admonition-frog"></a>
</div>
<div>
<p>Custom frog directive</p>
</div>
</div>
<div id="admonition-default" class="admonition admonish-warning">
<div id="admonition-default" class="admonition admonish-warning" role="note">
<div>
<p>No title, only body</p>
</div>
</div>
<div id="admonition-error-rendering-admonishment" class="admonition admonish-bug">
<div id="admonition-error-rendering-admonishment" class="admonition admonish-bug" role="note" aria-labelledby="admonition-error-rendering-admonishment-title">
<div class="admonition-title">
<div id="admonition-error-rendering-admonishment-title">
<p>Error rendering admonishment</p>
<p><a class="admonition-anchor-link" href="#admonition-error-rendering-admonishment"></a></p>
</div>
<a class="admonition-anchor-link" href="#admonition-error-rendering-admonishment"></a>
</div>
<div>
<p>Failed with:</p>
<pre><code class="language-log">'title=&quot;' is not a valid directive or TOML key-value pair.
<pre><code class="language-log">'title="' is not a valid directive or TOML key-value pair.
TOML parsing error: TOML parse error at line 1, column 21
|
1 | config = { title=&quot; }
1 | config = { title=" }
| ^
invalid basic string
</code></pre>
<p>Original markdown input:</p>
<pre><code class="language-markdown">```admonish title=&quot;
<pre><code class="language-markdown">```admonish title="
No title, only body
```
</code></pre>
</div>
</div>
<details id="admonition-note-1" class="admonition admonish-note">
<details id="admonition-note-1" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-1-title">
<summary class="admonition-title">
<div id="admonition-note-1-title">
<p>Note</p>
<p><a class="admonition-anchor-link" href="#admonition-note-1"></a></p>
</div>
<a class="admonition-anchor-link" href="#admonition-note-1"></a>
</summary>
<div>
<p>Hidden on load</p>
</div>
</details>
<div id="admonition-warning" class="admonition admonish-warning">
<div id="admonition-warning" class="admonition admonish-warning" role="note" aria-labelledby="admonition-warning-title">
<div class="admonition-title">
<div id="admonition-warning-title">
<p>Warning</p>
<p><a class="admonition-anchor-link" href="#admonition-warning"></a></p>
</div>
<a class="admonition-anchor-link" href="#admonition-warning"></a>
</div>
<div>
<p>This is a commonly shared warning!</p>
</div>
</div>
<div id="admonition-note-2" class="admonition admonish-note">
<div id="admonition-note-2" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-2-title">
<div class="admonition-title">
<div id="admonition-note-2-title">
<p>Note</p>
<p><a class="admonition-anchor-link" href="#admonition-note-2"></a></p>
</div>
<a class="admonition-anchor-link" href="#admonition-note-2"></a>
</div>
<div>
<pre><code class="language-bash">Nested code block
</code></pre>
</div>
</div>
<div id="admonition-note-3" class="admonition admonish-note">
<div id="admonition-note-3" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-3-title">
<div class="admonition-title">
<div id="admonition-note-3-title">
<p>Note</p>
<p><a class="admonition-anchor-link" href="#admonition-note-3"></a></p>
</div>
<a class="admonition-anchor-link" href="#admonition-note-3"></a>
</div>
<div>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
@@ -110,15 +126,34 @@ let x = 20;
</li>
<li>
<p>Thing two</p>
<div id="admonition-note-4" class="admonition admonish-note">
<div id="admonition-note-4" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-4-title">
<div class="admonition-title">
<div id="admonition-note-4-title">
<p>Note</p>
<p><a class="admonition-anchor-link" href="#admonition-note-4"></a></p>
</div>
<a class="admonition-anchor-link" href="#admonition-note-4"></a>
</div>
<div>
<p>Thing two</p>
</div>
</div>
<ol>
<li>
<p>Thing two nested</p>
<div id="admonition-note-5" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-5-title">
<div class="admonition-title">
<div id="admonition-note-5-title">
<p>Note</p>
</div>
<a class="admonition-anchor-link" href="#admonition-note-5"></a>
</div>
<div>
<p>Thing two nested (should not be a code block)
Regression tests for https://github.com/tommilligan/mdbook-admonish/issues/223</p>
</div>
</div>
</li>
</ol>
</li>
<li>
<p>Thing three</p>

View File

@@ -60,6 +60,13 @@ In a list:
Thing two
```
1. Thing two nested
```admonish
Thing two nested (should not be a code block)
Regression tests for https://github.com/tommilligan/mdbook-admonish/issues/223
```
1. Thing three
```sh

View File

@@ -8,7 +8,7 @@ function eprintln() {
>&2 echo "$1"
}
VERSION="0.4.35"
VERSION="0.4.51"
eprintln "Checking if mdbook $VERSION is installed"
if [[ "$(mdbook --version)" != "mdbook v$VERSION" ]]; then

View File

@@ -1 +1 @@
3.0.2
3.1.0

View File

@@ -86,6 +86,11 @@ html :is(.admonition-title, summary.admonition-title):last-child {
display: initial;
}
@media print {
details.admonition::details-content {
display: contents;
}
}
details.admonition > summary.admonition-title::after {
position: absolute;
top: 0.625em;
@@ -106,6 +111,9 @@ details.admonition > summary.admonition-title::after {
details[open].admonition > summary.admonition-title::after {
transform: rotate(90deg);
}
summary.admonition-title::-webkit-details-marker {
display: none;
}
:root {
--md-details-icon: url("data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M8.59 16.58 13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.42Z'/></svg>");

View File

@@ -125,7 +125,7 @@ struct Preprocessors {
/// Load the plugin specific config as a toml string, for private deserialization.
fn admonish_config_string(config: &Config) -> Result<String> {
Ok(toml_mdbook::to_string(
Ok(toml::to_string(
&config
.preprocessor
.admonish

View File

@@ -11,11 +11,12 @@ use crate::types::{AdmonitionDefaults, BuiltinDirective, BuiltinDirectiveConfig}
/// Roundtrips config to string, to avoid linking the plugin's internal version of toml
/// to the one publically exposed by the mdbook library.
pub(crate) fn admonish_config_from_context(ctx: &PreprocessorContext) -> Result<Config> {
let table: String = toml_mdbook::to_string(
let table: String = toml::to_string(
ctx.config
.get_preprocessor("admonish")
.context("No configuration for mdbook-admonish in book.toml")?,
)?;
)
.context("Could not serialize mdbook-admonish config. This is a bug in the toml library.")?;
admonish_config_from_str(&table)
}

View File

@@ -6,7 +6,10 @@ mod book_config;
mod config;
#[doc(hidden)]
pub mod custom;
#[cfg(feature = "parser-markdown")]
mod markdown;
#[cfg(feature = "parser-pandoc")]
mod pandoc;
mod parse;
mod preprocessor;
mod render;

View File

@@ -155,11 +155,13 @@ Text
let expected = r##"# Chapter
<div id="admonition-note" class="admonition admonish-note">
<div id="admonition-note" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-title">
<div class="admonition-title">
<div id="admonition-note-title">
Note
</div>
<a class="admonition-anchor-link" href="#admonition-note"></a>
</div>
<div>
@@ -187,11 +189,13 @@ Text
let expected = r##"# Chapter
<div id="admonition-note" class="admonition admonish-note">
<div id="admonition-note" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-title">
<div class="admonition-title">
<div id="admonition-note-title">
Note
</div>
<a class="admonition-anchor-link" href="#admonition-note"></a>
</div>
<div>
@@ -219,11 +223,13 @@ Text
let expected = r##"# Chapter
<div id="admonition-warning" class="admonition admonish-warning">
<div id="admonition-warning" class="admonition admonish-warning" role="note" aria-labelledby="admonition-warning-title">
<div class="admonition-title">
<div id="admonition-warning-title">
Warning
</div>
<a class="admonition-anchor-link" href="#admonition-warning"></a>
</div>
<div>
@@ -249,11 +255,13 @@ Text
let expected = r##"# Chapter
<div id="admonition-caution" class="admonition admonish-warning">
<div id="admonition-caution" class="admonition admonish-warning" role="note" aria-labelledby="admonition-caution-title">
<div class="admonition-title">
<div id="admonition-caution-title">
Caution
</div>
<a class="admonition-anchor-link" href="#admonition-caution"></a>
</div>
<div>
@@ -279,11 +287,13 @@ Text
let expected = r##"# Chapter
<div id="admonition-read-this" class="admonition admonish-warning">
<div id="admonition-read-this" class="admonition admonish-warning" role="note" aria-labelledby="admonition-read-this-title">
<div class="admonition-title">
<div id="admonition-read-this-title">
Read **this**!
</div>
<a class="admonition-anchor-link" href="#admonition-read-this"></a>
</div>
<div>
@@ -373,11 +383,13 @@ hello
let expected = r##"
<div id="admonition-and-in-the-title" class="admonition admonish-note">
<div id="admonition-and-in-the-title" class="admonition admonish-note" role="note" aria-labelledby="admonition-and-in-the-title-title">
<div class="admonition-title">
<div id="admonition-and-in-the-title-title">
And "<i>in</i>" the title
</div>
<a class="admonition-anchor-link" href="#admonition-and-in-the-title"></a>
</div>
<div>
@@ -403,11 +415,13 @@ hello
let expected = r##"
<div id="admonition-trademark" class="admonition admonish-warning">
<div id="admonition-trademark" class="admonition admonish-warning" role="note" aria-labelledby="admonition-trademark-title">
<div class="admonition-title">
<div id="admonition-trademark-title">
Trademark™
</div>
<a class="admonition-anchor-link" href="#admonition-trademark"></a>
</div>
<div>
@@ -432,11 +446,13 @@ Will have bonus classnames
let expected = r##"
<div id="admonition-tip" class="admonition admonish-tip my-style other-style">
<div id="admonition-tip" class="admonition admonish-tip my-style other-style" role="note" aria-labelledby="admonition-tip-title">
<div class="admonition-title">
<div id="admonition-tip-title">
Tip
</div>
<a class="admonition-anchor-link" href="#admonition-tip"></a>
</div>
<div>
@@ -460,11 +476,13 @@ Will have bonus classnames
let expected = r##"
<div id="admonition-developers-dont-want-you-to-know-this-one-weird-tip" class="admonition admonish-tip my-style other-style">
<div id="admonition-developers-dont-want-you-to-know-this-one-weird-tip" class="admonition admonish-tip my-style other-style" role="note" aria-labelledby="admonition-developers-dont-want-you-to-know-this-one-weird-tip-title">
<div class="admonition-title">
<div id="admonition-developers-dont-want-you-to-know-this-one-weird-tip-title">
Developers don't want you to know this one weird tip!
</div>
<a class="admonition-anchor-link" href="#admonition-developers-dont-want-you-to-know-this-one-weird-tip"></a>
</div>
<div>
@@ -487,7 +505,7 @@ Will have bonus classnames
let expected = r#"
<div id="admonition-default" class="admonition admonish-note">
<div id="admonition-default" class="admonition admonish-note" role="note">
<div>
@@ -513,11 +531,13 @@ Content one.
let expected = r##"
<div id="admonition-my-note" class="admonition admonish-note">
<div id="admonition-my-note" class="admonition admonish-note" role="note" aria-labelledby="admonition-my-note-title">
<div class="admonition-title">
<div id="admonition-my-note-title">
My Note
</div>
<a class="admonition-anchor-link" href="#admonition-my-note"></a>
</div>
<div>
@@ -528,11 +548,13 @@ Content zero.
</div>
<div id="admonition-my-note-1" class="admonition admonish-note">
<div id="admonition-my-note-1" class="admonition admonish-note" role="note" aria-labelledby="admonition-my-note-1-title">
<div class="admonition-title">
<div id="admonition-my-note-1-title">
My Note
</div>
<a class="admonition-anchor-link" href="#admonition-my-note-1"></a>
</div>
<div>
@@ -556,11 +578,13 @@ Bonus content!
let expected = r##"
<div id="admonition-article-heading" class="admonition admonish-tip my other-style">
<div id="admonition-article-heading" class="admonition admonish-tip my other-style" role="note" aria-labelledby="admonition-article-heading-title">
<div class="admonition-title">
<div id="admonition-article-heading-title">
Article Heading
</div>
<a class="admonition-anchor-link" href="#admonition-article-heading"></a>
</div>
<div>
@@ -584,11 +608,13 @@ Bonus content!
let expected = r##"
<div id="admonition-error-rendering-admonishment" class="admonition admonish-bug">
<div id="admonition-error-rendering-admonishment" class="admonition admonish-bug" role="note" aria-labelledby="admonition-error-rendering-admonishment-title">
<div class="admonition-title">
<div id="admonition-error-rendering-admonishment-title">
Error rendering admonishment
</div>
<a class="admonition-anchor-link" href="#admonition-error-rendering-admonishment"></a>
</div>
<div>
@@ -686,11 +712,13 @@ Hidden
let expected = r##"
<details id="admonition-note" class="admonition admonish-note">
<details id="admonition-note" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-title">
<summary class="admonition-title">
<div id="admonition-note-title">
Note
</div>
<a class="admonition-anchor-link" href="#admonition-note"></a>
</summary>
<div>
@@ -715,11 +743,13 @@ Text
let expected = r##"# Chapter
<div id="admonition-admonish" class="admonition admonish-note">
<div id="admonition-admonish" class="admonition admonish-note" role="note" aria-labelledby="admonition-admonish-title">
<div class="admonition-title">
<div id="admonition-admonish-title">
Admonish
</div>
<a class="admonition-anchor-link" href="#admonition-admonish"></a>
</div>
<div>
@@ -759,7 +789,7 @@ Text
let expected = r#"# Chapter
<div id="admonition-default" class="admonition admonish-note">
<div id="admonition-default" class="admonition admonish-note" role="note">
<div>
A simple admonition.
@@ -797,7 +827,7 @@ Text
let expected = r#"# Chapter
<div id="admonition-default" class="admonition admonish-note">
<div id="admonition-default" class="admonition admonish-note" role="note">
<div>
A simple admonition.
@@ -821,11 +851,13 @@ Text
let expected = r##"# Chapter
<div id="yay-custom-id" class="admonition admonish-success">
<div id="yay-custom-id" class="admonition admonish-success" role="note" aria-labelledby="yay-custom-id-title">
<div class="admonition-title">
<div id="yay-custom-id-title">
Check
</div>
<a class="admonition-anchor-link" href="#yay-custom-id"></a>
</div>
<div>
@@ -851,11 +883,13 @@ Text
let expected = r##"# Chapter
<div id="admonition-check" class="admonition admonish-success">
<div id="admonition-check" class="admonition admonish-success" role="note" aria-labelledby="admonition-check-title">
<div class="admonition-title">
<div id="admonition-check-title">
Check
</div>
<a class="admonition-anchor-link" href="#admonition-check"></a>
</div>
<div>
@@ -881,11 +915,13 @@ Text
let expected = r##"# Chapter
<div id="admonition-check-mark" class="admonition admonish-success">
<div id="admonition-check-mark" class="admonition admonish-success" role="note" aria-labelledby="admonition-check-mark-title">
<div class="admonition-title">
<div id="admonition-check-mark-title">
Check Mark
</div>
<a class="admonition-anchor-link" href="#admonition-check-mark"></a>
</div>
<div>
@@ -914,11 +950,13 @@ Text
let expected = r##"# Chapter
<div id="admonition-check-mark" class="admonition admonish-success">
<div id="admonition-check-mark" class="admonition admonish-success" role="note" aria-labelledby="admonition-check-mark-title">
<div class="admonition-title">
<div id="admonition-check-mark-title">
Check <span class="emphasis">Mark</span>
</div>
<a class="admonition-anchor-link" href="#admonition-check-mark"></a>
</div>
<div>
@@ -944,11 +982,13 @@ Text
let expected = r##"# Chapter
<div id="info" class="admonition admonish-info">
<div id="info" class="admonition admonish-info" role="note" aria-labelledby="info-title">
<div class="admonition-title">
<div id="info-title">
Info
</div>
<a class="admonition-anchor-link" href="#info"></a>
</div>
<div>
@@ -988,11 +1028,13 @@ Text
let expected = r##"# Chapter
<div id="prefix-my-title" class="admonition admonish-info">
<div id="prefix-my-title" class="admonition admonish-info" role="note" aria-labelledby="prefix-my-title-title">
<div class="admonition-title">
<div id="prefix-my-title-title">
My Title
</div>
<a class="admonition-anchor-link" href="#prefix-my-title"></a>
</div>
<div>
@@ -1032,11 +1074,13 @@ Text
let expected = r##"# Chapter
<div id="my-section-id" class="admonition admonish-info">
<div id="my-section-id" class="admonition admonish-info" role="note" aria-labelledby="my-section-id-title">
<div class="admonition-title">
<div id="my-section-id-title">
My Title
</div>
<a class="admonition-anchor-link" href="#my-section-id"></a>
</div>
<div>
@@ -1099,17 +1143,19 @@ Text
1. Thing two
<div id="admonition-note" class="admonition admonish-note">
<div id="admonition-note" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-title">
<div class="admonition-title">
<div id="admonition-note-title">
Note
</div>
<a class="admonition-anchor-link" href="#admonition-note"></a>
</div>
<div>
Thing two
Thing two
</div>
</div>
@@ -1122,4 +1168,97 @@ Text
assert_eq!(expected, prep(content));
}
// Regression test for https://github.com/tommilligan/mdbook-admonish/issues/223
#[test]
fn nested_list_should_not_render_indented_code_block() {
let content = r#"# Chapter
- Level one
```admonish
Thing one
line two
```
- Level two
```admonish
Thing two
line two
```
- Level three
```admonish
Thing three
line two
```
"#;
let expected = r##"# Chapter
- Level one
<div id="admonition-note" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-title">
<div class="admonition-title">
<div id="admonition-note-title">
Note
</div>
<a class="admonition-anchor-link" href="#admonition-note"></a>
</div>
<div>
Thing one
line two
</div>
</div>
- Level two
<div id="admonition-note-1" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-1-title">
<div class="admonition-title">
<div id="admonition-note-1-title">
Note
</div>
<a class="admonition-anchor-link" href="#admonition-note-1"></a>
</div>
<div>
Thing two
line two
</div>
</div>
- Level three
<div id="admonition-note-2" class="admonition admonish-note" role="note" aria-labelledby="admonition-note-2-title">
<div class="admonition-title">
<div id="admonition-note-2-title">
Note
</div>
<a class="admonition-anchor-link" href="#admonition-note-2"></a>
</div>
<div>
Thing three
line two
</div>
</div>
"##;
assert_eq!(expected, prep(content));
}
}

78
src/pandoc.rs Normal file
View File

@@ -0,0 +1,78 @@
use mdbook::errors::Result as MdbookResult;
use crate::{
book_config::OnFailure,
types::{Overrides, RenderTextMode},
};
pub(crate) fn preprocess(
content: &str,
on_failure: OnFailure,
overrides: &Overrides,
render_text_mode: RenderTextMode,
) -> MdbookResult<String> {
if render_text_mode == RenderTextMode::Strip {
// We don't support stripping in pandoc - they just show up as divs.
return Ok(content.to_owned());
}
let ADMONISH_KIND = "note".to_owned();
let mut pandoc = pandoc::new();
pandoc.set_input(pandoc::InputKind::Pipe(content.into()));
pandoc.set_input_format(pandoc::InputFormat::CommonmarkX, Vec::new());
pandoc.set_output_format(pandoc::OutputFormat::CommonmarkX, Vec::new());
pandoc.set_output(pandoc::OutputKind::Pipe);
pandoc.add_filter(|json| {
pandoc_ast::filter(json, |mut pandoc| {
for block in &mut pandoc.blocks {
println!("{:?}", &block);
use pandoc_ast::Block;
use pandoc_ast::Inline;
match block {
Block::Div((identifier, classes, kv), content) => {
if let Some(ADMONISH_KIND) = classes.first() {
// TODO: generate values for below by reading the pandoc input
// decide on a syntax for kv and classes, admonition type, and plug
// that in to our existing structs which deal with links and overrides
// etc.
*block = Block::Div(
(
// TODO: get from kv
"admonition-title".to_owned(),
// TODO: compute from input above
vec!["admonition".to_owned(), "admonish-note".to_owned()],
vec![
("role".to_owned(), "note".to_owned()),
(
"aria-labelledby".to_owned(),
"admonition-title-title".to_owned(),
),
],
),
vec![
Block::Div(
// TODO: add anchor link elements (a, href, etc)
("admonition-title-title".to_owned(), vec![], vec![]),
vec![Block::Plain(vec![Inline::Str("Title".to_owned())])],
),
Block::Div(("".to_owned(), vec![], vec![]), content.clone()),
],
)
}
}
_ => {}
}
}
pandoc
})
});
let result = pandoc.execute().unwrap();
use pandoc::PandocOutput::*;
let output = match result {
ToBuffer(output) => output,
ToFile(_) | ToBufferRaw(_) => panic!("expected buffer output"),
};
Ok(output)
}

View File

@@ -7,10 +7,14 @@ use mdbook::{
use crate::{
book_config::{admonish_config_from_context, Config, RenderMode},
markdown::preprocess,
types::{Overrides, RenderTextMode},
};
#[cfg(feature = "parser-markdown")]
use crate::markdown::preprocess;
#[cfg(feature = "parser-pandoc")]
use crate::pandoc::preprocess;
pub struct Admonish;
impl Preprocessor for Admonish {
@@ -166,8 +170,9 @@ mod test {
serde_json::from_value(value).unwrap()
}
#[cfg(feature = "parser-markdown")]
#[test]
fn run_html() {
fn run_markdown_to_html() {
let content = r#"
````admonish title="Title"
```rust
@@ -178,11 +183,13 @@ x = 20;
"#;
let expected_content = r##"
<div id="admonition-title" class="admonition admonish-note">
<div id="admonition-title" class="admonition admonish-note" role="note" aria-labelledby="admonition-title-title">
<div class="admonition-title">
<div id="admonition-title-title">
Title
</div>
<a class="admonition-anchor-link" href="#admonition-title"></a>
</div>
<div>
@@ -208,6 +215,43 @@ x = 20;
assert_eq!(Admonish.run(&ctx, book).unwrap(), expected_book)
}
#[cfg(feature = "parser-pandoc")]
#[test]
fn run_pandoc_to_html() {
let content = r#"
:::{.note title="Title"}
```rust
let x = 10;
x = 20;
```
:::
"#;
let expected_content = r##"::::: {#admonition-title .admonition .admonish-note role="note" aria-labelledby="admonition-title-title"}
::: {#admonition-title-title}
Title
:::
::: {}
``` rust
let x = 10;
x = 20;
```
:::
:::::
"##;
let ctx = mock_context(
&json!({
"assets_version": "3.0.0"
}),
"html",
);
let book = mock_book(content);
let expected_book = mock_book(expected_content);
assert_eq!(Admonish.run(&ctx, book).unwrap(), expected_book)
}
#[test]
fn run_test_preserves_by_default() {
let content = r#"
@@ -230,8 +274,9 @@ x = 20;
assert_eq!(Admonish.run(&ctx, book).unwrap(), expected_book)
}
#[cfg(feature = "parser-markdown")]
#[test]
fn run_test_can_strip() {
fn run_test_can_strip_markdown() {
let content = r#"
````admonish title="Title"
```rust

View File

@@ -35,9 +35,9 @@ impl<'a> Admonition<'a> {
}
}
pub(crate) fn html(&self, id_counter: &mut HashMap<String, usize>) -> String {
pub(crate) fn html(self, id_counter: &mut HashMap<String, usize>) -> String {
let anchor_id = match &self.css_id {
CssId::Verbatim(id) => Cow::Borrowed(id),
CssId::Verbatim(id) => Cow::Borrowed(id.as_str()),
CssId::Prefix(prefix) => {
let id = unique_id_from_content(
if !self.title.is_empty() {
@@ -56,53 +56,79 @@ impl<'a> Admonition<'a> {
let content = &self.content;
let indent = " ".repeat(self.indent);
let title_block = if self.collapsible { "summary" } else { "div" };
let title_html = if !title.is_empty() {
Cow::Owned(format!(
r##"{indent}<{title_block} class="admonition-title">
let (titlebar_html, title_id) = if !title.is_empty() {
let titlebar_element = if self.collapsible { "summary" } else { "div" };
let title_id = format!("{anchor_id}-title");
let titlebar_html = Cow::Owned(format!(
r##"{indent}<{titlebar_element} class="admonition-title">
{indent}<div id="{title_id}">
{indent}
{indent}{title}
{indent}
{indent}</div>
{indent}<a class="admonition-anchor-link" href="#{anchor_id}"></a>
{indent}</{title_block}>
{indent}</{titlebar_element}>
"##
))
));
(titlebar_html, Some(title_id))
} else {
Cow::Borrowed("")
(Cow::Borrowed(""), None)
};
let mut additional_class = format!("admonish-{}", self.directive);
if !self.additional_classnames.is_empty() {
for additional_classname in &self.additional_classnames {
additional_class.push(' ');
additional_class.push_str(additional_classname);
}
}
let mut classes = vec![
"admonition".to_owned(),
format!("admonish-{}", self.directive),
];
classes.extend(self.additional_classnames);
let classes = classes.join(" ");
let admonition_block = if self.collapsible { "details" } else { "div" };
let mut attributes = vec![
("id", anchor_id),
("class", Cow::Owned(classes)),
("role", Cow::Borrowed("note")),
];
if let Some(title_id) = title_id {
attributes.push(("aria-labelledby", Cow::Owned(title_id)));
}
let attributes = join_attributes(&attributes);
let admonition_element = if self.collapsible { "details" } else { "div" };
// Notes on the HTML template:
// - the additional whitespace around the content are deliberate
// In line with the commonmark spec, this allows the inner content to be
// rendered as markdown paragraphs.
// - We should not indent the inner content, as it retains the indent
// it is written with.
format!(
r#"
{indent}<{admonition_block} id="{anchor_id}" class="admonition {additional_class}">
{title_html}{indent}<div>
{indent}
{indent}{content}
{indent}
{indent}<{admonition_element} {attributes}>
{titlebar_html}{indent}<div>
{content}
{indent}</div>
{indent}</{admonition_block}>"#,
{indent}</{admonition_element}>"#,
)
}
/// Strips all admonish syntax, leaving the plain content of the block.
pub(crate) fn strip(&self) -> String {
pub(crate) fn strip(self) -> String {
// Add in newlines to preserve line numbering for test output
// These replace the code fences we stripped out
format!("\n{}\n", self.content)
}
}
fn join_attributes(attributes: &[(impl AsRef<str>, impl AsRef<str>)]) -> String {
let mut buffer = String::new();
for (key, value) in attributes {
buffer.push_str(key.as_ref());
buffer.push_str(r#"=""#);
buffer.push_str(value.as_ref());
buffer.push_str(r#"" "#);
}
buffer.pop();
buffer
}
const ANCHOR_ID_DEFAULT: &str = "default";