mirror of
https://github.com/vran-dev/databasir.git
synced 2025-09-18 01:37:12 +08:00
fix: replace new line symbol to <br/> in exported markdown (#265)
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package com.databasir.core.domain.document.generator.markdown;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MarkdownBuilder {
|
||||
|
||||
private static final String LINE = "\n";
|
||||
|
||||
private static final String DOUBLE_LINE = LINE + LINE;
|
||||
|
||||
private StringBuilder builder = new StringBuilder(1024);
|
||||
|
||||
private MarkdownBuilder() {
|
||||
}
|
||||
|
||||
public static MarkdownBuilder builder() {
|
||||
return new MarkdownBuilder();
|
||||
}
|
||||
|
||||
public MarkdownBuilder primaryTitle(String title) {
|
||||
builder.append("# ").append(title).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder secondTitle(String title) {
|
||||
builder.append("## ").append(title).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder thirdTitle(String title) {
|
||||
builder.append("### ").append(title).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder text(String text) {
|
||||
builder.append(text).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder table(List<String> titles, List<List<String>> rows) {
|
||||
if (titles == null || titles.isEmpty()) {
|
||||
throw new IllegalArgumentException("titles must not be null or empty");
|
||||
}
|
||||
// build titles
|
||||
builder.append("| ");
|
||||
for (String title : titles) {
|
||||
builder.append(title).append(" | ");
|
||||
}
|
||||
builder.append(LINE);
|
||||
|
||||
// build separators
|
||||
builder.append("| ");
|
||||
for (String title : titles) {
|
||||
builder.append("------").append(" | ");
|
||||
}
|
||||
builder.append(LINE);
|
||||
|
||||
// build rows
|
||||
for (List<String> row : rows) {
|
||||
builder.append("| ");
|
||||
for (String data : row) {
|
||||
builder.append(convertNewLineToBr(data)).append(" | ");
|
||||
}
|
||||
builder.append(LINE);
|
||||
}
|
||||
builder.append(LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder orderedList(List<String> list) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
builder.append(i + 1).append(". ").append(list.get(i)).append(LINE);
|
||||
}
|
||||
builder.append(LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder unorderedList(List<String> list) {
|
||||
for (String item : list) {
|
||||
builder.append("- ").append(item).append(LINE);
|
||||
}
|
||||
builder.append(LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder blockquotes(String content) {
|
||||
builder.append("> ").append(content).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder code(String languageType, String statement) {
|
||||
builder.append("```").append(languageType).append(LINE)
|
||||
.append(statement)
|
||||
.append("```")
|
||||
.append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder link(String text, String link) {
|
||||
builder.append("[").append(text).append("]")
|
||||
.append("(").append(link).append(")");
|
||||
return this;
|
||||
}
|
||||
|
||||
private String convertNewLineToBr(String content) {
|
||||
if (content == null) {
|
||||
return "";
|
||||
}
|
||||
return content
|
||||
.replace("\r\n", "<br/>")
|
||||
.replace("\n", "<br/>");
|
||||
}
|
||||
|
||||
public String build() {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
@@ -1,12 +1,13 @@
|
||||
package com.databasir.core.domain.document.generator;
|
||||
package com.databasir.core.domain.document.generator.markdown;
|
||||
|
||||
import com.alibaba.excel.util.StringUtils;
|
||||
import com.databasir.common.SystemException;
|
||||
import com.databasir.core.domain.document.data.DatabaseDocumentResponse;
|
||||
import com.databasir.core.domain.document.data.DocumentTemplatePropertiesResponse;
|
||||
import com.databasir.core.domain.document.data.TableDocumentResponse;
|
||||
import com.databasir.core.domain.document.generator.DocumentFileGenerator;
|
||||
import com.databasir.core.domain.document.generator.DocumentFileType;
|
||||
import com.databasir.core.domain.document.service.DocumentTemplateService;
|
||||
import com.databasir.core.render.markdown.MarkdownBuilder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -82,10 +83,23 @@ public class MarkdownDocumentFileGenerator implements DocumentFileGenerator {
|
||||
// table document build
|
||||
doc.getTables().forEach(table -> {
|
||||
if (StringUtils.isNotBlank(table.getComment())) {
|
||||
builder.secondTitle(table.getName() + " /\\*" + table.getComment() + "\\*/");
|
||||
String convertedComment = table.getComment()
|
||||
.replace("\r\n", " ")
|
||||
.replace("\n", " ");
|
||||
String comment;
|
||||
if (convertedComment.length() > 30) {
|
||||
comment = convertedComment.substring(0, 30) + "...";
|
||||
} else {
|
||||
comment = convertedComment;
|
||||
}
|
||||
builder.secondTitle(table.getName() + " /\\*" + comment + "\\*/");
|
||||
} else {
|
||||
builder.secondTitle(table.getName());
|
||||
}
|
||||
builder.secondTitle(table.getName());
|
||||
if (StringUtils.isNotBlank(table.getComment())) {
|
||||
builder.blockquotes(table.getComment());
|
||||
}
|
||||
columnBuild(builder, table, columnTitleMap);
|
||||
indexBuild(builder, table, indexTitleMap);
|
||||
foreignKeyBuild(builder, table, foreignKeyTitleMap);
|
Reference in New Issue
Block a user