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(citations): prettify links #1557

Open
wants to merge 1 commit into
base: v4
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions docs/biblography.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@article{templeton2024scaling,
title={Scaling Monosemanticity: Extracting Interpretable Features from Claude 3 Sonnet},
author={Templeton, Adly and Conerly, Tom and Marcus, Jonathan and Lindsey, Jack and Bricken, Trenton and Chen, Brian and Pearce, Adam and Citro, Craig and Ameisen, Emmanuel and Jones, Andy and Cunningham, Hoagy and Turner, Nicholas L and McDougall, Callum and MacDiarmid, Monte and Freeman, C. Daniel and Sumers, Theodore R. and Rees, Edward and Batson, Joshua and Jermyn, Adam and Carter, Shan and Olah, Chris and Henighan, Tom},
year={2024},
journal={Transformer Circuits Thread},
url={https://transformer-circuits.pub/2024/scaling-monosemanticity/index.html}
}

@incollection{dreyfus2008why,
title={Why Heideggerian AI Failed and How Fixing It Would Require Making It More Heideggerian},
author={Dreyfus, Hubert L.},
booktitle={The Mechanical Mind in History},
pages={331--362},
year={2008},
publisher={MIT Press},
address={Cambridge, MA}
}
27 changes: 27 additions & 0 deletions docs/features/Citations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: Citation
tags:
- feature/transformer
---

Quartz uses [rehype-citation](https://github.com/timlrx/rehype-citation) to support parsing biblography file.

An example of a citation as below:

> [@templeton2024scaling] showed that features are generally interpretable and monosemantic, and many are safety-relevant.

> [!tip]- Syntax
>
> See [source](https://github.com/jackyzha0/quartz/blob/v4/docs/features/Citations.md) and [biblography example](https://github.com/jackyzha0/quartz/blob/v4/docs/biblography.bib)

> [!note] Behaviour of references
>
> By default, the references will be included at the end of the file. To control where the references to be included,uses `[^ref]`

## Customization

Citation parsing is a functionality of the [[plugins/Citations|Citation]] plugin. See the plugin page for customization options.

## References

[^ref]
24 changes: 24 additions & 0 deletions docs/plugins/Citations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: "Citations"
tags:
- plugin/transformer
---

This plugin adds Citation support to Quartz.

> [!note]
> For information on how to add, remove or configure plugins, see the [[configuration#Plugins|Configuration]] page.

This plugin accepts the following configuration options:

- `bibliographyFile`: the path to the bibliography file. Defaults to `./bibliography.bib`. This is relative to git source of your vault.
- `suppressBibliography`: whether to suppress the bibliography at the end of the document. Defaults to `false`.
- `linkCitations`: whether to link citations to the bibliography. Defaults to `false`.
- `csl`: the citation style to use. Defaults to `apa`.
- `prettyLink`: whether to use pretty links for citations. Defaults to `true`.

## API

- Category: Transformer
- Function name: `Plugin.Citations()`.
- Source: [`quartz/plugins/transformers/citations.ts`](https://github.com/jackyzha0/quartz/blob/v4/quartz/plugins/transformers/citations.ts).
1 change: 1 addition & 0 deletions quartz.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const config: QuartzConfig = {
Plugin.CrawlLinks({ markdownLinkResolution: "shortest" }),
Plugin.Description(),
Plugin.Latex({ renderEngine: "katex" }),
Plugin.Citations({ bibliographyFile: "./docs/biblography.bib" }),
],
filters: [Plugin.RemoveDrafts()],
emitters: [
Expand Down
86 changes: 86 additions & 0 deletions quartz/plugins/transformers/citations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,88 @@ import rehypeCitation from "rehype-citation"
import { PluggableList } from "unified"
import { visit } from "unist-util-visit"
import { QuartzTransformerPlugin } from "../types"
import { Element, Text, Root as HtmlRoot } from "hast"

export interface Options {
bibliographyFile: string
suppressBibliography: boolean
linkCitations: boolean
csl: string
prettyLink?: boolean
}

const defaultOptions: Options = {
bibliographyFile: "./bibliography.bib",
suppressBibliography: false,
linkCitations: false,
csl: "apa",
prettyLink: true,
}

const URL_PATTERN = /https?:\/\/[^\s<>)"]+/g

function processTextNode(node: Text): (Element | Text)[] {
const text = node.value
const matches = Array.from(text.matchAll(URL_PATTERN))

if (matches.length === 0) {
return [node]
}

const result: (Element | Text)[] = []
let lastIndex = 0

matches.forEach((match) => {
const url = match[0]
const startIndex = match.index!

// Add text before URL if exists
if (startIndex > lastIndex) {
result.push({
type: "text",
value: text.slice(lastIndex, startIndex),
})
}

// Add anchor element for URL
result.push({
type: "element",
tagName: "a",
properties: {
href: url,
target: "_blank",
rel: "noopener noreferrer",
},
children: [{ type: "text", value: url }],
})
lastIndex = startIndex + url.length
})

// Add remaining text after last URL if exists
if (lastIndex < text.length) {
result.push({
type: "text",
value: text.slice(lastIndex),
})
}

return result
}

// Function to process a list of nodes
function processNodes(nodes: (Element | Text)[]): (Element | Text)[] {
return nodes.flatMap((node) => {
if (node.type === "text") {
return processTextNode(node)
}
if (node.type === "element") {
return {
...node,
children: processNodes(node.children as (Element | Text)[]),
}
}
return [node]
})
}

export const Citations: QuartzTransformerPlugin<Partial<Options>> = (userOpts) => {
Expand Down Expand Up @@ -48,6 +117,23 @@ export const Citations: QuartzTransformerPlugin<Partial<Options>> = (userOpts) =
}
})

// Format external links correctly
if (opts.prettyLink) {
plugins.push(() => {
return (tree: HtmlRoot, _file) => {
visit(tree, "element", (node) => {
if ((node.properties?.className as string[])?.includes("references")) {
visit(node, "element", (entry) => {
if ((entry.properties?.className as string[])?.includes("csl-entry")) {
entry.children = processNodes(entry.children as (Element | Text)[])
}
})
}
})
}
})
}

return plugins
},
}
Expand Down