From a6daf45c2caafa58326143178d37905aecfdef0f Mon Sep 17 00:00:00 2001 From: Erik Paulson Date: Wed, 17 May 2023 20:05:26 -0500 Subject: [PATCH 1/3] support an option to add .html suffixes to the generated sites --- packages/cli/src/commands/make-site.tsx | 14 +++++++++++--- packages/cli/src/model/project.ts | 4 +++- packages/explorer-views/src/context.ts | 1 + packages/explorer-worker/src/main.tsx | 2 ++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/commands/make-site.tsx b/packages/cli/src/commands/make-site.tsx index 87ec67a..32914c1 100644 --- a/packages/cli/src/commands/make-site.tsx +++ b/packages/cli/src/commands/make-site.tsx @@ -62,11 +62,12 @@ class Website implements RenderContext { readonly outputs: Record = {}; readonly rootClasses: ReadonlySet | null; - constructor(readonly title: string, readonly baseURL: string, rootClasses?: Iterable) { + constructor(readonly title: string, readonly baseURL: string, readonly appendHtmlSuffix: boolean, rootClasses?: Iterable) { this.graph = Graph.from(this.dataset); this.schema = Schema.decompile(this.dataset, this.graph); this.prefixes = new PrefixTable(this.namespaces); this.rootClasses = rootClasses ? new Set(rootClasses) : null; + this.appendHtmlSuffix = appendHtmlSuffix; } getPrefixes(): ReadonlyArray<[string, string]> { @@ -79,7 +80,13 @@ class Website implements RenderContext { rewriteHrefAsData(iri: string): string | undefined { const result: string | undefined = this.outputs[iri]; - return result ? resolveHref(result, this.baseURL) : undefined; + if(result) { + let resolved = resolveHref(result, this.baseURL) + if(this.appendHtmlSuffix) { + resolved = resolved + ".html" + } + return resolved + } else return undefined } beforecompile(): void { @@ -221,7 +228,8 @@ export default function main(options: Options): void { const project = new Project(options.project); const icons = project.json.siteOptions?.icons || []; const assets = project.json.siteOptions?.assets || {}; - const context = new Website(project.json.siteOptions?.title || DEFAULT_TITLE, new URL(options.base || project.json.siteOptions?.baseURL || DEFAULT_BASE, DEFAULT_BASE).href, project.json.siteOptions?.roots); + + const context = new Website(project.json.siteOptions?.title || DEFAULT_TITLE, new URL(options.base || project.json.siteOptions?.baseURL || DEFAULT_BASE, DEFAULT_BASE).href, (project.json.siteOptions?.appendHtmlSuffix || false), project.json.siteOptions?.roots); const site = new Workspace(project.package.resolve(options.output || project.json.siteOptions?.outDir || "public")); context.beforecompile(); diff --git a/packages/cli/src/model/project.ts b/packages/cli/src/model/project.ts index d4c2328..47304ac 100644 --- a/packages/cli/src/model/project.ts +++ b/packages/cli/src/model/project.ts @@ -31,6 +31,7 @@ export interface SiteConfig { baseURL?: string; outDir?: string; roots?: Array; + appendHtmlSuffix?: boolean; } export namespace SiteConfig { @@ -43,7 +44,8 @@ export namespace SiteConfig { && (Is.undefined(candidate.assets) || Is.objectLiteral(candidate.assets)) && (Is.undefined(candidate.baseURL) || Is.string(candidate.baseURL)) && (Is.undefined(candidate.outDir) || Is.string(candidate.outDir)) - && (Is.undefined(candidate.roots) || Is.typedArray(candidate.roots, Is.string)); + && (Is.undefined(candidate.roots) || Is.typedArray(candidate.roots, Is.string)) + && (Is.undefined(candidate.appendHtmlSuffix) || Is.boolean(candidate.appendHtmlSuffix)); } } diff --git a/packages/explorer-views/src/context.ts b/packages/explorer-views/src/context.ts index b9d46b9..7a27af5 100644 --- a/packages/explorer-views/src/context.ts +++ b/packages/explorer-views/src/context.ts @@ -7,6 +7,7 @@ export interface RenderContext { readonly graph: Graph; readonly schema: Schema; readonly rootClasses: ReadonlySet | null; + readonly appendHtmlSuffix: boolean; getPrefixes(): ReadonlyArray<[string, string]>; lookupPrefixedName(iri: string): { readonly prefixLabel: string, readonly localName: string } | null; diff --git a/packages/explorer-worker/src/main.tsx b/packages/explorer-worker/src/main.tsx index 5742135..9b0d5dc 100644 --- a/packages/explorer-worker/src/main.tsx +++ b/packages/explorer-worker/src/main.tsx @@ -64,12 +64,14 @@ class BackendImpl implements Backend, RenderContext { prefixes: PrefixTable; readonly rootClasses: ReadonlySet | null; + readonly appendHtmlSuffix = false; constructor(private readonly frontend: Frontend) { this.graph = Graph.from(this.dataset); this.schema = Schema.decompile(this.dataset, this.graph); this.prefixes = new PrefixTable(this.namespaces); this.rootClasses = null; + this.appendHtmlSuffix = false; } getPrefixes(): ReadonlyArray<[string, string]> { From 4d37e0b1fcc3102c16a38ab961a977bfb423e665 Mon Sep 17 00:00:00 2001 From: ektrah Date: Thu, 18 May 2023 08:15:02 +0200 Subject: [PATCH 2/3] Simplify option to add .html suffixes to generated sites --- packages/cli/src/commands/make-site.tsx | 17 +++++------------ packages/explorer-views/src/context.ts | 1 - packages/explorer-worker/src/main.tsx | 2 -- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/packages/cli/src/commands/make-site.tsx b/packages/cli/src/commands/make-site.tsx index 32914c1..ca0ceed 100644 --- a/packages/cli/src/commands/make-site.tsx +++ b/packages/cli/src/commands/make-site.tsx @@ -67,7 +67,6 @@ class Website implements RenderContext { this.schema = Schema.decompile(this.dataset, this.graph); this.prefixes = new PrefixTable(this.namespaces); this.rootClasses = rootClasses ? new Set(rootClasses) : null; - this.appendHtmlSuffix = appendHtmlSuffix; } getPrefixes(): ReadonlyArray<[string, string]> { @@ -80,13 +79,7 @@ class Website implements RenderContext { rewriteHrefAsData(iri: string): string | undefined { const result: string | undefined = this.outputs[iri]; - if(result) { - let resolved = resolveHref(result, this.baseURL) - if(this.appendHtmlSuffix) { - resolved = resolved + ".html" - } - return resolved - } else return undefined + return result ? resolveHref(result, this.baseURL) : undefined; } beforecompile(): void { @@ -119,7 +112,7 @@ class Website implements RenderContext { for (const term of terms) { const prefixedName = this.prefixes.lookup(term.value); if (prefixedName) { - this.outputs[term.value] = path.join(prefixedName.prefixLabel, prefixedName.localName); + this.outputs[term.value] = path.join(prefixedName.prefixLabel, prefixedName.localName) + (this.appendHtmlSuffix ? ".html" : ""); } else { const string = term.value; @@ -129,7 +122,7 @@ class Website implements RenderContext { hash = ((hash << 5) - hash) + char; hash = hash & hash; } - this.outputs[term.value] = (hash & ((1 << 31) - 1)).toString().padStart(10, "0"); + this.outputs[term.value] = (hash & ((1 << 31) - 1)).toString().padStart(10, "0") + (this.appendHtmlSuffix ? ".html" : ""); } } } @@ -229,7 +222,7 @@ export default function main(options: Options): void { const icons = project.json.siteOptions?.icons || []; const assets = project.json.siteOptions?.assets || {}; - const context = new Website(project.json.siteOptions?.title || DEFAULT_TITLE, new URL(options.base || project.json.siteOptions?.baseURL || DEFAULT_BASE, DEFAULT_BASE).href, (project.json.siteOptions?.appendHtmlSuffix || false), project.json.siteOptions?.roots); + const context = new Website(project.json.siteOptions?.title || DEFAULT_TITLE, new URL(options.base || project.json.siteOptions?.baseURL || DEFAULT_BASE, DEFAULT_BASE).href, !!project.json.siteOptions?.appendHtmlSuffix, project.json.siteOptions?.roots); const site = new Workspace(project.package.resolve(options.output || project.json.siteOptions?.outDir || "public")); context.beforecompile(); @@ -270,6 +263,6 @@ export default function main(options: Options): void { site.write(ERROR_FILE_NAME, Buffer.from("\n" + renderHTML(render404(context, links, scripts, navigation)))); for (const iri in context.outputs) { - site.write(context.outputs[iri] + ".html", Buffer.from("\n" + renderHTML(renderPage(iri, context, links, scripts, navigation)))); + site.write(context.outputs[iri] + (context.appendHtmlSuffix ? "" : ".html"), Buffer.from("\n" + renderHTML(renderPage(iri, context, links, scripts, navigation)))); } } diff --git a/packages/explorer-views/src/context.ts b/packages/explorer-views/src/context.ts index 7a27af5..b9d46b9 100644 --- a/packages/explorer-views/src/context.ts +++ b/packages/explorer-views/src/context.ts @@ -7,7 +7,6 @@ export interface RenderContext { readonly graph: Graph; readonly schema: Schema; readonly rootClasses: ReadonlySet | null; - readonly appendHtmlSuffix: boolean; getPrefixes(): ReadonlyArray<[string, string]>; lookupPrefixedName(iri: string): { readonly prefixLabel: string, readonly localName: string } | null; diff --git a/packages/explorer-worker/src/main.tsx b/packages/explorer-worker/src/main.tsx index 9b0d5dc..5742135 100644 --- a/packages/explorer-worker/src/main.tsx +++ b/packages/explorer-worker/src/main.tsx @@ -64,14 +64,12 @@ class BackendImpl implements Backend, RenderContext { prefixes: PrefixTable; readonly rootClasses: ReadonlySet | null; - readonly appendHtmlSuffix = false; constructor(private readonly frontend: Frontend) { this.graph = Graph.from(this.dataset); this.schema = Schema.decompile(this.dataset, this.graph); this.prefixes = new PrefixTable(this.namespaces); this.rootClasses = null; - this.appendHtmlSuffix = false; } getPrefixes(): ReadonlyArray<[string, string]> { From 6bc44d298b3e01ce68928b5a796c648ceb25eb1f Mon Sep 17 00:00:00 2001 From: ektrah Date: Thu, 18 May 2023 08:20:12 +0200 Subject: [PATCH 3/3] Rename appendHtmlSuffix to cleanUrls --- explorer/rdfconfig.json | 3 ++- packages/cli/src/commands/make-site.tsx | 10 +++++----- packages/cli/src/model/project.ts | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/explorer/rdfconfig.json b/explorer/rdfconfig.json index d73d110..bb80535 100644 --- a/explorer/rdfconfig.json +++ b/explorer/rdfconfig.json @@ -17,6 +17,7 @@ "favicon.ico": "favicon.ico", "robots.txt": "robots.txt" }, - "outDir": "public" + "outDir": "public", + "cleanUrls": true } } diff --git a/packages/cli/src/commands/make-site.tsx b/packages/cli/src/commands/make-site.tsx index ca0ceed..d6b519f 100644 --- a/packages/cli/src/commands/make-site.tsx +++ b/packages/cli/src/commands/make-site.tsx @@ -62,7 +62,7 @@ class Website implements RenderContext { readonly outputs: Record = {}; readonly rootClasses: ReadonlySet | null; - constructor(readonly title: string, readonly baseURL: string, readonly appendHtmlSuffix: boolean, rootClasses?: Iterable) { + constructor(readonly title: string, readonly baseURL: string, readonly cleanUrls: boolean, rootClasses?: Iterable) { this.graph = Graph.from(this.dataset); this.schema = Schema.decompile(this.dataset, this.graph); this.prefixes = new PrefixTable(this.namespaces); @@ -112,7 +112,7 @@ class Website implements RenderContext { for (const term of terms) { const prefixedName = this.prefixes.lookup(term.value); if (prefixedName) { - this.outputs[term.value] = path.join(prefixedName.prefixLabel, prefixedName.localName) + (this.appendHtmlSuffix ? ".html" : ""); + this.outputs[term.value] = path.join(prefixedName.prefixLabel, prefixedName.localName) + (this.cleanUrls ? "" : ".html"); } else { const string = term.value; @@ -122,7 +122,7 @@ class Website implements RenderContext { hash = ((hash << 5) - hash) + char; hash = hash & hash; } - this.outputs[term.value] = (hash & ((1 << 31) - 1)).toString().padStart(10, "0") + (this.appendHtmlSuffix ? ".html" : ""); + this.outputs[term.value] = (hash & ((1 << 31) - 1)).toString().padStart(10, "0") + (this.cleanUrls ? "" : ".html"); } } } @@ -222,7 +222,7 @@ export default function main(options: Options): void { const icons = project.json.siteOptions?.icons || []; const assets = project.json.siteOptions?.assets || {}; - const context = new Website(project.json.siteOptions?.title || DEFAULT_TITLE, new URL(options.base || project.json.siteOptions?.baseURL || DEFAULT_BASE, DEFAULT_BASE).href, !!project.json.siteOptions?.appendHtmlSuffix, project.json.siteOptions?.roots); + const context = new Website(project.json.siteOptions?.title || DEFAULT_TITLE, new URL(options.base || project.json.siteOptions?.baseURL || DEFAULT_BASE, DEFAULT_BASE).href, !!project.json.siteOptions?.cleanUrls, project.json.siteOptions?.roots); const site = new Workspace(project.package.resolve(options.output || project.json.siteOptions?.outDir || "public")); context.beforecompile(); @@ -263,6 +263,6 @@ export default function main(options: Options): void { site.write(ERROR_FILE_NAME, Buffer.from("\n" + renderHTML(render404(context, links, scripts, navigation)))); for (const iri in context.outputs) { - site.write(context.outputs[iri] + (context.appendHtmlSuffix ? "" : ".html"), Buffer.from("\n" + renderHTML(renderPage(iri, context, links, scripts, navigation)))); + site.write(context.outputs[iri] + (context.cleanUrls ? ".html" : ""), Buffer.from("\n" + renderHTML(renderPage(iri, context, links, scripts, navigation)))); } } diff --git a/packages/cli/src/model/project.ts b/packages/cli/src/model/project.ts index 47304ac..84bafc9 100644 --- a/packages/cli/src/model/project.ts +++ b/packages/cli/src/model/project.ts @@ -31,7 +31,7 @@ export interface SiteConfig { baseURL?: string; outDir?: string; roots?: Array; - appendHtmlSuffix?: boolean; + cleanUrls?: boolean; } export namespace SiteConfig { @@ -45,7 +45,7 @@ export namespace SiteConfig { && (Is.undefined(candidate.baseURL) || Is.string(candidate.baseURL)) && (Is.undefined(candidate.outDir) || Is.string(candidate.outDir)) && (Is.undefined(candidate.roots) || Is.typedArray(candidate.roots, Is.string)) - && (Is.undefined(candidate.appendHtmlSuffix) || Is.boolean(candidate.appendHtmlSuffix)); + && (Is.undefined(candidate.cleanUrls) || Is.boolean(candidate.cleanUrls)); } }