Hisoka-X commented on code in PR #9760:
URL: https://github.com/apache/seatunnel/pull/9760#discussion_r2306101859


##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/config/FileFormat.java:
##########
@@ -167,7 +159,21 @@ public ReadStrategy getReadStrategy() {
             throw new UnsupportedOperationException(
                     "File format 'maxwell_json' does not support reading.");
         }
-    };
+    },
+    MARKDOWN("md", "markdown") {
+        @Override
+        public WriteStrategy getWriteStrategy(FileSinkConfig fileSinkConfig) {
+            throw new UnsupportedOperationException(
+                    "File format 'markdown' does not support reading.");

Review Comment:
   ```suggestion
                       "File format 'markdown' does not support writing.");
   ```



##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/source/reader/MarkdownReadStrategy.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.connectors.seatunnel.file.source.reader;
+
+import com.vladsch.flexmark.ast.*;
+import com.vladsch.flexmark.ext.tables.TableBlock;
+import com.vladsch.flexmark.ext.tables.TableCell;
+import com.vladsch.flexmark.ext.tables.TableRow;
+import com.vladsch.flexmark.parser.Parser;
+import com.vladsch.flexmark.util.ast.Node;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.seatunnel.api.source.Collector;
+import org.apache.seatunnel.api.table.type.BasicType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import 
org.apache.seatunnel.connectors.seatunnel.file.exception.FileConnectorException;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+
+@Slf4j
+public class MarkdownReadStrategy extends AbstractReadStrategy {
+
+    @Override
+    public void read(String path, String tableId, Collector<SeaTunnelRow> 
output) throws IOException, FileConnectorException {
+        String markdown = new String(Files.readAllBytes(Paths.get(path)));
+        Parser parser = Parser.builder().build();
+        Node document = parser.parse(markdown);
+
+        List<SeaTunnelRow> rows = new ArrayList<>();
+        traverse(document, rows);
+
+        for (SeaTunnelRow row : rows) {
+            output.collect(row);
+        }
+    }
+
+    private void traverse(Node node, List<SeaTunnelRow> rows) {
+        for (Node child = node.getFirstChild(); child != null; child = 
child.getNext()) {
+            String type = child.getClass().getSimpleName();
+
+            if (child instanceof Heading || child instanceof Paragraph || 
child instanceof ListItem ||
+                    child instanceof BulletList || child instanceof 
OrderedList ||
+                    child instanceof BlockQuote || child instanceof 
FencedCodeBlock ||
+                    child instanceof TableBlock) {
+                String value = extractValue(child);
+                if (value != null && !value.trim().isEmpty()) {
+                    rows.add(new SeaTunnelRow(new Object[]{type, 
value.trim()}));
+                    log.debug("Added row: type={} value={}", type, 
value.trim());
+                }
+                continue;
+            }
+
+            traverse(child, rows);
+        }
+    }
+
+    private String extractValue(Node node) {
+        if (node instanceof Heading || node instanceof Paragraph || node 
instanceof ListItem) {
+            return extractTextFromChildren(node);
+        } else if (node instanceof BulletList) {
+            return bulletListToString((BulletList) node);
+        } else if (node instanceof OrderedList) {
+            return orderedListToString((OrderedList) node);
+        } else if (node instanceof Code) {
+            return ((Code) node).getText().toString();
+        } else if (node instanceof FencedCodeBlock) {
+            return ((FencedCodeBlock) node).getContentChars().toString();
+        } else if (node instanceof BlockQuote) {
+            return extractTextFromChildren(node);
+        } else if (node instanceof ThematicBreak) {
+            return "---";
+        } else if (node instanceof Link) {
+            return ((Link) node).getUrl().toString();
+        } else if (node instanceof Image) {
+            return ((Image) node).getUrl().toString();
+        } else if (node instanceof TableBlock) {
+            return tableToString((TableBlock) node);
+        }
+        return node.getChars().toString();
+    }
+
+    private String extractTextFromChildren(Node node) {
+        StringBuilder sb = new StringBuilder();
+        for (Node child = node.getFirstChild(); child != null; child = 
child.getNext()) {
+            sb.append(child.getChars());
+        }
+
+        return sb.toString().trim();
+    }
+
+    private String bulletListToString(BulletList list) {
+        StringBuilder sb = new StringBuilder();
+        for (Node item = list.getFirstChild(); item != null; item = 
item.getNext()) {
+            if (item instanceof ListItem) {
+                sb.append("- ")
+                        .append(extractTextFromChildren(item))
+                        .append("\n");
+            }
+        }
+
+        return sb.toString();
+    }
+
+    private String orderedListToString(OrderedList list) {
+        StringBuilder sb = new StringBuilder();
+        int num = 1;
+        for (Node item = list.getFirstChild(); item != null; item = 
item.getNext()) {
+            if (item instanceof ListItem) {
+                sb.append(num++)
+                        .append(". ")
+                        .append(extractTextFromChildren(item))
+                        .append("\n");
+            }
+        }
+
+        return sb.toString();
+    }
+
+    private String tableToString(TableBlock table) {
+        StringBuilder sb = new StringBuilder();
+        for (Node row = table.getFirstChild(); row != null; row = 
row.getNext()) {
+            if (row instanceof TableRow) {
+                for (Node cell = row.getFirstChild(); cell != null; cell = 
cell.getNext()) {
+                    if (cell instanceof TableCell) {
+                        sb.append(((TableCell) 
cell).getText().toString()).append(" | ");
+                    }
+                }
+                sb.append("\n");
+            }
+        }
+
+        return sb.toString();
+    }
+
+    @Override
+    public SeaTunnelRowType getSeaTunnelRowTypeInfo(String path) throws 
FileConnectorException {
+        return new SeaTunnelRowType(
+                new String[]{"type", "value"},
+                new org.apache.seatunnel.api.table.type.SeaTunnelDataType[]{
+                        BasicType.STRING_TYPE, BasicType.STRING_TYPE

Review Comment:
   The row fields too simple.
   We should contains:
   - element_id
   - element_type
   - heading_level
   - text
   - page_number
   - position_index
   - parent_id
   - child_ids
   
   For example:
   ```markdown
   # Data Source Configuration
   
   ## Kafka Configuration
   Users can configure Kafka by specifying the bootstrap.servers, topic, and 
group.id.
   
   ## MySQL Configuration
   MySQL requires a JDBC URL, username, and password.
   
   ### Notes
   Make sure to test the connection before deploying.
   ```
   The row should be
   ```json
   {
         "element_id": "uuid-elem-1",
         "element_type": "heading",
         "heading_level": 1,
         "text": "Data Source Configuration",
         "page_number": null,
         "position_index": 0,
         "parent_id": null,
         "child_ids": ["uuid-elem-2","uuid-elem-4"],
    }
   {
         "element_id": "uuid-elem-2",
         "element_type": "heading",
         "heading_level": 2,
         "text": "Kafka Configuration",
         "page_number": null,
         "position_index": 1,
         "parent_id": "uuid-elem-1",
         "child_ids": ["uuid-elem-3"]
   }
   {
         "element_id": "uuid-elem-3",
         "element_type": "paragraph",
         "heading_level": null,
         "text": "Users can configure Kafka by specifying the 
bootstrap.servers, topic, and group.id.",
         "page_number": null,
         "position_index": 2,
         "parent_id": "uuid-elem-2",
         "child_ids": []
       }
       {
         "element_id": "uuid-elem-4",
         "element_type": "heading",
         "heading_level": 2,
         "text": "MySQL Configuration",
         "page_number": null,
         "position_index": 3,
         "parent_id": "uuid-elem-1",
         "child_ids": ["uuid-elem-5","uuid-elem-6"]
       }
       {
         "element_id": "uuid-elem-5",
         "element_type": "paragraph",
         "heading_level": null,
         "text": "MySQL requires a JDBC URL, username, and password.",
         "page_number": null,
         "position_index": 4,
         "parent_id": "uuid-elem-4",
         "child_ids": []
       }
       {
         "element_id": "uuid-elem-6",
         "element_type": "heading",
         "heading_level": 3,
         "text": "Notes",
         "page_number": null,
         "position_index": 5,
         "parent_id": "uuid-elem-4",
         "child_ids": ["uuid-elem-7"]
       }
       {
         "element_id": "uuid-elem-7",
         "element_type": "paragraph",
         "heading_level": null,
         "text": "Make sure to test the connection before deploying.",
         "page_number": null,
         "position_index": 6,
         "parent_id": "uuid-elem-6",
         "child_ids": []
       }
   ```
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to