Skip to content

Commit 2f0f650

Browse files
committed
feat(markdown): add docxToMarkdown File/InputStream/byte[]/path shortcuts
Agent/RAG consumers can now extract markdown from a docx in one call without manually loading a WordprocessingMLPackage first. All overloads delegate to the existing pkg-based implementation; null inputs return ''. EasyMarkdownTest: +3 cases (file / stream+bytes+path / null inputs). Verified (JDK 17): xhtml markdown 6/6 green.
1 parent d908f60 commit 2f0f650

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

easydoc-xhtml/src/main/java/io/github/easy4j/doc/xhtml/markdown/EasyMarkdown.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.github.easy4j.doc.xhtml.markdown;
22

3+
import java.io.ByteArrayInputStream;
34
import java.io.ByteArrayOutputStream;
5+
import java.io.File;
6+
import java.io.InputStream;
47
import java.util.Map;
58

69
import org.docx4j.Docx4J;
@@ -32,6 +35,40 @@ public static WordprocessingMLPackage markdownToDocx(String markdown,
3235
return template.process(html, vars);
3336
}
3437

38+
/** docx 文件 → Markdown。null 输入返回空串。 */
39+
public static String docxToMarkdown(File file) throws Exception {
40+
if (file == null) {
41+
return "";
42+
}
43+
return docxToMarkdown(WordprocessingMLPackage.load(file));
44+
}
45+
46+
/** docx 输入流 → Markdown(流由本方法负责关闭)。null 输入返回空串。 */
47+
public static String docxToMarkdown(InputStream in) throws Exception {
48+
if (in == null) {
49+
return "";
50+
}
51+
try (InputStream closeable = in) {
52+
return docxToMarkdown(WordprocessingMLPackage.load(closeable));
53+
}
54+
}
55+
56+
/** docx 字节数组 → Markdown。null 输入返回空串。 */
57+
public static String docxToMarkdown(byte[] bytes) throws Exception {
58+
if (bytes == null) {
59+
return "";
60+
}
61+
return docxToMarkdown(WordprocessingMLPackage.load(new ByteArrayInputStream(bytes)));
62+
}
63+
64+
/** docx 文件路径 → Markdown。null/空白路径返回空串。 */
65+
public static String docxToMarkdown(String path) throws Exception {
66+
if (path == null || path.trim().isEmpty()) {
67+
return "";
68+
}
69+
return docxToMarkdown(new File(path));
70+
}
71+
3572
/** docx → Markdown(经 docx4j HTML 导出 + 简化 HTML→MD 映射)。null 输入返回空串。 */
3673
public static String docxToMarkdown(WordprocessingMLPackage pkg) throws Exception {
3774
if (pkg == null) {

easydoc-xhtml/src/test/java/io/github/easy4j/doc/xhtml/markdown/EasyMarkdownTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
import static org.junit.jupiter.api.Assertions.assertNotNull;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

6+
import java.io.File;
7+
import java.io.FileInputStream;
8+
import java.io.InputStream;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
612
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
713
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.io.TempDir;
815

916
class EasyMarkdownTest {
1017

@@ -31,4 +38,39 @@ void docxToMarkdownConvertsHeadingAndBold() throws Exception {
3138
assertTrue(md.contains("标题"), "heading text must appear in markdown output");
3239
assertTrue(md.contains("加粗"), "text must appear in markdown output");
3340
}
41+
42+
@Test
43+
void docxToMarkdownFromFile(@TempDir Path tempDir) throws Exception {
44+
WordprocessingMLPackage pkg = EasyMarkdown.markdownToDocx("# 标题\n\n**加粗** 内容");
45+
File file = tempDir.resolve("doc.docx").toFile();
46+
pkg.save(file);
47+
String md = EasyMarkdown.docxToMarkdown(file);
48+
assertTrue(md.contains("标题"), "heading text must appear in markdown output");
49+
assertTrue(md.contains("加粗"), "text must appear in markdown output");
50+
}
51+
52+
@Test
53+
void docxToMarkdownFromInputStreamBytesAndPath(@TempDir Path tempDir) throws Exception {
54+
WordprocessingMLPackage pkg = EasyMarkdown.markdownToDocx("# 标题");
55+
File file = tempDir.resolve("doc2.docx").toFile();
56+
pkg.save(file);
57+
try (InputStream in = new FileInputStream(file)) {
58+
assertTrue(EasyMarkdown.docxToMarkdown(in).contains("标题"),
59+
"input stream input must produce markdown");
60+
}
61+
byte[] bytes = Files.readAllBytes(file.toPath());
62+
assertTrue(EasyMarkdown.docxToMarkdown(bytes).contains("标题"),
63+
"byte[] input must produce markdown");
64+
assertTrue(EasyMarkdown.docxToMarkdown(file.getAbsolutePath()).contains("标题"),
65+
"path input must produce markdown");
66+
}
67+
68+
@Test
69+
void docxToMarkdownNullInputsReturnEmpty() throws Exception {
70+
assertTrue(EasyMarkdown.docxToMarkdown((File) null).isEmpty());
71+
assertTrue(EasyMarkdown.docxToMarkdown((InputStream) null).isEmpty());
72+
assertTrue(EasyMarkdown.docxToMarkdown((byte[]) null).isEmpty());
73+
assertTrue(EasyMarkdown.docxToMarkdown((String) null).isEmpty());
74+
assertTrue(EasyMarkdown.docxToMarkdown(" ").isEmpty());
75+
}
3476
}

0 commit comments

Comments
 (0)