@airalogy/aimd-editor 1.11.0 → 1.12.0

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.
@@ -1,18 +1,45 @@
1
1
  import { parseMediaContent, parseVarDefinition, validateMediaDefinition, validateVarDefinition } from '@airalogy/aimd-core/parser'
2
+ import {
3
+ normalizeAimdFigureIds,
4
+ scanAimdFigureBlocks,
5
+ type AimdTextEdit,
6
+ } from '../authoring'
2
7
 
3
8
  export type AimdDiagnosticSeverity = 'warning' | 'error'
4
9
 
5
10
  export interface AimdDiagnostic {
11
+ code?: string
6
12
  message: string
7
13
  severity: AimdDiagnosticSeverity
8
14
  startOffset: number
9
15
  endOffset: number
16
+ quickFix?: {
17
+ title: string
18
+ edit: AimdTextEdit
19
+ }
20
+ }
21
+
22
+ export interface AimdDiagnosticMessages {
23
+ figureMissingId: string
24
+ figureMissingSrc: string
25
+ figureDuplicateId: (id: string) => string
26
+ generateFigureId: string
10
27
  }
11
28
 
12
29
  const AIMD_VAR_TEMPLATE_PATTERN = /\{\{\s*(var_table|var)\s*\|([\s\S]*?)\}\}/g
13
30
  const AIMD_MEDIA_BLOCK_PATTERN = /^[ \t]*(```|~~~)[ \t]*media(?:[^\n]*)?\r?\n([\s\S]*?)^[ \t]*\1[ \t]*$/gm
14
31
 
15
- export function collectAimdDiagnostics(content: string): AimdDiagnostic[] {
32
+ const DEFAULT_DIAGNOSTIC_MESSAGES: AimdDiagnosticMessages = {
33
+ figureMissingId: 'This figure is missing the required ID. Generate or enter a unique ID.',
34
+ figureMissingSrc: 'This figure is missing the required src field.',
35
+ figureDuplicateId: id => `Figure ID "${id}" is already used in this document.`,
36
+ generateFigureId: 'Generate figure ID',
37
+ }
38
+
39
+ export function collectAimdDiagnostics(
40
+ content: string,
41
+ messages: AimdDiagnosticMessages = DEFAULT_DIAGNOSTIC_MESSAGES,
42
+ ): AimdDiagnostic[] {
16
43
  const diagnostics: AimdDiagnostic[] = []
17
44
 
18
45
  for (const match of content.matchAll(AIMD_VAR_TEMPLATE_PATTERN)) {
@@ -57,5 +84,58 @@ export function collectAimdDiagnostics(content: string): AimdDiagnostic[] {
57
84
  }
58
85
  }
59
86
 
87
+ const figures = scanAimdFigureBlocks(content)
88
+ const generatedByBlock = new Map(
89
+ normalizeAimdFigureIds(content).generated.map(item => [item.blockStartOffset, item]),
90
+ )
91
+ const seenFigureIds = new Set<string>()
92
+
93
+ for (const figure of figures) {
94
+ const id = figure.id?.value ?? ''
95
+ const src = figure.src?.value ?? ''
96
+
97
+ if (!id) {
98
+ const generated = generatedByBlock.get(figure.startOffset)
99
+ diagnostics.push({
100
+ code: 'fig-missing-id',
101
+ message: messages.figureMissingId,
102
+ severity: 'error',
103
+ startOffset: figure.startOffset,
104
+ endOffset: figure.endOffset,
105
+ ...(generated
106
+ ? {
107
+ quickFix: {
108
+ title: messages.generateFigureId,
109
+ edit: generated.edit,
110
+ },
111
+ }
112
+ : {}),
113
+ })
114
+ }
115
+
116
+ if (!src) {
117
+ diagnostics.push({
118
+ code: 'fig-missing-src',
119
+ message: messages.figureMissingSrc,
120
+ severity: 'error',
121
+ startOffset: figure.startOffset,
122
+ endOffset: figure.endOffset,
123
+ })
124
+ }
125
+
126
+ if (id) {
127
+ if (seenFigureIds.has(id)) {
128
+ diagnostics.push({
129
+ code: 'fig-duplicate-id',
130
+ message: messages.figureDuplicateId(id),
131
+ severity: 'error',
132
+ startOffset: figure.startOffset,
133
+ endOffset: figure.endOffset,
134
+ })
135
+ }
136
+ seenFigureIds.add(id)
137
+ }
138
+ }
139
+
60
140
  return diagnostics
61
141
  }
@@ -207,6 +207,12 @@ export interface AimdEditorMessages {
207
207
  quizBlockLabel: string
208
208
  quizBlock: string
209
209
  }
210
+ diagnostics: {
211
+ figureMissingId: string
212
+ figureMissingSrc: string
213
+ figureDuplicateId: (id: string) => string
214
+ generateFigureId: string
215
+ }
210
216
  }
211
217
 
212
218
  export type AimdEditorMessagesInput = DeepPartial<AimdEditorMessages>
@@ -422,6 +428,12 @@ const EN_US_MESSAGES: AimdEditorMessages = {
422
428
  quizBlockLabel: 'quiz block',
423
429
  quizBlock: 'Insert AIMD quiz code block',
424
430
  },
431
+ diagnostics: {
432
+ figureMissingId: 'This figure is missing the required ID. Generate or enter a unique ID.',
433
+ figureMissingSrc: 'This figure is missing the required src field.',
434
+ figureDuplicateId: id => `Figure ID "${id}" is already used in this document.`,
435
+ generateFigureId: 'Generate figure ID',
436
+ },
425
437
  }
426
438
 
427
439
  const ZH_CN_MESSAGES: AimdEditorMessages = {
@@ -619,6 +631,12 @@ const ZH_CN_MESSAGES: AimdEditorMessages = {
619
631
  quizBlockLabel: '题目代码块',
620
632
  quizBlock: '插入 AIMD 题目代码块',
621
633
  },
634
+ diagnostics: {
635
+ figureMissingId: '该图片缺少必填的 ID,请生成或填写一个唯一 ID。',
636
+ figureMissingSrc: '该图片缺少必填的 src 字段。',
637
+ figureDuplicateId: id => `图片 ID“${id}”在当前文档中已存在。`,
638
+ generateFigureId: '生成图片 ID',
639
+ },
622
640
  }
623
641
 
624
642
  const BASE_MESSAGES: Record<AimdEditorLocale, AimdEditorMessages> = {