Skip to content

Commit

Permalink
fix relative path resolution in router and link crawling
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Aug 17, 2023
1 parent 2326521 commit 2f6747b
Show file tree
Hide file tree
Showing 44 changed files with 160 additions and 106 deletions.
2 changes: 0 additions & 2 deletions content/features/upcoming features.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ draft: true
- debounce cfg rebuild on large repos
- investigate content rebuild triggering multiple times even when debounced, causing an esbuild deadlock
- dereference symlink for npx quartz sync
- test/fix with subpath
- fix docs with deploy from github

## high priority backlog

Expand Down
5 changes: 4 additions & 1 deletion content/hosting.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ jobs:
uses: actions/deploy-pages@v2
```
Then, commit these changes by doing `npx quartz sync`. This should deploy your site to `<github-username>.github.io/<repository-name>`.
Then:
1. Head to "Settings" tab of your forked repository and in the sidebar, click "Pages". Under "Source", select "GitHub Actions".
2. Commit these changes by doing `npx quartz sync`. This should deploy your site to `<github-username>.github.io/<repository-name>`.

### Custom Domain

Expand Down
10 changes: 0 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"check": "tsc --noEmit && npx prettier . --check",
"format": "npx prettier . --write",
"test": "tsx ./quartz/path.test.ts",
"test": "tsx ./quartz/util/path.test.ts",
"profile": "0x -D prof ./quartz/bootstrap-cli.mjs build --concurrency=1"
},
"keywords": [
Expand Down Expand Up @@ -89,7 +89,6 @@
"@types/js-yaml": "^4.0.5",
"@types/node": "^20.1.2",
"@types/pretty-time": "^1.1.2",
"@types/serve-handler": "^6.1.1",
"@types/source-map-support": "^0.5.6",
"@types/workerpool": "^6.4.0",
"@types/ws": "^8.5.5",
Expand Down
74 changes: 61 additions & 13 deletions quartz/bootstrap-cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ const BuildArgv = {
default: false,
describe: "run a local server to live-preview your Quartz",
},
baseDir: {
string: true,
describe: "base path to serve your local server on",
},
port: {
number: true,
default: 8080,
Expand Down Expand Up @@ -384,19 +388,63 @@ See the [documentation](https://quartz.jzhao.xyz) for how to get started.

await build(clientRefresh)
const server = http.createServer(async (req, res) => {
await serveHandler(req, res, {
public: argv.output,
directoryListing: false,
trailingSlash: true,
})
const status = res.statusCode
const statusString =
status >= 200 && status < 300
? chalk.green(`[${status}]`)
: status >= 300 && status < 400
? chalk.yellow(`[${status}]`)
: chalk.red(`[${status}]`)
console.log(statusString + chalk.grey(` ${req.url}`))
const serve = async (fp) => {
await serveHandler(req, res, {
public: argv.output,
directoryListing: false,
})
const status = res.statusCode
const statusString =
status >= 200 && status < 300 ? chalk.green(`[${status}]`) : chalk.red(`[${status}]`)
console.log(statusString + chalk.grey(` ${req.url}`))
}

const redirect = (newFp) => {
res.writeHead(301, {
Location: newFp,
})
console.log(chalk.yellow("[301]") + chalk.grey(` ${req.url} -> ${newFp}`))
return res.end()
}

let fp = req.url?.split("?")[0] ?? "/"

// handle redirects
if (fp.endsWith("/")) {
// /trailing/
// does /trailing/index.html exist? if so, serve it
const indexFp = path.posix.join(fp, "index.html")
if (fs.existsSync(path.posix.join(argv.output, indexFp))) {
return serve(indexFp)
}

// does /trailing.html exist? if so, redirect to /trailing
let base = fp.slice(0, -1)
if (path.extname(base) === "") {
base += ".html"
}
if (fs.existsSync(path.posix.join(argv.output, base))) {
return redirect(base)
}
} else {
// /regular
// does /regular.html exist? if so, serve it
let base = fp
if (path.extname(base) === "") {
base += ".html"
}
if (fs.existsSync(path.posix.join(argv.output, base))) {
return serve(base)
}

// does /regular/index.html exist? if so, redirect to /regular/
let indexFp = path.posix.join(fp, "index.html")
if (fs.existsSync(path.posix.join(argv.output, indexFp))) {
return redirect(fp + "/")
}
}

return serve(fp)
})
server.listen(argv.port)
console.log(chalk.cyan(`Started a Quartz server listening at http://localhost:${argv.port}`))
Expand Down
12 changes: 6 additions & 6 deletions quartz/build.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import sourceMapSupport from "source-map-support"
sourceMapSupport.install(options)
import path from "path"
import { PerfTimer } from "./perf"
import { PerfTimer } from "./util/perf"
import { rimraf } from "rimraf"
import { isGitIgnored } from "globby"
import chalk from "chalk"
import { parseMarkdown } from "./processors/parse"
import { filterContent } from "./processors/filter"
import { emitContent } from "./processors/emit"
import cfg from "../quartz.config"
import { FilePath, joinSegments, slugifyFilePath } from "./path"
import { FilePath, joinSegments, slugifyFilePath } from "./util/path"
import chokidar from "chokidar"
import { ProcessedContent } from "./plugins/vfile"
import { Argv, BuildCtx } from "./ctx"
import { glob, toPosixPath } from "./glob"
import { trace } from "./trace"
import { options } from "./sourcemap"
import { Argv, BuildCtx } from "./util/ctx"
import { glob, toPosixPath } from "./util/glob"
import { trace } from "./util/trace"
import { options } from "./util/sourcemap"

