Skip to content

Commit

Permalink
temp: site lib gen + lay
Browse files Browse the repository at this point in the history
  • Loading branch information
vanyauhalin committed Dec 4, 2024
1 parent 93dbd8c commit 53039ce
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 0 deletions.
233 changes: 233 additions & 0 deletions site/generations/library-next.ts
Original file line number Diff line number Diff line change
@@ -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<string, SitemapData> = {}

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<string, number>()

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<string, number>()

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
}
}
15 changes: 15 additions & 0 deletions site/internal/library.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Site.NextLibrary.Library
entity={d.declaration}
onLink={d.onLink}
onRetrieve={d.onRetrieve}
>
<Site.NextLibrary.LibraryDescription>
{Markdown}
</Site.NextLibrary.LibraryDescription>
<Site.NextLibrary.LibrarySyntaxHighlight>
{SyntaxHighlight}
</Site.NextLibrary.LibrarySyntaxHighlight>
</Site.NextLibrary.Library>
}

return <Site.Library {...d}>
<Site.LibraryHeading for="Constructors">
<h2>Constructors</h2>
Expand Down

0 comments on commit 53039ce

Please sign in to comment.