reallysimpledocs 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,27 @@
1
+ import lunr from "lunr";
2
+ import config from "virtual:reallysimpledocs/config";
3
+ import { getSearchDocs } from "../lib/docs.js";
4
+
5
+ export function GET() {
6
+ const documents = getSearchDocs(config);
7
+ const index = lunr(function () {
8
+ this.ref("slug");
9
+ this.field("title", { boost: 10 });
10
+ this.field("body");
11
+ this.metadataWhitelist = ["position"];
12
+
13
+ documents.forEach((doc) => this.add(doc));
14
+ });
15
+
16
+ return new Response(
17
+ JSON.stringify({
18
+ index,
19
+ documents: documents.map(({ slug, title, path, body }) => ({ slug, title, path, body })),
20
+ }),
21
+ {
22
+ headers: {
23
+ "content-type": "application/json; charset=utf-8",
24
+ },
25
+ },
26
+ );
27
+ }
@@ -0,0 +1,54 @@
1
+ import Copy from "lucide-static/icons/copy.svg?raw";
2
+ import Check from "lucide-static/icons/check.svg?raw";
3
+
4
+ const copyText = async (text) => {
5
+ if (window.isSecureContext && navigator.clipboard?.writeText) {
6
+ return navigator.clipboard.writeText(text);
7
+ }
8
+
9
+ const textarea = document.createElement("textarea");
10
+ textarea.value = text;
11
+ textarea.setAttribute("readonly", "");
12
+ textarea.style.position = "fixed";
13
+ textarea.style.top = "-9999px";
14
+ textarea.style.left = "-9999px";
15
+ document.body.appendChild(textarea);
16
+ textarea.select();
17
+ const ok = document.execCommand("copy");
18
+ textarea.remove();
19
+ if (!ok) throw new Error("Copy failed");
20
+ };
21
+
22
+ const initCopyCode = (codeBlock) => {
23
+ const codeEl = codeBlock.querySelector(":scope > code");
24
+ if (!codeEl) return;
25
+
26
+ const button = document.createElement("button");
27
+ button.type = "button";
28
+ button.className = "code-block-copy";
29
+ button.setAttribute("aria-label", "Copy code");
30
+ button.innerHTML = Copy;
31
+
32
+ button.addEventListener("click", async () => {
33
+ const text = (codeEl.textContent || "").replace(/\n$/, "");
34
+ if (!text) return;
35
+
36
+ button.disabled = true;
37
+ try {
38
+ await copyText(text);
39
+ button.innerHTML = Check;
40
+ } catch (error) {
41
+ console.error("[copy] Failed to copy:", error);
42
+ button.textContent = "Failed";
43
+ }
44
+ window.setTimeout(() => {
45
+ button.disabled = false;
46
+ button.innerHTML = Copy;
47
+ }, 1200);
48
+ });
49
+
50
+ codeBlock.insertBefore(button, codeBlock.firstChild);
51
+ codeBlock.dataset.copyCodeInitialized = true;
52
+ };
53
+
54
+ window.basecoat?.register("copy-code", "pre:not([data-copy-code-initialized])", initCopyCode);