Skip to content

Very simple find-and-replace remark plugin enters a recursive loop #1201

Answered by remcohaszing
TheOnlyTails asked this question in Q&A
Discussion options

You must be logged in to vote

You are trying to modify the HTML output, not markdown. This can be done better as a rehype plugin. It looks something like below. The main difference is that this adds the style to the generated <code> node instead of wrapping it in a <span>.

import { visit } from 'unist-util-visit'

export default function rehypeColorCode({ cssVariable }) {
  function transformer(ast) {
    visit(ast, 'element', (node) => {
      if (node.tagName !== 'code') {
        return
      }

      if (node.children.length !== 1) {
        return
      }

      const child = node.children[0]
      if (child.type !== 'text') {
        return
      }

      const match = child.value.match(colorRegex)?.[0]
      if (!

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
5 replies
@TheOnlyTails
Comment options

@wooorm
Comment options

@TheOnlyTails
Comment options

@wooorm
Comment options

@remcohaszing
Comment options

Answer selected by TheOnlyTails
Comment options

You must be logged in to vote
1 reply
@wooorm
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants