Refactored offline plugin

This commit is contained in:
squidfunk 2023-08-10 18:30:55 +02:00
parent 554976f90f
commit d005e85cdb
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
2 changed files with 20 additions and 22 deletions

View File

@ -32,12 +32,13 @@ from material.plugins.offline.config import OfflineConfig
# Offline plugin
class OfflinePlugin(BasePlugin[OfflineConfig]):
# Initialize plugin
# Set configuration for offline build
def on_config(self, config):
if not self.config.enabled:
return
# Ensure correct resolution of links
# Ensure correct resolution of links when viewing the site from the
# file system by disabling directory URLs
config.use_directory_urls = False
# Append iframe-worker to polyfills/shims
@ -46,7 +47,8 @@ class OfflinePlugin(BasePlugin[OfflineConfig]):
worker = "https://unpkg.com/iframe-worker/shim"
config.extra.polyfills.append(worker)
# Support offline search (run latest)
# 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
@event_priority(-100)
def on_post_build(self, *, config):
if not self.config.enabled:
@ -55,15 +57,12 @@ class OfflinePlugin(BasePlugin[OfflineConfig]):
# Check for existence of search index
base = os.path.join(config.site_dir, "search")
path = os.path.join(base, "search_index.json")
if not os.path.exists(path):
if not os.path.isfile(path):
return
# Retrieve search index
with open(path, "r") as data:
index = data.read()
# Inline search index into script
# Create script with inlined search index
with open(path, "r") as f:
utils.write_file(
f"var __index = {index}".encode("utf-8"),
os.path.join(base, "search_index.js")
f"var __index = {f.read()}".encode("utf-8"),
path.replace(".json", ".js"),
)

View File

@ -32,12 +32,13 @@ from material.plugins.offline.config import OfflineConfig
# Offline plugin
class OfflinePlugin(BasePlugin[OfflineConfig]):
# Initialize plugin
# Set configuration for offline build
def on_config(self, config):
if not self.config.enabled:
return
# Ensure correct resolution of links
# Ensure correct resolution of links when viewing the site from the
# file system by disabling directory URLs
config.use_directory_urls = False
# Append iframe-worker to polyfills/shims
@ -46,7 +47,8 @@ class OfflinePlugin(BasePlugin[OfflineConfig]):
worker = "https://unpkg.com/iframe-worker/shim"
config.extra.polyfills.append(worker)
# Support offline search (run latest)
# 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
@event_priority(-100)
def on_post_build(self, *, config):
if not self.config.enabled:
@ -55,15 +57,12 @@ class OfflinePlugin(BasePlugin[OfflineConfig]):
# Check for existence of search index
base = os.path.join(config.site_dir, "search")
path = os.path.join(base, "search_index.json")
if not os.path.exists(path):
if not os.path.isfile(path):
return
# Retrieve search index
with open(path, "r") as data:
index = data.read()
# Inline search index into script
# Create script with inlined search index
with open(path, "r") as f:
utils.write_file(
f"var __index = {index}".encode("utf-8"),
os.path.join(base, "search_index.js")
f"var __index = {f.read()}".encode("utf-8"),
path.replace(".json", ".js"),
)