File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change 3333from hashlib import sha1
3434from mkdocs .config .config_options import ExtraScriptValue
3535from mkdocs .config .defaults import MkDocsConfig
36+ from mkdocs .exceptions import PluginError
3637from mkdocs .plugins import BasePlugin , event_priority
3738from mkdocs .structure .files import File , Files
3839from mkdocs .utils import is_error_template
@@ -241,9 +242,18 @@ def _parse_fragment(self, fragment: str):
241242 parser .feed (fragment )
242243 parser .close ()
243244
244- # Return element
245- assert isinstance (parser .result , Element )
246- return parser .result
245+ # Check parse result and return element
246+ if isinstance (parser .result , Element ):
247+ return parser .result
248+
249+ # Otherwise, raise a plugin error - if the author accidentally used
250+ # invalid HTML inside of the tag, e.g., forget a opening or closing
251+ # quote, we need to catch this here, as we're using pretty basic
252+ # regular expression based extraction
253+ raise PluginError (
254+ f"Could not parse due to possible syntax error in HTML: \n \n "
255+ + fragment
256+ )
247257
248258 # Parse and extract all external assets from a media file using a preset
249259 # regular expression, and return all URLs found.
Original file line number Diff line number Diff line change 3333from hashlib import sha1
3434from mkdocs .config .config_options import ExtraScriptValue
3535from mkdocs .config .defaults import MkDocsConfig
36+ from mkdocs .exceptions import PluginError
3637from mkdocs .plugins import BasePlugin , event_priority
3738from mkdocs .structure .files import File , Files
3839from mkdocs .utils import is_error_template
@@ -241,9 +242,18 @@ def _parse_fragment(self, fragment: str):
241242 parser .feed (fragment )
242243 parser .close ()
243244
244- # Return element
245- assert isinstance (parser .result , Element )
246- return parser .result
245+ # Check parse result and return element
246+ if isinstance (parser .result , Element ):
247+ return parser .result
248+
249+ # Otherwise, raise a plugin error - if the author accidentally used
250+ # invalid HTML inside of the tag, e.g., forget a opening or closing
251+ # quote, we need to catch this here, as we're using pretty basic
252+ # regular expression based extraction
253+ raise PluginError (
254+ f"Could not parse due to possible syntax error in HTML: \n \n "
255+ + fragment
256+ )
247257
248258 # Parse and extract all external assets from a media file using a preset
249259 # regular expression, and return all URLs found.
You can’t perform that action at this time.
0 commit comments