rendering of markdown gives bad result
Problem Description
rendering of markdown gives bad result if it contains headings, a fenced code block and some specific html
Steps to reproduce the behavior:
test script: markdown_test.py
"""
# markdown test
## subsection 1
```
fenced code block without syntax highlighting
```
## subsection 2
```python
# fenced code block with syntax highlighting
print("hello")
```
## subsection 3
<h2> an html heading </h2>
## subsection 4 with an html table
<ul>
<li> one </li>
<li> two </li>
<li> three </li>
</ul>
<div>
bla
</div>
Run pdoc on test script:
pdoc markdown_test.py
Gives this result in the browser:

Note, how the headings aren´t rendered correctly starting with the fenced python code block. If I remove the python syntax highlighting or the html <div> section at the end or even only the </div> marker at the very end of the test script, the result looks fine.
System Information
I encountered the problem first from a conda environment on windows when I tried to include a jupyter notebook exported to markdown into a project documentation. Then I tried on a linux machine with a conda environment and didn´t see the problem. But when I updated conda (conda update --all), the same issue came up. The update was from
pdoc 12.0.2 + python 3.9.12
to
pdoc 12.0.2 + python 3.9.13
(my windows conda also has the second version combination)
In detail, the following packages were updated:
_openmp_mutex 4.5-1_gnu --> 4.5-2_gnu
alsa-lib 1.2.3-h516909a_0 --> 1.2.6.1-h7f98852_0
anyio 3.5.0-py39hf3d152e_0 --> 3.6.1-py39hf3d152e_0
astroid conda-forge::astroid-2.11.2-py39hf3d1~ --> pkgs/main::astroid-2.11.7-py39h06a4308_0
attrs 21.4.0-pyhd8ed1ab_0 --> 22.1.0-pyh71513ae_1
babel 2.9.1-pyh44b312d_0 --> 2.10.3-pyhd8ed1ab_0
black 22.3.0-pyhd8ed1ab_0 --> 22.6.0-pyhd8ed1ab_0
bleach 5.0.0-pyhd8ed1ab_0 --> 5.0.1-pyhd8ed1ab_0
cffi 1.15.0-py39h4bc2ebd_0 --> 1.15.1-py39he91dace_0
charset-normalizer 2.0.12-pyhd8ed1ab_0 --> 2.1.0-pyhd8ed1ab_0
click 8.1.2-py39hf3d152e_0 --> 8.1.3-py39hf3d152e_0
colorama 0.4.4-pyh9f0ad1d_0 --> 0.4.5-pyhd8ed1ab_0
cryptography 36.0.2-py39hd97740a_1 --> 37.0.4-py39hd97740a_0
debugpy 1.5.1-py39he80948d_0 --> 1.6.0-py39h5a03fae_0
dill 0.3.4-pyhd8ed1ab_0 --> 0.3.5.1-pyhd8ed1ab_0
executing 0.8.3-pyhd8ed1ab_0 --> 0.9.1-pyhd8ed1ab_0
fonttools 4.32.0-py39hb9d737c_0 --> 4.34.4-py39hb9d737c_0
gst-plugins-base 1.20.1-hcf0ee16_1 --> 1.20.3-hf6a322e_0
gstreamer 1.20.1-hd4edc92_1 --> 1.20.3-hd4edc92_0
icu 69.1-h9c3ff4c_0 --> 70.1-h27087fc_0
importlib-metadata 4.11.3-py39hf3d152e_1 --> 4.11.4-py39hf3d152e_0
importlib_resourc~ 5.6.0-pyhd8ed1ab_0 --> 5.9.0-pyhd8ed1ab_0
ipykernel conda-forge/linux-64::ipykernel-6.9.2~ --> conda-forge/noarch::ipykernel-6.15.1-pyh210e3f2_0
ipympl 0.8.8-pyhd8ed1ab_0 --> 0.9.1-pyhd8ed1ab_0
ipython 8.2.0-py39hf3d152e_0 --> 8.4.0-py39hf3d152e_0
ipywidgets 7.7.0-pyhd8ed1ab_0 --> 7.7.1-pyhd8ed1ab_0
jinja2 3.1.1-pyhd8ed1ab_0 --> 3.1.2-pyhd8ed1ab_1
jpeg 9e-h7f98852_0 --> 9e-h166bdaf_2
jsonschema 4.4.0-pyhd8ed1ab_0 --> 4.9.0-pyhd8ed1ab_0
jupyter_client 7.2.2-pyhd8ed1ab_1 --> 7.3.4-pyhd8ed1ab_0
jupyter_core 4.9.2-py39hf3d152e_0 --> 4.11.1-py39hf3d152e_0
jupyter_server 1.16.0-pyhd8ed1ab_1 --> 1.18.1-pyhd8ed1ab_0
jupyterlab 3.3.3-pyhd8ed1ab_0 --> 3.4.4-pyhd8ed1ab_0
jupyterlab_pygmen~ 0.2.0-pyhd8ed1ab_0 --> 0.2.2-pyhd8ed1ab_0
jupyterlab_server 2.12.0-pyhd8ed1ab_0 --> 2.15.0-pyhd8ed1ab_0
jupyterlab_widgets 1.1.0-pyhd8ed1ab_0 --> 1.1.1-pyhd8ed1ab_0
kiwisolver 1.4.2-py39hf939315_1 --> 1.4.4-py39hf939315_0
lerc 3.0-h9c3ff4c_0 --> 4.0.0-h27087fc_0
libblas 3.9.0-14_linux64_openblas --> 3.9.0-15_linux64_openblas
libcblas 3.9.0-14_linux64_openblas --> 3.9.0-15_linux64_openblas
libclang 13.0.1-default_hc23dcda_0 --> 14.0.6-default_h2e3cab8_0
libdeflate 1.10-h7f98852_0 --> 1.12-h166bdaf_0
libgcc-ng 11.2.0-h1d223b6_15 --> 12.1.0-h8d9b700_16
libgfortran-ng 11.2.0-h69a702a_15 --> 12.1.0-h69a702a_16
libgfortran5 11.2.0-h5c6108e_15 --> 12.1.0-hdcd56e2_16
libglib 2.70.2-h174f98d_4 --> 2.72.1-h2d90d5f_0
libgomp 11.2.0-h1d223b6_15 --> 12.1.0-h8d9b700_16
liblapack 3.9.0-14_linux64_openblas --> 3.9.0-15_linux64_openblas
libopenblas 0.3.20-pthreads_h78a6416_0 --> 0.3.20-pthreads_h78a6416_1
libpng 1.6.37-h21135ba_2 --> 1.6.37-h753d276_3
libpq 14.2-hd57d9b9_0 --> 14.4-hd77ab85_0
libstdcxx-ng 11.2.0-he4da1e4_15 --> 12.1.0-ha89aaad_16
libtiff 4.3.0-h542a066_3 --> 4.4.0-h0d92c0b_2
libwebp 1.2.2-h3452ae3_0 --> 1.2.3-h522a892_1
libwebp-base 1.2.2-h7f98852_1 --> 1.2.3-h166bdaf_2
libxml2 2.9.12-h885dcf4_1 --> 2.9.14-h22db469_3
libxslt 1.1.33-h0ef7038_3 --> 1.1.35-h8affb1d_0
libzlib 1.2.11-h166bdaf_1014 --> 1.2.12-h166bdaf_2
lxml 4.8.0-py39hb9d737c_1 --> 4.9.1-py39hb9d737c_0
matplotlib-base 3.5.1-py39h2fa2bec_0 --> 3.5.2-py39h700656a_1
mysql-common 8.0.28-haf5c9bc_3 --> 8.0.29-haf5c9bc_1
mysql-libs 8.0.28-h28c427c_3 --> 8.0.29-h28c427c_1
nbclassic 0.3.7-pyhd8ed1ab_0 --> 0.4.3-pyhd8ed1ab_0
nbclient 0.5.13-pyhd8ed1ab_0 --> 0.6.6-pyhd8ed1ab_0
nbconvert 6.4.5-pyhd8ed1ab_2 --> 6.5.0-pyhd8ed1ab_0
nbconvert-core 6.4.5-pyhd8ed1ab_2 --> 6.5.0-pyhd8ed1ab_0
nbconvert-pandoc 6.4.5-pyhd8ed1ab_2 --> 6.5.0-pyhd8ed1ab_0
nbformat 5.3.0-pyhd8ed1ab_0 --> 5.4.0-pyhd8ed1ab_0
notebook 6.4.10-pyha770c72_0 --> 6.4.12-pyha770c72_0
nss 3.77-h2350873_0 --> 3.78-h2350873_0
numpy 1.22.3-py39h18676bf_1 --> 1.23.1-py39hba7629e_0
openssl 1.1.1n-h166bdaf_0 --> 1.1.1q-h166bdaf_0
pandas 1.4.2-py39h1832856_0 --> 1.4.3-py39h1832856_0
pandoc 2.17.1.1-ha770c72_0 --> 2.18-ha770c72_0
pillow 9.1.0-py39hae2aec6_0 --> 9.2.0-py39hae2aec6_0
pip 22.0.4-pyhd8ed1ab_0 --> 22.2.1-pyhd8ed1ab_0
platformdirs 2.5.1-pyhd8ed1ab_0 --> 2.5.2-pyhd8ed1ab_1
prompt-toolkit 3.0.29-pyha770c72_0 --> 3.0.30-pyha770c72_0
psutil 5.9.0-py39hb9d737c_1 --> 5.9.1-py39hb9d737c_0
pylint 2.13.5-pyhd8ed1ab_0 --> 2.14.5-pyhd8ed1ab_0
pyparsing 3.0.7-pyhd8ed1ab_0 --> 3.0.9-pyhd8ed1ab_0
pyqt 5.12.3-py39hf3d152e_8 --> 5.15.7-py39h18e9c17_0
pyqt5-sip 4.19.18-py39he80948d_8 --> 12.11.0-py39h5a03fae_0
python 3.9.12-h9a8a25e_1_cpython --> 3.9.13-h9a8a25e_0_cpython
python-fastjsonsc~ 2.15.3-pyhd8ed1ab_0 --> 2.16.1-pyhd8ed1ab_0
pyzmq 22.3.0-py39headdf64_2 --> 23.2.0-py39headdf64_0
readline 8.1-h46c0cb4_0 --> 8.1.2-h0f457ee_0
requests 2.27.1-pyhd8ed1ab_0 --> 2.28.1-pyhd8ed1ab_0
ruamel_yaml 0.15.80-py39h3811e60_1006 --> 0.15.80-py39hb9d737c_1007
scipy 1.8.0-py39hee8e79c_1 --> 1.9.0-py39h8ba3f38_0
setuptools 62.0.0-py39hf3d152e_0 --> 63.3.0-py39hf3d152e_0
soupsieve 2.3.1-pyhd8ed1ab_0 --> 2.3.2.post1-pyhd8ed1ab_0
sqlite 3.37.1-h4ff8645_0 --> 3.39.2-h4ff8645_0
stack_data 0.2.0-pyhd8ed1ab_0 --> 0.3.0-pyhd8ed1ab_0
terminado 0.13.3-py39hf3d152e_1 --> 0.15.0-py39hf3d152e_0
tornado 6.1-py39hb9d737c_3 --> 6.2-py39hb9d737c_0
traitlets 5.1.1-pyhd8ed1ab_0 --> 5.3.0-pyhd8ed1ab_0
typed-ast 1.5.2-py39h3811e60_0 --> 1.5.4-py39hb9d737c_0
typing-extensions 4.1.1-hd8ed1ab_0 --> 4.3.0-hd8ed1ab_0
typing_extensions 4.1.1-pyha770c72_0 --> 4.3.0-pyha770c72_0
urllib3 1.26.9-pyhd8ed1ab_0 --> 1.26.11-pyhd8ed1ab_0
websocket-client 1.3.2-pyhd8ed1ab_0 --> 1.3.3-pyhd8ed1ab_0
widgetsnbextension conda-forge/linux-64::widgetsnbextens~ --> conda-forge/noarch::widgetsnbextension-3.6.1-pyha770c72_0
wrapt 1.14.0-py39hb9d737c_1 --> 1.14.1-py39hb9d737c_0
zlib 1.2.11-h166bdaf_1014 --> 1.2.12-h166bdaf_2
zstd 1.5.2-ha95c52a_0 --> 1.5.2-h8a70e8d_2
Thanks for the detailed report. This is a bug in the Markdown library we use, I've filed https://github.com/trentm/python-markdown2/pull/462 with a fix. :)