Skip to content

Commit

Permalink
fix: use relative paths to retrieve css and js contents from path_to_…
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
AdrianDsg committed Jan 26, 2024
1 parent 15219bc commit b8d308b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mkdocs_minify_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,37 @@ def _minify(self, file_type: str, config: MkDocsConfig) -> None:
if not isinstance(file_paths, list):
file_paths = [file_paths]

site_dir = Path(config['site_dir'])

file_paths2 = []
for file_path in file_paths.copy():
if "*" in file_path:
glob_parts = file_path.split("*", maxsplit=1)
glob_dir = Path(config['site_dir']) / Path(glob_parts[0])
glob_dir = site_dir / Path(glob_parts[0])
file_path = glob_dir.glob(f"*{glob_parts[1]}")

for glob_file in file_path:
file_paths2.append(glob_file)
else:
file_paths2.append(Path(config['site_dir']) / file_path)
file_paths2.append(site_dir / file_path)

# remove duplicates
file_paths2 = list(set(file_paths2))

for file_path in file_paths2:
site_file_path: str = str(file_path.as_posix())
rel_file_path: str = site_file_path.replace(site_dir.as_posix(), "").strip("/")

with open(site_file_path, mode="r+", encoding="utf8") as file:
if self.config["cache_safe"]:
file.write(self.path_to_data[file_path])
file.write(self.path_to_data[rel_file_path])
else:
minified: str = self._minify_file_data_with_func(file.read(), minify_func)
file.seek(0)
file.write(minified)
file.truncate()

file_hash: str = self.path_to_hash.get(file_path, "")
file_hash: str = self.path_to_hash.get(rel_file_path, "")

# Rename to [.hash].min.{file_type}
os.rename(site_file_path, self._minified_asset(site_file_path, file_type, file_hash))
Expand Down

0 comments on commit b8d308b

Please sign in to comment.