Home / Function/ normalizeMarkdown() — supabase Function Reference

normalizeMarkdown() — supabase Function Reference

Architecture documentation for the normalizeMarkdown() function in Reference.utils.ts from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  5f91f1b9_fb79_2b93_7769_dd2c8ebeddec["normalizeMarkdown()"]
  d0714c90_b2eb_810b_c046_3cb9bc737b59["FunctionSection()"]
  d0714c90_b2eb_810b_c046_3cb9bc737b59 -->|calls| 5f91f1b9_fb79_2b93_7769_dd2c8ebeddec
  style 5f91f1b9_fb79_2b93_7769_dd2c8ebeddec fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/docs/features/docs/Reference.utils.ts lines 213–272

export function normalizeMarkdown(markdownUnescaped: string): string {
  /**
   * Need to first escape the braces so that the MDX parser doesn't choke on
   * them. Unlike the MDX parser, the regular Markdown parser handles braces
   * gracefully, so we use it to find the positions of the code blocks, then
   * escape all other braces before the final conversion with the MDX parser.
   */
  const markdownTree = fromMarkdown(markdownUnescaped)

  const codeBlocks = [] as Array<{
    type: string
    start: number
    end: number
  }>
  visit(markdownTree, ['code', 'inlineCode'], (node) => {
    codeBlocks.push({
      type: node.type,
      start: node.position?.start?.offset || 0,
      end: node.position?.end?.offset || 0,
    })
  })
  // Sort code blocks by start offset in descending order
  codeBlocks.sort((a, b) => b.start - a.start)

  let markdown = markdownUnescaped
  let lastIndex = markdown.length

  // Iterate through the sorted code blocks
  for (const block of codeBlocks) {
    // Escape braces in the text between the current code block and the last processed position
    const textBetween = markdown.slice(block.end, lastIndex)
    const escapedTextBetween = textBetween.replace(/(?<!\\)([{}])/g, '\\$1')

    // Replace the original text with the escaped version
    markdown = markdown.slice(0, block.end) + escapedTextBetween + markdown.slice(lastIndex)

    // Update the last processed position
    lastIndex = block.start
  }

  // Escape braces in the remaining text before the first code block
  if (lastIndex > 0) {
    const remainingText = markdown.slice(0, lastIndex)
    const escapedRemainingText = remainingText.replace(/(?<!\\)([{}])/g, '\\$1')
    markdown = escapedRemainingText + markdown.slice(lastIndex)
  }

  const mdxTree = fromMarkdown(markdown, {
    extensions: [mdxjs()],
    mdastExtensions: [mdxFromMarkdown()],
  })
  visit(mdxTree, 'text', (node) => {
    node.value = node.value.replace(/\n/g, ' ')
  })
  const content = toMarkdown(mdxTree, {
    extensions: [mdxToMarkdown()],
  })

  return content
}

Subdomains

Called By

Frequently Asked Questions

What does normalizeMarkdown() do?
normalizeMarkdown() is a function in the supabase codebase.
What calls normalizeMarkdown()?
normalizeMarkdown() is called by 1 function(s): FunctionSection.

Analyze Your Own Codebase

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

Try Supermodel Free