Skip to content

Commit 53db159

Browse files
committed
Integrate markdown-callouts extension
Enable it for doc comments content and add custom admonition styles
1 parent 7c803e0 commit 53db159

4 files changed

Lines changed: 126 additions & 0 deletions

File tree

docs/extras.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,63 @@
44

55
For most [usages it is recommended](README.md#usage) to enable the "deduplicate-toc" Markdown extension, which comes bundled with *mkdocstrings-crystal*. It de-duplicates consecutive items that have the same title in the table of contents. This is important because Crystal can have multiple overloads of a method but in the ToC only their names are shown.
66

7+
## "callouts" extension
8+
9+
*mkdocstrings-crystal* auto-enables the ["callouts" extension][] for Markdown (only within doc comments' content), so you can use that syntax instead of the common ["admonition" extension][]'s syntax.
10+
11+
!!! example "example.cr"
12+
```crystal
13+
# Frobs the bar
14+
#
15+
# DEPRECATED: Use `baz` instead.
16+
def frob(bar)
17+
end
18+
```
19+
20+
You can also enable that extension for the whole site:
21+
22+
!!! example "mkdocs.yml"
23+
```yaml
24+
markdown_extensions:
25+
- callouts
26+
```
27+
28+
["callouts" extension]: https://github.com/oprypin/markdown-callouts/
29+
["admonition" extension]: https://python-markdown.github.io/extensions/admonition/
30+
31+
### Admonition styles
32+
33+
In addition to the [usual admonition styles](https://squidfunk.github.io/mkdocs-material/reference/admonitions/#supported-types), *mkdocstrings-crystal* injects styling for the Material theme to enable the following admonition kinds, used [in Crystal documentation](https://crystal-lang.org/reference/syntax_and_semantics/documenting_code.html#admonitions):
34+
35+
<style>
36+
--8<-- "mkdocstrings/templates/crystal/material/style.css"
37+
38+
.admonition p>code {
39+
display: inline-block;
40+
width: 49%;
41+
}
42+
</style>
43+
44+
!!! todo
45+
`TODO: ` `!!! todo`
46+
47+
!!! note
48+
`NOTE: ` `!!! note`
49+
50+
!!! bug
51+
`BUG: ` `!!! bug`
52+
53+
!!! fixme
54+
`FIXME: ` `!!! fixme`
55+
56+
!!! deprecated
57+
`DEPRECATED: ` `!!! deprecated`
58+
59+
!!! optimize
60+
`OPTIMIZE: ` `!!! optimize`
61+
62+
Both the default styles and the extra styles work with both the ["callouts" extension][] (write them in all-uppercase) and the ["admonition" extension][] (write them in all-lowercase).
63+
764
## Support for [MkDocs "macros" plugin](https://github.com/fralau/mkdocs_macros_plugin)
865

966
*Without* support, you have to access the doc root as

mkdocstrings/handlers/crystal/renderer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Optional, TypeVar, Union
44

55
import jinja2
6+
import markdown_callouts
67
from markdown import Markdown # type: ignore
78
from markdown.extensions import Extension, fenced_code # type: ignore
89
from markdown.treeprocessors import Treeprocessor
@@ -54,6 +55,7 @@ def update_env(self, md: Markdown, config: dict) -> None:
5455
del md.inlinePatterns["html"]
5556

5657
md.treeprocessors.register(_RefInsertingTreeprocessor(md), "mkdocstrings_crystal_xref", 12)
58+
markdown_callouts.CalloutsExtension().extendMarkdown(md)
5759

5860
self.env.trim_blocks = True
5961
self.env.lstrip_blocks = True

mkdocstrings/templates/crystal/material/style.css

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,69 @@ a.doc-source-link::after {
1212
content: "]";
1313
color: var(--md-typeset-color);
1414
}
15+
16+
/* Adaptation of https://crystal-lang.org/reference/syntax_and_semantics/documenting_code.html#admonitions to mkdocs-material */
17+
18+
:root {
19+
/* Icons from https://github.com/Templarian/MaterialDesign/tree/e42f3f41dd46bc033f11c0e5eed7fb742af6e74e/svg */
20+
/* format-list-checkbox */
21+
--md-admonition-icon--todo: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M21,19V17H8V19H21M21,13V11H8V13H21M8,7H21V5H8V7M4,5V7H6V5H4M3,5A1,1 0 0,1 4,4H6A1,1 0 0,1 7,5V7A1,1 0 0,1 6,8H4A1,1 0 0,1 3,7V5M4,11V13H6V11H4M3,11A1,1 0 0,1 4,10H6A1,1 0 0,1 7,11V13A1,1 0 0,1 6,14H4A1,1 0 0,1 3,13V11M4,17V19H6V17H4M3,17A1,1 0 0,1 4,16H6A1,1 0 0,1 7,17V19A1,1 0 0,1 6,20H4A1,1 0 0,1 3,19V17Z" /></svg>');
22+
/* close-octagon */
23+
--md-admonition-icon--deprecated: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M8.27,3L3,8.27V15.73L8.27,21H15.73L21,15.73V8.27L15.73,3M8.41,7L12,10.59L15.59,7L17,8.41L13.41,12L17,15.59L15.59,17L12,13.41L8.41,17L7,15.59L10.59,12L7,8.41" /></svg>');
24+
}
25+
26+
/* Based on https://github.com/squidfunk/mkdocs-material/blob/b3c0163b3b6b6e4d2cb4d50fbd535018042c4970/src/assets/stylesheets/main/extensions/markdown/_admonition.scss */
27+
28+
/* "TODO": custom icon */
29+
.md-typeset .todo>.admonition-title:before, .md-typeset .todo>summary:before {
30+
-webkit-mask-image: var(--md-admonition-icon--todo);
31+
mask-image: var(--md-admonition-icon--todo);
32+
}
33+
34+
/* "FIXME": "bug" icon, "failure" color */
35+
.md-typeset .admonition.fixme, .md-typeset details.fixme {
36+
border-color: #ff5252
37+
}
38+
.md-typeset .fixme>.admonition-title, .md-typeset .fixme>summary {
39+
background-color: rgba(255, 82, 82, .1);
40+
border-color: #ff5252
41+
}
42+
.md-typeset .fixme>.admonition-title:before, .md-typeset .fixme>summary:before {
43+
background-color: #ff5252;
44+
-webkit-mask-image: var(--md-admonition-icon--bug);
45+
mask-image: var(--md-admonition-icon--bug);
46+
mask-repeat: no-repeat;
47+
mask-size: contain
48+
}
49+
50+
/* "DEPRECATED": custom icon, "warning" color */
51+
.md-typeset .admonition.deprecated, .md-typeset details.deprecated {
52+
border-color: #ff9100
53+
}
54+
.md-typeset .deprecated>.admonition-title, .md-typeset .deprecated>summary {
55+
background-color: rgba(255, 145, 0, .1);
56+
border-color: #ff9100
57+
}
58+
.md-typeset .deprecated>.admonition-title:before, .md-typeset .deprecated>summary:before {
59+
background-color: #ff9100;
60+
-webkit-mask-image: var(--md-admonition-icon--deprecated);
61+
mask-image: var(--md-admonition-icon--deprecated);
62+
mask-repeat: no-repeat;
63+
mask-size: contain
64+
}
65+
66+
/* "OPTIMIZE": "danger" icon, "question" color */
67+
.md-typeset .admonition.optimize, .md-typeset details.optimize {
68+
border-color: #64dd17
69+
}
70+
.md-typeset .optimize>.admonition-title, .md-typeset .optimize>summary {
71+
background-color: rgba(100, 221, 23, .1);
72+
border-color: #64dd17
73+
}
74+
.md-typeset .optimize>.admonition-title:before, .md-typeset .optimize>summary:before {
75+
background-color: #64dd17;
76+
-webkit-mask-image: var(--md-admonition-icon--danger);
77+
mask-image: var(--md-admonition-icon--danger);
78+
mask-repeat: no-repeat;
79+
mask-size: contain
80+
}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ deduplicate-toc = "mkdocstrings.handlers.crystal.deduplicate_toc:DeduplicateTocE
1717
[tool.poetry.dependencies]
1818
python = "^3.7"
1919
mkdocstrings = "^0.15.0"
20+
markdown-callouts = "^0.1.0"
2021
markupsafe = "^1.1.1"
2122
cached-property = "^1.5.2"
2223
Jinja2 = "^2.11.2"

0 commit comments

Comments
 (0)