async function buildQuartz(argv: Argv, clientRefresh: () => void) {
const ctx: BuildCtx = {
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/Backlinks.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QuartzComponentConstructor, QuartzComponentProps } from "./types"
import style from "./styles/backlinks.scss"
import { canonicalizeServer, resolveRelative } from "../path"
import { canonicalizeServer, resolveRelative } from "../util/path"

function Backlinks({ fileData, allFiles }: QuartzComponentProps) {
const slug = canonicalizeServer(fileData.slug!)
Expand Down
4 changes: 2 additions & 2 deletions quartz/components/Head.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { canonicalizeServer, pathToRoot } from "../path"
import { JSResourceToScriptElement } from "../resources"
import { canonicalizeServer, pathToRoot } from "../util/path"
import { JSResourceToScriptElement } from "../util/resources"
import { QuartzComponentConstructor, QuartzComponentProps } from "./types"

export default (() => {
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/PageList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CanonicalSlug, canonicalizeServer, resolveRelative } from "../path"
import { CanonicalSlug, canonicalizeServer, resolveRelative } from "../util/path"
import { QuartzPluginData } from "../plugins/vfile"
import { Date } from "./Date"
import { QuartzComponentProps } from "./types"
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/PageTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { canonicalizeServer, pathToRoot } from "../path"
import { canonicalizeServer, pathToRoot } from "../util/path"
import { QuartzComponentConstructor, QuartzComponentProps } from "./types"

function PageTitle({ fileData, cfg }: QuartzComponentProps) {
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/TagList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { canonicalizeServer, pathToRoot, slugTag } from "../path"
import { canonicalizeServer, pathToRoot, slugTag } from "../util/path"
import { QuartzComponentConstructor, QuartzComponentProps } from "./types"

function TagList({ fileData }: QuartzComponentProps) {
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/pages/FolderContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from "path"

import style from "../styles/listPage.scss"
import { PageList } from "../PageList"
import { canonicalizeServer } from "../../path"
import { canonicalizeServer } from "../../util/path"

function FolderContent(props: QuartzComponentProps) {
const { tree, fileData, allFiles } = props
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/pages/TagContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Fragment, jsx, jsxs } from "preact/jsx-runtime"
import { toJsxRuntime } from "hast-util-to-jsx-runtime"
import style from "../styles/listPage.scss"
import { PageList } from "../PageList"
import { ServerSlug, canonicalizeServer, getAllSegmentPrefixes } from "../../path"
import { ServerSlug, canonicalizeServer, getAllSegmentPrefixes } from "../../util/path"
import { QuartzPluginData } from "../../plugins/vfile"

const numPages = 10
Expand Down
4 changes: 2 additions & 2 deletions quartz/components/renderPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { render } from "preact-render-to-string"
import { QuartzComponent, QuartzComponentProps } from "./types"
import HeaderConstructor from "./Header"
import BodyConstructor from "./Body"
import { JSResourceToScriptElement, StaticResources } from "../resources"
import { CanonicalSlug, pathToRoot } from "../path"
import { JSResourceToScriptElement, StaticResources } from "../util/resources"
import { CanonicalSlug, pathToRoot } from "../util/path"

interface RenderComponents {
head: QuartzComponent
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/scripts/graph.inline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ContentDetails } from "../../plugins/emitters/contentIndex"
import * as d3 from "d3"
import { registerEscapeHandler, removeAllChildren } from "./util"
import { CanonicalSlug, getCanonicalSlug, getClientSlug, resolveRelative } from "../../path"
import { CanonicalSlug, getCanonicalSlug, getClientSlug, resolveRelative } from "../../util/path"

type NodeData = {
id: CanonicalSlug
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/scripts/search.inline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Document } from "flexsearch"
import { ContentDetails } from "../../plugins/emitters/contentIndex"
import { registerEscapeHandler, removeAllChildren } from "./util"
import { CanonicalSlug, getClientSlug, resolveRelative } from "../../path"
import { CanonicalSlug, getClientSlug, resolveRelative } from "../../util/path"

interface Item {
id: number
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/scripts/spa.inline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import micromorph from "micromorph"
import { CanonicalSlug, RelativeURL, getCanonicalSlug } from "../../path"
import { CanonicalSlug, RelativeURL, getCanonicalSlug } from "../../util/path"

// adapted from `micromorph`
// https://github.com/natemoo-re/micromorph
Expand Down
2 changes: 1 addition & 1 deletion quartz/plugins/emitters/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ServerSlug,
canonicalizeServer,
resolveRelative,
} from "../../path"
} from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import path from "path"

Expand Down
4 changes: 2 additions & 2 deletions quartz/plugins/emitters/assets.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FilePath, joinSegments, slugifyFilePath } from "../../path"
import { FilePath, joinSegments, slugifyFilePath } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import path from "path"
import fs from "fs"
import { glob } from "../../glob"
import { glob } from "../../util/glob"

export const Assets: QuartzEmitterPlugin = () => {
return {
Expand Down
8 changes: 4 additions & 4 deletions quartz/plugins/emitters/componentResources.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FilePath, ServerSlug } from "../../path"
import { FilePath, ServerSlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"

// @ts-ignore
Expand All @@ -9,10 +9,10 @@ import plausibleScript from "../../components/scripts/plausible.inline"
import popoverScript from "../../components/scripts/popover.inline"
import styles from "../../styles/base.scss"
import popoverStyle from "../../components/styles/popover.scss"
import { BuildCtx } from "../../ctx"
import { StaticResources } from "../../resources"
import { BuildCtx } from "../../util/ctx"
import { StaticResources } from "../../util/resources"
import { QuartzComponent } from "../../components/types"
import { googleFontHref, joinStyles } from "../../theme"
import { googleFontHref, joinStyles } from "../../util/theme"
import { Features, transform } from "lightningcss"

type ComponentResources = {
Expand Down
8 changes: 7 additions & 1 deletion quartz/plugins/emitters/contentIndex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { GlobalConfiguration } from "../../cfg"
import { CanonicalSlug, ClientSlug, FilePath, ServerSlug, canonicalizeServer } from "../../path"
import {
CanonicalSlug,
ClientSlug,
FilePath,
ServerSlug,
canonicalizeServer,
} from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import path from "path"

Expand Down
2 changes: 1 addition & 1 deletion quartz/plugins/emitters/contentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 "../../path"
import { FilePath, canonicalizeServer } from "../../util/path"
import { defaultContentPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { Content } from "../../components"

Expand Down
8 changes: 7 additions & 1 deletion quartz/plugins/emitters/folderPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ 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 "../../path"
import {
CanonicalSlug,
FilePath,
ServerSlug,
canonicalizeServer,
joinSegments,
} from "../../util/path"
import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { FolderContent } from "../../components"

Expand Down
4 changes: 2 additions & 2 deletions quartz/plugins/emitters/static.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FilePath, QUARTZ, joinSegments } from "../../path"
import { FilePath, QUARTZ, joinSegments } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import fs from "fs"
import { glob } from "../../glob"
import { glob } from "../../util/glob"

export const Static: QuartzEmitterPlugin = () => ({
name: "Static",
Expand Down
4 changes: 2 additions & 2 deletions quartz/plugins/emitters/tagPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ServerSlug,
getAllSegmentPrefixes,
joinSegments,
} from "../../path"
} from "../../util/path"
import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { TagContent } from "../../components"

Expand Down Expand Up @@ -41,7 +41,7 @@ export const TagPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
allFiles.flatMap((data) => data.frontmatter?.tags ?? []).flatMap(getAllSegmentPrefixes),
)
// add base tag
tags.add("")
tags.add("index")

const tagDescriptions: Record<string, ProcessedContent> = Object.fromEntries(
[...tags].map((tag) => {
Expand Down
6 changes: 3 additions & 3 deletions quartz/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StaticResources } from "../resources"
import { FilePath, ServerSlug } from "../path"
import { BuildCtx } from "../ctx"
import { StaticResources } from "../util/resources"
import { FilePath, ServerSlug } from "../util/path"
import { BuildCtx } from "../util/ctx"

export function getStaticResourcesFromPlugins(ctx: BuildCtx) {
const staticResources: StaticResources = {
Expand Down
2 changes: 1 addition & 1 deletion quartz/plugins/transformers/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import matter from "gray-matter"
import remarkFrontmatter from "remark-frontmatter"
import { QuartzTransformerPlugin } from "../types"
import yaml from "js-yaml"
import { slugTag } from "../../path"
import { slugTag } from "../../util/path"

export interface Options {
delims: string | string[]
Expand Down
2 changes: 1 addition & 1 deletion quartz/plugins/transformers/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
joinSegments,
splitAnchor,
transformLink,
} from "../../path"
} from "../../util/path"
import path from "path"
import { visit } from "unist-util-visit"
import isAbsoluteUrl from "is-absolute-url"
Expand Down
Loading

0 comments on commit 2f6747b

Please sign in to comment.