From 53039cefa6f2f2296a8669b496ace8b9a12b05b0 Mon Sep 17 00:00:00 2001 From: vanyauhalin Date: Tue, 3 Dec 2024 18:56:56 +0400 Subject: [PATCH] temp: site lib gen + lay --- site/generations/library-next.ts | 233 +++++++++++++++++++++++++++++++ site/internal/library.tsx | 15 ++ 2 files changed, 248 insertions(+) create mode 100644 site/generations/library-next.ts diff --git a/site/generations/library-next.ts b/site/generations/library-next.ts new file mode 100644 index 000000000..cc37a2862 --- /dev/null +++ b/site/generations/library-next.ts @@ -0,0 +1,233 @@ +import {type SitemapData, SitemapDatum} from "@onlyoffice/eleventy-sitemap" +import {type Data} from "@onlyoffice/eleventy-types" +import {type Entity} from "@onlyoffice/library-declaration/next.ts" +import {cutSuffix} from "@onlyoffice/strings" +import {LibraryDatum} from "../internal/library.tsx" +import {Sitemap} from "../internal/sitemap.ts" +import {type Resource} from "../resources/docspace-plugin-sdk.ts" + +type R = typeof Resource["shared"] + +export function data(r: R): Data { + const sp = new SpecificPather() + const vp = new VirtualPather() + + const sr: Record = {} + + return { + layout: "library", + + items: r.list(), + + pagination: { + data: "items", + size: 1, + addAllPagesToCollections: true, + }, + + doWrite(data) { + if (!data.pagination || !data.pagination.items) { + throw new Error("No pagination") + } + + const [e]: Entity[] = data.pagination.items + + if (e.type === "group") { + return false + } + + if (e.type === "declaration") { + return true + } + + // @ts-expect-error + throw new Error(`Unknown entity type: ${e.type}`) + }, + + virtualPath(data) { + if (!data.pagination || !data.pagination.items) { + throw new Error("No pagination") + } + const [e]: Entity[] = data.pagination.items + const p = vp.path(r, e) + return `${p}/index.html` + }, + + specificPath(data) { + if (!data.pagination || !data.pagination.items) { + throw new Error("No pagination") + } + const [e]: Entity[] = data.pagination.items + const p = sp.path(r, e) + return `${p}/index.html` + }, + + eleventyComputed: { + title(data) { + if (!data.pagination || !data.pagination.items) { + throw new Error("No pagination") + } + + const [e]: Entity[] = data.pagination.items + + if (e.type === "group") { + return e.group.name + } + + if (e.type === "declaration") { + return e.declaration.name + } + + // @ts-expect-error + throw new Error(`Unknown entity type: ${e.type}`) + }, + + sitemap(data) { + if (!data.pagination || !data.pagination.items) { + throw new Error("No pagination") + } + + const a = data.defaultSitemap + + if (!a) { + return + } + + const b = new SitemapDatum() + + const [e]: Entity[] = data.pagination.items + + if (e.type === "group") { + b.type = "group" + } else if (e.type === "declaration") { + b.type = "page" + } else { + // @ts-expect-error + throw new Error(`Unknown entity type: ${e.type}`) + } + + const c = SitemapDatum.merge(a, b) + + sr[e.id] = c + + return c + }, + + library(data) { + if (!data.pagination || !data.pagination.items) { + throw new Error("No pagination") + } + + const [e]: Entity[] = data.pagination.items + + const d = new LibraryDatum() + d.declaration = e + + d.onLink = function onLink(t) { + const s = Sitemap.shared + + const e = r.retrieve(t.id) + if (!e) { + return "" + } + + const m = sr[e.id] + if (!m || !m.url) { + return "" + } + + const p = s.findPageByUrl(m.url) + return p.canonicalUrl + } + + d.onRetrieve = function onRetrieve(id) { + return r.retrieve(id) + } + + return d + }, + }, + } +} + +class VirtualPather { + #m = new Map() + + path(r: R, e: Entity): string { + let s = "" + let i = 0 + + let c: Entity | undefined = e + + while (c) { + if (c.type === "group") { + s = `${c.group.name}/${s}` + } + + if (c.type === "declaration") { + s = `${c.declaration.name}/${s}` + } + + c = r.retrieve(c.parentId) + } + + [s] = cutSuffix(s, "/") + + while (true) { + const id = this.#m.get(s) + + if (!id) { + this.#m.set(s, e.id) + break + } + + if (id === e.id) { + break + } + + i += 1 + s = `${s}-${i}` + } + + return s + } +} + +class SpecificPather { + #m = new Map() + + path(r: R, e: Entity): string { + let s = "" + let i = 0 + + let c: Entity | undefined = e + + while (c) { + if (c.type === "declaration") { + s = `${c.declaration.name}/${s}` + } + + c = r.retrieve(c.parentId) + } + + [s] = cutSuffix(s, "/") + + while (true) { + const id = this.#m.get(s) + + if (!id) { + this.#m.set(s, e.id) + break + } + + if (id === e.id) { + break + } + + i += 1 + s = `${s}-${i}` + } + + return s + } +} diff --git a/site/internal/library.tsx b/site/internal/library.tsx index 522a297e5..03c696607 100644 --- a/site/internal/library.tsx +++ b/site/internal/library.tsx @@ -63,6 +63,21 @@ export function Library(p: LibraryProperties): JSX.Element { const e = s.findPageByUrl(p.sitemapUrl) const d = e.library + if ("parentId" in d.declaration) { + return + + {Markdown} + + + {SyntaxHighlight} + + + } + return

Constructors