From 2efc46fff2c5884ecae99838ac34547cecae057f Mon Sep 17 00:00:00 2001 From: Aleksandr Vasilenko Date: Thu, 22 Jan 2026 09:37:00 -0600 Subject: [PATCH 1/4] Update wapi.zig --- src/wapi.zig | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/wapi.zig b/src/wapi.zig index 86f6c33..48cd84c 100644 --- a/src/wapi.zig +++ b/src/wapi.zig @@ -119,6 +119,21 @@ export fn zpdf_extract_all(handle: i32, out_len: *usize) ?[*]u8 { return null; } +/// Extract text from all pages as Markdown with semantic formatting +/// Returns pointer to Markdown text buffer, sets out_len to buffer length +/// Caller must free with wasm_free +export fn zpdf_extract_all_markdown(handle: i32, out_len: *usize) ?[*]u8 { + if (handle < 0 or handle >= MAX_DOCUMENTS) return null; + const idx: usize = @intCast(handle); + + if (documents[idx]) |doc| { + const result = doc.extractAllMarkdown(wasm_allocator) catch return null; + out_len.* = result.len; + return result.ptr; + } + return null; +} + /// Get page dimensions /// Returns 0 on success, -1 on error export fn zpdf_get_page_info(handle: i32, page_num: i32, width: *f64, height: *f64, rotation: *i32) i32 { From 0ade580959a914a75f65298aa05536139c3cce6d Mon Sep 17 00:00:00 2001 From: Aleksandr Vasilenko Date: Thu, 22 Jan 2026 09:50:54 -0600 Subject: [PATCH 2/4] Implement CodeRabbit suggestion --- src/wapi.zig | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/wapi.zig b/src/wapi.zig index 48cd84c..9bb89e7 100644 --- a/src/wapi.zig +++ b/src/wapi.zig @@ -128,6 +128,13 @@ export fn zpdf_extract_all_markdown(handle: i32, out_len: *usize) ?[*]u8 { if (documents[idx]) |doc| { const result = doc.extractAllMarkdown(wasm_allocator) catch return null; + + // Handle empty buffer - toOwnedSlice returns undefined ptr for empty slice + if (result.len == 0) { + out_len.* = 0; + return null; + } + out_len.* = result.len; return result.ptr; } From 070a5512b1967570eb5adfc5a8e06b21ae1a53e8 Mon Sep 17 00:00:00 2001 From: Aleksandr Vasilenko Date: Thu, 22 Jan 2026 09:58:15 -0600 Subject: [PATCH 3/4] More CodeRabbit review fixes --- src/capi.zig | 7 +++++++ src/wapi.zig | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/capi.zig b/src/capi.zig index bc723e9..c80589e 100644 --- a/src/capi.zig +++ b/src/capi.zig @@ -192,6 +192,13 @@ export fn zpdf_extract_all_markdown(handle: ?*ZpdfDocument, out_len: *usize) ?[* if (handle) |h| { const doc: *zpdf.Document = @ptrCast(@alignCast(h)); const result = doc.extractAllMarkdown(c_allocator) catch return null; + + // extractAllMarkdown returns an allocated slice; treat zero-length as "no data" + if (result.len == 0) { + out_len.* = 0; + return null; + } + out_len.* = result.len; return result.ptr; } diff --git a/src/wapi.zig b/src/wapi.zig index 9bb89e7..0e1be51 100644 --- a/src/wapi.zig +++ b/src/wapi.zig @@ -129,7 +129,7 @@ export fn zpdf_extract_all_markdown(handle: i32, out_len: *usize) ?[*]u8 { if (documents[idx]) |doc| { const result = doc.extractAllMarkdown(wasm_allocator) catch return null; - // Handle empty buffer - toOwnedSlice returns undefined ptr for empty slice + // extractAllMarkdown returns an allocated slice; treat zero-length as "no data" if (result.len == 0) { out_len.* = 0; return null; From c632babfc4b71b1cc17218a5eda35c3b5a4733ac Mon Sep 17 00:00:00 2001 From: Aleksandr Vasilenko Date: Thu, 22 Jan 2026 11:04:51 -0600 Subject: [PATCH 4/4] Free the zero-length buffer before returning null --- src/capi.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/src/capi.zig b/src/capi.zig index c80589e..2b3dadc 100644 --- a/src/capi.zig +++ b/src/capi.zig @@ -195,6 +195,7 @@ export fn zpdf_extract_all_markdown(handle: ?*ZpdfDocument, out_len: *usize) ?[* // extractAllMarkdown returns an allocated slice; treat zero-length as "no data" if (result.len == 0) { + c_allocator.free(result); out_len.* = 0; return null; }