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