base path refactor to better support subpath hosting

This commit is contained in:
Jacky Zhao 2023-08-19 15:52:25 -07:00
parent 3201f83b70
commit c874e7e937
29 changed files with 257 additions and 389 deletions

View file

@ -1,10 +1,4 @@
import {
CanonicalSlug,
FilePath,
ServerSlug,
canonicalizeServer,
resolveRelative,
} from "../../util/path"
import { FilePath, FullSlug, resolveRelative, simplifySlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import path from "path"
@ -17,10 +11,10 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({
const fps: FilePath[] = []
for (const [_tree, file] of content) {
const ogSlug = canonicalizeServer(file.data.slug!)
const ogSlug = simplifySlug(file.data.slug!)
const dir = path.posix.relative(argv.directory, file.dirname ?? argv.directory)
let aliases: CanonicalSlug[] = []
let aliases: FullSlug[] = []
if (file.data.frontmatter?.aliases) {
aliases = file.data.frontmatter?.aliases
} else if (file.data.frontmatter?.alias) {
@ -28,9 +22,8 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({
}
for (const alias of aliases) {
const slug = path.posix.join(dir, alias) as ServerSlug
const redirUrl = resolveRelative(canonicalizeServer(slug), ogSlug)
const slug = path.posix.join(dir, alias) as FullSlug
const redirUrl = resolveRelative(slug, file.data.slug!)
const fp = await emit({
content: `
<!DOCTYPE html>

View file

@ -1,4 +1,4 @@
import { FilePath, ServerSlug } from "../../util/path"
import { FilePath, FullSlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
// @ts-ignore
@ -154,7 +154,7 @@ export const ComponentResources: QuartzEmitterPlugin<Options> = (opts?: Partial<
const postscript = joinScripts(componentResources.afterDOMLoaded)
const fps = await Promise.all([
emit({
slug: "index" as ServerSlug,
slug: "index" as FullSlug,
ext: ".css",
content: transform({
filename: "index.css",
@ -171,12 +171,12 @@ export const ComponentResources: QuartzEmitterPlugin<Options> = (opts?: Partial<
}).code.toString(),
}),
emit({
slug: "prescript" as ServerSlug,
slug: "prescript" as FullSlug,
ext: ".js",
content: prescript,
}),
emit({
slug: "postscript" as ServerSlug,
slug: "postscript" as FullSlug,
ext: ".js",
content: postscript,
}),

View file

@ -1,18 +1,12 @@
import { GlobalConfiguration } from "../../cfg"
import {
CanonicalSlug,
ClientSlug,
FilePath,
ServerSlug,
canonicalizeServer,
} from "../../util/path"
import { FilePath, FullSlug, SimpleSlug, simplifySlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import path from "path"
export type ContentIndex = Map<CanonicalSlug, ContentDetails>
export type ContentIndex = Map<FullSlug, ContentDetails>
export type ContentDetails = {
title: string
links: CanonicalSlug[]
links: SimpleSlug[]
tags: string[]
content: string
date?: Date
@ -33,21 +27,21 @@ const defaultOptions: Options = {
function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndex): string {
const base = cfg.baseUrl ?? ""
const createURLEntry = (slug: CanonicalSlug, content: ContentDetails): string => `<url>
const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => `<url>
<loc>https://${base}/${slug}</loc>
<lastmod>${content.date?.toISOString()}</lastmod>
</url>`
const urls = Array.from(idx)
.map(([slug, content]) => createURLEntry(slug, content))
.map(([slug, content]) => createURLEntry(simplifySlug(slug), content))
.join("")
return `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${urls}</urlset>`
}
function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex): string {
const base = cfg.baseUrl ?? ""
const root = `https://${base}` as ClientSlug
const root = `https://${base}`
const createURLEntry = (slug: CanonicalSlug, content: ContentDetails): string => `<items>
const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => `<items>
<title>${content.title}</title>
<link>${root}/${slug}</link>
<guid>${root}/${slug}</guid>
@ -56,7 +50,7 @@ function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex): string {
</items>`
const items = Array.from(idx)
.map(([slug, content]) => createURLEntry(slug, content))
.map(([slug, content]) => createURLEntry(simplifySlug(slug), content))
.join("")
return `<rss xmlns:atom="http://www.w3.org/2005/atom" version="2.0">
<channel>
@ -79,7 +73,7 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
const emitted: FilePath[] = []
const linkIndex: ContentIndex = new Map()
for (const [_tree, file] of content) {
const slug = canonicalizeServer(file.data.slug!)
const slug = file.data.slug!
const date = file.data.dates?.modified ?? new Date()
if (opts?.includeEmptyFiles || (file.data.text && file.data.text !== "")) {
linkIndex.set(slug, {
@ -97,7 +91,7 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
emitted.push(
await emit({
content: generateSiteMap(cfg, linkIndex),
slug: "sitemap" as ServerSlug,
slug: "sitemap" as FullSlug,
ext: ".xml",
}),
)
@ -107,13 +101,13 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
emitted.push(
await emit({
content: generateRSSFeed(cfg, linkIndex),
slug: "index" as ServerSlug,
slug: "index" as FullSlug,
ext: ".xml",
}),
)
}
const fp = path.join("static", "contentIndex") as ServerSlug
const fp = path.join("static", "contentIndex") as FullSlug
const simplifiedIndex = Object.fromEntries(
Array.from(linkIndex).map(([slug, content]) => {
// remove description and from content index as nothing downstream

View file

@ -4,7 +4,7 @@ import HeaderConstructor from "../../components/Header"
import BodyConstructor from "../../components/Body"
import { pageResources, renderPage } from "../../components/renderPage"
import { FullPageLayout } from "../../cfg"
import { FilePath, canonicalizeServer } from "../../util/path"
import { FilePath } from "../../util/path"
import { defaultContentPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { Content } from "../../components"
@ -30,7 +30,7 @@ export const ContentPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOp
const fps: FilePath[] = []
const allFiles = content.map((c) => c[1].data)
for (const [tree, file] of content) {
const slug = canonicalizeServer(file.data.slug!)
const slug = file.data.slug!
const externalResources = pageResources(slug, resources)
const componentData: QuartzComponentProps = {
fileData: file.data,
@ -44,7 +44,7 @@ export const ContentPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOp
const content = renderPage(slug, componentData, opts, externalResources)
const fp = await emit({
content,
slug: file.data.slug!,
slug,
ext: ".html",
})

View file

@ -6,13 +6,7 @@ import { pageResources, renderPage } from "../../components/renderPage"
import { ProcessedContent, defaultProcessedContent } from "../vfile"
import { FullPageLayout } from "../../cfg"
import path from "path"
import {
CanonicalSlug,
FilePath,
ServerSlug,
canonicalizeServer,
joinSegments,
} from "../../util/path"
import { FilePath, FullSlug, SimpleSlug, joinSegments, simplifySlug } from "../../util/path"
import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { FolderContent } from "../../components"
@ -38,10 +32,10 @@ export const FolderPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
const allFiles = content.map((c) => c[1].data)
const cfg = ctx.cfg.configuration
const folders: Set<CanonicalSlug> = new Set(
const folders: Set<SimpleSlug> = new Set(
allFiles.flatMap((data) => {
const slug = data.slug
const folderName = path.dirname(slug ?? "") as CanonicalSlug
const folderName = path.dirname(slug ?? "") as SimpleSlug
if (slug && folderName !== "." && folderName !== "tags") {
return [folderName]
}
@ -53,21 +47,21 @@ export const FolderPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
[...folders].map((folder) => [
folder,
defaultProcessedContent({
slug: joinSegments(folder, "index") as ServerSlug,
slug: joinSegments(folder, "index") as FullSlug,
frontmatter: { title: `Folder: ${folder}`, tags: [] },
}),
]),
)
for (const [tree, file] of content) {
const slug = canonicalizeServer(file.data.slug!)
const slug = simplifySlug(file.data.slug!)
if (folders.has(slug)) {
folderDescriptions[slug] = [tree, file]
}
}
for (const folder of folders) {
const slug = folder
const slug = joinSegments(folder, "index") as FullSlug
const externalResources = pageResources(slug, resources)
const [tree, file] = folderDescriptions[folder]
const componentData: QuartzComponentProps = {
@ -82,7 +76,7 @@ export const FolderPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
const content = renderPage(slug, componentData, opts, externalResources)
const fp = await emit({
content,
slug: file.data.slug!,
slug,
ext: ".html",
})

View file

@ -5,13 +5,7 @@ import BodyConstructor from "../../components/Body"
import { pageResources, renderPage } from "../../components/renderPage"
import { ProcessedContent, defaultProcessedContent } from "../vfile"
import { FullPageLayout } from "../../cfg"
import {
CanonicalSlug,
FilePath,
ServerSlug,
getAllSegmentPrefixes,
joinSegments,
} from "../../util/path"
import { FilePath, FullSlug, getAllSegmentPrefixes, joinSegments } from "../../util/path"
import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { TagContent } from "../../components"
@ -49,7 +43,7 @@ export const TagPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
return [
tag,
defaultProcessedContent({
slug: joinSegments("tags", tag) as ServerSlug,
slug: joinSegments("tags", tag) as FullSlug,
frontmatter: { title, tags: [] },
}),
]
@ -67,7 +61,7 @@ export const TagPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
}
for (const tag of tags) {
const slug = joinSegments("tags", tag) as CanonicalSlug
const slug = joinSegments("tags", tag) as FullSlug
const externalResources = pageResources(slug, resources)
const [tree, file] = tagDescriptions[tag]
const componentData: QuartzComponentProps = {