kordoc 1.2.0 → 1.4.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.
package/dist/index.d.cts CHANGED
@@ -1,3 +1,61 @@
1
+ /** kordoc 공통 타입 정의 */
2
+ interface CellContext {
3
+ text: string;
4
+ colSpan: number;
5
+ rowSpan: number;
6
+ }
7
+ interface IRBlock {
8
+ type: "paragraph" | "table";
9
+ text?: string;
10
+ table?: IRTable;
11
+ }
12
+ interface IRTable {
13
+ rows: number;
14
+ cols: number;
15
+ cells: IRCell[][];
16
+ hasHeader: boolean;
17
+ }
18
+ interface IRCell {
19
+ text: string;
20
+ colSpan: number;
21
+ rowSpan: number;
22
+ }
23
+ /** 문서 메타데이터 — 각 포맷에서 추출 가능한 필드만 채워짐 */
24
+ interface DocumentMetadata {
25
+ /** 문서 제목 */
26
+ title?: string;
27
+ /** 작성자 */
28
+ author?: string;
29
+ /** 작성 프로그램 (예: "한글 2020", "Adobe Acrobat") */
30
+ creator?: string;
31
+ /** 생성일시 (ISO 8601) */
32
+ createdAt?: string;
33
+ /** 수정일시 (ISO 8601) */
34
+ modifiedAt?: string;
35
+ /** 페이지/섹션 수 */
36
+ pageCount?: number;
37
+ /** 문서 포맷 버전 (예: HWP "5.1.0.1") */
38
+ version?: string;
39
+ /** 설명 */
40
+ description?: string;
41
+ /** 키워드 */
42
+ keywords?: string[];
43
+ }
44
+ /** 파싱 옵션 — parse() 함수에 전달 */
45
+ interface ParseOptions {
46
+ /**
47
+ * 파싱할 페이지/섹션 범위 (1-based).
48
+ * - 배열: [1, 2, 3]
49
+ * - 문자열: "1-3", "1,3,5-7"
50
+ *
51
+ * PDF: 정확한 페이지 단위. HWP/HWPX: 섹션 단위 근사치.
52
+ */
53
+ pages?: number[] | string;
54
+ /** 이미지 기반 PDF용 OCR 프로바이더 (선택) */
55
+ ocr?: OcrProvider;
56
+ }
57
+ /** 구조화된 에러 코드 — 프로그래밍적 에러 핸들링용 */
58
+ type ErrorCode = "EMPTY_INPUT" | "UNSUPPORTED_FORMAT" | "ENCRYPTED" | "DRM_PROTECTED" | "CORRUPTED" | "DECOMPRESSION_BOMB" | "ZIP_BOMB" | "IMAGE_BASED_PDF" | "NO_SECTIONS" | "PARSE_ERROR";
1
59
  type FileType = "hwpx" | "hwp" | "pdf" | "unknown";
