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
4f10a612fa
commit
6a6d2d8edf
@ -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}")
|
||||||
|
@ -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}")
|
||||||
|
Loading…
Reference in New Issue
Block a user