Skip to content

Commit

Permalink
feat: Integration modules UUID redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkheir committed Oct 16, 2024
1 parent fbfb055 commit f7436d3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
1 change: 0 additions & 1 deletion action_library

This file was deleted.

1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ plugins:
xdr/features/investigate/dork_language.md: xdr/features/investigate/events_query_language.md
- redoc
- intakes_by_uuid
- modules_by_uuid
repo_url: https://github.com/SEKOIA-IO/documentation
site_name: Sekoia.io Documentation
site_url: https://docs.sekoia.io
Expand Down
72 changes: 72 additions & 0 deletions plugins/modules_by_uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import re
import string
from pathlib import Path

import mkdocs
from mkdocs.config import Config
from mkdocs.structure.files import File, Files
from mkdocs.utils.meta import get_data


class ModulesByUUIDPlugin(mkdocs.plugins.BasePlugin):
"""Reading Markdown files that contains an `uuid` metadata to provide
a redirection.
When such a file is identified, a new
`integration/action_library/uuid//uuid/$uuid.md` file is faked
which will redirect to it."""

template = """<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<link rel="canonical" href="../../../../{destination}">
<meta name="robots" content="noindex">
<script>var anchor=window.location.hash.substr(1);location.href="../../../../{destination}"+(anchor?"#"+anchor:"")</script>
<meta http-equiv="refresh" content="0; url=../../../../{destination}">
</head>
<body>
Redirecting...
</body>
</html>"""

_redirection_table: dict[str, str] = {}

def on_files(self, files: Files, config: Config):
for source_file in files:
if not source_file.src_path.endswith(".md"):
continue

filename = Path(config["docs_dir"]) / Path(source_file.src_path)
try:
with filename.open() as f:
_, metadata = get_data(f.read())
except:
# File may have been generated by an other plugin
continue

if (
"uuid" not in metadata
or metadata.get("type").lower() != "playbook"
or not source_file.url.startswith("integration/action_library/")
):
continue

self._redirection_table[metadata["uuid"]] = source_file.url

files._files.append(
File(
path=f"integration/action_library/uuid/{metadata['uuid']}.md",
src_dir="integration/action_library/uuid",
dest_dir=config["site_dir"],
use_directory_urls=True,
)
)

def on_page_read_source(self, page, config):
if page.file.src_path.startswith("integration/action_library/uuid/"):
if page.file.name in self._redirection_table:
return self.template.format(
destination=self._redirection_table[page.file.name]
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ packages = [
[tool.poetry.plugins."mkdocs.plugins"]
redoc = "plugins.redoc:RedocPlugin"
intakes_by_uuid = "plugins.intakes_by_uuid:IntakesByUUIDPlugin"
modules_by_uuid = "plugins.modules_by_uuid:ModulesByUUIDPlugin"

[tool.poetry.dependencies]
python = "^3.11"
Expand Down

0 comments on commit f7436d3

Please sign in to comment.