feat: add heading anchor links

This commit is contained in:
radishzzz 2025-05-13 03:09:23 +01:00
parent 328104ed1a
commit e5ecbe1341
8 changed files with 117 additions and 29 deletions

View file

@ -3,8 +3,10 @@ import partytown from '@astrojs/partytown'
import sitemap from '@astrojs/sitemap'
import robotsTxt from 'astro-robots-txt'
import { defineConfig } from 'astro/config'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeExternalLinks from 'rehype-external-links'
import rehypeKatex from 'rehype-katex'
import rehypeSlug from 'rehype-slug'
import remarkDirective from 'remark-directive'
import remarkMath from 'remark-math'
import UnoCSS from 'unocss/astro'
@ -19,15 +21,10 @@ const url = themeConfig.site.url
const locale = themeConfig.global.locale
const linkPrefetch = themeConfig.preload.linkPrefetch
const imageHostURL = themeConfig.preload.imageHostURL
// Configure domains and remotePatterns to optimize remote images in Markdown files using ![alt](src) syntax
// Docs: https://docs.astro.build/en/guides/images/#authorizing-remote-images
const imageConfig = imageHostURL
? {
// Configure domains and remotePatterns to optimize remote images in Markdown files using ![alt](src) syntax
// Docs: https://docs.astro.build/en/guides/images/#authorizing-remote-images
image: {
domains: [imageHostURL],
remotePatterns: [{ protocol: 'https' }],
},
}
? { image: { domains: [imageHostURL], remotePatterns: [{ protocol: 'https' }] } }
: {}
export default defineConfig({
@ -69,7 +66,44 @@ export default defineConfig({
],
rehypePlugins: [
rehypeKatex,
rehypeSlug,
rehypeImgToFigure,
[
rehypeAutolinkHeadings,
{
behavior: 'append',
test: ['h1', 'h2', 'h3', 'h4'],
content: {
type: 'element',
tagName: 'svg',
properties: {
'viewBox': '0 0 24 24',
'aria-hidden': 'true',
'fill': 'currentColor',
},
children: [
{
type: 'element',
tagName: 'path',
properties: {
d: 'm7.374 15.182 7.85-7.849 1.413 1.414-7.848 7.85z',
},
},
{
type: 'element',
tagName: 'path',
properties: {
d: 'M10.6 20c-1.8 1.8-4.7 1.8-6.5 0-.9-.9-1.4-2-1.4-3.3s.5-2.4 1.4-3.3l3.8-3.8-1.4-1.4-4.2 4.2c-2.5 2.5-2.5 6.7 0 9.3 1.3 1.3 3 1.9 4.6 1.9s3.4-.6 4.6-1.9l4.2-4.2-1.4-1.4-3.8 3.8ZM21.7 2.3C20.5 1.1 18.8.4 17.1.4s-3.4.7-4.6 1.9L8.3 6.5l1.4 1.4 3.8-3.8c1.8-1.8 4.7-1.8 6.5 0 .9.9 1.4 2 1.4 3.3s-.5 2.4-1.4 3.3l-3.8 3.8 1.4 1.4 4.2-4.2c1.2-1.2 1.9-2.9 1.9-4.6s-.7-3.4-1.9-4.6Z',
},
},
],
},
properties: {
className: ['heading-anchor-link'],
ariaLabel: 'Link to this section',
},
},
],
[
rehypeExternalLinks,
{