Improved resilience of privacy plugin

This commit is contained in:
squidfunk 2024-03-31 17:30:44 +08:00
parent 6c9ba875bf
commit 29cf44b71c
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
2 changed files with 26 additions and 6 deletions

View File

@ -33,6 +33,7 @@ from concurrent.futures import Future, ThreadPoolExecutor, wait
from hashlib import sha1 from hashlib import sha1
from mkdocs.config.config_options import ExtraScriptValue from mkdocs.config.config_options import ExtraScriptValue
from mkdocs.config.defaults import MkDocsConfig from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin, event_priority from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.structure.files import File, Files from mkdocs.structure.files import File, Files
from mkdocs.utils import is_error_template from mkdocs.utils import is_error_template
@ -241,9 +242,18 @@ class PrivacyPlugin(BasePlugin[PrivacyConfig]):
parser.feed(fragment) parser.feed(fragment)
parser.close() parser.close()
# Return element # Check parse result and return element
assert isinstance(parser.result, Element) if isinstance(parser.result, Element):
return parser.result return parser.result
# Otherwise, raise a plugin error - if the author accidentally used
# invalid HTML inside of the tag, e.g., forget a opening or closing
# quote, we need to catch this here, as we're using pretty basic
# regular expression based extraction
raise PluginError(
f"Could not parse due to possible syntax error in HTML: \n\n"
+ fragment
)
# Parse and extract all external assets from a media file using a preset # Parse and extract all external assets from a media file using a preset
# regular expression, and return all URLs found. # regular expression, and return all URLs found.

View File

@ -33,6 +33,7 @@ from concurrent.futures import Future, ThreadPoolExecutor, wait
from hashlib import sha1 from hashlib import sha1
from mkdocs.config.config_options import ExtraScriptValue from mkdocs.config.config_options import ExtraScriptValue
from mkdocs.config.defaults import MkDocsConfig from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin, event_priority from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.structure.files import File, Files from mkdocs.structure.files import File, Files
from mkdocs.utils import is_error_template from mkdocs.utils import is_error_template
@ -241,9 +242,18 @@ class PrivacyPlugin(BasePlugin[PrivacyConfig]):
parser.feed(fragment) parser.feed(fragment)
parser.close() parser.close()
# Return element # Check parse result and return element
assert isinstance(parser.result, Element) if isinstance(parser.result, Element):
return parser.result return parser.result
# Otherwise, raise a plugin error - if the author accidentally used
# invalid HTML inside of the tag, e.g., forget a opening or closing
# quote, we need to catch this here, as we're using pretty basic
# regular expression based extraction
raise PluginError(
f"Could not parse due to possible syntax error in HTML: \n\n"
+ fragment
)
# Parse and extract all external assets from a media file using a preset # Parse and extract all external assets from a media file using a preset
# regular expression, and return all URLs found. # regular expression, and return all URLs found.