Home / Function/ fetchSourceCodeContent() — supabase Function Reference

fetchSourceCodeContent() — supabase Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  4bf39249_b505_f8a8_4985_5d5c1fd4d45b["fetchSourceCodeContent()"]
  c690e42d_3181_4be7_bd9b_5b78597b4ee9["codeSampleRemark()"]
  c690e42d_3181_4be7_bd9b_5b78597b4ee9 -->|calls| 4bf39249_b505_f8a8_4985_5d5c1fd4d45b
  9ff1763e_9377_474c_84fd_ebadabfa99e4["getAttributeValueExpression()"]
  4bf39249_b505_f8a8_4985_5d5c1fd4d45b -->|calls| 9ff1763e_9377_474c_84fd_ebadabfa99e4
  790ab825_fd72_cafc_9ee2_d8f5b37c7cf2["getAttributeValue()"]
  4bf39249_b505_f8a8_4985_5d5c1fd4d45b -->|calls| 790ab825_fd72_cafc_9ee2_d8f5b37c7cf2
  6ee90c34_7a08_40d3_74aa_eb45730bfecf["fetchWithNextOptions()"]
  4bf39249_b505_f8a8_4985_5d5c1fd4d45b -->|calls| 6ee90c34_7a08_40d3_74aa_eb45730bfecf
  style 4bf39249_b505_f8a8_4985_5d5c1fd4d45b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/docs/features/directives/CodeSample.ts lines 136–252

async function fetchSourceCodeContent(tree: Root, deps: Dependencies) {
  const codeSampleNodes = [] as MdxJsxFlowElement[]
  const metadata = [] as CodeSampleMeta[]
  const pendingFetches = [] as Promise<string>[]

  visitParents(tree, 'mdxJsxFlowElement', (node: MdxJsxFlowElement, ancestors) => {
    if (node.name !== '$CodeSample') return

    const codeHikeAncestorIndex = ancestors.findLastIndex(
      (ancestor) => ancestor.type === 'mdxJsxFlowElement' && ancestor.name === 'CH.Code'
    )
    const codeHikeAncestor = codeHikeAncestorIndex === -1 ? null : ancestors[codeHikeAncestorIndex]
    const codeHikeAncestorParent =
      codeHikeAncestorIndex <= 0 ? null : ancestors[codeHikeAncestorIndex - 1]
    const parent = ancestors[ancestors.length - 1]

    const isExternal = getAttributeValueExpression(getAttributeValue(node, 'external')) === 'true'

    if (isExternal) {
      if (!IS_PLATFORM) {
        node.name = 'CodeSampleDummy'
        node.attributes = []
        return
      }

      const org = getAttributeValue(node, 'org')
      const repo = getAttributeValue(node, 'repo')
      const commit = getAttributeValue(node, 'commit')
      const path = getAttributeValue(node, 'path')
      const lines = getAttributeValueExpression(getAttributeValue(node, 'lines'))
      const meta = getAttributeValue(node, 'meta')
      const hideElidedLines = getAttributeValueExpression(
        getAttributeValue(node, 'hideElidedLines')
      )
      const convertToJs = getAttributeValueExpression(getAttributeValue(node, 'convertToJs'))

      const result = codeSampleExternalSchema.safeParse({
        external: isExternal,
        org,
        repo,
        commit,
        path,
        lines,
        meta,
        hideElidedLines,
        convertToJs,
      })

      if (!result.success) {
        throw new Error(
          `Invalid $CodeSample directive: ${(result as SafeParseError<ICodeSampleExternal>).error.message}`
        )
      }

      const fetchTask = deps.fetchFromGitHub({
        org: result.data.org,
        repo: result.data.repo,
        path: result.data.path,
        branch: result.data.commit,
        options: {
          onError: (error: unknown) => {
            throw Error(
              `Failed to fetch code sample from ${org}/${repo}@${commit} at path ${path}: ${error}`
            )
          },
          fetch: fetchWithNextOptions({ cache: 'force-cache' }),
        },
      })

      codeSampleNodes.push(node)
      metadata.push({ ...result.data, parent, codeHikeAncestor, codeHikeAncestorParent })
      pendingFetches.push(fetchTask)
    } else {
      const path = getAttributeValue(node, 'path')
      const lines = getAttributeValueExpression(getAttributeValue(node, 'lines'))
      const meta = getAttributeValue(node, 'meta')
      const hideElidedLines = getAttributeValueExpression(
        getAttributeValue(node, 'hideElidedLines')
      )
      const convertToJs = getAttributeValueExpression(getAttributeValue(node, 'convertToJs'))

      const result = codeSampleInternalSchema.safeParse({
        external: isExternal,
        path,
        lines,
        meta,
        hideElidedLines,
        convertToJs,
      })

      if (!result.success) {
        throw new Error(
          `Invalid $CodeSample directive: ${(result as SafeParseError<ICodeSampleInternal>).error.message}`
        )
      }

      const filePath = join(EXAMPLES_DIRECTORY, result.data.path)
      if (!filePath.startsWith(EXAMPLES_DIRECTORY)) {
        throw new Error(`Invalid $CodeSample settings: Path must be inside ${EXAMPLES_DIRECTORY}`)
      }
      const fetchTask = readFile(filePath, 'utf-8')

      codeSampleNodes.push(node)
      metadata.push({ ...result.data, parent, codeHikeAncestor, codeHikeAncestorParent })
      pendingFetches.push(fetchTask)
    }
  })

  const resolvedContent = await Promise.all(pendingFetches)

  const nodeContentMap = new Map<MdxJsxFlowElement, [CodeSampleMeta, string]>()
  codeSampleNodes.forEach((node, index) => {
    nodeContentMap.set(node, [metadata[index], resolvedContent[index]])
  })

  return nodeContentMap
}

Subdomains

Called By

Frequently Asked Questions

What does fetchSourceCodeContent() do?
fetchSourceCodeContent() is a function in the supabase codebase.
What does fetchSourceCodeContent() call?
fetchSourceCodeContent() calls 3 function(s): fetchWithNextOptions, getAttributeValue, getAttributeValueExpression.
What calls fetchSourceCodeContent()?
fetchSourceCodeContent() is called by 1 function(s): codeSampleRemark.

Analyze Your Own Codebase

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

Try Supermodel Free