summaryrefslogtreecommitdiff
path: root/packages/pagefind/lektor_pagefind.py
blob: 17627fccaadb0634474d3bce34ff0eacaf867183 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from __future__ import annotations

import subprocess
from pathlib import Path
from typing import Any

from lektor.pluginsystem import Plugin

COMPONENT_UI_FILES = (
    "pagefind-component-ui.css",
    "pagefind-component-ui.js",
)


class PagefindPlugin(Plugin):
    name = "Pagefind"
    description = "Generate the Pagefind search index."

    def on_after_prune(
        self,
        builder: Any,
        all: bool,
        **extra: Any,
    ) -> None:
        if all:
            return

        site_path = Path(builder.destination_path)

        subprocess.run(
            [
                "npx",
                "pagefind",
                "--site",
                str(site_path),
            ],
            cwd=self.env.root_path,
            check=True,
        )

        pagefind_path = site_path / "pagefind"

        for filename in COMPONENT_UI_FILES:
            (pagefind_path / filename).unlink(missing_ok=True)