heading linking

This commit is contained in:
Jacky Zhao 2023-06-01 19:48:38 -04:00
parent 04eeb2d10c
commit fcd81353f8
7 changed files with 104 additions and 11 deletions
quartz
components
path.ts
plugins/transformers
styles

View file

@ -26,7 +26,6 @@ export default function({ title, description, slug, externalResources }: HeadPro
<link rel="icon" href={iconPath} />
<meta name="description" content={description} />
<meta name="generator" content="Quartz" />
<base href={slug} />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link rel="stylesheet" type="text/css" href={stylePath} />

View file

@ -1,4 +1,7 @@
import path from 'path'
import SlugAnchor from 'github-slugger'
const slugAnchor = new SlugAnchor()
function slugSegment(s: string): string {
return s.replace(/\s/g, '-')
@ -6,7 +9,7 @@ function slugSegment(s: string): string {
export function slugify(s: string): string {
const [fp, anchor] = s.split("#", 2)
const sluggedAnchor = anchor === undefined ? "" : "#" + slugSegment(anchor)
const sluggedAnchor = anchor === undefined ? "" : "#" + slugAnchor.slug(anchor)
const withoutFileExt = fp.replace(new RegExp(path.extname(fp) + '$'), '')
const rawSlugSegments = withoutFileExt.split(path.sep)
const slugParts: string = rawSlugSegments

View file

@ -2,13 +2,17 @@ import { PluggableList } from "unified"
import remarkGfm from "remark-gfm"
import smartypants from 'remark-smartypants'
import { QuartzTransformerPlugin } from "../types"
import rehypeSlug from "rehype-slug"
import rehypeAutolinkHeadings from "rehype-autolink-headings/lib"
export interface Options {
enableSmartyPants: boolean
linkHeadings: boolean
}
const defaultOptions: Options = {
enableSmartyPants: true
enableSmartyPants: true,
linkHeadings: true
}
export class GitHubFlavoredMarkdown extends QuartzTransformerPlugin {
@ -25,6 +29,13 @@ export class GitHubFlavoredMarkdown extends QuartzTransformerPlugin {
}
htmlPlugins(): PluggableList {
return []
return this.opts.linkHeadings
? [rehypeSlug, [rehypeAutolinkHeadings, {
behavior: 'append', content: {
type: 'text',
value: '§'
}
}]]
: []
}
}

View file

@ -42,9 +42,9 @@ export class ObsidianFlavoredMarkdown extends QuartzTransformerPlugin {
} else {
const [path, rawHeader, rawAlias] = capture
const header = rawHeader?.slice(1).trim() ?? ""
const anchor = rawHeader?.slice(1).trim() ?? ""
const alias = rawAlias?.slice(1).trim() ?? path
const url = slugify(path.trim() + header)
const url = slugify(path.trim() + anchor)
return {
type: 'link',
url,

View file

@ -85,12 +85,24 @@ thead {
font-weight: revert;
margin: 2rem 0 0;
&:hover > .hanchor {
color: var(--secondary);
}
article > & > a {
color: var(--dark)
color: var(--dark);
&.internal {
background-color: transparent;
}
}
}
h1, h2, h3, h4, h5, h6 {
&[id] > a {
margin: 0 0.5rem;
opacity: 0;
transition: opacity 0.2s ease;
font-family: var(--codeFont);
user-select: none;
}
&[id]:hover > a {
opacity: 1;
}
}