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

fix(backlinks): Improve backlink perf and fix aliased backlinks #1708

Open
wants to merge 1 commit into
base: v4
Choose a base branch
from
Open
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
34 changes: 29 additions & 5 deletions quartz/components/Backlinks.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
import style from "./styles/backlinks.scss"
import { resolveRelative, simplifySlug } from "../util/path"
import { FullSlug, resolveRelative, SimpleSlug, simplifySlug } from "../util/path"
import { i18n } from "../i18n"
import { classNames } from "../util/lang"

Expand All @@ -15,14 +15,38 @@
export default ((opts?: Partial<BacklinksOptions>) => {
const options: BacklinksOptions = { ...defaultOptions, ...opts }

let backlinks: Map<SimpleSlug, Array<{ slug: FullSlug; title: string }>> | undefined

const Backlinks: QuartzComponent = ({
fileData,
allFiles,
displayClass,
cfg,
}: QuartzComponentProps) => {
const slug = simplifySlug(fileData.slug!)
const backlinkFiles = allFiles.filter((file) => file.links?.includes(slug))
if (!backlinks) {
backlinks = new Map()

const aliasMap = new Map<SimpleSlug, SimpleSlug>()
for (const file of allFiles) {
for (const alias of file.aliases ?? []) {

Check failure on line 31 in quartz/components/Backlinks.tsx

View workflow job for this annotation

GitHub Actions / build-and-test (ubuntu-latest)

Type '{}' must have a '[Symbol.iterator]()' method that returns an iterator.
aliasMap.set(simplifySlug(alias), simplifySlug(file.slug!))
}
}

for (const file of allFiles) {
for (let link of file.links ?? []) {
link = aliasMap.get(link) ?? link
let ref = backlinks.get(link)
if (!ref) {
backlinks.set(link, (ref = []))
}
ref.push({ slug: file.slug!, title: file.frontmatter?.title! })
}
}
}

const backlinkFiles = backlinks.get(simplifySlug(fileData.slug!)) ?? []

if (options.hideWhenEmpty && backlinkFiles.length == 0) {
return null
}
Expand All @@ -33,8 +57,8 @@
{backlinkFiles.length > 0 ? (
backlinkFiles.map((f) => (
<li>
<a href={resolveRelative(fileData.slug!, f.slug!)} class="internal">
{f.frontmatter?.title}
<a href={resolveRelative(fileData.slug!, f.slug)} class="internal">
{f.title}
</a>
</li>
))
Expand Down
Loading