mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
refactor(github): organize concrete feature owners (#41601)
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import type { Blockquote, Nodes } from 'mdast'
|
||||
|
||||
const GITHUB_CALLOUT_KINDS = ['note', 'tip', 'important', 'warning', 'caution'] as const
|
||||
export type GitHubCalloutKind = (typeof GITHUB_CALLOUT_KINDS)[number]
|
||||
|
||||
// rehype-sanitize allowlist entry for <blockquote>; without it the attribute is stripped.
|
||||
export const GITHUB_CALLOUT_SANITIZE_ATTRIBUTE: [string, ...string[]] = [
|
||||
'dataCallout',
|
||||
...GITHUB_CALLOUT_KINDS
|
||||
]
|
||||
|
||||
// GitHub only reads a marker that has its line to itself: `> [!NOTE] aside` stays a quote.
|
||||
const GITHUB_CALLOUT_MARKER = /^\[!([a-z]+)\](?:\r?\n|$)/i
|
||||
|
||||
export function readGitHubCalloutKind(value: unknown): GitHubCalloutKind | null {
|
||||
return GITHUB_CALLOUT_KINDS.find((kind) => kind === value) ?? null
|
||||
}
|
||||
|
||||
/** Strips a callout marker from a blockquote and returns its kind, or null when it has none. */
|
||||
function takeGitHubCalloutMarker(node: Blockquote): GitHubCalloutKind | null {
|
||||
const paragraph = node.children[0]
|
||||
if (paragraph?.type !== 'paragraph') {
|
||||
return null
|
||||
}
|
||||
const text = paragraph.children[0]
|
||||
if (text?.type !== 'text') {
|
||||
return null
|
||||
}
|
||||
const match = GITHUB_CALLOUT_MARKER.exec(text.value)
|
||||
const kind = readGitHubCalloutKind(match?.[1]?.toLowerCase())
|
||||
if (!match || !kind) {
|
||||
return null
|
||||
}
|
||||
|
||||
const remainder = text.value.slice(match[0].length)
|
||||
if (remainder) {
|
||||
text.value = remainder
|
||||
} else if (match[0].endsWith('\n') || paragraph.children.length === 1) {
|
||||
paragraph.children.shift()
|
||||
if (paragraph.children.length === 0) {
|
||||
node.children.shift()
|
||||
}
|
||||
} else {
|
||||
// `[!NOTE]*aside*`: an inline sibling shares the marker's line.
|
||||
return null
|
||||
}
|
||||
return kind
|
||||
}
|
||||
|
||||
/** Each GitHub alert (`> [!NOTE]`) in the tree, its marker removed. Desktop and mobile mark them.
|
||||
* Why: must run before remark-breaks, which splits the marker's newline into a break node. */
|
||||
export function forEachGitHubCallout(
|
||||
node: Nodes,
|
||||
mark: (quote: Blockquote, kind: GitHubCalloutKind) => void
|
||||
): void {
|
||||
if ('children' in node) {
|
||||
node.children.forEach((child) => forEachGitHubCallout(child, mark))
|
||||
}
|
||||
const kind = node.type === 'blockquote' ? takeGitHubCalloutMarker(node) : null
|
||||
if (kind && node.type === 'blockquote') {
|
||||
mark(node, kind)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import type { Repo } from '../../repo-types'
|
||||
import {
|
||||
githubHostFromIdentityKey,
|
||||
repoUpstreamIdentityKey
|
||||
} from '../repositories/repository-identity-key'
|
||||
|
||||
export type GitHubRepoReference = {
|
||||
owner: string
|
||||
repo: string
|
||||
}
|
||||
|
||||
type MarkdownNode = {
|
||||
type: string
|
||||
value?: string
|
||||
url?: string
|
||||
title?: string | null
|
||||
children?: MarkdownNode[]
|
||||
}
|
||||
|
||||
const GITHUB_REFERENCE_PATTERN = /(?:\b([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+))?#([1-9][0-9]*)\b/g
|
||||
|
||||
/** Where bare `#123` points: upstream unless the repo prefers origin. github.com only. */
|
||||
export function githubReferenceRepo(
|
||||
repo: Pick<Repo, 'upstream' | 'issueSourcePreference'>,
|
||||
originIdentityKey: string
|
||||
): GitHubRepoReference | null {
|
||||
const upstreamKey = repoUpstreamIdentityKey(repo, originIdentityKey)
|
||||
const key =
|
||||
upstreamKey && repo.issueSourcePreference !== 'origin' ? upstreamKey : originIdentityKey
|
||||
if (githubHostFromIdentityKey(key)) {
|
||||
return null
|
||||
}
|
||||
const [owner, name] = key.split('/')
|
||||
return owner && name ? { owner, repo: name } : null
|
||||
}
|
||||
|
||||
function createGitHubIssueUrl(owner: string, repo: string, number: string): string {
|
||||
return `https://github.com/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/issues/${number}`
|
||||
}
|
||||
|
||||
function isEmbeddedGitHubReference(value: string, index: number): boolean {
|
||||
if (index === 0) {
|
||||
return false
|
||||
}
|
||||
return /[A-Za-z0-9_./-]/.test(value[index - 1] ?? '')
|
||||
}
|
||||
|
||||
function splitGitHubReferenceText(value: string, defaultRepo: GitHubRepoReference): MarkdownNode[] {
|
||||
const parts: MarkdownNode[] = []
|
||||
let cursor = 0
|
||||
|
||||
for (const match of value.matchAll(GITHUB_REFERENCE_PATTERN)) {
|
||||
const label = match[0]
|
||||
const index = match.index ?? 0
|
||||
if (isEmbeddedGitHubReference(value, index)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const owner = match[1] ?? defaultRepo.owner
|
||||
const repo = match[2] ?? defaultRepo.repo
|
||||
const number = match[3]
|
||||
if (!number) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (index > cursor) {
|
||||
parts.push({ type: 'text', value: value.slice(cursor, index) })
|
||||
}
|
||||
parts.push({
|
||||
type: 'link',
|
||||
url: createGitHubIssueUrl(owner, repo, number),
|
||||
title: null,
|
||||
children: [{ type: 'text', value: label }]
|
||||
})
|
||||
cursor = index + label.length
|
||||
}
|
||||
|
||||
if (cursor === 0) {
|
||||
return [{ type: 'text', value }]
|
||||
}
|
||||
if (cursor < value.length) {
|
||||
parts.push({ type: 'text', value: value.slice(cursor) })
|
||||
}
|
||||
return parts
|
||||
}
|
||||
|
||||
/** Turns `#123` and `owner/repo#123` in an mdast tree's text into issue links, in place. */
|
||||
export function linkGitHubReferences(node: MarkdownNode, defaultRepo: GitHubRepoReference): void {
|
||||
if (!node.children || node.type === 'link' || node.type === 'image') {
|
||||
return
|
||||
}
|
||||
|
||||
const nextChildren: MarkdownNode[] = []
|
||||
for (const child of node.children) {
|
||||
if (child.type === 'text' && child.value !== undefined) {
|
||||
// Agent replies can contain thousands of issue refs; append iteratively to avoid V8’s argument limit.
|
||||
for (const part of splitGitHubReferenceText(child.value, defaultRepo)) {
|
||||
nextChildren.push(part)
|
||||
}
|
||||
} else {
|
||||
linkGitHubReferences(child, defaultRepo)
|
||||
nextChildren.push(child)
|
||||
}
|
||||
}
|
||||
|
||||
node.children = nextChildren
|
||||
}
|
||||
Reference in New Issue
Block a user