kill_tags¶
mdx_steroids.kill_tags deletes HTML elements from the finished output. You
choose the elements with CSS selectors or XPath expressions. It also deletes
elements that are empty, such as a <p> with nothing in it.
Example¶
With kill: [".ad"] and kill_known: true, this Markdown:
becomes (blank lines shortened):
<del>but not this</del>was removed by the built-in selectors thatkill_known: trueadds. The text after it,., was kept.<div class="ad">matched the selector.adand was removed with its content.- The empty
<p></p>was removed becausepis in the defaultkill_emptylist.
How it works¶
The extension is a postprocessor: it runs on the HTML string after Python-Markdown has serialised the document.
- If
normalizeis true, it reparses the HTML with BeautifulSoup andhtml5lib. - It parses the HTML as a fragment with
lxml, inside a temporary<div>. - It converts each entry of
killto XPath. An entry that starts with!is used as XPath directly; any other entry is a CSS selector and is converted withcssselect. - With
kill_known: true, it adds two built-in XPath expressions (see below). - It removes every element that matches, with its content. Text that followed the element (its "tail") is kept and attached to the previous element or to the parent.
- For each tag name in
kill_empty, it removes elements of that name that contain no text (other than whitespace) and have no attributes, at any depth. - It serialises the fragment back to HTML. If
normalizeis true, it runs BeautifulSoup over the result again.
The known selectors¶
kill_known: true adds these two XPath expressions:
//pre[@class and contains(concat(' ', normalize-space(@class), ' '), ' highlight ') and code[@class and contains(concat(' ', normalize-space(@class), ' '), ' language-del ')]]
descendant-or-self::del
The first removes a highlighted code block whose language is del. The second
removes every <del> element. Together they let you keep text in the Markdown
source, visible in a plain preview such as GitHub, that never reaches the
built HTML:
Visible paragraph.
```del
This block is visible on GitHub but removed from the HTML.
```
Visible ~~on GitHub only~~ text.
The first expression only matches <pre class="highlight"><code class="language-del">.
pymdownx.superfences produces that markup when pymdownx.highlight has
use_pygments: false. With Pygments turned on (the default), the class
highlight sits on a surrounding <div> and the block is not removed. The
~~text~~ form needs pymdownx.tilde, which turns it into <del>.
This configuration removes both parts of the example above:
markdown_extensions:
- pymdownx.highlight:
use_pygments: false
- pymdownx.superfences
- pymdownx.tilde
- mdx_steroids.kill_tags:
kill_known: true
The output is:
Options¶
| Option | Type | Default | Meaning |
|---|---|---|---|
kill |
list of str | [] |
CSS selectors, or XPath expressions prefixed with !. Matching elements are removed with their content. |
kill_known |
bool | false |
Also remove <del> elements and code blocks in the del language. |
kill_empty |
list of str | ["p", "div", "h1", "h2", "h3", "h4", "h5", "h6", "pre"] |
Tag names. Elements with these names are removed when they have no text and no attributes. |
normalize |
bool | false |
Pass the HTML through BeautifulSoup (html5lib) before and after the removal. |
normalize adds a document wrapper
html5lib builds a full document, so with normalize: true the output is
wrapped in <html><head></head><body>…</body></html>. Leave it off when the
output is inserted into a page, as in MkDocs.
Empty elements with attributes are kept
An empty element that has any attribute, such as <div class="spacer"></div>
or <p id="x"></p>, is not removed by kill_empty. An element that only
contains an image with a src is kept for the same reason.
Configuration examples¶
MkDocs:
markdown_extensions:
- pymdownx.tilde
- mdx_steroids.kill_tags:
kill:
- .advertisement-banner
- "!//figure[not(img)]"
kill_known: true
kill_empty: [p, div, span]
Python, with an XPath selector: