Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.2 KB
Raw Blame
export type MarkdownInlineMatch = { 0: string; index: number; end: number }

/** Merge a global non-link regex with links; search starts must advance between calls. */
export function createMarkdownInlineMatcher(
  text: string,
  nonLinkPattern: RegExp
): { lastIndex: number; exec: () => MarkdownInlineMatch | null } {
  let nextOther: MarkdownInlineMatch | null | undefined
  let nextLink: MarkdownInlineMatch | null | undefined
  let labelEnd = -1
  let destinationEnd = -1
  let noMoreLabels = false
  let noMoreDestinations = false

  function findLink(from: number): MarkdownInlineMatch | null {
    if (noMoreLabels || noMoreDestinations) {
      return null
    }
    let open = text.indexOf('[', from)
    while (open !== -1) {
      if (labelEnd < open + 1) {
        labelEnd = text.indexOf(']', open + 1)
      }
      if (labelEnd === -1) {
        noMoreLabels = true
        return null
      }
      if (labelEnd > open + 1 && text[labelEnd + 1] === '(') {
        if (destinationEnd < labelEnd + 2) {
          destinationEnd = text.indexOf(')', labelEnd + 2)
        }
        if (destinationEnd === -1) {
          noMoreDestinations = true
          return null
        }
        if (destinationEnd > labelEnd + 2) {
          return { 0: text.slice(open, destinationEnd + 1), index: open, end: destinationEnd + 1 }
        }
      }
      // Every opener before this closing bracket shares the same invalid suffix.
      open = text.indexOf('[', labelEnd + 1)
    }
    return null
  }

  const matcher = {
    lastIndex: 0,
    exec(): MarkdownInlineMatch | null {
      const from = matcher.lastIndex
      if (nextOther === undefined || (nextOther !== null && nextOther.index < from)) {
        nonLinkPattern.lastIndex = from
        const match = nonLinkPattern.exec(text)
        nextOther = match
          ? { 0: match[0], index: match.index, end: nonLinkPattern.lastIndex }
          : null
      }
      if (nextLink === undefined || (nextLink !== null && nextLink.index < from)) {
        nextLink = findLink(from)
      }
      const match =
        nextLink && (!nextOther || nextLink.index < nextOther.index) ? nextLink : nextOther
      if (match) {
        matcher.lastIndex = match.end
      }
      return match
    }
  }
  return matcher
}