Skip to content

Commit 49e1d29

Browse files
authored
Add an HtmlStash section to docs
1 parent 12be7c9 commit 49e1d29

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

docs/extensions/api.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,25 @@ def set_link_class(self, element):
554554
For more information about working with ElementTree see the [ElementTree
555555
Documentation][ElementTree].
556556

557+
## Working with Raw HTML {: #working_with_raw_html }
558+
559+
Occasionally an extension may need to call out to a third party library which returns a pre-made string
560+
of raw HTML that needs to be inserted into the document unmodified. Raw strings can be stashed for later
561+
retrieval using an `htmlStash` instance, rather than converting them into `ElementTree` objects. A raw string
562+
(which may or may not be raw HTML) passed to `self.md.htmlStash.store()` will be saved to the stash and a
563+
placeholder string will be returned which should be inserted into the tree instead. After the tree is
564+
serialized, a postprocessor will replace the placeholder with the raw string. This prevents subsequent
565+
processing steps from modifying the HTML data. For example,
566+
567+
```python
568+
html = "<p>This is some <em>raw</em> HTML data</p>"
569+
el = etree.Element("div")
570+
el.text = self.md.htmlStash.store(html)
571+
```
572+
573+
For the global `htmlStash` instance to be available from a processor, the `markdown.Markdown` instance must
574+
be passed to the processor from [extendMarkdown](#extendmarkdown) and will be available as `self.md.htmlStash`.
575+
557576
## Integrating Your Code Into Markdown {: #integrating_into_markdown }
558577

559578
Once you have the various pieces of your extension built, you need to tell Markdown about them and ensure that they

0 commit comments

Comments
 (0)