CWorld Site

Back

本来 rehype-autolink-headings 是一个 @unified 系列的 rehype 插件,用于给标题添加链接锚点。但是咱也不知道为啥,就这么一个简单的添加链接的插件,却塞了一堆乱七八糟的东西和依赖,所以就…重写了。

bundlejs

其他用法应该都是兼容旧库的。就这样吧。

cworld1 / rehype-autolink-headings-simple

Waiting for api.github.com...

???
???
???
?????

Detail#

A simple version of rehype-autolink-headings.

Less dependencies and less useless code. Using typescript to develop.

Usage#

npm install rehype-autolink-headings-simple
bash
import { rehypeAutolinkHeadings } from 'rehype-autolink-headings-simple'
import { parse } from 'rehype-parse'
import { stringify } from 'rehype-stringify'
import { unified } from 'unified'

const processor = unified()
  .use(parse)
  .use(rehypeAutolinkHeadings, {
    behavior: 'append',
    properties: {
      behavior: 'append',
      properties: { className: ['anchor'] },
      content: { type: 'text', value: '#' }
    }
  })
  .use(stringify)

const result = processor.processSync('<h1>Heading</h1>').toString()
js

Known issues#

Astro should change render order manually. See Astro: Heading IDs and plugins

License#

The MIT License (MIT).

rehype-autolink-headings Simple Version
https://cworld0.com/blog/rehype-autolink-headings
Author CWorld
Published at December 3, 2024
Comment seems to stuck. Try to refresh?✨