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

Feat: Adds logic to look for links in frontmatter #944

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
16 changes: 16 additions & 0 deletions quartz/plugins/transformers/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
TransformOptions,
stripSlashes,
simplifySlug,
slugifyFilePath,
splitAnchor,
transformLink,
joinSegments,
FilePath,
} from "../../util/path"
import path from "path"
import { visit } from "unist-util-visit"
Expand All @@ -23,6 +25,7 @@ interface Options {
openLinksInNewTab: boolean
lazyLoad: boolean
externalLinkIcon: boolean
includeFrontmatterLinks: boolean
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
includeFrontmatterLinks: boolean
includeFrontmatter: boolean

}

const defaultOptions: Options = {
Expand All @@ -31,6 +34,7 @@ const defaultOptions: Options = {
openLinksInNewTab: false,
lazyLoad: false,
externalLinkIcon: true,
includeFrontmatterLinks: true,
}

export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => {
Expand All @@ -49,6 +53,18 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> =
allSlugs: ctx.allSlugs,
}

// Add frontmatter links to outgoing links
if (opts.includeFrontmatterLinks && file.data.frontmatter) {
for (const [fmKey, fmValue] of Object.entries(file.data.frontmatter)) {
if (fmValue && typeof fmValue === "string") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah you might need to take a look at how we handle links in ofm.ts to handle alias, intra-links docs, and marks.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do this in ofm instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah you might need to take a look at how we handle links in ofm.ts to handle alias, intra-links docs, and marks

I tried to but I didn't manage to get a grasp of how it works. This is still too high level typescript for me 🤣

const dest = fmValue.match(/\[\[(.*)\]\]/)?.[1] as FilePath ?? null
if (dest) {
outgoing.add(simplifySlug(slugifyFilePath(dest)))
}
}
}
}

visit(tree, "element", (node, _index, _parent) => {
// rewrite all links
if (
Expand Down
Loading