Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] v4 from jackyzha0:v4 #22

Merged
merged 5 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/features/breadcrumbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ For example, here's what the default configuration looks like:

```typescript title="quartz.layout.ts"
Component.Breadcrumbs({
spacerSymbol: ">", // symbol between crumbs
spacerSymbol: "", // symbol between crumbs
rootName: "Home", // name of first/root element
resolveFrontmatterTitle: false, // wether to resolve folder names through frontmatter titles (more computationally expensive)
hideOnRoot: true, // wether to hide breadcrumbs on root `index.md` page
resolveFrontmatterTitle: true, // whether to resolve folder names through frontmatter titles
hideOnRoot: true, // whether to hide breadcrumbs on root `index.md` page
})
```

Expand Down
51 changes: 25 additions & 26 deletions quartz/components/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ interface BreadcrumbOptions {
}

const defaultOptions: BreadcrumbOptions = {
spacerSymbol: ">",
spacerSymbol: "",
rootName: "Home",
resolveFrontmatterTitle: false,
resolveFrontmatterTitle: true,
hideOnRoot: true,
}

Expand All @@ -41,25 +41,13 @@ function formatCrumb(displayName: string, baseSlug: FullSlug, currentSlug: Simpl
}
}

// given a folderName (e.g. "features"), search for the corresponding `index.md` file
function findCurrentFile(allFiles: QuartzPluginData[], folderName: string) {
return allFiles.find((file) => {
if (file.slug?.endsWith("index")) {
const folderParts = file.filePath?.split("/")
if (folderParts) {
const name = folderParts[folderParts?.length - 2]
if (name === folderName) {
return true
}
}
}
})
}

export default ((opts?: Partial<BreadcrumbOptions>) => {
// Merge options with defaults
const options: BreadcrumbOptions = { ...defaultOptions, ...opts }

// computed index of folder name to its associated file data
let folderIndex: Map<string, QuartzPluginData> | undefined

function Breadcrumbs({ fileData, allFiles, displayClass }: QuartzComponentProps) {
// Hide crumbs on root if enabled
if (options.hideOnRoot && fileData.slug === "index") {
Expand All @@ -70,28 +58,39 @@ export default ((opts?: Partial<BreadcrumbOptions>) => {
const firstEntry = formatCrumb(options.rootName, fileData.slug!, "/" as SimpleSlug)
const crumbs: CrumbData[] = [firstEntry]

if (!folderIndex && options.resolveFrontmatterTitle) {
folderIndex = new Map()
// construct the index for the first time
for (const file of allFiles) {
if (file.slug?.endsWith("index")) {
const folderParts = file.filePath?.split("/")
if (folderParts) {
const folderName = folderParts[folderParts?.length - 2]
folderIndex.set(folderName, file)
}
}
}
}

// Split slug into hierarchy/parts
const slugParts = fileData.slug?.split("/")
if (slugParts) {
// full path until current part
let currentPath = ""
for (let i = 0; i < slugParts.length - 1; i++) {
let currentTitle = slugParts[i]
let curPathSegment = slugParts[i]

// TODO: performance optimizations/memoizing
// Try to resolve frontmatter folder title
if (options?.resolveFrontmatterTitle) {
// try to find file for current path
const currentFile = findCurrentFile(allFiles, currentTitle)
if (currentFile) {
currentTitle = currentFile.frontmatter!.title
}
const currentFile = folderIndex?.get(curPathSegment)
if (currentFile) {
curPathSegment = currentFile.frontmatter!.title
}

// Add current slug to full path
currentPath += slugParts[i] + "/"

// Format and add current crumb
const crumb = formatCrumb(currentTitle, fileData.slug!, currentPath as SimpleSlug)
const crumb = formatCrumb(curPathSegment, fileData.slug!, currentPath as SimpleSlug)
crumbs.push(crumb)
}

Expand Down
10 changes: 9 additions & 1 deletion quartz/components/scripts/popover.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ async function mouseEnterHandler(
})
}

const hasAlreadyBeenFetched = () =>
[...link.children].some((child) => child.classList.contains("popover"))

// dont refetch if there's already a popover
if ([...link.children].some((child) => child.classList.contains("popover"))) {
if (hasAlreadyBeenFetched()) {
return setPosition(link.lastChild as HTMLElement)
}

Expand All @@ -49,6 +52,11 @@ async function mouseEnterHandler(
console.error(err)
})

// bailout if another popover exists
if (hasAlreadyBeenFetched()) {
return
}

if (!contents) return
const html = p.parseFromString(contents, "text/html")
normalizeRelativeURLs(html, targetUrl)
Expand Down
13 changes: 11 additions & 2 deletions quartz/plugins/emitters/contentIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { FilePath, FullSlug, SimpleSlug, simplifySlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import { toHtml } from "hast-util-to-html"
import path from "path"
import { byDateAndAlphabetical } from "../../components/PageList"

export type ContentIndex = Map<FullSlug, ContentDetails>
export type ContentDetails = {
Expand Down Expand Up @@ -60,7 +59,17 @@ function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex, limit?: nu
</item>`

const items = Array.from(idx)
.sort((a, b) => byDateAndAlphabetical(cfg)(a[1], b[1]))
.sort(([_, f1], [__, f2]) => {
if (f1.date && f2.date) {
return f2.date.getTime() - f1.date.getTime()
} else if (f1.date && !f2.date) {
return -1
} else if (!f1.date && f2.date) {
return 1
}

return f1.title.localeCompare(f2.title)
})
.map(([slug, content]) => createURLEntry(simplifySlug(slug), content))
.slice(0, limit ?? idx.size)
.join("")
Expand Down
2 changes: 1 addition & 1 deletion quartz/styles/base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ a {
color: var(--tertiary) !important;
}

&.internal {
&.internal:not(:has(img)) {
text-decoration: none;
background-color: var(--highlight);
padding: 0 0.1rem;
Expand Down