dsh-spec-collab 0.1.6 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ import MarkdownIt from 'markdown-it'
2
+ import type { ReviewItem } from '../protocol.ts'
3
+
4
+ const parser = new MarkdownIt({ html: false })
5
+ export const reviewBlockSelector = 'h1, h2, h3, h4, p, li, td, th, blockquote'
6
+
7
+ export interface ReviewSourceSnippet {
8
+ key: string
9
+ label: string
10
+ markdown: string
11
+ line?: number
12
+ }
13
+
14
+ export interface ReviewAnchorTerm {
15
+ kind: 'quote' | 'acceptance' | 'section'
16
+ label: string
17
+ value: string
18
+ }
19
+
20
+ export function normalizeReviewText(value: string): string {
21
+ return value.replace(/[`*_>#\[\]()~-]/g, '').replace(/\s+/g, '').toLocaleLowerCase()
22
+ }
23
+
24
+ export function reviewAnchorTerms(item: ReviewItem): ReviewAnchorTerm[] {
25
+ const terms: ReviewAnchorTerm[] = []
26
+ for (const anchor of item.sourceAnchors ?? []) {
27
+ if (anchor.quote.trim()) terms.push({ kind: 'quote', label: anchor.heading?.trim() || '正文引用', value: anchor.quote.trim() })
28
+ }
29
+ for (const id of item.affectedAcceptanceIds) if (id.trim()) terms.push({ kind: 'acceptance', label: id.trim(), value: id.trim() })
30
+ for (const section of item.affectedSections) if (section.trim()) terms.push({ kind: 'section', label: section.trim(), value: section.trim() })
31
+ return terms
32
+ }
33
+
34
+ export function reviewTargetElements(root: HTMLElement, item: ReviewItem): HTMLElement[] {
35
+ const blocks = Array.from(root.querySelectorAll<HTMLElement>(reviewBlockSelector))
36
+ const targets: HTMLElement[] = []
37
+ for (const term of reviewAnchorTerms(item)) {
38
+ const candidates = (term.kind === 'section' ? blocks.filter(block => /^H[1-4]$/.test(block.tagName)) : blocks)
39
+ .filter(block => normalizeReviewText(block.textContent ?? '').includes(normalizeReviewText(term.value)))
40
+ .sort((left, right) => (left.textContent?.length ?? 0) - (right.textContent?.length ?? 0))
41
+ const target = candidates[0]
42
+ if (target && !targets.includes(target)) targets.push(target)
43
+ }
44
+ return targets
45
+ }
46
+
47
+ export function reviewSourceSnippets(markdown: string, item: ReviewItem): ReviewSourceSnippet[] {
48
+ const lines = markdown.split('\n')
49
+ const tokens = parser.parse(markdown, {})
50
+ const snippets: ReviewSourceSnippet[] = []
51
+ const seen = new Set<string>()
52
+ const add = (snippet: ReviewSourceSnippet): void => {
53
+ const key = normalizeReviewText(snippet.markdown)
54
+ if (!key || seen.has(key) || snippets.length >= 4) return
55
+ seen.add(key)
56
+ snippets.push(snippet)
57
+ }
58
+
59
+ for (const [index, anchor] of (item.sourceAnchors ?? []).entries()) {
60
+ const quote = anchor.quote.trim()
61
+ if (!quote || !normalizeReviewText(markdown).includes(normalizeReviewText(quote))) continue
62
+ const exactLine = lines.findIndex(line => normalizeReviewText(line).includes(normalizeReviewText(quote)))
63
+ add({ key: `quote-${index}`, label: anchor.heading?.trim() || '正文引用', markdown: quote, ...(exactLine < 0 ? {} : { line: exactLine + 1 }) })
64
+ }
65
+
66
+ for (const acceptanceId of item.affectedAcceptanceIds) {
67
+ const token = tokens.find(candidate => candidate.type === 'inline' && candidate.map && normalizeReviewText(candidate.content).includes(normalizeReviewText(acceptanceId)))
68
+ if (!token?.map) continue
69
+ const [start, end] = token.map
70
+ add({ key: `acceptance-${acceptanceId}`, label: acceptanceId, markdown: lines.slice(start, end).join('\n').trim(), line: start + 1 })
71
+ }
72
+
73
+ if (snippets.length === 0) {
74
+ for (const section of item.affectedSections) {
75
+ const headingIndex = tokens.findIndex((token, index) => token.type === 'heading_open' && tokens[index + 1]?.type === 'inline' && normalizeReviewText(tokens[index + 1]!.content).includes(normalizeReviewText(section)))
76
+ const heading = tokens[headingIndex]
77
+ if (headingIndex < 0 || !heading?.map) continue
78
+ const level = Number(heading.tag.slice(1))
79
+ const nextHeading = tokens.slice(headingIndex + 1).find(token => token.type === 'heading_open' && Number(token.tag.slice(1)) <= level && token.map)
80
+ const start = heading.map[0]
81
+ const end = Math.min(nextHeading?.map?.[0] ?? lines.length, start + 9)
82
+ add({ key: `section-${section}`, label: section, markdown: lines.slice(start, end).join('\n').trim(), line: start + 1 })
83
+ }
84
+ }
85
+
86
+ return snippets
87
+ }