mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2024-06-14 11:52:32 +03:00
Refactored offline plugin
This commit is contained in:
parent
554976f90f
commit
d005e85cdb
@ -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"),
|
||||
)
|
||||
|
@ -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"),
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user