|
This version is still in development and is not considered stable yet. For the latest stable version, please use Spring AI 2.1.0! |
Markdown Document Reader
The MarkdownDocumentReader processes Markdown documents, converting them into a list of Document objects.
Dependencies
Add the dependency to your project using Maven or Gradle.
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-markdown-document-reader</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.ai:spring-ai-markdown-document-reader'
}
Example
@Component
class MyMarkdownReader {
private final Resource resource;
MyMarkdownReader(@Value("classpath:code.md") Resource resource) {
this.resource = resource;
}
List<Document> loadMarkdown() {
MarkdownDocumentReaderConfig config = MarkdownDocumentReaderConfig.builder()
.withHorizontalRuleCreateDocument(true)
.withIncludeCodeBlock(false)
.withIncludeBlockquote(false)
.withAdditionalMetadata("filename", "code.md")
.build();
MarkdownDocumentReader reader = new MarkdownDocumentReader(this.resource, config);
return reader.get();
}
}
The MarkdownDocumentReaderConfig allows you to customize the behavior of the MarkdownDocumentReader:
-
horizontalRuleCreateDocument: When set totrue, horizontal rules in the Markdown will create newDocumentobjects. -
includeCodeBlock: When set totrue, code blocks will be included in the sameDocumentas the surrounding text. Whenfalse, code blocks create separateDocumentobjects. -
includeBlockquote: When set totrue, blockquotes will be included in the sameDocumentas the surrounding text. Whenfalse, blockquotes create separateDocumentobjects. -
additionalMetadata: Allows you to add custom metadata to all createdDocumentobjects.
Sample Document: code.md
This is a Java sample application:
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
Markdown also provides the possibility to `use inline code formatting throughout` the entire sentence.
---
Another possibility is to set block code without specific highlighting:
```
./mvnw spring-javaformat:apply
```
Behavior: The MarkdownDocumentReader processes the Markdown content and creates Document objects based on the configuration:
-
Headers become metadata in the Document objects.
-
Paragraphs become the content of Document objects.
-
Code blocks can be separated into their own Document objects or included with surrounding text.
-
Blockquotes can be separated into their own Document objects or included with surrounding text.
-
Horizontal rules can be used to split the content into separate Document objects.
The reader preserves formatting like inline code, lists, and text styling within the content of the Document objects.