| 1 |
--- |
| 2 |
icon: lucide/chevrons-up-down |
| 3 |
--- |
| 4 |
[:octicons-file-code-24:][_details]{: .source-link } |
| 5 |
|
| 6 |
# Details |
| 7 |
|
| 8 |
/// tip |
| 9 |
9.10 has added a new approach to creating details. Checkout the new [Details extension here](./blocks/plugins/details.md)! |
| 10 |
|
| 11 |
If an approach like Obsidian is preferred, 10.20 adds support via the [Quotes extension](./quotes.md) for Obsidian |
| 12 |
style, collapsible callouts. |
| 13 |
/// |
| 14 |
|
| 15 |
## Overview |
| 16 |
|
| 17 |
Details is an extension that creates collapsible elements that hide their content. It uses the HTML5 |
| 18 |
`#!html <details><summary>` tags to accomplish this. It supports nesting and you can also force the default state to be |
| 19 |
open. And if you want to style some different than others, you can optionally feed in a custom class. |
| 20 |
|
| 21 |
The Details extension can be included in Python Markdown by using the following: |
| 22 |
|
| 23 |
```py3 |
| 24 |
import markdown |
| 25 |
md = markdown.Markdown(extensions=['pymdownx.details']) |
| 26 |
``` |
| 27 |
|
| 28 |
## Syntax |
| 29 |
|
| 30 |
Details must contain a blank line before they start. Use `???` to start a details block or `???+` if you want to start a |
| 31 |
details block whose default state is 'open'. Follow the start of the block with an optional class or classes (separated |
| 32 |
with spaces) and the summary contained in quotes. Content is placed below the header and must be indented. |
| 33 |
|
| 34 |
``` |
| 35 |
??? optional-class "Summary" |
| 36 |
Here's some content. |
| 37 |
|
| 38 |
??? multiple optional-class "Summary" |
| 39 |
Here's some content. |
| 40 |
``` |
| 41 |
|
| 42 |
```text title="Details" |
| 43 |
???+ note "Open styled details" |
| 44 |
|
| 45 |
??? danger "Nested details!" |
| 46 |
And more content again. |
| 47 |
``` |
| 48 |
|
| 49 |
/// html | div.result |
| 50 |
|
| 51 |
???+ note "Open styled details" |
| 52 |
|
| 53 |
??? danger "Nested details!" |
| 54 |
And more content again. |
| 55 |
/// |
| 56 |
|
| 57 |
It is also possible to provide only a class. If this is done, the title will be derived from the *first* class. |
| 58 |
|
| 59 |
```text title="Class from Title" |
| 60 |
??? success |
| 61 |
Content. |
| 62 |
|
| 63 |
??? warning classes |
| 64 |
Content. |
| 65 |
``` |
| 66 |
|
| 67 |
/// html | div.result |
| 68 |
|
| 69 |
??? success |
| 70 |
Content. |
| 71 |
|
| 72 |
??? warning classes |
| 73 |
Content. |
| 74 |
/// |
| 75 |
|
| 76 |
Details will be output in the format below. The content will always be encapsulated in tags of some kind. |
| 77 |
|
| 78 |
```html |
| 79 |
<details class="optional-class"><summary>Text</summary><p>Content</p></details> |
| 80 |
``` |
| 81 |
|
| 82 |
## Browser Support |
| 83 |
|
| 84 |
Unfortunately, due to how new `#!html <details><summary>` tags are, not all browsers support them yet. In order to have |
| 85 |
them supported in all new browsers, you will have to provide some fallback styling and JavaScript until all browsers |
| 86 |
catch up. |
| 87 |
|
| 88 |
This extension's goal is not to provide you with the perfect polyfill, but this is a basic example that provides basic |
| 89 |
support. There are more elaborate polyfills available that support jQuery, add keyboard events, or even support back to |
| 90 |
IE8. Feel free to modify what is here or find a solution that fits your needs. |
| 91 |
|
| 92 |
/// settings | Basic Polyfill Setup |
| 93 |
Here is the basic CSS that can be used. It is meant to provide a consistent CSS in both browsers that support |
| 94 |
`#!html <details><summary>` tags and those that do not. |
| 95 |
|
| 96 |
//// collapse-code |
| 97 |
```css |
| 98 |
details { |
| 99 |
display: block; |
| 100 |
} |
| 101 |
|
| 102 |
details[open] > summary::before { |
| 103 |
content: "\25BC"; |
| 104 |
} |
| 105 |
|
| 106 |
details summary { |
| 107 |
display: block; |
| 108 |
cursor: pointer; |
| 109 |
} |
| 110 |
|
| 111 |
details summary:focus { |
| 112 |
outline: none; |
| 113 |
} |
| 114 |
|
| 115 |
details summary::before { |
| 116 |
content: "\25B6"; |
| 117 |
padding-right: 0.5em; |
| 118 |
} |
| 119 |
|
| 120 |
details summary::-webkit-details-marker { |
| 121 |
display: none; |
| 122 |
} |
| 123 |
|
| 124 |
/* Attach the "no-details" class to details tags |
| 125 |
in browsers that do not support them to get |
| 126 |
open/show functionality. */ |
| 127 |
details.no-details:not([open]) > * { |
| 128 |
display: none; |
| 129 |
} |
| 130 |
|
| 131 |
details.no-details:not([open]) summary { |
| 132 |
display: block; |
| 133 |
} |
| 134 |
``` |
| 135 |
//// |
| 136 |
|
| 137 |
And below is the JavaScript that will detect browsers that do not support `#!html <details><summary>` tags and apply |
| 138 |
a `no-details` class to all details in those browsers. It will also attach a click event that will toggle the open |
| 139 |
state. The CSS above will target the `no-details` class and the `open` attribute to hide/show the content of your |
| 140 |
`#!html <details>` tag. Just run the code after the HTML content is loaded. |
| 141 |
|
| 142 |
There are plenty of things that aren't covered here, like jumping to a footnote or ID inside a closed polyfilled |
| 143 |
detail element, but this is left up to the user to figure out, or for a complete 3rd party polyfill. |
| 144 |
|
| 145 |
//// collapse-code |
| 146 |
```js |
| 147 |
(function () { |
| 148 |
'use strict'; |
| 149 |
/** |
| 150 |
* Converts details/summary tags into working elements in browsers that don't yet support them. |
| 151 |
* @return {void} |
| 152 |
*/ |
| 153 |
var details = (function () { |
| 154 |
|
| 155 |
var isDetailsSupported = function () { |
| 156 |
// https://mathiasbynens.be/notes/html5-details-jquery#comment-35 |
| 157 |
// Detect if details is supported in the browser |
| 158 |
var el = document.createElement("details"); |
| 159 |
var fake = false; |
| 160 |
|
| 161 |
if (!("open" in el)) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
var root = document.body || function () { |
| 166 |
var de = document.documentElement; |
| 167 |
fake = true; |
| 168 |
return de.insertBefore(document.createElement("body"), de.firstElementChild || de.firstChild); |
| 169 |
}(); |
| 170 |
|
| 171 |
el.innerHTML = "<summary>a</summary>b"; |
| 172 |
el.style.display = "block"; |
| 173 |
root.appendChild(el); |
| 174 |
var diff = el.offsetHeight; |
| 175 |
el.open = true; |
| 176 |
diff = diff !== el.offsetHeight; |
| 177 |
root.removeChild(el); |
| 178 |
|
| 179 |
if (fake) { |
| 180 |
root.parentNode.removeChild(root); |
| 181 |
} |
| 182 |
|
| 183 |
return diff; |
| 184 |
}(); |
| 185 |
|
| 186 |
if (!isDetailsSupported) { |
| 187 |
var blocks = document.querySelectorAll("details>summary"); |
| 188 |
for (var i = 0; i < blocks.length; i++) { |
| 189 |
var summary = blocks[i]; |
| 190 |
var details = summary.parentNode; |
| 191 |
|
| 192 |
// Apply "no-details" to for unsupported details tags |
| 193 |
if (!details.className.match(new RegExp("(\\s|^)no-details(\\s|$)"))) { |
| 194 |
details.className += " no-details"; |
| 195 |
} |
| 196 |
|
| 197 |
summary.addEventListener("click", function (e) { |
| 198 |
var node = e.target.parentNode; |
| 199 |
if (node.hasAttribute("open")) { |
| 200 |
node.removeAttribute("open"); |
| 201 |
} else { |
| 202 |
node.setAttribute("open", "open"); |
| 203 |
} |
| 204 |
}); |
| 205 |
} |
| 206 |
} |
| 207 |
}); |
| 208 |
|
| 209 |
(function () { |
| 210 |
var onReady = function onReady(fn) { |
| 211 |
if (document.addEventListener) { |
| 212 |
document.addEventListener("DOMContentLoaded", fn); |
| 213 |
} else { |
| 214 |
document.attachEvent("onreadystatechange", function () { |
| 215 |
if (document.readyState === "interactive") { |
| 216 |
fn(); |
| 217 |
} |
| 218 |
}); |
| 219 |
} |
| 220 |
}; |
| 221 |
|
| 222 |
onReady(function () { |
| 223 |
details(); |
| 224 |
}); |
| 225 |
})(); |
| 226 |
|
| 227 |
}()); |
| 228 |
``` |
| 229 |
//// |
| 230 |
/// |