Skip to content

Commit

Permalink
fix(aliases): Fix wikilink resolution for aliases
Browse files Browse the repository at this point in the history
This is kind of hacky, but I haven't found a better way to augment
`allSlugs` - we need to have markdown parsed to get aliases from the
frontmatter, and we need to add the slugs before links get transformed
in the html plugin pass.

Fixes jackyzha0#904, don't know why you closed that
  • Loading branch information
necauqua committed Dec 25, 2024
1 parent a582505 commit 474c3c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
45 changes: 18 additions & 27 deletions quartz/plugins/emitters/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ import { QuartzEmitterPlugin } from "../types"
import path from "path"
import { write } from "./helpers"
import DepGraph from "../../depgraph"
import { VFile } from "vfile"
import { BuildCtx } from "../../util/ctx"

export function getAliasSlugs({ argv }: BuildCtx, file: VFile): FullSlug[] {
const dir = path.posix.relative(argv.directory, path.dirname(file.data.filePath!))
const aliases = file.data.frontmatter?.aliases ?? []
const slugs: FullSlug[] = aliases.map((alias) => path.posix.join(dir, alias) as FullSlug)
const permalink = file.data.frontmatter?.permalink
if (typeof permalink === "string") {
slugs.push(permalink as FullSlug)
}
// fix any slugs that have trailing slash
return slugs.map((slug) =>
slug.endsWith("/") ? (joinSegments(slug, "index") as FullSlug) : slug,
)
}

export const AliasRedirects: QuartzEmitterPlugin = () => ({
name: "AliasRedirects",
Expand All @@ -14,20 +30,7 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({

const { argv } = ctx
for (const [_tree, file] of content) {
const dir = path.posix.relative(argv.directory, path.dirname(file.data.filePath!))
const aliases = file.data.frontmatter?.aliases ?? []
const slugs = aliases.map((alias) => path.posix.join(dir, alias) as FullSlug)
const permalink = file.data.frontmatter?.permalink
if (typeof permalink === "string") {
slugs.push(permalink as FullSlug)
}

for (let slug of slugs) {
// fix any slugs that have trailing slash
if (slug.endsWith("/")) {
slug = joinSegments(slug, "index") as FullSlug
}

for (const slug of getAliasSlugs(ctx, file)) {
graph.addEdge(file.data.filePath!, joinSegments(argv.output, slug + ".html") as FilePath)
}
}
Expand All @@ -40,20 +43,8 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({

for (const [_tree, file] of content) {
const ogSlug = simplifySlug(file.data.slug!)
const dir = path.posix.relative(argv.directory, path.dirname(file.data.filePath!))
const aliases = file.data.frontmatter?.aliases ?? []
const slugs: FullSlug[] = aliases.map((alias) => path.posix.join(dir, alias) as FullSlug)
const permalink = file.data.frontmatter?.permalink
if (typeof permalink === "string") {
slugs.push(permalink as FullSlug)
}

for (let slug of slugs) {
// fix any slugs that have trailing slash
if (slug.endsWith("/")) {
slug = joinSegments(slug, "index") as FullSlug
}

for (const slug of getAliasSlugs(ctx, file)) {
const redirUrl = resolveRelative(slug, file.data.slug!)
const fp = await write({
ctx,
Expand Down
9 changes: 8 additions & 1 deletion quartz/plugins/transformers/ofm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { toHtml } from "hast-util-to-html"
import { PhrasingContent } from "mdast-util-find-and-replace/lib"
import { capitalize } from "../../util/lang"
import { PluggableList } from "unified"
import { getAliasSlugs } from "../emitters/aliases"

export interface Options {
comments: boolean
Expand Down Expand Up @@ -211,9 +212,15 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>>

return src
},
markdownPlugins(_ctx) {
markdownPlugins(ctx) {
const plugins: PluggableList = []

// alias slugs
plugins.push(() => (tree, file) => {
ctx.allSlugs.push(...getAliasSlugs(ctx, file))
return tree
})

// regex replacements
plugins.push(() => {
return (tree: Root, file) => {
Expand Down

0 comments on commit 474c3c7

Please sign in to comment.