spacefn-docs 0.0.0-stage → 0.11.1
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.
- package/LICENSE +200 -0
- package/README.md +15 -2
- package/assets/reader.css +1131 -0
- package/assets/reader.js +324 -0
- package/content/1-getting-started.md +107 -0
- package/content/2-conventions.md +138 -0
- package/content/3-best-practices.md +68 -0
- package/content/4-folders/1-app/1-entry.md +48 -0
- package/content/4-folders/1-app/2-routes.md +92 -0
- package/content/4-folders/1-app/3-db.md +81 -0
- package/content/4-folders/1-app/4-queues.md +46 -0
- package/content/4-folders/1-app/5-crons.md +30 -0
- package/content/4-folders/1-app/6-assets.md +53 -0
- package/content/4-folders/1-app/7-durable-objects.md +60 -0
- package/content/4-folders/1-app/8-workflows.md +46 -0
- package/content/4-folders/1-app/index.md +43 -0
- package/content/4-folders/2-modules.md +71 -0
- package/content/4-folders/3-lib.md +60 -0
- package/content/4-folders/4-public.md +24 -0
- package/content/4-folders/5-docs.md +23 -0
- package/content/4-folders/6-configs.md +57 -0
- package/content/4-folders/7-space.md +34 -0
- package/content/4-folders/8-env.md +35 -0
- package/content/4-folders/9-package-json.md +37 -0
- package/content/4-folders/index.md +50 -0
- package/content/5-deployment/1-cloudflare.md +70 -0
- package/content/5-deployment/2-bun.md +68 -0
- package/content/5-deployment/3-infrastructure.md +67 -0
- package/content/5-deployment/4-ci-cd.md +179 -0
- package/content/5-deployment/index.md +48 -0
- package/content/6-guides/1-entry.md +113 -0
- package/content/6-guides/2-routes.md +162 -0
- package/content/6-guides/3-pages.md +148 -0
- package/content/6-guides/4-frontend.md +232 -0
- package/content/6-guides/5-database.md +178 -0
- package/content/6-guides/6-services.md +125 -0
- package/content/6-guides/7-jobs.md +61 -0
- package/content/6-guides/8-auth.md +111 -0
- package/content/6-guides/9-testing.md +70 -0
- package/content/6-guides/index.md +21 -0
- package/content/7-reference/1-config.md +67 -0
- package/content/7-reference/2-cli.md +45 -0
- package/content/7-reference/3-writing-docs.md +206 -0
- package/content/7-reference/index.md +15 -0
- package/content/index.md +49 -0
- package/content/logo.svg +12 -0
- package/dist/index.d.mts +72 -0
- package/dist/index.mjs +713 -0
- package/examples/README.md +18 -0
- package/examples/datastar/.env.example +2 -0
- package/examples/datastar/README.md +17 -0
- package/examples/datastar/app/assets/main.ts +4 -0
- package/examples/datastar/app/assets/site.css +3 -0
- package/examples/datastar/app/db/main/config.json +5 -0
- package/examples/datastar/app/db/main/migrations/0001_votes.sql +8 -0
- package/examples/datastar/app/db/main/queries/add-vote.sql +3 -0
- package/examples/datastar/app/db/main/queries/tally.sql +1 -0
- package/examples/datastar/app/entry.ts +28 -0
- package/examples/datastar/app/routes/_middleware.ts +4 -0
- package/examples/datastar/app/routes/health.ts +7 -0
- package/examples/datastar/app/routes/index.ts +43 -0
- package/examples/datastar/configs/env.ts +6 -0
- package/examples/datastar/configs/infra.ts +88 -0
- package/examples/datastar/configs/tsconfig.json +15 -0
- package/examples/datastar/configs/vite.ts +4 -0
- package/examples/datastar/configs/vitest.ts +9 -0
- package/examples/datastar/lib/types/rocket.d.ts +30 -0
- package/examples/datastar/lib/ui/base.css +17 -0
- package/examples/datastar/lib/ui/layout.ts +27 -0
- package/examples/datastar/modules/poll/client/poll-meter.ts +39 -0
- package/examples/datastar/modules/poll/poll.css +13 -0
- package/examples/datastar/modules/poll/poll.test.ts +48 -0
- package/examples/datastar/modules/poll/poll.ts +62 -0
- package/examples/datastar/modules/poll/views.ts +52 -0
- package/examples/datastar/package.json +33 -0
- package/examples/datastar/tsconfig.json +4 -0
- package/examples/lightningcss/.env.example +1 -0
- package/examples/lightningcss/README.md +15 -0
- package/examples/lightningcss/app/assets/site.css +3 -0
- package/examples/lightningcss/app/entry.ts +28 -0
- package/examples/lightningcss/app/routes/_middleware.ts +4 -0
- package/examples/lightningcss/app/routes/health.ts +7 -0
- package/examples/lightningcss/app/routes/index.ts +12 -0
- package/examples/lightningcss/configs/Dockerfile +26 -0
- package/examples/lightningcss/configs/Dockerfile.dockerignore +9 -0
- package/examples/lightningcss/configs/env.ts +4 -0
- package/examples/lightningcss/configs/infra.ts +55 -0
- package/examples/lightningcss/configs/tsconfig.json +15 -0
- package/examples/lightningcss/configs/vite.ts +19 -0
- package/examples/lightningcss/configs/vitest.ts +9 -0
- package/examples/lightningcss/lib/ui/layout.ts +17 -0
- package/examples/lightningcss/lib/ui/tokens.css +23 -0
- package/examples/lightningcss/modules/releases/releases.css +20 -0
- package/examples/lightningcss/modules/releases/releases.test.ts +18 -0
- package/examples/lightningcss/modules/releases/releases.ts +21 -0
- package/examples/lightningcss/modules/releases/views.ts +25 -0
- package/examples/lightningcss/package.json +33 -0
- package/examples/lightningcss/tsconfig.json +4 -0
- package/examples/mini-shopify/.env.example +6 -0
- package/examples/mini-shopify/README.md +19 -0
- package/examples/mini-shopify/app/assets/site.css +2 -0
- package/examples/mini-shopify/app/db/platform/config.json +5 -0
- package/examples/mini-shopify/app/db/platform/migrations/0001_shops.sql +10 -0
- package/examples/mini-shopify/app/db/platform/queries/add-shop.sql +4 -0
- package/examples/mini-shopify/app/db/platform/queries/find-shop.sql +3 -0
- package/examples/mini-shopify/app/db/platform/queries/list-shops.sql +1 -0
- package/examples/mini-shopify/app/db/shop/config.json +6 -0
- package/examples/mini-shopify/app/db/shop/migrations/0001_products.sql +11 -0
- package/examples/mini-shopify/app/db/shop/queries/add-product.sql +4 -0
- package/examples/mini-shopify/app/db/shop/queries/list-products.sql +1 -0
- package/examples/mini-shopify/app/db/shop/queries/sell-product.sql +2 -0
- package/examples/mini-shopify/app/entry.ts +25 -0
- package/examples/mini-shopify/app/routes/_middleware.ts +4 -0
- package/examples/mini-shopify/app/routes/health.ts +7 -0
- package/examples/mini-shopify/app/routes/index.ts +26 -0
- package/examples/mini-shopify/app/routes/s/[shop]/_middleware.ts +6 -0
- package/examples/mini-shopify/app/routes/s/[shop]/index.ts +32 -0
- package/examples/mini-shopify/configs/Dockerfile +26 -0
- package/examples/mini-shopify/configs/Dockerfile.dockerignore +9 -0
- package/examples/mini-shopify/configs/env.ts +14 -0
- package/examples/mini-shopify/configs/infra.ts +55 -0
- package/examples/mini-shopify/configs/tsconfig.json +15 -0
- package/examples/mini-shopify/configs/vite.ts +4 -0
- package/examples/mini-shopify/configs/vitest.ts +9 -0
- package/examples/mini-shopify/lib/ui/base.css +19 -0
- package/examples/mini-shopify/lib/ui/layout.ts +17 -0
- package/examples/mini-shopify/modules/catalog/catalog.ts +31 -0
- package/examples/mini-shopify/modules/catalog/views.ts +39 -0
- package/examples/mini-shopify/modules/shops/host.test.ts +38 -0
- package/examples/mini-shopify/modules/shops/host.ts +73 -0
- package/examples/mini-shopify/modules/shops/shops.test.ts +63 -0
- package/examples/mini-shopify/modules/shops/shops.ts +64 -0
- package/examples/mini-shopify/modules/shops/views.ts +28 -0
- package/examples/mini-shopify/package.json +35 -0
- package/examples/mini-shopify/tsconfig.json +4 -0
- package/examples/react/.env.example +2 -0
- package/examples/react/README.md +18 -0
- package/examples/react/app/assets/site.css +3 -0
- package/examples/react/app/entry.ts +28 -0
- package/examples/react/app/routes/_middleware.ts +4 -0
- package/examples/react/app/routes/about.ts +10 -0
- package/examples/react/app/routes/health.ts +7 -0
- package/examples/react/app/routes/index.ts +12 -0
- package/examples/react/configs/Dockerfile +26 -0
- package/examples/react/configs/Dockerfile.dockerignore +9 -0
- package/examples/react/configs/env.ts +6 -0
- package/examples/react/configs/infra.ts +55 -0
- package/examples/react/configs/tsconfig.json +16 -0
- package/examples/react/configs/vite.ts +7 -0
- package/examples/react/configs/vitest.ts +9 -0
- package/examples/react/lib/ui/base.css +12 -0
- package/examples/react/lib/ui/layout.ts +18 -0
- package/examples/react/modules/about/views.ts +8 -0
- package/examples/react/modules/shop/products.ts +18 -0
- package/examples/react/modules/shop/shop.css +13 -0
- package/examples/react/modules/shop/shop.test.ts +28 -0
- package/examples/react/modules/shop/shop.ts +13 -0
- package/examples/react/modules/shop/ui/Cart.island.tsx +31 -0
- package/examples/react/modules/shop/views.ts +16 -0
- package/examples/react/package.json +38 -0
- package/examples/react/tsconfig.json +4 -0
- package/examples/services-bun/README.md +10 -0
- package/examples/services-bun/app/assets/main.ts +4 -0
- package/examples/services-bun/app/assets/style.css +1 -0
- package/examples/services-bun/app/crons/nightly.ts +5 -0
- package/examples/services-bun/app/entry.ts +9 -0
- package/examples/services-bun/app/queues/jobs.ts +5 -0
- package/examples/services-bun/app/routes/index.ts +10 -0
- package/examples/services-bun/app/routes/runtime.ts +5 -0
- package/examples/services-bun/app/routes/send.ts +8 -0
- package/examples/services-bun/app/routes/version.ts +5 -0
- package/examples/services-bun/configs/Dockerfile +10 -0
- package/examples/services-bun/configs/Dockerfile.dockerignore +4 -0
- package/examples/services-bun/configs/infra.ts +28 -0
- package/examples/services-bun/configs/tsconfig.json +15 -0
- package/examples/services-bun/configs/vite.ts +4 -0
- package/examples/services-bun/lib/ui/base.css +3 -0
- package/examples/services-bun/lib/ui/layout.ts +13 -0
- package/examples/services-bun/modules/status/client/ready.ts +4 -0
- package/examples/services-bun/modules/status/status.ts +20 -0
- package/examples/services-bun/modules/status/views.ts +4 -0
- package/examples/services-bun/package.json +30 -0
- package/examples/services-bun/public/robots.txt +1 -0
- package/examples/services-bun/tsconfig.json +4 -0
- package/examples/services-cloudflare/README.md +9 -0
- package/examples/services-cloudflare/app/assets/main.ts +4 -0
- package/examples/services-cloudflare/app/crons/nightly.ts +5 -0
- package/examples/services-cloudflare/app/db/main/config.json +4 -0
- package/examples/services-cloudflare/app/db/main/migrations/0001_notes.sql +2 -0
- package/examples/services-cloudflare/app/db/main/queries/add-note.sql +1 -0
- package/examples/services-cloudflare/app/db/main/queries/count-notes.sql +3 -0
- package/examples/services-cloudflare/app/entry.ts +10 -0
- package/examples/services-cloudflare/app/queues/jobs.ts +5 -0
- package/examples/services-cloudflare/app/routes/files.ts +5 -0
- package/examples/services-cloudflare/app/routes/index.ts +10 -0
- package/examples/services-cloudflare/app/routes/limited.ts +7 -0
- package/examples/services-cloudflare/app/routes/send.ts +8 -0
- package/examples/services-cloudflare/configs/env.ts +3 -0
- package/examples/services-cloudflare/configs/infra.ts +88 -0
- package/examples/services-cloudflare/configs/tsconfig.json +15 -0
- package/examples/services-cloudflare/configs/vite.ts +4 -0
- package/examples/services-cloudflare/lib/ui/layout.ts +12 -0
- package/examples/services-cloudflare/modules/files/files.ts +9 -0
- package/examples/services-cloudflare/modules/status/client/ready.ts +4 -0
- package/examples/services-cloudflare/modules/status/status.ts +27 -0
- package/examples/services-cloudflare/modules/status/views.ts +8 -0
- package/examples/services-cloudflare/package.json +30 -0
- package/examples/services-cloudflare/public/robots.txt +1 -0
- package/examples/services-cloudflare/tsconfig.json +4 -0
- package/examples/solid/.env.example +2 -0
- package/examples/solid/README.md +18 -0
- package/examples/solid/app/assets/site.css +3 -0
- package/examples/solid/app/entry.ts +28 -0
- package/examples/solid/app/routes/_middleware.ts +4 -0
- package/examples/solid/app/routes/about.ts +10 -0
- package/examples/solid/app/routes/health.ts +7 -0
- package/examples/solid/app/routes/index.ts +12 -0
- package/examples/solid/configs/Dockerfile +26 -0
- package/examples/solid/configs/Dockerfile.dockerignore +9 -0
- package/examples/solid/configs/env.ts +6 -0
- package/examples/solid/configs/infra.ts +55 -0
- package/examples/solid/configs/tsconfig.json +17 -0
- package/examples/solid/configs/vite.ts +7 -0
- package/examples/solid/configs/vitest.ts +12 -0
- package/examples/solid/lib/ui/base.css +12 -0
- package/examples/solid/lib/ui/layout.ts +18 -0
- package/examples/solid/modules/about/views.ts +8 -0
- package/examples/solid/modules/shop/products.ts +18 -0
- package/examples/solid/modules/shop/shop.css +13 -0
- package/examples/solid/modules/shop/shop.test.ts +28 -0
- package/examples/solid/modules/shop/shop.ts +13 -0
- package/examples/solid/modules/shop/ui/Cart.island.tsx +31 -0
- package/examples/solid/modules/shop/views.ts +16 -0
- package/examples/solid/package.json +35 -0
- package/examples/solid/tsconfig.json +4 -0
- package/examples/starter-bun/.env.example +2 -0
- package/examples/starter-bun/AGENTS.md +9 -0
- package/examples/starter-bun/README.md +47 -0
- package/examples/starter-bun/app/assets/site.css +3 -0
- package/examples/starter-bun/app/db/main/config.json +5 -0
- package/examples/starter-bun/app/db/main/migrations/0001_todos.sql +9 -0
- package/examples/starter-bun/app/db/main/queries/add-todo.sql +2 -0
- package/examples/starter-bun/app/db/main/queries/list-todos.sql +2 -0
- package/examples/starter-bun/app/db/main/queries/toggle-todo.sql +2 -0
- package/examples/starter-bun/app/entry.ts +28 -0
- package/examples/starter-bun/app/routes/_middleware.ts +4 -0
- package/examples/starter-bun/app/routes/health.ts +7 -0
- package/examples/starter-bun/app/routes/index.ts +44 -0
- package/examples/starter-bun/configs/Dockerfile +26 -0
- package/examples/starter-bun/configs/Dockerfile.dockerignore +9 -0
- package/examples/starter-bun/configs/env.ts +6 -0
- package/examples/starter-bun/configs/infra.ts +55 -0
- package/examples/starter-bun/configs/tsconfig.json +15 -0
- package/examples/starter-bun/configs/vite.ts +4 -0
- package/examples/starter-bun/configs/vitest.ts +9 -0
- package/examples/starter-bun/docs/index.md +5 -0
- package/examples/starter-bun/lib/ui/base.css +17 -0
- package/examples/starter-bun/lib/ui/layout.ts +22 -0
- package/examples/starter-bun/modules/todos/todos.css +16 -0
- package/examples/starter-bun/modules/todos/todos.test.ts +33 -0
- package/examples/starter-bun/modules/todos/todos.ts +31 -0
- package/examples/starter-bun/modules/todos/views.ts +38 -0
- package/examples/starter-bun/package.json +32 -0
- package/examples/starter-bun/public/favicon.svg +1 -0
- package/examples/starter-bun/tsconfig.json +4 -0
- package/examples/starter-cloudflare/.env.example +2 -0
- package/examples/starter-cloudflare/AGENTS.md +9 -0
- package/examples/starter-cloudflare/README.md +47 -0
- package/examples/starter-cloudflare/app/assets/site.css +3 -0
- package/examples/starter-cloudflare/app/db/main/config.json +5 -0
- package/examples/starter-cloudflare/app/db/main/migrations/0001_todos.sql +9 -0
- package/examples/starter-cloudflare/app/db/main/queries/add-todo.sql +2 -0
- package/examples/starter-cloudflare/app/db/main/queries/list-todos.sql +2 -0
- package/examples/starter-cloudflare/app/db/main/queries/toggle-todo.sql +2 -0
- package/examples/starter-cloudflare/app/entry.ts +28 -0
- package/examples/starter-cloudflare/app/routes/_middleware.ts +4 -0
- package/examples/starter-cloudflare/app/routes/health.ts +7 -0
- package/examples/starter-cloudflare/app/routes/index.ts +44 -0
- package/examples/starter-cloudflare/configs/env.ts +6 -0
- package/examples/starter-cloudflare/configs/infra.ts +88 -0
- package/examples/starter-cloudflare/configs/tsconfig.json +15 -0
- package/examples/starter-cloudflare/configs/vite.ts +4 -0
- package/examples/starter-cloudflare/configs/vitest.ts +9 -0
- package/examples/starter-cloudflare/docs/index.md +5 -0
- package/examples/starter-cloudflare/lib/ui/base.css +17 -0
- package/examples/starter-cloudflare/lib/ui/layout.ts +22 -0
- package/examples/starter-cloudflare/modules/todos/todos.css +16 -0
- package/examples/starter-cloudflare/modules/todos/todos.test.ts +33 -0
- package/examples/starter-cloudflare/modules/todos/todos.ts +31 -0
- package/examples/starter-cloudflare/modules/todos/views.ts +38 -0
- package/examples/starter-cloudflare/package.json +35 -0
- package/examples/starter-cloudflare/public/favicon.svg +1 -0
- package/examples/starter-cloudflare/tsconfig.json +4 -0
- package/examples/svelte/.env.example +2 -0
- package/examples/svelte/README.md +18 -0
- package/examples/svelte/app/assets/site.css +3 -0
- package/examples/svelte/app/entry.ts +28 -0
- package/examples/svelte/app/routes/_middleware.ts +4 -0
- package/examples/svelte/app/routes/about.ts +10 -0
- package/examples/svelte/app/routes/health.ts +7 -0
- package/examples/svelte/app/routes/index.ts +12 -0
- package/examples/svelte/configs/env.ts +6 -0
- package/examples/svelte/configs/infra.ts +88 -0
- package/examples/svelte/configs/tsconfig.json +15 -0
- package/examples/svelte/configs/vite.ts +9 -0
- package/examples/svelte/configs/vitest.ts +9 -0
- package/examples/svelte/lib/types/svelte.d.ts +8 -0
- package/examples/svelte/lib/ui/base.css +12 -0
- package/examples/svelte/lib/ui/layout.ts +18 -0
- package/examples/svelte/modules/about/views.ts +8 -0
- package/examples/svelte/modules/shop/products.ts +18 -0
- package/examples/svelte/modules/shop/shop.css +13 -0
- package/examples/svelte/modules/shop/shop.test.ts +28 -0
- package/examples/svelte/modules/shop/shop.ts +13 -0
- package/examples/svelte/modules/shop/ui/Cart.island.svelte +23 -0
- package/examples/svelte/modules/shop/views.ts +16 -0
- package/examples/svelte/package.json +35 -0
- package/examples/svelte/tsconfig.json +4 -0
- package/examples/tailwind/.env.example +2 -0
- package/examples/tailwind/README.md +16 -0
- package/examples/tailwind/app/assets/site.css +2 -0
- package/examples/tailwind/app/entry.ts +28 -0
- package/examples/tailwind/app/routes/_middleware.ts +4 -0
- package/examples/tailwind/app/routes/health.ts +7 -0
- package/examples/tailwind/app/routes/index.ts +30 -0
- package/examples/tailwind/configs/Dockerfile +26 -0
- package/examples/tailwind/configs/Dockerfile.dockerignore +9 -0
- package/examples/tailwind/configs/env.ts +6 -0
- package/examples/tailwind/configs/infra.ts +55 -0
- package/examples/tailwind/configs/tsconfig.json +15 -0
- package/examples/tailwind/configs/vite.ts +7 -0
- package/examples/tailwind/configs/vitest.ts +9 -0
- package/examples/tailwind/lib/ui/components.ts +21 -0
- package/examples/tailwind/lib/ui/layout.ts +21 -0
- package/examples/tailwind/lib/ui/theme.css +10 -0
- package/examples/tailwind/modules/team/team.test.ts +30 -0
- package/examples/tailwind/modules/team/team.ts +34 -0
- package/examples/tailwind/modules/team/views.ts +45 -0
- package/examples/tailwind/package.json +35 -0
- package/examples/tailwind/tsconfig.json +4 -0
- package/examples/vue/.env.example +2 -0
- package/examples/vue/README.md +18 -0
- package/examples/vue/app/assets/site.css +3 -0
- package/examples/vue/app/entry.ts +28 -0
- package/examples/vue/app/routes/_middleware.ts +4 -0
- package/examples/vue/app/routes/about.ts +10 -0
- package/examples/vue/app/routes/health.ts +7 -0
- package/examples/vue/app/routes/index.ts +12 -0
- package/examples/vue/configs/env.ts +6 -0
- package/examples/vue/configs/infra.ts +88 -0
- package/examples/vue/configs/tsconfig.json +15 -0
- package/examples/vue/configs/vite.ts +7 -0
- package/examples/vue/configs/vitest.ts +9 -0
- package/examples/vue/lib/types/vue.d.ts +8 -0
- package/examples/vue/lib/ui/base.css +12 -0
- package/examples/vue/lib/ui/layout.ts +18 -0
- package/examples/vue/modules/about/views.ts +8 -0
- package/examples/vue/modules/shop/products.ts +18 -0
- package/examples/vue/modules/shop/shop.css +13 -0
- package/examples/vue/modules/shop/shop.test.ts +28 -0
- package/examples/vue/modules/shop/shop.ts +13 -0
- package/examples/vue/modules/shop/ui/Cart.island.vue +27 -0
- package/examples/vue/modules/shop/views.ts +16 -0
- package/examples/vue/package.json +35 -0
- package/examples/vue/tsconfig.json +4 -0
- package/package.json +44 -3
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,713 @@
|
|
|
1
|
+
import { existsSync, readFileSync, watch } from "node:fs";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { extname, join, resolve, sep } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
6
|
+
import { transformerMetaHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight } from "@shikijs/transformers";
|
|
7
|
+
import { Marked } from "marked";
|
|
8
|
+
import { bundledLanguages, createHighlighter } from "shiki";
|
|
9
|
+
// src/markdown.ts
|
|
10
|
+
const frontmatter = (source) => {
|
|
11
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/.exec(source);
|
|
12
|
+
if (!match) return {
|
|
13
|
+
data: {},
|
|
14
|
+
body: source
|
|
15
|
+
};
|
|
16
|
+
const data = {};
|
|
17
|
+
for (const line of match[1].split(/\r?\n/)) {
|
|
18
|
+
const pair = /^([\w-]+):\s*(.*)$/.exec(line);
|
|
19
|
+
if (pair) data[pair[1]] = pair[2].trim().replace(/^(["'])(.*)\1$/, "$2");
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
data,
|
|
23
|
+
body: source.slice(match[0].length)
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
const escapeHtml = (text) => text.replace(/[&<>"']/g, (c) => ({
|
|
27
|
+
"&": "&",
|
|
28
|
+
"<": "<",
|
|
29
|
+
">": ">",
|
|
30
|
+
"\"": """,
|
|
31
|
+
"'": "'"
|
|
32
|
+
})[c]);
|
|
33
|
+
const ENTITIES = {
|
|
34
|
+
amp: "&",
|
|
35
|
+
lt: "<",
|
|
36
|
+
gt: ">",
|
|
37
|
+
quot: "\"",
|
|
38
|
+
"#39": "'",
|
|
39
|
+
nbsp: " "
|
|
40
|
+
};
|
|
41
|
+
const decodeHtml = (text) => text.replace(/&(#x[0-9a-f]+|#\d+|\w+);/gi, (match, name) => {
|
|
42
|
+
if (name[0] === "#") return String.fromCodePoint(name[1]?.toLowerCase() === "x" ? parseInt(name.slice(2), 16) : Number(name.slice(1)));
|
|
43
|
+
return ENTITIES[name] ?? match;
|
|
44
|
+
});
|
|
45
|
+
/** HTML to the text a reader sees. */
|
|
46
|
+
const textOf = (html) => decodeHtml(html.replace(/<(script|style)\b[\s\S]*?<\/\1>/gi, " ").replace(/<\/?(span|a|em|strong|code|b|i|mark|kbd|sub|sup)\b[^>]*>/gi, "").replace(/<[^>]+>/g, " ").replace(/\s+/g, " ")).trim();
|
|
47
|
+
/** A slug function that suffixes repeats, like GitHub: `a`, `a-1`, `a-2`. */
|
|
48
|
+
const slugger = () => {
|
|
49
|
+
const seen = /* @__PURE__ */ new Map();
|
|
50
|
+
return (text) => {
|
|
51
|
+
const base = text.toLowerCase().replace(/[^\p{L}\p{N}\s-]/gu, "").trim().replace(/\s+/g, "-") || "section";
|
|
52
|
+
const n = seen.get(base) ?? 0;
|
|
53
|
+
seen.set(base, n + 1);
|
|
54
|
+
return n ? `${base}-${n}` : base;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
/** A fence's info string: ```ts [app/routes/index.ts] {2,4-5}``` → language, title, the rest. */
|
|
58
|
+
const parseInfo = (info) => {
|
|
59
|
+
const [lang = "", ...rest] = info.trim().split(/\s+/);
|
|
60
|
+
const meta = rest.join(" ");
|
|
61
|
+
const title = /\[([^\]]+)\]/.exec(meta)?.[1] ?? null;
|
|
62
|
+
return {
|
|
63
|
+
lang: lang.toLowerCase(),
|
|
64
|
+
title,
|
|
65
|
+
meta: meta.replace(/\[[^\]]*\]/, "").trim()
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
const THEMES = {
|
|
69
|
+
light: "github-light",
|
|
70
|
+
dark: "github-dark"
|
|
71
|
+
};
|
|
72
|
+
const ALIASES = {
|
|
73
|
+
sh: "bash",
|
|
74
|
+
shell: "bash",
|
|
75
|
+
zsh: "bash",
|
|
76
|
+
console: "bash"
|
|
77
|
+
};
|
|
78
|
+
const PLAIN = /* @__PURE__ */ new Set([
|
|
79
|
+
"",
|
|
80
|
+
"text",
|
|
81
|
+
"txt",
|
|
82
|
+
"plain",
|
|
83
|
+
"mermaid"
|
|
84
|
+
]);
|
|
85
|
+
let highlighter;
|
|
86
|
+
const getHighlighter = () => highlighter ??= createHighlighter({
|
|
87
|
+
themes: Object.values(THEMES),
|
|
88
|
+
langs: []
|
|
89
|
+
});
|
|
90
|
+
const known = (lang) => {
|
|
91
|
+
const name = ALIASES[lang] ?? lang;
|
|
92
|
+
return name in bundledLanguages ? name : null;
|
|
93
|
+
};
|
|
94
|
+
const highlight = (shiki, code, lang, meta) => {
|
|
95
|
+
return shiki.codeToHtml(code, {
|
|
96
|
+
lang: known(lang) ?? "text",
|
|
97
|
+
themes: THEMES,
|
|
98
|
+
defaultColor: "light",
|
|
99
|
+
meta: { __raw: meta },
|
|
100
|
+
transformers: [
|
|
101
|
+
transformerNotationDiff(),
|
|
102
|
+
transformerNotationHighlight(),
|
|
103
|
+
transformerNotationFocus(),
|
|
104
|
+
transformerNotationErrorLevel(),
|
|
105
|
+
transformerMetaHighlight()
|
|
106
|
+
]
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
const BLOCK = "<!--space:code-block-->";
|
|
110
|
+
const codeBlock = (lang, title, inner) => {
|
|
111
|
+
const label = title ?? lang;
|
|
112
|
+
const head = label ? `<div class="code-head"><span class="code-title">${escapeHtml(label)}</span>${title && lang ? `<span class="code-lang">${escapeHtml(lang)}</span>` : ""}</div>` : "";
|
|
113
|
+
return `${BLOCK}<div class="code-block" data-lang="${escapeHtml(lang)}"${title ? ` data-title="${escapeHtml(title)}"` : ""}>${head}${inner}</div>`;
|
|
114
|
+
};
|
|
115
|
+
/** `<code-group>` around fenced blocks: one tab per block, named by its `[title]` or language. */
|
|
116
|
+
const codeGroups = (html) => html.replace(/<code-group>([\s\S]*?)<\/code-group>/g, (_match, inner) => {
|
|
117
|
+
const blocks = inner.split(BLOCK).map((part) => part.trim()).filter((part) => part.startsWith("<div class=\"code-block\""));
|
|
118
|
+
if (blocks.length === 0) return inner;
|
|
119
|
+
return `<div class="code-group"><div class="code-tabs" role="tablist">${blocks.map((block) => decodeHtml(/data-title="([^"]*)"/.exec(block)?.[1] ?? "") || decodeHtml(/data-lang="([^"]*)"/.exec(block)?.[1] ?? "") || "code").map((label, i) => `<button type="button" role="tab" aria-selected="${i === 0}" data-label="${escapeHtml(label)}">${escapeHtml(label)}</button>`).join("")}</div>${blocks.map((block, i) => block.replace("<div class=\"code-block\"", `<div class="code-block" role="tabpanel"${i === 0 ? "" : " hidden"}`)).join("")}</div>`;
|
|
120
|
+
});
|
|
121
|
+
const ALERTS = {
|
|
122
|
+
NOTE: "Note",
|
|
123
|
+
TIP: "Tip",
|
|
124
|
+
IMPORTANT: "Important",
|
|
125
|
+
WARNING: "Warning",
|
|
126
|
+
CAUTION: "Caution"
|
|
127
|
+
};
|
|
128
|
+
/** GitHub's `> [!NOTE]` blockquotes, as callouts. `> [!TIP] Title` names one. */
|
|
129
|
+
const alerts = (html) => html.replace(/<blockquote>\s*<p>\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\][ \t]*([^\n<]*)(?:\n|(?=<\/p>))/g, (_match, kind, title) => `<blockquote class="callout callout-${kind.toLowerCase()}"><p class="callout-title">${title.trim() || ALERTS[kind]}</p><p>`).replace(/<p><\/p>/g, "");
|
|
130
|
+
/** Wraps tables so wide ones scroll on their own instead of the page. */
|
|
131
|
+
const wrapTables = (html) => html.replace(/<table>/g, "<div class=\"table-wrap\"><table>").replace(/<\/table>/g, "</table></div>");
|
|
132
|
+
const sectionsOf = (html) => {
|
|
133
|
+
const sections = [];
|
|
134
|
+
const heading = /<h([23]) id="([^"]+)">([\s\S]*?)<\/h\1>/g;
|
|
135
|
+
let last = 0;
|
|
136
|
+
let current = {
|
|
137
|
+
id: null,
|
|
138
|
+
heading: null
|
|
139
|
+
};
|
|
140
|
+
for (const match of html.matchAll(heading)) {
|
|
141
|
+
sections.push({
|
|
142
|
+
...current,
|
|
143
|
+
text: textOf(html.slice(last, match.index))
|
|
144
|
+
});
|
|
145
|
+
current = {
|
|
146
|
+
id: match[2],
|
|
147
|
+
heading: textOf(match[3].replace(/<a class="heading-anchor"[^>]*>#<\/a>/, ""))
|
|
148
|
+
};
|
|
149
|
+
last = match.index + match[0].length;
|
|
150
|
+
}
|
|
151
|
+
sections.push({
|
|
152
|
+
...current,
|
|
153
|
+
text: textOf(html.slice(last))
|
|
154
|
+
});
|
|
155
|
+
return sections.filter((section) => section.heading || section.text);
|
|
156
|
+
};
|
|
157
|
+
const renderMarkdown = async (source) => {
|
|
158
|
+
const { data, body } = frontmatter(source);
|
|
159
|
+
const shiki = await getHighlighter();
|
|
160
|
+
const slug = slugger();
|
|
161
|
+
const toc = [];
|
|
162
|
+
let title = data.title ?? null;
|
|
163
|
+
let mermaid = false;
|
|
164
|
+
const marked = new Marked({ gfm: true });
|
|
165
|
+
marked.use({ renderer: {
|
|
166
|
+
code({ text, lang }) {
|
|
167
|
+
const info = parseInfo(lang ?? "");
|
|
168
|
+
if (info.lang === "mermaid") {
|
|
169
|
+
mermaid = true;
|
|
170
|
+
return `<div class="mermaid-block"><pre class="mermaid">${escapeHtml(text)}</pre></div>`;
|
|
171
|
+
}
|
|
172
|
+
return codeBlock(info.lang, info.title, highlight(shiki, text, info.lang, info.meta));
|
|
173
|
+
},
|
|
174
|
+
heading({ tokens, depth }) {
|
|
175
|
+
const inner = this.parser.parseInline(tokens);
|
|
176
|
+
const text = textOf(inner);
|
|
177
|
+
if (depth === 1) {
|
|
178
|
+
title ??= text;
|
|
179
|
+
return `<h1>${inner}</h1>\n`;
|
|
180
|
+
}
|
|
181
|
+
const id = slug(text);
|
|
182
|
+
if (depth === 2 || depth === 3) toc.push({
|
|
183
|
+
depth,
|
|
184
|
+
text,
|
|
185
|
+
id
|
|
186
|
+
});
|
|
187
|
+
return `<h${depth} id="${id}"><a class="heading-anchor" href="#${id}" aria-hidden="true" tabindex="-1">#</a>${inner}</h${depth}>\n`;
|
|
188
|
+
}
|
|
189
|
+
} });
|
|
190
|
+
const langs = /* @__PURE__ */ new Set();
|
|
191
|
+
const tokens = marked.lexer(body);
|
|
192
|
+
marked.walkTokens(tokens, (token) => {
|
|
193
|
+
if (token.type !== "code") return;
|
|
194
|
+
const name = known(parseInfo(token.lang ?? "").lang);
|
|
195
|
+
if (name && !PLAIN.has(name)) langs.add(name);
|
|
196
|
+
});
|
|
197
|
+
const missing = [...langs].filter((lang) => !shiki.getLoadedLanguages().includes(lang));
|
|
198
|
+
if (missing.length > 0) await shiki.loadLanguage(...missing);
|
|
199
|
+
const html = wrapTables(alerts(codeGroups(marked.parser(tokens)))).replaceAll(BLOCK, "");
|
|
200
|
+
return {
|
|
201
|
+
html,
|
|
202
|
+
title,
|
|
203
|
+
toc,
|
|
204
|
+
sections: sectionsOf(html),
|
|
205
|
+
mermaid,
|
|
206
|
+
frontmatter: data
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
// src/tree.ts
|
|
210
|
+
const INDEX_NAMES = ["index.md", "README.md"];
|
|
211
|
+
const isIndex = (rel) => INDEX_NAMES.includes(rel.split("/").pop());
|
|
212
|
+
/** A number before a name, `2-pages.md`, orders it in the sidebar; URLs and labels leave it out. */
|
|
213
|
+
const ORDER_PREFIX = /^(\d+)-/;
|
|
214
|
+
/** A file or folder's name in URLs: `2-pages.md` → `pages.md`. */
|
|
215
|
+
const urlName = (name) => name.replace(ORDER_PREFIX, "");
|
|
216
|
+
/** A path in URLs: `3-guides/2-pages.md` → `guides/pages.md`. */
|
|
217
|
+
const urlPath = (rel) => rel.split("/").map(urlName).join("/");
|
|
218
|
+
/** `1-getting-started.md` → `Getting started`. */
|
|
219
|
+
const labelFromName = (name) => {
|
|
220
|
+
const words = urlName(name).replace(/\.md$/, "").replace(/[-_]+/g, " ").trim();
|
|
221
|
+
return words ? words[0].toUpperCase() + words.slice(1) : name;
|
|
222
|
+
};
|
|
223
|
+
const stripInline = (text) => text.replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1").replace(/[`*_]/g, "").trim();
|
|
224
|
+
/** The page's title: frontmatter `title`, else its first `# heading`, skipping code fences. */
|
|
225
|
+
const titleOf = (body) => {
|
|
226
|
+
let fence = false;
|
|
227
|
+
for (const line of body.split(/\r?\n/)) if (/^\s*(```|~~~)/.test(line)) fence = !fence;
|
|
228
|
+
else if (!fence) {
|
|
229
|
+
const match = /^#\s+(.*?)(?:\s+#+\s*)?$/.exec(line);
|
|
230
|
+
if (match) return stripInline(match[1]);
|
|
231
|
+
}
|
|
232
|
+
return null;
|
|
233
|
+
};
|
|
234
|
+
const prefixOrder = (name) => {
|
|
235
|
+
const match = ORDER_PREFIX.exec(name);
|
|
236
|
+
return match ? Number(match[1]) : Number.POSITIVE_INFINITY;
|
|
237
|
+
};
|
|
238
|
+
/**
|
|
239
|
+
* Index pages first, then `order` (frontmatter or a numeric prefix), so a
|
|
240
|
+
* numbered folder sits among the pages at its number; then pages before
|
|
241
|
+
* folders, then name.
|
|
242
|
+
*/
|
|
243
|
+
const byOrder = (a, b) => {
|
|
244
|
+
const ai = isIndex(a.rel) ? 0 : 1;
|
|
245
|
+
const bi = isIndex(b.rel) ? 0 : 1;
|
|
246
|
+
if (ai !== bi) return ai - bi;
|
|
247
|
+
if (a.order !== b.order) return a.order - b.order;
|
|
248
|
+
if (a.kind !== b.kind) return a.kind === "page" ? -1 : 1;
|
|
249
|
+
return a.rel.localeCompare(b.rel, void 0, { numeric: true });
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* Frontmatter `icon`: a name from tabler.io/icons, `rocket` or `heart-filled`; a
|
|
253
|
+
* `tabler-` or `ti-` before it is allowed. Anything else is no icon.
|
|
254
|
+
*/
|
|
255
|
+
const iconName = (value) => {
|
|
256
|
+
const name = value?.trim().toLowerCase().replace(/^(tabler|ti)-/, "");
|
|
257
|
+
return name && /^[a-z0-9]+(-[a-z0-9]+)*$/.test(name) ? name : null;
|
|
258
|
+
};
|
|
259
|
+
const readPage = async (path, rel, name) => {
|
|
260
|
+
const source = await readFile(path, "utf8").catch(() => "");
|
|
261
|
+
const { data, body } = frontmatter(source);
|
|
262
|
+
const order = Number(data.order);
|
|
263
|
+
return {
|
|
264
|
+
kind: "page",
|
|
265
|
+
rel,
|
|
266
|
+
title: data.title ?? titleOf(body) ?? labelFromName(name),
|
|
267
|
+
description: data.description ?? null,
|
|
268
|
+
order: Number.isFinite(order) ? order : prefixOrder(name),
|
|
269
|
+
icon: iconName(data.icon)
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
const walk = async (dir, prefix) => {
|
|
273
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);
|
|
274
|
+
return (await Promise.all(entries.filter((entry) => !entry.name.startsWith(".") && entry.name !== "node_modules").map(async (entry) => {
|
|
275
|
+
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
276
|
+
const path = join(dir, entry.name);
|
|
277
|
+
if (entry.isFile() && entry.name.endsWith(".md")) return readPage(path, rel, entry.name);
|
|
278
|
+
if (!entry.isDirectory()) return void 0;
|
|
279
|
+
const children = await walk(path, rel);
|
|
280
|
+
if (children.length === 0) return void 0;
|
|
281
|
+
const home = children[0].kind === "page" && isIndex(children[0].rel) ? children[0] : void 0;
|
|
282
|
+
return {
|
|
283
|
+
kind: "dir",
|
|
284
|
+
rel,
|
|
285
|
+
label: home?.kind === "page" ? home.title : labelFromName(entry.name),
|
|
286
|
+
order: home && Number.isFinite(home.order) ? home.order : prefixOrder(entry.name),
|
|
287
|
+
icon: home?.kind === "page" ? home.icon : null,
|
|
288
|
+
children
|
|
289
|
+
};
|
|
290
|
+
}))).filter((node) => node !== void 0).sort(byOrder);
|
|
291
|
+
};
|
|
292
|
+
const scanDocs = (dir) => walk(dir, "");
|
|
293
|
+
const flattenPages = (nodes) => nodes.flatMap((node) => node.kind === "page" ? [node] : flattenPages(node.children));
|
|
294
|
+
const findDir = (nodes, rel) => {
|
|
295
|
+
for (const node of nodes) {
|
|
296
|
+
if (node.kind !== "dir") continue;
|
|
297
|
+
if (node.rel === rel) return node;
|
|
298
|
+
if (rel.startsWith(`${node.rel}/`)) return findDir(node.children, rel);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
/** The top-level group a page is in, for search results and the eyebrow above its title. */
|
|
302
|
+
const groupOf = (nodes, rel) => {
|
|
303
|
+
for (const node of nodes) if (node.kind === "dir" && rel.startsWith(`${node.rel}/`)) return node.label;
|
|
304
|
+
return null;
|
|
305
|
+
};
|
|
306
|
+
/** The Mermaid build the browser loads: kept off disk, since it'd add 100+ MB to every app. */
|
|
307
|
+
const MERMAID_URL = "https://cdn.jsdelivr.net/npm/mermaid@12.3.0/dist/mermaid.esm.min.mjs";
|
|
308
|
+
/** Tabler's icons, by name, from a CDN like Mermaid: offline, the sidebar shows no icons. */
|
|
309
|
+
const TABLER_URL = "https://cdn.jsdelivr.net/npm/@tabler/icons@3.50.0/icons/";
|
|
310
|
+
/** A sidebar icon: the SVG as a mask, so it takes the link's color. `-filled` names the filled set. */
|
|
311
|
+
const tabler = (name) => {
|
|
312
|
+
if (!name) return "";
|
|
313
|
+
const filled = name.endsWith("-filled");
|
|
314
|
+
const file = `${filled ? "filled" : "outline"}/${filled ? name.slice(0, -7) : name}.svg`;
|
|
315
|
+
return `<span class="nav-icon" style="--icon:url(${TABLER_URL}${file})" aria-hidden="true"></span>`;
|
|
316
|
+
};
|
|
317
|
+
const icon = (body, extra = "") => `<svg class="icon${extra}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${body}</svg>`;
|
|
318
|
+
/**
|
|
319
|
+
* The reader's own icons: Tabler's outline icons (MIT), by the name in the comment, inlined
|
|
320
|
+
* so the top bar works offline, unlike the sidebar's icons from the CDN.
|
|
321
|
+
*/
|
|
322
|
+
const ICONS = {
|
|
323
|
+
search: icon("<path d=\"M3 10a7 7 0 1 0 14 0a7 7 0 1 0 -14 0\"/><path d=\"M21 21l-6 -6\"/>"),
|
|
324
|
+
sun: icon("<path d=\"M8 12a4 4 0 1 0 8 0a4 4 0 1 0 -8 0\"/><path d=\"M3 12h1m8 -9v1m8 8h1m-9 8v1m-6.4 -15.4l.7 .7m12.1 -.7l-.7 .7m0 11.4l.7 .7m-12.1 -.7l-.7 .7\"/>", " icon-sun"),
|
|
325
|
+
moon: icon("<path d=\"M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454l0 .008\"/>", " icon-moon"),
|
|
326
|
+
menu: icon("<path d=\"M4 6l16 0\"/><path d=\"M4 12l16 0\"/><path d=\"M4 18l16 0\"/>"),
|
|
327
|
+
list: icon("<path d=\"M9 6l11 0\"/><path d=\"M9 12l11 0\"/><path d=\"M9 18l11 0\"/><path d=\"M5 6l0 .01\"/><path d=\"M5 12l0 .01\"/><path d=\"M5 18l0 .01\"/>"),
|
|
328
|
+
folder: icon("<path d=\"M5 4h4l3 3h7a2 2 0 0 1 2 2v8a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2v-11a2 2 0 0 1 2 -2\"/>"),
|
|
329
|
+
file: icon("<path d=\"M14 3v4a1 1 0 0 0 1 1h4\"/><path d=\"M17 21h-10a2 2 0 0 1 -2 -2v-14a2 2 0 0 1 2 -2h7l5 5v11a2 2 0 0 1 -2 2\"/>")
|
|
330
|
+
};
|
|
331
|
+
const pageHref = (source, rel) => source.href + urlPath(rel).split("/").map(encodeURIComponent).join("/");
|
|
332
|
+
const dirHref = (source, rel) => `${pageHref(source, rel)}/`;
|
|
333
|
+
const active = (on) => on ? " class=\"active\" aria-current=\"page\"" : "";
|
|
334
|
+
/**
|
|
335
|
+
* A sidebar folder's `<details>` attributes: open when it holds the current page, and
|
|
336
|
+
* `data-nav` to remember it by, so `NAV_BOOT` and the reader's script keep the ones you
|
|
337
|
+
* opened or closed that way on the next page.
|
|
338
|
+
*/
|
|
339
|
+
const folderState = (source, rel, current) => {
|
|
340
|
+
return `${current.startsWith(`${rel}/`) ? " open data-current" : ""} data-nav="${escapeHtml(source.href + rel)}"`;
|
|
341
|
+
};
|
|
342
|
+
const navList = (source, nodes, current) => {
|
|
343
|
+
return `<ul class="nav-list">${nodes.map((node) => {
|
|
344
|
+
if (node.kind === "page") return `<li><a href="${pageHref(source, node.rel)}"${active(node.rel === current)}>${tabler(node.icon)}${escapeHtml(node.title)}</a></li>`;
|
|
345
|
+
return `<li><details${folderState(source, node.rel, current)}><summary><span>${tabler(node.icon)}${escapeHtml(node.label)}</span></summary>${navList(source, node.children, current)}</details></li>`;
|
|
346
|
+
}).join("")}</ul>`;
|
|
347
|
+
};
|
|
348
|
+
/** The tree in reading order: each run of loose pages as a list, each folder as a titled group. */
|
|
349
|
+
const sidebar = (input) => {
|
|
350
|
+
const { source, tree, rel } = input;
|
|
351
|
+
const parts = [];
|
|
352
|
+
let pages = [];
|
|
353
|
+
const flush = () => {
|
|
354
|
+
if (pages.length) parts.push(`<ul class="nav-top">${pages.map((page) => `<li><a href="${pageHref(source, page.rel)}"${active(page.rel === rel)}>${tabler(page.icon)}${escapeHtml(page.title)}</a></li>`).join("")}</ul>`);
|
|
355
|
+
pages = [];
|
|
356
|
+
};
|
|
357
|
+
for (const node of tree) {
|
|
358
|
+
if (node.kind === "page") {
|
|
359
|
+
pages.push(node);
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
flush();
|
|
363
|
+
parts.push(`<details class="nav-group"${folderState(source, node.rel, rel)}><summary class="nav-group-title">${tabler(node.icon)}${escapeHtml(node.label)}</summary>${navList(source, node.children, rel)}</details>`);
|
|
364
|
+
}
|
|
365
|
+
flush();
|
|
366
|
+
return `<aside class="sidebar" id="sidebar">${input.sources.length > 1 ? `<div class="nav-sources">${sourceTabs(input)}</div>` : ""}<nav aria-label="Docs">${parts.join("")}</nav><script>${NAV_BOOT}<\/script></aside>`;
|
|
367
|
+
};
|
|
368
|
+
const sourceTabs = ({ sources, source }) => sources.map((view) => `<a class="source-tab${view === source ? " active" : ""}" href="${view.href}">${escapeHtml(view.label)}${view.version ? `<span class="version">v${escapeHtml(view.version)}</span>` : ""}</a>`).join("");
|
|
369
|
+
const toc = (entries) => {
|
|
370
|
+
if (entries.length < 2) return "<aside class=\"toc\"></aside>";
|
|
371
|
+
const items = entries.map((entry) => `<li class="depth-${entry.depth}"><a href="#${entry.id}">${escapeHtml(entry.text)}</a></li>`).join("");
|
|
372
|
+
return `<aside class="toc"><p class="toc-title">${ICONS.list}On this page</p><ul>${items}</ul></aside>`;
|
|
373
|
+
};
|
|
374
|
+
const pager = ({ source, tree, rel }) => {
|
|
375
|
+
const pages = flattenPages(tree);
|
|
376
|
+
const i = pages.findIndex((page) => page.rel === rel);
|
|
377
|
+
if (i === -1) return "";
|
|
378
|
+
const link = (page, cls, label) => page ? `<a class="${cls}" href="${pageHref(source, page.rel)}"><small>${label}</small><strong>${escapeHtml(page.title)}</strong></a>` : "";
|
|
379
|
+
const prev = link(pages[i - 1], "prev", "← Previous");
|
|
380
|
+
const next = link(pages[i + 1], "next", "Next →");
|
|
381
|
+
return prev || next ? `<nav class="pager" aria-label="Pagination">${prev}${next}</nav>` : "";
|
|
382
|
+
};
|
|
383
|
+
/** A folder without an index page: a card per entry. */
|
|
384
|
+
const cards = (source, nodes) => {
|
|
385
|
+
return `<ul class="cards">${nodes.map((node) => {
|
|
386
|
+
const [href, title, meta, glyph] = node.kind === "dir" ? [
|
|
387
|
+
dirHref(source, node.rel),
|
|
388
|
+
node.label,
|
|
389
|
+
`${flattenPages(node.children).length} pages`,
|
|
390
|
+
ICONS.folder
|
|
391
|
+
] : [
|
|
392
|
+
pageHref(source, node.rel),
|
|
393
|
+
node.title,
|
|
394
|
+
node.description ?? "",
|
|
395
|
+
ICONS.file
|
|
396
|
+
];
|
|
397
|
+
return `<li><a class="card" href="${href}"><span class="card-title">${glyph}${escapeHtml(title)}</span>${meta ? `<span class="card-meta">${escapeHtml(meta)}</span>` : ""}</a></li>`;
|
|
398
|
+
}).join("")}</ul>`;
|
|
399
|
+
};
|
|
400
|
+
const NAV_BOOT = "(function(){try{var s=JSON.parse(localStorage.getItem('spacefn-docs-nav')||'{}');document.querySelectorAll('#sidebar details[data-nav]:not([data-current])').forEach(function(e){var o=s[e.dataset.nav];if(o!==undefined)e.open=o})}catch(e){}})()";
|
|
401
|
+
const THEME_BOOT = "(function(){try{var t=localStorage.getItem('spacefn-docs-theme');if(t==='dark'||(!t&&matchMedia('(prefers-color-scheme: dark)').matches))document.documentElement.classList.add('dark')}catch(e){}})()";
|
|
402
|
+
const renderPage = (input) => {
|
|
403
|
+
const { title, html, base, source, name } = input;
|
|
404
|
+
const group = groupOf(input.tree, input.rel);
|
|
405
|
+
const eyebrow = group ? `<div class="eyebrow">${escapeHtml(group)}</div>` : "";
|
|
406
|
+
const logoUrl = input.sources[0]?.logo ?? source.logo;
|
|
407
|
+
const logo = logoUrl ? `<img src="${escapeHtml(logoUrl)}" alt="" width="28" height="28">` : "";
|
|
408
|
+
const next = pager(input);
|
|
409
|
+
const foot = next ? `<footer class="page-foot">${next}</footer>` : "";
|
|
410
|
+
const pageTitle = [name, `${name} docs`].includes(title) ? `${name} docs` : `${title} · ${name} docs`;
|
|
411
|
+
const config = JSON.stringify({
|
|
412
|
+
base,
|
|
413
|
+
mermaid: input.mermaid ? MERMAID_URL : null
|
|
414
|
+
}).replace(/</g, "\\u003c");
|
|
415
|
+
return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escapeHtml(pageTitle)}</title><script>${THEME_BOOT}<\/script><link rel="stylesheet" href="${base}_/reader.css"></head><body>
|
|
416
|
+
<header class="topbar">
|
|
417
|
+
<button class="icon-btn menu-btn" type="button" aria-label="Toggle navigation" aria-controls="sidebar" aria-expanded="false">${ICONS.menu}</button>
|
|
418
|
+
<a class="brand" href="${input.sources[0]?.href ?? source.href}">${logo}<span>${escapeHtml(name)}</span><span class="brand-tag">docs</span></a>
|
|
419
|
+
<nav class="topbar-sources" aria-label="Doc sets">${input.sources.length > 1 ? sourceTabs(input) : ""}</nav>
|
|
420
|
+
<button class="topbar-search" type="button" aria-label="Search docs">${ICONS.search}<span>Search…</span><kbd>⌘K</kbd></button>
|
|
421
|
+
<button class="icon-btn theme-toggle" type="button" aria-label="Toggle dark mode">${ICONS.sun}${ICONS.moon}</button>
|
|
422
|
+
</header>
|
|
423
|
+
<div class="shell">${sidebar(input)}<main class="content">${eyebrow}<article class="prose">${html}</article>${foot}</main>${toc(input.toc)}</div>
|
|
424
|
+
<dialog class="search-dialog" aria-label="Search docs"><div class="search-field">${ICONS.search}<input type="search" placeholder="Search the docs" autocomplete="off" spellcheck="false"><kbd>esc</kbd></div><ul class="search-results" role="listbox"></ul></dialog>
|
|
425
|
+
<script type="application/json" id="docs-config">${config}<\/script><script type="module" src="${base}_/reader.js"><\/script></body></html>`;
|
|
426
|
+
};
|
|
427
|
+
// src/reader.ts
|
|
428
|
+
/**
|
|
429
|
+
* The file a URL path names, relative to `dir`: each segment is a name as it is,
|
|
430
|
+
* or one with its order number left out (`pages.md` finds `2-pages.md`).
|
|
431
|
+
*/
|
|
432
|
+
const locate = async (dir, path) => {
|
|
433
|
+
let rel = "";
|
|
434
|
+
for (const segment of path.split("/").filter(Boolean)) {
|
|
435
|
+
const names = await readdir(join(dir, rel)).catch(() => []);
|
|
436
|
+
const name = names.includes(segment) ? segment : names.find((candidate) => urlName(candidate) === segment);
|
|
437
|
+
if (name === void 0) return void 0;
|
|
438
|
+
rel = rel ? `${rel}/${name}` : name;
|
|
439
|
+
}
|
|
440
|
+
return rel;
|
|
441
|
+
};
|
|
442
|
+
const ASSET_DIR = fileURLToPath(new URL("../assets/", import.meta.url));
|
|
443
|
+
const TYPES = {
|
|
444
|
+
".svg": "image/svg+xml",
|
|
445
|
+
".png": "image/png",
|
|
446
|
+
".jpg": "image/jpeg",
|
|
447
|
+
".jpeg": "image/jpeg",
|
|
448
|
+
".gif": "image/gif",
|
|
449
|
+
".webp": "image/webp",
|
|
450
|
+
".avif": "image/avif",
|
|
451
|
+
".ico": "image/x-icon",
|
|
452
|
+
".mp4": "video/mp4",
|
|
453
|
+
".webm": "video/webm",
|
|
454
|
+
".pdf": "application/pdf",
|
|
455
|
+
".css": "text/css; charset=utf-8",
|
|
456
|
+
".js": "text/javascript; charset=utf-8",
|
|
457
|
+
".json": "application/json; charset=utf-8",
|
|
458
|
+
".txt": "text/plain; charset=utf-8"
|
|
459
|
+
};
|
|
460
|
+
const LOGOS = [
|
|
461
|
+
"logo.svg",
|
|
462
|
+
"logo.png",
|
|
463
|
+
"logo.webp"
|
|
464
|
+
];
|
|
465
|
+
const within = (dir, path) => path === dir || path.startsWith(dir + sep);
|
|
466
|
+
const exists = (path) => stat(path).then((s) => s, () => void 0);
|
|
467
|
+
const createReader = (options) => {
|
|
468
|
+
const base = options.base.endsWith("/") ? options.base : `${options.base}/`;
|
|
469
|
+
const sources = [...options.sources].map((source) => ({
|
|
470
|
+
...source,
|
|
471
|
+
dir: resolve(source.dir)
|
|
472
|
+
})).sort((a, b) => b.prefix.length - a.prefix.length);
|
|
473
|
+
const shown = options.sources.map((source) => sources.find((s) => s.prefix === source.prefix));
|
|
474
|
+
const cache = /* @__PURE__ */ new Map();
|
|
475
|
+
const clients = /* @__PURE__ */ new Set();
|
|
476
|
+
let watchers = [];
|
|
477
|
+
const render = async (path) => {
|
|
478
|
+
const { mtimeMs } = await stat(path);
|
|
479
|
+
const hit = cache.get(path);
|
|
480
|
+
if (hit?.mtime === mtimeMs) return hit.rendered;
|
|
481
|
+
const rendered = await renderMarkdown(await readFile(path, "utf8"));
|
|
482
|
+
cache.set(path, {
|
|
483
|
+
mtime: mtimeMs,
|
|
484
|
+
rendered
|
|
485
|
+
});
|
|
486
|
+
return rendered;
|
|
487
|
+
};
|
|
488
|
+
const views = /* @__PURE__ */ new Map();
|
|
489
|
+
const viewOf = async (source) => {
|
|
490
|
+
const known = views.get(source);
|
|
491
|
+
if (known) return known;
|
|
492
|
+
const href = base + source.prefix;
|
|
493
|
+
let logo = null;
|
|
494
|
+
for (const name of LOGOS) if (await exists(join(source.dir, name))) logo ??= href + name;
|
|
495
|
+
const view = {
|
|
496
|
+
label: source.label,
|
|
497
|
+
href,
|
|
498
|
+
version: source.version ?? null,
|
|
499
|
+
logo
|
|
500
|
+
};
|
|
501
|
+
views.set(source, view);
|
|
502
|
+
return view;
|
|
503
|
+
};
|
|
504
|
+
const send = (res, status, type, body) => {
|
|
505
|
+
res.statusCode = status;
|
|
506
|
+
res.setHeader("Content-Type", type);
|
|
507
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
508
|
+
res.end(body);
|
|
509
|
+
};
|
|
510
|
+
const redirect = (res, location) => {
|
|
511
|
+
res.statusCode = 302;
|
|
512
|
+
res.setHeader("Location", location);
|
|
513
|
+
res.end();
|
|
514
|
+
};
|
|
515
|
+
const searchIndex = async () => {
|
|
516
|
+
return (await Promise.all(sources.map(async (source) => {
|
|
517
|
+
const view = await viewOf(source);
|
|
518
|
+
const tree = await scanDocs(source.dir);
|
|
519
|
+
return Promise.all(flattenPages(tree).map(async (page) => {
|
|
520
|
+
const rendered = await render(join(source.dir, page.rel)).catch(() => void 0);
|
|
521
|
+
return {
|
|
522
|
+
source: source.label,
|
|
523
|
+
title: page.title,
|
|
524
|
+
href: pageHref(view, page.rel),
|
|
525
|
+
group: groupOf(tree, page.rel),
|
|
526
|
+
sections: (rendered?.sections ?? []).map((section) => ({
|
|
527
|
+
...section,
|
|
528
|
+
text: section.text.slice(0, 4e3)
|
|
529
|
+
}))
|
|
530
|
+
};
|
|
531
|
+
}));
|
|
532
|
+
}))).flat();
|
|
533
|
+
};
|
|
534
|
+
const events = (req, res) => {
|
|
535
|
+
res.writeHead(200, {
|
|
536
|
+
"Content-Type": "text/event-stream",
|
|
537
|
+
"Cache-Control": "no-cache",
|
|
538
|
+
Connection: "keep-alive"
|
|
539
|
+
});
|
|
540
|
+
res.write(": connected\n\n");
|
|
541
|
+
clients.add(res);
|
|
542
|
+
req.once("close", () => clients.delete(res));
|
|
543
|
+
if (watchers.length > 0) return;
|
|
544
|
+
let timer;
|
|
545
|
+
watchers = sources.flatMap((source) => {
|
|
546
|
+
try {
|
|
547
|
+
return [watch(source.dir, { recursive: true }, () => {
|
|
548
|
+
clearTimeout(timer);
|
|
549
|
+
timer = setTimeout(() => {
|
|
550
|
+
for (const client of clients) client.write("data: reload\n\n");
|
|
551
|
+
}, 80);
|
|
552
|
+
})];
|
|
553
|
+
} catch {
|
|
554
|
+
return [];
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
};
|
|
558
|
+
const page = async (res, source, rel, content, status = 200) => {
|
|
559
|
+
const view = await viewOf(source);
|
|
560
|
+
const tree = await scanDocs(source.dir);
|
|
561
|
+
send(res, status, "text/html; charset=utf-8", renderPage({
|
|
562
|
+
title: content.title,
|
|
563
|
+
html: content.html,
|
|
564
|
+
toc: content.rendered?.toc ?? [],
|
|
565
|
+
mermaid: content.rendered?.mermaid ?? false,
|
|
566
|
+
base,
|
|
567
|
+
sources: await Promise.all(shown.map(viewOf)),
|
|
568
|
+
source: view,
|
|
569
|
+
tree,
|
|
570
|
+
rel,
|
|
571
|
+
name: options.name
|
|
572
|
+
}));
|
|
573
|
+
};
|
|
574
|
+
const notFound = async (res, source, rel) => {
|
|
575
|
+
if (!source) return send(res, 404, "text/plain", "not found");
|
|
576
|
+
const view = await viewOf(source);
|
|
577
|
+
await page(res, source, rel, {
|
|
578
|
+
title: "Not found",
|
|
579
|
+
html: `<h1>Not found</h1><p>There's no <code>${escapeHtml(rel)}</code> in ${escapeHtml(source.label)}'s docs. Go to <a href="${view.href}">the start</a>, or search.</p>`
|
|
580
|
+
}, 404);
|
|
581
|
+
};
|
|
582
|
+
const handle = async (req, res, next) => {
|
|
583
|
+
const full = req.originalUrl ?? req.url;
|
|
584
|
+
const url = new URL(full ?? "/", "http://localhost");
|
|
585
|
+
let path;
|
|
586
|
+
try {
|
|
587
|
+
path = decodeURIComponent(url.pathname);
|
|
588
|
+
} catch {
|
|
589
|
+
return send(res, 400, "text/plain", "bad request");
|
|
590
|
+
}
|
|
591
|
+
if (`${path}/` === base) return redirect(res, base);
|
|
592
|
+
if (!path.startsWith(base)) return next ? next() : send(res, 404, "text/plain", "not found");
|
|
593
|
+
const rest = path.slice(base.length);
|
|
594
|
+
if (rest.startsWith("_/")) {
|
|
595
|
+
const name = rest.slice(2);
|
|
596
|
+
if (name === "search.json") return send(res, 200, TYPES[".json"], JSON.stringify(await searchIndex()));
|
|
597
|
+
if (name === "events") return events(req, res);
|
|
598
|
+
if (name === "reader.css" || name === "reader.js") return send(res, 200, TYPES[extname(name)], await readFile(join(ASSET_DIR, name)));
|
|
599
|
+
return send(res, 404, "text/plain", "not found");
|
|
600
|
+
}
|
|
601
|
+
const source = sources.find((s) => rest.startsWith(s.prefix) || `${rest}/` === s.prefix);
|
|
602
|
+
if (!source) return redirect(res, base + (shown[0]?.prefix ?? ""));
|
|
603
|
+
if (`${rest}/` === source.prefix) return redirect(res, base + source.prefix);
|
|
604
|
+
const asked = rest.slice(source.prefix.length).replace(/\/+$/, "");
|
|
605
|
+
if (!within(source.dir, resolve(source.dir, asked))) return send(res, 403, "text/plain", "forbidden");
|
|
606
|
+
const rel = await locate(source.dir, asked) ?? asked;
|
|
607
|
+
const target = resolve(source.dir, rel);
|
|
608
|
+
const found = await exists(target);
|
|
609
|
+
if (found && urlPath(rel) !== asked) return redirect(res, pageHref(await viewOf(source), rel) + (found.isDirectory() ? "/" : "") + url.search);
|
|
610
|
+
if (!found) {
|
|
611
|
+
if (rel === "" && source !== shown.at(-1)) return redirect(res, base + shown.at(-1).prefix);
|
|
612
|
+
return notFound(res, source, rel);
|
|
613
|
+
}
|
|
614
|
+
if (found.isDirectory()) {
|
|
615
|
+
if (rel && !rest.endsWith("/")) return redirect(res, `${pageHref(await viewOf(source), rel)}/`);
|
|
616
|
+
for (const name of INDEX_NAMES) {
|
|
617
|
+
const file = join(target, name);
|
|
618
|
+
if (!await exists(file)) continue;
|
|
619
|
+
const rendered = await render(file);
|
|
620
|
+
return page(res, source, rel ? `${rel}/${name}` : name, {
|
|
621
|
+
title: rendered.title ?? labelFromName(rel.split("/").pop() || source.label),
|
|
622
|
+
html: rendered.html,
|
|
623
|
+
rendered
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
const tree = await scanDocs(source.dir);
|
|
627
|
+
const dir = rel ? findDir(tree, rel) : void 0;
|
|
628
|
+
const label = dir?.label ?? source.label;
|
|
629
|
+
return page(res, source, rel, {
|
|
630
|
+
title: label,
|
|
631
|
+
html: `<h1>${escapeHtml(label)}</h1>${cards(await viewOf(source), dir?.children ?? tree)}`
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
if (target.endsWith(".md")) {
|
|
635
|
+
const rendered = await render(target);
|
|
636
|
+
return page(res, source, rel, {
|
|
637
|
+
title: rendered.title ?? labelFromName(rel.split("/").pop()),
|
|
638
|
+
html: rendered.html,
|
|
639
|
+
rendered
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
const type = TYPES[extname(target).toLowerCase()];
|
|
643
|
+
if (type) return send(res, 200, type, await readFile(target));
|
|
644
|
+
return notFound(res, source, rel);
|
|
645
|
+
};
|
|
646
|
+
return {
|
|
647
|
+
handle: (req, res, next) => handle(req, res, next).catch((error) => {
|
|
648
|
+
if (!res.headersSent) send(res, 500, "text/plain; charset=utf-8", `space docs: ${error instanceof Error ? error.stack : String(error)}`);
|
|
649
|
+
}),
|
|
650
|
+
close: () => {
|
|
651
|
+
for (const watcher of watchers) watcher.close();
|
|
652
|
+
for (const client of clients) client.end();
|
|
653
|
+
watchers = [];
|
|
654
|
+
clients.clear();
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
};
|
|
658
|
+
// src/index.ts
|
|
659
|
+
/** spacefn's own docs, as installed. */
|
|
660
|
+
const contentDir = fileURLToPath(new URL("../content/", import.meta.url));
|
|
661
|
+
const manifest = (dir) => {
|
|
662
|
+
try {
|
|
663
|
+
return JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
|
|
664
|
+
} catch {
|
|
665
|
+
return {};
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
/** The installed version, which matches the spacefn it documents. */
|
|
669
|
+
const version = manifest(fileURLToPath(new URL("..", import.meta.url))).version ?? "0.0.0";
|
|
670
|
+
/** spacefn's docs. */
|
|
671
|
+
const spacefnSource = (prefix) => ({
|
|
672
|
+
label: "spacefn",
|
|
673
|
+
dir: contentDir,
|
|
674
|
+
prefix,
|
|
675
|
+
version
|
|
676
|
+
});
|
|
677
|
+
/** The reader for an app: its own `docs/` (if any) and spacefn's docs. */
|
|
678
|
+
const appDocs = ({ root, base }) => {
|
|
679
|
+
const docs = join(root, "docs");
|
|
680
|
+
const name = manifest(root).name ?? "app";
|
|
681
|
+
const own = existsSync(docs);
|
|
682
|
+
return createReader({
|
|
683
|
+
base,
|
|
684
|
+
name: own ? name : "spacefn",
|
|
685
|
+
sources: own ? [{
|
|
686
|
+
label: name,
|
|
687
|
+
dir: docs,
|
|
688
|
+
prefix: ""
|
|
689
|
+
}, spacefnSource("spacefn/")] : [spacefnSource("")]
|
|
690
|
+
});
|
|
691
|
+
};
|
|
692
|
+
/** `space docs`: the app's reader on its own server. */
|
|
693
|
+
const serve = async ({ root, port = 4321, host = "localhost" }) => {
|
|
694
|
+
const reader = appDocs({
|
|
695
|
+
root,
|
|
696
|
+
base: "/"
|
|
697
|
+
});
|
|
698
|
+
const server = createServer((req, res) => void reader.handle(req, res));
|
|
699
|
+
await new Promise((done, fail) => {
|
|
700
|
+
server.once("error", fail);
|
|
701
|
+
server.listen(port, host, done);
|
|
702
|
+
});
|
|
703
|
+
const address = server.address();
|
|
704
|
+
return {
|
|
705
|
+
url: `http://${host}:${typeof address === "object" && address ? address.port : port}/`,
|
|
706
|
+
close: () => new Promise((done) => {
|
|
707
|
+
reader.close();
|
|
708
|
+
server.closeAllConnections();
|
|
709
|
+
server.close(() => done());
|
|
710
|
+
})
|
|
711
|
+
};
|
|
712
|
+
};
|
|
713
|
+
export { appDocs, contentDir, createReader, renderMarkdown, serve, spacefnSource, version };
|