2
60
  interface ParseResultBase {
3
61
  fileType: FileType;
@@ -10,13 +68,104 @@ interface ParseSuccess extends ParseResultBase {
10
68
  success: true;
11
69
  /** 추출된 마크다운 텍스트 */
12
70
  markdown: string;
71
+ /** 중간 표현 블록 (구조화된 데이터 접근용) */
72
+ blocks: IRBlock[];
73
+ /** 문서 메타데이터 */
74
+ metadata?: DocumentMetadata;
13
75
  }
14
76
  interface ParseFailure extends ParseResultBase {
15
77
  success: false;
16
78
  /** 오류 메시지 */
17
79
  error: string;
80
+ /** 구조화된 에러 코드 */
81
+ code?: ErrorCode;
18
82
  }
19
83
  type ParseResult = ParseSuccess | ParseFailure;
84
+ type DiffChangeType = "added" | "removed" | "modified" | "unchanged";
85
+ interface BlockDiff {
86
+ type: DiffChangeType;
87
+ /** 원본 블록 (added이면 undefined) */
88
+ before?: IRBlock;
89
+ /** 변경 후 블록 (removed이면 undefined) */
90
+ after?: IRBlock;
91
+ /** modified 테이블의 셀 단위 diff */
92
+ cellDiffs?: CellDiff[][];
93
+ /** 유사도 (0-1) */
94
+ similarity?: number;
95
+ }
96
+ interface CellDiff {
97
+ type: DiffChangeType;
98
+ before?: string;
99
+ after?: string;
100
+ }
101
+ interface DiffResult {
102
+ stats: {
103
+ added: number;
104
+ removed: number;
105
+ modified: number;
106
+ unchanged: number;
107
+ };
108
+ diffs: BlockDiff[];
109
+ }
110
+ interface FormField {
111
+ label: string;
112
+ value: string;
113
+ /** 0-based 소스 행 */
114
+ row: number;
115
+ /** 0-based 소스 열 */
116
+ col: number;
117
+ }
118
+ interface FormResult {
119
+ fields: FormField[];
120
+ /** 양식 확신도 (0-1) */
121
+ confidence: number;
122
+ }
123
+ /** 사용자 제공 OCR 함수 — 페이지 이미지를 받아 텍스트 반환 */
124
+ type OcrProvider = (pageImage: Uint8Array, pageNumber: number, mimeType: "image/png") => Promise<string>;
125
+ interface WatchOptions {
126
+ dir: string;
127
+ outDir?: string;
128
+ webhook?: string;
129
+ format?: "markdown" | "json";
130
+ pages?: string;
131
+ silent?: boolean;
132
+ }
133
+
134
+ /** 문서 비교 엔진 — IR 레벨 블록 비교로 신구대조표 생성 */
135
+
136
+ /**
137
+ * 두 문서를 비교하여 블록 단위 diff 생성.
138
+ * 크로스 포맷 지원 — HWP vs HWPX 비교 가능 (IR 레벨).
139
+ */
140
+ declare function compare(bufferA: ArrayBuffer, bufferB: ArrayBuffer, options?: ParseOptions): Promise<DiffResult>;
141
+ /** IRBlock[] 간 diff — LCS 기반 정렬 */
142
+ declare function diffBlocks(blocksA: IRBlock[], blocksB: IRBlock[]): DiffResult;
143
+
144
+ /** 양식(서식) 필드 인식 — 테이블 기반 label-value 패턴 매칭 */
145
+
146
+ /**
147
+ * IRBlock[]에서 양식 필드를 인식하여 추출.
148
+ * 테이블의 label-value 패턴을 감지.
149
+ */
150
+ declare function extractFormFields(blocks: IRBlock[]): FormResult;
151
+
152
+ /**
153
+ * Markdown → HWPX 역변환 (MVP)
154
+ *
155
+ * 지원: 단락, 헤딩, 테이블 (텍스트+구조만, 스타일 없음)
156
+ * jszip으로 HWPX ZIP 패키징.
157
+ */
158
+ /**
159
+ * 마크다운 텍스트를 HWPX (ArrayBuffer)로 변환.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { markdownToHwpx } from "kordoc"
164
+ * const hwpxBuffer = await markdownToHwpx("# 제목\n\n본문 텍스트")
165
+ * writeFileSync("output.hwpx", Buffer.from(hwpxBuffer))
166
+ * ```
167
+ */
168
+ declare function markdownToHwpx(markdown: string): Promise<ArrayBuffer>;
20
169
 
21
170
  /** 매직 바이트 기반 파일 포맷 감지 */
22
171
 
@@ -29,6 +178,10 @@ declare function isPdfFile(buffer: ArrayBuffer): boolean;
29
178
  /** 버퍼로부터 파일 포맷 감지 */
30
179
  declare function detectFormat(buffer: ArrayBuffer): FileType;
31
180
 
181
+ /** 2-pass colSpan/rowSpan 테이블 빌더 및 Markdown 변환 */
182
+
183
+ declare function blocksToMarkdown(blocks: IRBlock[]): string;
184
+
32
185
  /** kordoc 공용 유틸리티 */
33
186
  declare const VERSION: string;
34
187
 
@@ -45,15 +198,19 @@ declare const VERSION: string;
45
198
  * ```ts
46
199
  * import { parse } from "kordoc"
47
200
  * const result = await parse(buffer)
48
- * if (result.success) console.log(result.markdown)
201
+ * if (result.success) {
202
+ * console.log(result.markdown) // 마크다운 텍스트
203
+ * console.log(result.blocks) // IRBlock[] 구조화 데이터
204
+ * console.log(result.metadata) // 문서 메타데이터
205
+ * }
49
206
  * ```
50
207
  */
51
- declare function parse(buffer: ArrayBuffer): Promise<ParseResult>;
208
+ declare function parse(buffer: ArrayBuffer, options?: ParseOptions): Promise<ParseResult>;
52
209
  /** HWPX 파일을 Markdown으로 변환 */
53
- declare function parseHwpx(buffer: ArrayBuffer): Promise<ParseResult>;
210
+ declare function parseHwpx(buffer: ArrayBuffer, options?: ParseOptions): Promise<ParseResult>;
54
211
  /** HWP 5.x 바이너리 파일을 Markdown으로 변환 */
55
- declare function parseHwp(buffer: ArrayBuffer): Promise<ParseResult>;
212
+ declare function parseHwp(buffer: ArrayBuffer, options?: ParseOptions): Promise<ParseResult>;
56
213
  /** PDF 파일에서 텍스트를 추출하여 Markdown으로 변환 */
57
- declare function parsePdf(buffer: ArrayBuffer): Promise<ParseResult>;
214
+ declare function parsePdf(buffer: ArrayBuffer, options?: ParseOptions): Promise<ParseResult>;
58
215
 
59
- export { type FileType, type ParseFailure, type ParseResult, type ParseSuccess, VERSION, detectFormat, isHwpxFile, isOldHwpFile, isPdfFile, parse, parseHwp, parseHwpx, parsePdf };
216
+ export { type BlockDiff, type CellContext, type CellDiff, type DiffChangeType, type DiffResult, type DocumentMetadata, type ErrorCode, type FileType, type FormField, type FormResult, type IRBlock, type IRCell, type IRTable, type OcrProvider, type ParseFailure, type ParseOptions, type ParseResult, type ParseSuccess, VERSION, type WatchOptions, blocksToMarkdown, compare, detectFormat, diffBlocks, extractFormFields, isHwpxFile, isOldHwpFile, isPdfFile, markdownToHwpx, parse, parseHwp, parseHwpx, parsePdf };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,61 @@
1
+ /** kordoc 공통 타입 정의 */
2
+ interface CellContext {
3
+ text: string;
4
+ colSpan: number;
5
+ rowSpan: number;
6
+ }
7
+ interface IRBlock {
8
+ type: "paragraph" | "table";
9
+ text?: string;
10
+ table?: IRTable;
11
+ }
12
+ interface IRTable {
13
+ rows: number;
14
+ cols: number;
15
+ cells: IRCell[][];
16
+ hasHeader: boolean;
17
+ }
18
+ interface IRCell {
19
+ text: string;
20
+ colSpan: number;
21
+ rowSpan: number;
22
+ }
23
+ /** 문서 메타데이터 — 각 포맷에서 추출 가능한 필드만 채워짐 */
24
+ interface DocumentMetadata {
25
+ /** 문서 제목 */
26
+ title?: string;
27
+ /** 작성자 */
28
+ author?: string;
29
+ /** 작성 프로그램 (예: "한글 2020", "Adobe Acrobat") */
30
+ creator?: string;
31
+ /** 생성일시 (ISO 8601) */
32
+ createdAt?: string;
33
+ /** 수정일시 (ISO 8601) */
34
+ modifiedAt?: string;
35
+ /** 페이지/섹션 수 */
36
+ pageCount?: number;
37
+ /** 문서 포맷 버전 (예: HWP "5.1.0.1") */
38
+ version?: string;
39
+ /** 설명 */
40
+ description?: string;
41
+ /** 키워드 */
42
+ keywords?: string[];
43
+ }
44
+ /** 파싱 옵션 — parse() 함수에 전달 */
45
+ interface ParseOptions {
46
+ /**
47
+ * 파싱할 페이지/섹션 범위 (1-based).
48
+ * - 배열: [1, 2, 3]
49
+ * - 문자열: "1-3", "1,3,5-7"
50
+ *
51
+ * PDF: 정확한 페이지 단위. HWP/HWPX: 섹션 단위 근사치.
52
+ */
53
+ pages?: number[] | string;
54
+ /** 이미지 기반 PDF용 OCR 프로바이더 (선택) */
55
+ ocr?: OcrProvider;
56
+ }
57
+ /** 구조화된 에러 코드 — 프로그래밍적 에러 핸들링용 */
58
+ type ErrorCode = "EMPTY_INPUT" | "UNSUPPORTED_FORMAT" | "ENCRYPTED" | "DRM_PROTECTED" | "CORRUPTED" | "DECOMPRESSION_BOMB" | "ZIP_BOMB" | "IMAGE_BASED_PDF" | "NO_SECTIONS" | "PARSE_ERROR";
1
59
  type FileType = "hwpx" | "hwp" | "pdf" | "unknown";
2
60
  interface ParseResultBase {
3
61
  fileType: FileType;
@@ -10,13 +68,104 @@ interface ParseSuccess extends ParseResultBase {
10
68
  success: true;
11
69
  /** 추출된 마크다운 텍스트 */
12
70
  markdown: string;
71
+ /** 중간 표현 블록 (구조화된 데이터 접근용) */
72
+ blocks: IRBlock[];
73
+ /** 문서 메타데이터 */
74
+ metadata?: DocumentMetadata;
13
75
  }
14
76
  interface ParseFailure extends ParseResultBase {
15
77
  success: false;
16
78
  /** 오류 메시지 */
17
79
  error: string;
80
+ /** 구조화된 에러 코드 */
81
+ code?: ErrorCode;
18
82
  }
19
83
  type ParseResult = ParseSuccess | ParseFailure;
84
+ type DiffChangeType = "added" | "removed" | "modified" | "unchanged";
85
+ interface BlockDiff {
86
+ type: DiffChangeType;
87
+ /** 원본 블록 (added이면 undefined) */
88
+ before?: IRBlock;
89
+ /** 변경 후 블록 (removed이면 undefined) */
90
+ after?: IRBlock;
91
+ /** modified 테이블의 셀 단위 diff */
92
+ cellDiffs?: CellDiff[][];
93
+ /** 유사도 (0-1) */
94
+ similarity?: number;
95
+ }
96
+ interface CellDiff {
97
+ type: DiffChangeType;
98
+ before?: string;
99
+ after?: string;
100
+ }
101
+ interface DiffResult {
102
+ stats: {
103
+ added: number;
104
+ removed: number;
105
+ modified: number;
106
+ unchanged: number;
107
+ };
108
+ diffs: BlockDiff[];
109
+ }
110
+ interface FormField {
111
+ label: string;
112
+ value: string;
113
+ /** 0-based 소스 행 */
114
+ row: number;
115
+ /** 0-based 소스 열 */
116
+ col: number;
117
+ }
118
+ interface FormResult {
119
+ fields: FormField[];
120
+ /** 양식 확신도 (0-1) */
121
+ confidence: number;
122
+ }
123
+ /** 사용자 제공 OCR 함수 — 페이지 이미지를 받아 텍스트 반환 */
124
+ type OcrProvider = (pageImage: Uint8Array, pageNumber: number, mimeType: "image/png") => Promise<string>;
125
+ interface WatchOptions {
126
+ dir: string;
127
+ outDir?: string;
128
+ webhook?: string;
129
+ format?: "markdown" | "json";
130
+ pages?: string;
131
+ silent?: boolean;
132
+ }
133
+
134
+ /** 문서 비교 엔진 — IR 레벨 블록 비교로 신구대조표 생성 */
135
+
136
+ /**
137
+ * 두 문서를 비교하여 블록 단위 diff 생성.
138
+ * 크로스 포맷 지원 — HWP vs HWPX 비교 가능 (IR 레벨).
139
+ */
140
+ declare function compare(bufferA: ArrayBuffer, bufferB: ArrayBuffer, options?: ParseOptions): Promise<DiffResult>;
141
+ /** IRBlock[] 간 diff — LCS 기반 정렬 */
142
+ declare function diffBlocks(blocksA: IRBlock[], blocksB: IRBlock[]): DiffResult;
143
+
144
+ /** 양식(서식) 필드 인식 — 테이블 기반 label-value 패턴 매칭 */
145
+
146
+ /**
147
+ * IRBlock[]에서 양식 필드를 인식하여 추출.
148
+ * 테이블의 label-value 패턴을 감지.
149
+ */
150
+ declare function extractFormFields(blocks: IRBlock[]): FormResult;
151
+
152
+ /**
153
+ * Markdown → HWPX 역변환 (MVP)
154
+ *
155
+ * 지원: 단락, 헤딩, 테이블 (텍스트+구조만, 스타일 없음)
156
+ * jszip으로 HWPX ZIP 패키징.
157
+ */
158
+ /**
159
+ * 마크다운 텍스트를 HWPX (ArrayBuffer)로 변환.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { markdownToHwpx } from "kordoc"
164
+ * const hwpxBuffer = await markdownToHwpx("# 제목\n\n본문 텍스트")
165
+ * writeFileSync("output.hwpx", Buffer.from(hwpxBuffer))
166
+ * ```
167
+ */
168
+ declare function markdownToHwpx(markdown: string): Promise<ArrayBuffer>;
20
169
 
21
170
  /** 매직 바이트 기반 파일 포맷 감지 */
22
171
 
@@ -29,6 +178,10 @@ declare function isPdfFile(buffer: ArrayBuffer): boolean;
29
178
  /** 버퍼로부터 파일 포맷 감지 */
30
179
  declare function detectFormat(buffer: ArrayBuffer): FileType;
31
180
 
181
+ /** 2-pass colSpan/rowSpan 테이블 빌더 및 Markdown 변환 */
182
+
183
+ declare function blocksToMarkdown(blocks: IRBlock[]): string;
184
+
32
185
  /** kordoc 공용 유틸리티 */
33
186
  declare const VERSION: string;
34
187
 
@@ -45,15 +198,19 @@ declare const VERSION: string;
45
198
  * ```ts
46
199
  * import { parse } from "kordoc"
47
200
  * const result = await parse(buffer)
48
- * if (result.success) console.log(result.markdown)
201
+ * if (result.success) {
202
+ * console.log(result.markdown) // 마크다운 텍스트
203
+ * console.log(result.blocks) // IRBlock[] 구조화 데이터
204
+ * console.log(result.metadata) // 문서 메타데이터
205
+ * }
49
206
  * ```
50
207
  */
51
- declare function parse(buffer: ArrayBuffer): Promise<ParseResult>;
208
+ declare function parse(buffer: ArrayBuffer, options?: ParseOptions): Promise<ParseResult>;
52
209
  /** HWPX 파일을 Markdown으로 변환 */
53
- declare function parseHwpx(buffer: ArrayBuffer): Promise<ParseResult>;
210
+ declare function parseHwpx(buffer: ArrayBuffer, options?: ParseOptions): Promise<ParseResult>;
54
211
  /** HWP 5.x 바이너리 파일을 Markdown으로 변환 */
55
- declare function parseHwp(buffer: ArrayBuffer): Promise<ParseResult>;
212
+ declare function parseHwp(buffer: ArrayBuffer, options?: ParseOptions): Promise<ParseResult>;
56
213
  /** PDF 파일에서 텍스트를 추출하여 Markdown으로 변환 */
57
- declare function parsePdf(buffer: ArrayBuffer): Promise<ParseResult>;
214
+ declare function parsePdf(buffer: ArrayBuffer, options?: ParseOptions): Promise<ParseResult>;
58
215
 
59
- export { type FileType, type ParseFailure, type ParseResult, type ParseSuccess, VERSION, detectFormat, isHwpxFile, isOldHwpFile, isPdfFile, parse, parseHwp, parseHwpx, parsePdf };
216
+ export { type BlockDiff, type CellContext, type CellDiff, type DiffChangeType, type DiffResult, type DocumentMetadata, type ErrorCode, type FileType, type FormField, type FormResult, type IRBlock, type IRCell, type IRTable, type OcrProvider, type ParseFailure, type ParseOptions, type ParseResult, type ParseSuccess, VERSION, type WatchOptions, blocksToMarkdown, compare, detectFormat, diffBlocks, extractFormFields, isHwpxFile, isOldHwpFile, isPdfFile, markdownToHwpx, parse, parseHwp, parseHwpx, parsePdf };