Refactored offline plugin

This commit is contained in:
squidfunk 2023-08-23 15:00:05 +02:00
parent 4f10a612fa
commit 6a6d2d8edf
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
2 changed files with 32 additions and 28 deletions

View File

@ -21,7 +21,6 @@
import os import os
from mkdocs.plugins import BasePlugin, event_priority from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.utils import write_file
from .config import OfflineConfig from .config import OfflineConfig
@ -42,10 +41,10 @@ class OfflinePlugin(BasePlugin[OfflineConfig]):
config.use_directory_urls = False config.use_directory_urls = False
# Append iframe-worker to polyfills/shims # Append iframe-worker to polyfills/shims
config.extra.polyfills = config.extra.get("polyfills", []) config.extra["polyfills"] = config.extra.get("polyfills", [])
if not any("iframe-worker" in url for url in config.extra.polyfills): if not any("iframe-worker" in url for url in config.extra["polyfills"]):
worker = "https://unpkg.com/iframe-worker/shim" script = "https://unpkg.com/iframe-worker/shim"
config.extra.polyfills.append(worker) config.extra["polyfills"].append(script)
# Add support for offline search (run latest) - the search index is copied # Add support for offline search (run latest) - the search index is copied
# and inlined into a script, so that it can be used without a server # and inlined into a script, so that it can be used without a server
@ -54,14 +53,17 @@ class OfflinePlugin(BasePlugin[OfflineConfig]):
if not self.config.enabled: if not self.config.enabled:
return return
# Check for existence of search index # Ensure presence of search index
path = os.path.join(config.site_dir, "search", "search_index.json") path = os.path.join(config.site_dir, "search")
if not os.path.isfile(path): file = os.path.join(path, "search_index.json")
if not os.path.isfile(file):
return return
# Create script with inlined search index # Obtain search index contents
with open(path, encoding = "utf-8") as f: with open(file, encoding = "utf-8") as f:
write_file( data = f.read()
f"var __index = {f.read()}".encode("utf-8"),
path.replace(".json", ".js"), # Inline search index contents into script
) file = os.path.join(path, "search_index.js")
with open(file, "w", encoding = "utf-8") as f:
f.write(f"var __index = {data}")

View File

@ -21,7 +21,6 @@
import os import os
from mkdocs.plugins import BasePlugin, event_priority from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.utils import write_file
from .config import OfflineConfig from .config import OfflineConfig
@ -42,10 +41,10 @@ class OfflinePlugin(BasePlugin[OfflineConfig]):
config.use_directory_urls = False config.use_directory_urls = False
# Append iframe-worker to polyfills/shims # Append iframe-worker to polyfills/shims
config.extra.polyfills = config.extra.get("polyfills", []) config.extra["polyfills"] = config.extra.get("polyfills", [])
if not any("iframe-worker" in url for url in config.extra.polyfills): if not any("iframe-worker" in url for url in config.extra["polyfills"]):
worker = "https://unpkg.com/iframe-worker/shim" script = "https://unpkg.com/iframe-worker/shim"
config.extra.polyfills.append(worker) config.extra["polyfills"].append(script)
# Add support for offline search (run latest) - the search index is copied # Add support for offline search (run latest) - the search index is copied
# and inlined into a script, so that it can be used without a server # and inlined into a script, so that it can be used without a server
@ -54,14 +53,17 @@ class OfflinePlugin(BasePlugin[OfflineConfig]):
if not self.config.enabled: if not self.config.enabled:
return return
# Check for existence of search index # Ensure presence of search index
path = os.path.join(config.site_dir, "search", "search_index.json") path = os.path.join(config.site_dir, "search")
if not os.path.isfile(path): file = os.path.join(path, "search_index.json")
if not os.path.isfile(file):
return return
# Create script with inlined search index # Obtain search index contents
with open(path, encoding = "utf-8") as f: with open(file, encoding = "utf-8") as f:
write_file( data = f.read()
f"var __index = {f.read()}".encode("utf-8"),
path.replace(".json", ".js"), # Inline search index contents into script
) file = os.path.join(path, "search_index.js")
with open(file, "w", encoding = "utf-8") as f:
f.write(f"var __index = {data}")