Home / Function/ rewriteNodes() — supabase Function Reference

rewriteNodes() — supabase Function Reference

Architecture documentation for the rewriteNodes() function in CodeSample.ts from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  beb19324_0ee7_c826_cdeb_54c57d864501["rewriteNodes()"]
  c690e42d_3181_4be7_bd9b_5b78597b4ee9["codeSampleRemark()"]
  c690e42d_3181_4be7_bd9b_5b78597b4ee9 -->|calls| beb19324_0ee7_c826_cdeb_54c57d864501
  5ff6f4ae_9c54_d590_6f0e_af17d0a33505["rewriteNodes()"]
  5ff6f4ae_9c54_d590_6f0e_af17d0a33505 -->|calls| beb19324_0ee7_c826_cdeb_54c57d864501
  ae8fc91c_5fec_bf25_4932_abd9034ad167["matchLang()"]
  beb19324_0ee7_c826_cdeb_54c57d864501 -->|calls| ae8fc91c_5fec_bf25_4932_abd9034ad167
  9bbb38ac_608b_efa3_1103_f17a0989c67b["isExternalSource()"]
  beb19324_0ee7_c826_cdeb_54c57d864501 -->|calls| 9bbb38ac_608b_efa3_1103_f17a0989c67b
  4d76fb60_46bd_0571_ceeb_d232b92e476e["redactLines()"]
  beb19324_0ee7_c826_cdeb_54c57d864501 -->|calls| 4d76fb60_46bd_0571_ceeb_d232b92e476e
  790ab825_fd72_cafc_9ee2_d8f5b37c7cf2["getAttributeValue()"]
  beb19324_0ee7_c826_cdeb_54c57d864501 -->|calls| 790ab825_fd72_cafc_9ee2_d8f5b37c7cf2
  0dd79044_556b_5798_10c1_7a6c4304c7a0["createArrayAttributeValueExpression()"]
  beb19324_0ee7_c826_cdeb_54c57d864501 -->|calls| 0dd79044_556b_5798_10c1_7a6c4304c7a0
  style beb19324_0ee7_c826_cdeb_54c57d864501 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/docs/features/directives/CodeSample.ts lines 254–357

async function rewriteNodes(contentMap: Map<MdxJsxFlowElement, [CodeSampleMeta, string]>) {
  for (const [node, [meta, content]] of contentMap) {
    let lang = matchLang(meta.path.split('.').pop() || '')

    const source = isExternalSource(meta)
      ? `https://github.com/${meta.org}/${meta.repo}/blob/${meta.commit}${meta.path}`
      : `https://github.com/supabase/supabase/blob/${process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA ?? 'master'}/examples${meta.path}`

    let processedContent = content
    if (meta.convertToJs) {
      const { code } = amaro.transformSync(content, { mode: 'strip-only' })

      const prettierConfig = await prettier.resolveConfig('./.prettierrc.js')
      processedContent = await prettier.format(code, { ...prettierConfig, parser: 'typescript' })

      // Convert TypeScript/TSX language to JavaScript/JSX when converting types
      assert(
        lang === 'typescript' || lang === 'tsx',
        'Type stripping to JS is only supported for TypeScript and TSX'
      )
      if (lang === 'typescript') {
        lang = 'javascript'
      } else if (lang === 'tsx') {
        lang = 'jsx'
      }
    }

    const elidedContent = redactLines(processedContent, meta.lines, lang, meta.hideElidedLines)

    const replacementContent: MdxJsxFlowElement | Code = meta.codeHikeAncestor
      ? {
          type: 'code',
          lang,
          meta: meta.meta,
          value: elidedContent,
        }
      : {
          type: 'mdxJsxFlowElement',
          name: 'CodeSampleWrapper',
          attributes: [
            {
              type: 'mdxJsxAttribute',
              name: 'source',
              value: source,
            },
          ],
          children: [
            {
              type: 'code',
              lang,
              meta: meta.meta,
              value: elidedContent,
            },
          ],
        }
    meta.parent.children.splice(meta.parent.children.indexOf(node), 1, replacementContent)

    if (meta.codeHikeAncestor && meta.codeHikeAncestorParent) {
      const existingWrapper = meta.codeHikeAncestorParent.children.find(
        (child) =>
          child.type === 'mdxJsxFlowElement' &&
          (child as MdxJsxFlowElement).name === 'CodeSampleWrapper' &&
          (child as MdxJsxFlowElement).children?.[0] === meta.codeHikeAncestor
      ) as MdxJsxFlowElement | undefined
      if (existingWrapper) {
        const existingSource = getAttributeValue(existingWrapper, 'source')
        if (typeof existingSource === 'string' && existingSource !== source) {
          const newSource = createArrayAttributeValueExpression(existingSource, source)
          existingWrapper.attributes[0].value = newSource
        } else if (
          typeof existingSource !== 'string' &&
          existingSource &&
          existingSource.type === 'mdxJsxAttributeValueExpression'
        ) {
          const existingSourceArray =
            // @ts-ignore
            existingSource.data.estree.body[0]?.expression?.elements?.map(
              (element) => element.value
            ) ?? []
          const newSource = createArrayAttributeValueExpression(...existingSourceArray, source)
          existingWrapper.attributes[0].value = newSource
        }
      } else {
        const codeSampleWrapper: MdxJsxFlowElement = {
          type: 'mdxJsxFlowElement',
          name: 'CodeSampleWrapper',
          attributes: [
            {
              type: 'mdxJsxAttribute',
              name: 'source',
              value: source,
            },
          ],
          children: [meta.codeHikeAncestor as BlockContent],
        }
        meta.codeHikeAncestorParent.children.splice(
          meta.codeHikeAncestorParent.children.indexOf(meta.codeHikeAncestor),
          1,
          codeSampleWrapper
        )
      }
    }
  }
}

Subdomains

Frequently Asked Questions

What does rewriteNodes() do?
rewriteNodes() is a function in the supabase codebase.
What does rewriteNodes() call?
rewriteNodes() calls 5 function(s): createArrayAttributeValueExpression, getAttributeValue, isExternalSource, matchLang, redactLines.
What calls rewriteNodes()?
rewriteNodes() is called by 2 function(s): codeSampleRemark, rewriteNodes.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free