Skip to content

Commit

Permalink
compute all urls before the permalink generation
Browse files Browse the repository at this point in the history
  • Loading branch information
vanyauhalin committed Dec 9, 2024
1 parent d41d22b commit 6d6aa62
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 61 deletions.
5 changes: 0 additions & 5 deletions site/generations/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {type MenubarData, MenubarDatum} from "../internal/menubar.tsx"
import {type PageData, PageDatum} from "../internal/page.tsx"
import {type PartData, PartDatum} from "../internal/part.tsx"
import {type ServiceData, ServiceDatum} from "../internal/service.tsx"
import {writeUrl} from "../internal/url.ts"

declare module "@onlyoffice/eleventy-types" {
interface Data {
Expand Down Expand Up @@ -59,10 +58,6 @@ declare module "@onlyoffice/eleventy-types" {

export function data(): Data {
return {
permalink(d) {
return writeUrl(d)
},

layout: "article",

eleventyComputed: {
Expand Down
79 changes: 33 additions & 46 deletions site/internal/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {slug} from "github-slugger"

declare module "@onlyoffice/eleventy-types" {
interface Data {
isWritten?: boolean
doWrite?(this: void, data: Data): boolean | undefined
}
}
Expand All @@ -16,6 +17,8 @@ declare module "@onlyoffice/eleventy-types" {
canonicalUrl?: string
sitemapUrl?: string
virtualUrl?: string
specificUrl?: string
writeUrl?: string
}
}

Expand All @@ -31,26 +34,41 @@ export function eleventyUrl(uc: UserConfig): void {
await rmTempDir(c.dir.output)
})

uc.addGlobalData("eleventyComputed", {
canonicalUrl,
sitemapUrl,
virtualUrl,
uc.addGlobalData("permalink", () => {
return permalink
})
}

export function isWritten(d: Data): boolean {
const p = d.page
if (!p) {
throw new Error("The page data is missing")
function permalink(d: Data): string {
if (d.writeUrl) {
return d.writeUrl
}
return !isTempPath(p.outputPath)
}

export function writeUrl(d: Data): string {
if (doWrite(d)) {
return specificUrl(d)
const sp = computePath(d, d.specificPath)
const su = slugify(sp)

const vp = computePath(d, d.virtualPath)
const vu = slugify(vp)

const [mu] = cutSuffix(vu, "index.html")
const [cu] = cutSuffix(su, "index.html")

d.isWritten = doWrite(d)

if (d.isWritten) {
d.writeUrl = su
} else {
const tp = computePath(d, tempPath)
const tu = slugify(tp)
d.writeUrl = tu
}
return tempUrl()

d.specificUrl = su
d.virtualUrl = vu
d.sitemapUrl = mu
d.canonicalUrl = cu

return d.writeUrl
}

function doWrite(d: Data): boolean {
Expand All @@ -63,11 +81,6 @@ function doWrite(d: Data): boolean {
return true
}

function tempUrl(): string {
const p = tempPath()
return slugify(p)
}

async function rmTempDir(r: string): Promise<void> {
const d = both.join(r, "temp")
await rm(d, {recursive: true})
Expand All @@ -78,33 +91,7 @@ let tempCount = 0
function tempPath(): string {
const i = tempCount
tempCount += 1
return `temp/${i}.txt`
}

function isTempPath(u: string): boolean {
return /temp\/\d+\.txt$/.test(u)
}

function canonicalUrl(d: Data): string {
let u = specificUrl(d)
;[u] = cutSuffix(u, "index.html")
return u
}

function sitemapUrl(d: Data): string {
let u = virtualUrl(d)
;[u] = cutSuffix(u, "index.html")
return u
}

function virtualUrl(d: Data): string {
const p = computePath(d, d.virtualPath)
return slugify(p)
}

function specificUrl(d: Data): string {
const p = computePath(d, d.specificPath)
return slugify(p)
return `/temp/${i}.txt`
}

function slugify(s: string): string {
Expand Down
3 changes: 1 addition & 2 deletions site/layouts/article.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {type Context, type Data} from "@onlyoffice/eleventy-types"
import {type JSX, h} from "preact"
import {Article} from "../internal/article.tsx"
import {isWritten} from "../internal/url.ts"

export function data(): Data {
return {
Expand All @@ -10,7 +9,7 @@ export function data(): Data {
}

export function render(c: Context): JSX.Element {
if (!isWritten(c)) {
if (!c.isWritten) {
return <div>This page should not be rendered</div>
}

Expand Down
3 changes: 1 addition & 2 deletions site/layouts/chapter.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {type Context, type Data} from "@onlyoffice/eleventy-types"
import {type JSX, h} from "preact"
import {Chapter} from "../internal/chapter.tsx"
import {isWritten} from "../internal/url.ts"

export function data(): Data {
return {
Expand All @@ -13,7 +12,7 @@ export function data(): Data {
}

export function render(c: Context): JSX.Element {
if (!isWritten(c)) {
if (!c.isWritten) {
return <div>This page should not be rendered</div>
}

Expand Down
3 changes: 1 addition & 2 deletions site/layouts/html.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import {type Context} from "@onlyoffice/eleventy-types"
import {h} from "preact"
import {Html} from "../internal/html.tsx"
import {renderToString} from "../internal/preact.ts"
import {isWritten} from "../internal/url.ts"

export async function render(c: Context): Promise<string> {
if (!isWritten(c)) {
if (!c.isWritten) {
return "This page should not be rendered"
}
const e = <Html
Expand Down
3 changes: 1 addition & 2 deletions site/layouts/library.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {type Context, type Data} from "@onlyoffice/eleventy-types"
import {type JSX, h} from "preact"
import {Library} from "@/internal/library.tsx"
import {isWritten} from "../internal/url.ts"

export function data(): Data {
return {
Expand All @@ -10,7 +9,7 @@ export function data(): Data {
}

export function render(c: Context): JSX.Element {
if (!isWritten(c)) {
if (!c.isWritten) {
return <div>This page should not be rendered</div>
}

Expand Down
3 changes: 1 addition & 2 deletions site/layouts/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {type Context, type Data} from "@onlyoffice/eleventy-types"
import {type JSX, h} from "preact"
import {Page} from "../internal/page.tsx"
import {isWritten} from "../internal/url.ts"

export function data(): Data {
return {
Expand All @@ -10,7 +9,7 @@ export function data(): Data {
}

export function render(c: Context): JSX.Element {
if (!isWritten(c)) {
if (!c.isWritten) {
return <div>This page should not be rendered</div>
}

Expand Down

0 comments on commit 6d6aa62

Please sign in to comment.