-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93dbd8c
commit 53039ce
Showing
2 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters